Use __inline for Visual Studio before 2015
[isl.git] / isl_map.c
blobb3c5c02922b9b560b196543c6f8bcb815f8c7406
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <string.h>
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
21 #include <isl_blk.h>
22 #include <isl/constraint.h>
23 #include "isl_space_private.h"
24 #include "isl_equalities.h"
25 #include <isl_lp_private.h>
26 #include <isl_seq.h>
27 #include <isl/set.h>
28 #include <isl/map.h>
29 #include <isl_reordering.h>
30 #include "isl_sample.h"
31 #include <isl_sort.h>
32 #include "isl_tab.h"
33 #include <isl/vec.h>
34 #include <isl_mat_private.h>
35 #include <isl_vec_private.h>
36 #include <isl_dim_map.h>
37 #include <isl_local_space_private.h>
38 #include <isl_aff_private.h>
39 #include <isl_options_private.h>
40 #include <isl_morph.h>
41 #include <isl_val_private.h>
42 #include <isl/deprecated/map_int.h>
43 #include <isl/deprecated/set_int.h>
45 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
47 switch (type) {
48 case isl_dim_param: return dim->nparam;
49 case isl_dim_in: return dim->n_in;
50 case isl_dim_out: return dim->n_out;
51 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
52 default: return 0;
56 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
58 switch (type) {
59 case isl_dim_param: return 1;
60 case isl_dim_in: return 1 + dim->nparam;
61 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
62 default: return 0;
66 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
67 enum isl_dim_type type)
69 if (!bmap)
70 return 0;
71 switch (type) {
72 case isl_dim_cst: return 1;
73 case isl_dim_param:
74 case isl_dim_in:
75 case isl_dim_out: return isl_space_dim(bmap->dim, type);
76 case isl_dim_div: return bmap->n_div;
77 case isl_dim_all: return isl_basic_map_total_dim(bmap);
78 default: return 0;
82 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
84 return map ? n(map->dim, type) : 0;
87 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
89 return set ? n(set->dim, type) : 0;
92 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
93 enum isl_dim_type type)
95 isl_space *dim = bmap->dim;
96 switch (type) {
97 case isl_dim_cst: return 0;
98 case isl_dim_param: return 1;
99 case isl_dim_in: return 1 + dim->nparam;
100 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
101 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
102 default: return 0;
106 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
107 enum isl_dim_type type)
109 return isl_basic_map_offset(bset, type);
112 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
114 return pos(map->dim, type);
117 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
118 enum isl_dim_type type)
120 return isl_basic_map_dim(bset, type);
123 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
125 return isl_basic_set_dim(bset, isl_dim_set);
128 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
130 return isl_basic_set_dim(bset, isl_dim_param);
133 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
135 if (!bset)
136 return 0;
137 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
140 unsigned isl_set_n_dim(__isl_keep isl_set *set)
142 return isl_set_dim(set, isl_dim_set);
145 unsigned isl_set_n_param(__isl_keep isl_set *set)
147 return isl_set_dim(set, isl_dim_param);
150 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
152 return bmap ? bmap->dim->n_in : 0;
155 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
157 return bmap ? bmap->dim->n_out : 0;
160 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
162 return bmap ? bmap->dim->nparam : 0;
165 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
167 return bmap ? bmap->n_div : 0;
170 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
172 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
175 unsigned isl_map_n_in(const struct isl_map *map)
177 return map ? map->dim->n_in : 0;
180 unsigned isl_map_n_out(const struct isl_map *map)
182 return map ? map->dim->n_out : 0;
185 unsigned isl_map_n_param(const struct isl_map *map)
187 return map ? map->dim->nparam : 0;
190 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
192 int m;
193 if (!map || !set)
194 return -1;
195 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
196 if (m < 0 || !m)
197 return m;
198 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
199 set->dim, isl_dim_set);
202 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
203 struct isl_basic_set *bset)
205 int m;
206 if (!bmap || !bset)
207 return -1;
208 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
209 if (m < 0 || !m)
210 return m;
211 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
212 bset->dim, isl_dim_set);
215 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
217 int m;
218 if (!map || !set)
219 return -1;
220 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
221 if (m < 0 || !m)
222 return m;
223 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
224 set->dim, isl_dim_set);
227 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
228 struct isl_basic_set *bset)
230 int m;
231 if (!bmap || !bset)
232 return -1;
233 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
234 if (m < 0 || !m)
235 return m;
236 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
237 bset->dim, isl_dim_set);
240 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
242 return bmap ? bmap->ctx : NULL;
245 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
247 return bset ? bset->ctx : NULL;
250 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
252 return map ? map->ctx : NULL;
255 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
257 return set ? set->ctx : NULL;
260 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
262 if (!bmap)
263 return NULL;
264 return isl_space_copy(bmap->dim);
267 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
269 if (!bset)
270 return NULL;
271 return isl_space_copy(bset->dim);
274 /* Extract the divs in "bmap" as a matrix.
276 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
278 int i;
279 isl_ctx *ctx;
280 isl_mat *div;
281 unsigned total;
282 unsigned cols;
284 if (!bmap)
285 return NULL;
287 ctx = isl_basic_map_get_ctx(bmap);
288 total = isl_space_dim(bmap->dim, isl_dim_all);
289 cols = 1 + 1 + total + bmap->n_div;
290 div = isl_mat_alloc(ctx, bmap->n_div, cols);
291 if (!div)
292 return NULL;
294 for (i = 0; i < bmap->n_div; ++i)
295 isl_seq_cpy(div->row[i], bmap->div[i], cols);
297 return div;
300 /* Extract the divs in "bset" as a matrix.
302 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
304 return isl_basic_map_get_divs(bset);
307 __isl_give isl_local_space *isl_basic_map_get_local_space(
308 __isl_keep isl_basic_map *bmap)
310 isl_mat *div;
312 if (!bmap)
313 return NULL;
315 div = isl_basic_map_get_divs(bmap);
316 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
319 __isl_give isl_local_space *isl_basic_set_get_local_space(
320 __isl_keep isl_basic_set *bset)
322 return isl_basic_map_get_local_space(bset);
325 __isl_give isl_basic_map *isl_basic_map_from_local_space(
326 __isl_take isl_local_space *ls)
328 int i;
329 int n_div;
330 isl_basic_map *bmap;
332 if (!ls)
333 return NULL;
335 n_div = isl_local_space_dim(ls, isl_dim_div);
336 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
337 n_div, 0, 2 * n_div);
339 for (i = 0; i < n_div; ++i)
340 if (isl_basic_map_alloc_div(bmap) < 0)
341 goto error;
343 for (i = 0; i < n_div; ++i) {
344 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
345 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
346 goto error;
349 isl_local_space_free(ls);
350 return bmap;
351 error:
352 isl_local_space_free(ls);
353 isl_basic_map_free(bmap);
354 return NULL;
357 __isl_give isl_basic_set *isl_basic_set_from_local_space(
358 __isl_take isl_local_space *ls)
360 return isl_basic_map_from_local_space(ls);
363 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
365 if (!map)
366 return NULL;
367 return isl_space_copy(map->dim);
370 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
372 if (!set)
373 return NULL;
374 return isl_space_copy(set->dim);
377 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
378 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
380 bmap = isl_basic_map_cow(bmap);
381 if (!bmap)
382 return NULL;
383 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
384 if (!bmap->dim)
385 goto error;
386 bmap = isl_basic_map_finalize(bmap);
387 return bmap;
388 error:
389 isl_basic_map_free(bmap);
390 return NULL;
393 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
394 __isl_take isl_basic_set *bset, const char *s)
396 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
399 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
400 enum isl_dim_type type)
402 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
405 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
406 enum isl_dim_type type, const char *s)
408 int i;
410 map = isl_map_cow(map);
411 if (!map)
412 return NULL;
414 map->dim = isl_space_set_tuple_name(map->dim, type, s);
415 if (!map->dim)
416 goto error;
418 for (i = 0; i < map->n; ++i) {
419 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
420 if (!map->p[i])
421 goto error;
424 return map;
425 error:
426 isl_map_free(map);
427 return NULL;
430 /* Replace the identifier of the tuple of type "type" by "id".
432 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
433 __isl_take isl_basic_map *bmap,
434 enum isl_dim_type type, __isl_take isl_id *id)
436 bmap = isl_basic_map_cow(bmap);
437 if (!bmap)
438 goto error;
439 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
440 if (!bmap->dim)
441 return isl_basic_map_free(bmap);
442 bmap = isl_basic_map_finalize(bmap);
443 return bmap;
444 error:
445 isl_id_free(id);
446 return NULL;
449 /* Replace the identifier of the tuple by "id".
451 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
452 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
454 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
457 /* Does the input or output tuple have a name?
459 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
461 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
464 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
465 enum isl_dim_type type)
467 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
470 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
471 const char *s)
473 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
476 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
477 enum isl_dim_type type, __isl_take isl_id *id)
479 map = isl_map_cow(map);
480 if (!map)
481 goto error;
483 map->dim = isl_space_set_tuple_id(map->dim, type, id);
485 return isl_map_reset_space(map, isl_space_copy(map->dim));
486 error:
487 isl_id_free(id);
488 return NULL;
491 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
492 __isl_take isl_id *id)
494 return isl_map_set_tuple_id(set, isl_dim_set, id);
497 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
498 enum isl_dim_type type)
500 map = isl_map_cow(map);
501 if (!map)
502 return NULL;
504 map->dim = isl_space_reset_tuple_id(map->dim, type);
506 return isl_map_reset_space(map, isl_space_copy(map->dim));
509 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
511 return isl_map_reset_tuple_id(set, isl_dim_set);
514 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
516 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
519 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
520 enum isl_dim_type type)
522 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
525 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
527 return isl_map_has_tuple_id(set, isl_dim_set);
530 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
532 return isl_map_get_tuple_id(set, isl_dim_set);
535 /* Does the set tuple have a name?
537 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
539 if (!set)
540 return isl_bool_error;
541 return isl_space_has_tuple_name(set->dim, isl_dim_set);
545 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
547 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
550 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
552 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
555 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
556 enum isl_dim_type type, unsigned pos)
558 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
561 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
562 enum isl_dim_type type, unsigned pos)
564 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
567 /* Does the given dimension have a name?
569 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
570 enum isl_dim_type type, unsigned pos)
572 if (!map)
573 return isl_bool_error;
574 return isl_space_has_dim_name(map->dim, type, pos);
577 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
578 enum isl_dim_type type, unsigned pos)
580 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
583 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
584 enum isl_dim_type type, unsigned pos)
586 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
589 /* Does the given dimension have a name?
591 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
592 enum isl_dim_type type, unsigned pos)
594 if (!set)
595 return isl_bool_error;
596 return isl_space_has_dim_name(set->dim, type, pos);
599 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
600 __isl_take isl_basic_map *bmap,
601 enum isl_dim_type type, unsigned pos, const char *s)
603 bmap = isl_basic_map_cow(bmap);
604 if (!bmap)
605 return NULL;
606 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
607 if (!bmap->dim)
608 goto error;
609 return isl_basic_map_finalize(bmap);
610 error:
611 isl_basic_map_free(bmap);
612 return NULL;
615 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
616 enum isl_dim_type type, unsigned pos, const char *s)
618 int i;
620 map = isl_map_cow(map);
621 if (!map)
622 return NULL;
624 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
625 if (!map->dim)
626 goto error;
628 for (i = 0; i < map->n; ++i) {
629 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
630 if (!map->p[i])
631 goto error;
634 return map;
635 error:
636 isl_map_free(map);
637 return NULL;
640 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
641 __isl_take isl_basic_set *bset,
642 enum isl_dim_type type, unsigned pos, const char *s)
644 return (isl_basic_set *)isl_basic_map_set_dim_name(
645 (isl_basic_map *)bset, type, pos, s);
648 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
649 enum isl_dim_type type, unsigned pos, const char *s)
651 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
654 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
655 enum isl_dim_type type, unsigned pos)
657 if (!bmap)
658 return isl_bool_error;
659 return isl_space_has_dim_id(bmap->dim, type, pos);
662 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
663 enum isl_dim_type type, unsigned pos)
665 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
668 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
669 enum isl_dim_type type, unsigned pos)
671 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
674 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
675 enum isl_dim_type type, unsigned pos)
677 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
680 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
681 enum isl_dim_type type, unsigned pos)
683 return isl_map_has_dim_id(set, type, pos);
686 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
687 enum isl_dim_type type, unsigned pos)
689 return isl_map_get_dim_id(set, type, pos);
692 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
693 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
695 map = isl_map_cow(map);
696 if (!map)
697 goto error;
699 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
701 return isl_map_reset_space(map, isl_space_copy(map->dim));
702 error:
703 isl_id_free(id);
704 return NULL;
707 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
708 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
710 return isl_map_set_dim_id(set, type, pos, id);
713 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
714 __isl_keep isl_id *id)
716 if (!map)
717 return -1;
718 return isl_space_find_dim_by_id(map->dim, type, id);
721 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
722 __isl_keep isl_id *id)
724 return isl_map_find_dim_by_id(set, type, id);
727 /* Return the position of the dimension of the given type and name
728 * in "bmap".
729 * Return -1 if no such dimension can be found.
731 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
732 enum isl_dim_type type, const char *name)
734 if (!bmap)
735 return -1;
736 return isl_space_find_dim_by_name(bmap->dim, type, name);
739 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
740 const char *name)
742 if (!map)
743 return -1;
744 return isl_space_find_dim_by_name(map->dim, type, name);
747 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
748 const char *name)
750 return isl_map_find_dim_by_name(set, type, name);
753 /* Reset the user pointer on all identifiers of parameters and tuples
754 * of the space of "map".
756 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
758 isl_space *space;
760 space = isl_map_get_space(map);
761 space = isl_space_reset_user(space);
762 map = isl_map_reset_space(map, space);
764 return map;
767 /* Reset the user pointer on all identifiers of parameters and tuples
768 * of the space of "set".
770 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
772 return isl_map_reset_user(set);
775 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
777 if (!bmap)
778 return -1;
779 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
782 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
784 return isl_basic_map_is_rational(bset);
787 /* Does "bmap" contain any rational points?
789 * If "bmap" has an equality for each dimension, equating the dimension
790 * to an integer constant, then it has no rational points, even if it
791 * is marked as rational.
793 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
795 int has_rational = 1;
796 unsigned total;
798 if (!bmap)
799 return -1;
800 if (isl_basic_map_plain_is_empty(bmap))
801 return 0;
802 if (!isl_basic_map_is_rational(bmap))
803 return 0;
804 bmap = isl_basic_map_copy(bmap);
805 bmap = isl_basic_map_implicit_equalities(bmap);
806 if (!bmap)
807 return -1;
808 total = isl_basic_map_total_dim(bmap);
809 if (bmap->n_eq == total) {
810 int i, j;
811 for (i = 0; i < bmap->n_eq; ++i) {
812 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
813 if (j < 0)
814 break;
815 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
816 !isl_int_is_negone(bmap->eq[i][1 + j]))
817 break;
818 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
819 total - j - 1);
820 if (j >= 0)
821 break;
823 if (i == bmap->n_eq)
824 has_rational = 0;
826 isl_basic_map_free(bmap);
828 return has_rational;
831 /* Does "map" contain any rational points?
833 int isl_map_has_rational(__isl_keep isl_map *map)
835 int i;
836 int has_rational;
838 if (!map)
839 return -1;
840 for (i = 0; i < map->n; ++i) {
841 has_rational = isl_basic_map_has_rational(map->p[i]);
842 if (has_rational < 0)
843 return -1;
844 if (has_rational)
845 return 1;
847 return 0;
850 /* Does "set" contain any rational points?
852 int isl_set_has_rational(__isl_keep isl_set *set)
854 return isl_map_has_rational(set);
857 /* Is this basic set a parameter domain?
859 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
861 if (!bset)
862 return -1;
863 return isl_space_is_params(bset->dim);
866 /* Is this set a parameter domain?
868 isl_bool isl_set_is_params(__isl_keep isl_set *set)
870 if (!set)
871 return isl_bool_error;
872 return isl_space_is_params(set->dim);
875 /* Is this map actually a parameter domain?
876 * Users should never call this function. Outside of isl,
877 * a map can never be a parameter domain.
879 int isl_map_is_params(__isl_keep isl_map *map)
881 if (!map)
882 return -1;
883 return isl_space_is_params(map->dim);
886 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
887 struct isl_basic_map *bmap, unsigned extra,
888 unsigned n_eq, unsigned n_ineq)
890 int i;
891 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
893 bmap->ctx = ctx;
894 isl_ctx_ref(ctx);
896 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
897 if (isl_blk_is_error(bmap->block))
898 goto error;
900 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
901 if ((n_ineq + n_eq) && !bmap->ineq)
902 goto error;
904 if (extra == 0) {
905 bmap->block2 = isl_blk_empty();
906 bmap->div = NULL;
907 } else {
908 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
909 if (isl_blk_is_error(bmap->block2))
910 goto error;
912 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
913 if (!bmap->div)
914 goto error;
917 for (i = 0; i < n_ineq + n_eq; ++i)
918 bmap->ineq[i] = bmap->block.data + i * row_size;
920 for (i = 0; i < extra; ++i)
921 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
923 bmap->ref = 1;
924 bmap->flags = 0;
925 bmap->c_size = n_eq + n_ineq;
926 bmap->eq = bmap->ineq + n_ineq;
927 bmap->extra = extra;
928 bmap->n_eq = 0;
929 bmap->n_ineq = 0;
930 bmap->n_div = 0;
931 bmap->sample = NULL;
933 return bmap;
934 error:
935 isl_basic_map_free(bmap);
936 return NULL;
939 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
940 unsigned nparam, unsigned dim, unsigned extra,
941 unsigned n_eq, unsigned n_ineq)
943 struct isl_basic_map *bmap;
944 isl_space *space;
946 space = isl_space_set_alloc(ctx, nparam, dim);
947 if (!space)
948 return NULL;
950 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
951 return (struct isl_basic_set *)bmap;
954 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
955 unsigned extra, unsigned n_eq, unsigned n_ineq)
957 struct isl_basic_map *bmap;
958 if (!dim)
959 return NULL;
960 isl_assert(dim->ctx, dim->n_in == 0, goto error);
961 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
962 return (struct isl_basic_set *)bmap;
963 error:
964 isl_space_free(dim);
965 return NULL;
968 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
969 unsigned extra, unsigned n_eq, unsigned n_ineq)
971 struct isl_basic_map *bmap;
973 if (!dim)
974 return NULL;
975 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
976 if (!bmap)
977 goto error;
978 bmap->dim = dim;
980 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
981 error:
982 isl_space_free(dim);
983 return NULL;
986 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
987 unsigned nparam, unsigned in, unsigned out, unsigned extra,
988 unsigned n_eq, unsigned n_ineq)
990 struct isl_basic_map *bmap;
991 isl_space *dim;
993 dim = isl_space_alloc(ctx, nparam, in, out);
994 if (!dim)
995 return NULL;
997 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
998 return bmap;
1001 static void dup_constraints(
1002 struct isl_basic_map *dst, struct isl_basic_map *src)
1004 int i;
1005 unsigned total = isl_basic_map_total_dim(src);
1007 for (i = 0; i < src->n_eq; ++i) {
1008 int j = isl_basic_map_alloc_equality(dst);
1009 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1012 for (i = 0; i < src->n_ineq; ++i) {
1013 int j = isl_basic_map_alloc_inequality(dst);
1014 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1017 for (i = 0; i < src->n_div; ++i) {
1018 int j = isl_basic_map_alloc_div(dst);
1019 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1021 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1024 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1026 struct isl_basic_map *dup;
1028 if (!bmap)
1029 return NULL;
1030 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1031 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1032 if (!dup)
1033 return NULL;
1034 dup_constraints(dup, bmap);
1035 dup->flags = bmap->flags;
1036 dup->sample = isl_vec_copy(bmap->sample);
1037 return dup;
1040 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1042 struct isl_basic_map *dup;
1044 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1045 return (struct isl_basic_set *)dup;
1048 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1050 if (!bset)
1051 return NULL;
1053 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1054 bset->ref++;
1055 return bset;
1057 return isl_basic_set_dup(bset);
1060 struct isl_set *isl_set_copy(struct isl_set *set)
1062 if (!set)
1063 return NULL;
1065 set->ref++;
1066 return set;
1069 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1071 if (!bmap)
1072 return NULL;
1074 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1075 bmap->ref++;
1076 return bmap;
1078 bmap = isl_basic_map_dup(bmap);
1079 if (bmap)
1080 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1081 return bmap;
1084 struct isl_map *isl_map_copy(struct isl_map *map)
1086 if (!map)
1087 return NULL;
1089 map->ref++;
1090 return map;
1093 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1095 if (!bmap)
1096 return NULL;
1098 if (--bmap->ref > 0)
1099 return NULL;
1101 isl_ctx_deref(bmap->ctx);
1102 free(bmap->div);
1103 isl_blk_free(bmap->ctx, bmap->block2);
1104 free(bmap->ineq);
1105 isl_blk_free(bmap->ctx, bmap->block);
1106 isl_vec_free(bmap->sample);
1107 isl_space_free(bmap->dim);
1108 free(bmap);
1110 return NULL;
1113 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1115 return isl_basic_map_free((struct isl_basic_map *)bset);
1118 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1120 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1123 __isl_give isl_map *isl_map_align_params_map_map_and(
1124 __isl_take isl_map *map1, __isl_take isl_map *map2,
1125 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1126 __isl_take isl_map *map2))
1128 if (!map1 || !map2)
1129 goto error;
1130 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1131 return fn(map1, map2);
1132 if (!isl_space_has_named_params(map1->dim) ||
1133 !isl_space_has_named_params(map2->dim))
1134 isl_die(map1->ctx, isl_error_invalid,
1135 "unaligned unnamed parameters", goto error);
1136 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1137 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1138 return fn(map1, map2);
1139 error:
1140 isl_map_free(map1);
1141 isl_map_free(map2);
1142 return NULL;
1145 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1146 __isl_keep isl_map *map2,
1147 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1149 isl_bool r;
1151 if (!map1 || !map2)
1152 return isl_bool_error;
1153 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1154 return fn(map1, map2);
1155 if (!isl_space_has_named_params(map1->dim) ||
1156 !isl_space_has_named_params(map2->dim))
1157 isl_die(map1->ctx, isl_error_invalid,
1158 "unaligned unnamed parameters", return isl_bool_error);
1159 map1 = isl_map_copy(map1);
1160 map2 = isl_map_copy(map2);
1161 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1162 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1163 r = fn(map1, map2);
1164 isl_map_free(map1);
1165 isl_map_free(map2);
1166 return r;
1169 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1171 struct isl_ctx *ctx;
1172 if (!bmap)
1173 return -1;
1174 ctx = bmap->ctx;
1175 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1176 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1177 return -1);
1178 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1179 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1180 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1181 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1182 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1183 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1184 isl_int *t;
1185 int j = isl_basic_map_alloc_inequality(bmap);
1186 if (j < 0)
1187 return -1;
1188 t = bmap->ineq[j];
1189 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1190 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1191 bmap->eq[-1] = t;
1192 bmap->n_eq++;
1193 bmap->n_ineq--;
1194 bmap->eq--;
1195 return 0;
1197 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1198 bmap->extra - bmap->n_div);
1199 return bmap->n_eq++;
1202 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1204 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1207 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1209 if (!bmap)
1210 return -1;
1211 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1212 bmap->n_eq -= n;
1213 return 0;
1216 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1218 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1221 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1223 isl_int *t;
1224 if (!bmap)
1225 return -1;
1226 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1228 if (pos != bmap->n_eq - 1) {
1229 t = bmap->eq[pos];
1230 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1231 bmap->eq[bmap->n_eq - 1] = t;
1233 bmap->n_eq--;
1234 return 0;
1237 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1239 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1242 /* Turn inequality "pos" of "bmap" into an equality.
1244 * In particular, we move the inequality in front of the equalities
1245 * and move the last inequality in the position of the moved inequality.
1246 * Note that isl_tab_make_equalities_explicit depends on this particular
1247 * change in the ordering of the constraints.
1249 void isl_basic_map_inequality_to_equality(
1250 struct isl_basic_map *bmap, unsigned pos)
1252 isl_int *t;
1254 t = bmap->ineq[pos];
1255 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1256 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1257 bmap->eq[-1] = t;
1258 bmap->n_eq++;
1259 bmap->n_ineq--;
1260 bmap->eq--;
1261 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1262 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1263 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1264 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1267 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1269 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1272 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1274 struct isl_ctx *ctx;
1275 if (!bmap)
1276 return -1;
1277 ctx = bmap->ctx;
1278 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1279 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1280 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1281 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1282 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1283 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1284 1 + isl_basic_map_total_dim(bmap),
1285 bmap->extra - bmap->n_div);
1286 return bmap->n_ineq++;
1289 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1291 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1294 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1296 if (!bmap)
1297 return -1;
1298 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1299 bmap->n_ineq -= n;
1300 return 0;
1303 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1305 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1308 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1310 isl_int *t;
1311 if (!bmap)
1312 return -1;
1313 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1315 if (pos != bmap->n_ineq - 1) {
1316 t = bmap->ineq[pos];
1317 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1318 bmap->ineq[bmap->n_ineq - 1] = t;
1319 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1321 bmap->n_ineq--;
1322 return 0;
1325 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1327 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1330 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1331 isl_int *eq)
1333 int k;
1335 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1336 if (!bmap)
1337 return NULL;
1338 k = isl_basic_map_alloc_equality(bmap);
1339 if (k < 0)
1340 goto error;
1341 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1342 return bmap;
1343 error:
1344 isl_basic_map_free(bmap);
1345 return NULL;
1348 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1349 isl_int *eq)
1351 return (isl_basic_set *)
1352 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1355 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1356 isl_int *ineq)
1358 int k;
1360 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1361 if (!bmap)
1362 return NULL;
1363 k = isl_basic_map_alloc_inequality(bmap);
1364 if (k < 0)
1365 goto error;
1366 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1367 return bmap;
1368 error:
1369 isl_basic_map_free(bmap);
1370 return NULL;
1373 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1374 isl_int *ineq)
1376 return (isl_basic_set *)
1377 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1380 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1382 if (!bmap)
1383 return -1;
1384 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1385 isl_seq_clr(bmap->div[bmap->n_div] +
1386 1 + 1 + isl_basic_map_total_dim(bmap),
1387 bmap->extra - bmap->n_div);
1388 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1389 return bmap->n_div++;
1392 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1394 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1397 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1399 if (!bmap)
1400 return -1;
1401 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1402 bmap->n_div -= n;
1403 return 0;
1406 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1408 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1411 /* Copy constraint from src to dst, putting the vars of src at offset
1412 * dim_off in dst and the divs of src at offset div_off in dst.
1413 * If both sets are actually map, then dim_off applies to the input
1414 * variables.
1416 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1417 struct isl_basic_map *src_map, isl_int *src,
1418 unsigned in_off, unsigned out_off, unsigned div_off)
1420 unsigned src_nparam = isl_basic_map_n_param(src_map);
1421 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1422 unsigned src_in = isl_basic_map_n_in(src_map);
1423 unsigned dst_in = isl_basic_map_n_in(dst_map);
1424 unsigned src_out = isl_basic_map_n_out(src_map);
1425 unsigned dst_out = isl_basic_map_n_out(dst_map);
1426 isl_int_set(dst[0], src[0]);
1427 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1428 if (dst_nparam > src_nparam)
1429 isl_seq_clr(dst+1+src_nparam,
1430 dst_nparam - src_nparam);
1431 isl_seq_clr(dst+1+dst_nparam, in_off);
1432 isl_seq_cpy(dst+1+dst_nparam+in_off,
1433 src+1+src_nparam,
1434 isl_min(dst_in-in_off, src_in));
1435 if (dst_in-in_off > src_in)
1436 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1437 dst_in - in_off - src_in);
1438 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1439 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1440 src+1+src_nparam+src_in,
1441 isl_min(dst_out-out_off, src_out));
1442 if (dst_out-out_off > src_out)
1443 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1444 dst_out - out_off - src_out);
1445 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1446 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1447 src+1+src_nparam+src_in+src_out,
1448 isl_min(dst_map->extra-div_off, src_map->n_div));
1449 if (dst_map->n_div-div_off > src_map->n_div)
1450 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1451 div_off+src_map->n_div,
1452 dst_map->n_div - div_off - src_map->n_div);
1455 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1456 struct isl_basic_map *src_map, isl_int *src,
1457 unsigned in_off, unsigned out_off, unsigned div_off)
1459 isl_int_set(dst[0], src[0]);
1460 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1463 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1464 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1466 int i;
1467 unsigned div_off;
1469 if (!bmap1 || !bmap2)
1470 goto error;
1472 div_off = bmap1->n_div;
1474 for (i = 0; i < bmap2->n_eq; ++i) {
1475 int i1 = isl_basic_map_alloc_equality(bmap1);
1476 if (i1 < 0)
1477 goto error;
1478 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1479 i_pos, o_pos, div_off);
1482 for (i = 0; i < bmap2->n_ineq; ++i) {
1483 int i1 = isl_basic_map_alloc_inequality(bmap1);
1484 if (i1 < 0)
1485 goto error;
1486 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1487 i_pos, o_pos, div_off);
1490 for (i = 0; i < bmap2->n_div; ++i) {
1491 int i1 = isl_basic_map_alloc_div(bmap1);
1492 if (i1 < 0)
1493 goto error;
1494 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1495 i_pos, o_pos, div_off);
1498 isl_basic_map_free(bmap2);
1500 return bmap1;
1502 error:
1503 isl_basic_map_free(bmap1);
1504 isl_basic_map_free(bmap2);
1505 return NULL;
1508 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1509 struct isl_basic_set *bset2, unsigned pos)
1511 return (struct isl_basic_set *)
1512 add_constraints((struct isl_basic_map *)bset1,
1513 (struct isl_basic_map *)bset2, 0, pos);
1516 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1517 __isl_take isl_space *dim, unsigned extra,
1518 unsigned n_eq, unsigned n_ineq)
1520 struct isl_basic_map *ext;
1521 unsigned flags;
1522 int dims_ok;
1524 if (!dim)
1525 goto error;
1527 if (!base)
1528 goto error;
1530 dims_ok = isl_space_is_equal(base->dim, dim) &&
1531 base->extra >= base->n_div + extra;
1533 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1534 room_for_ineq(base, n_ineq)) {
1535 isl_space_free(dim);
1536 return base;
1539 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1540 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1541 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1542 extra += base->extra;
1543 n_eq += base->n_eq;
1544 n_ineq += base->n_ineq;
1546 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1547 dim = NULL;
1548 if (!ext)
1549 goto error;
1551 if (dims_ok)
1552 ext->sample = isl_vec_copy(base->sample);
1553 flags = base->flags;
1554 ext = add_constraints(ext, base, 0, 0);
1555 if (ext) {
1556 ext->flags = flags;
1557 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1560 return ext;
1562 error:
1563 isl_space_free(dim);
1564 isl_basic_map_free(base);
1565 return NULL;
1568 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1569 __isl_take isl_space *dim, unsigned extra,
1570 unsigned n_eq, unsigned n_ineq)
1572 return (struct isl_basic_set *)
1573 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1574 extra, n_eq, n_ineq);
1577 struct isl_basic_map *isl_basic_map_extend_constraints(
1578 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1580 if (!base)
1581 return NULL;
1582 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1583 0, n_eq, n_ineq);
1586 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1587 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1588 unsigned n_eq, unsigned n_ineq)
1590 struct isl_basic_map *bmap;
1591 isl_space *dim;
1593 if (!base)
1594 return NULL;
1595 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1596 if (!dim)
1597 goto error;
1599 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1600 return bmap;
1601 error:
1602 isl_basic_map_free(base);
1603 return NULL;
1606 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1607 unsigned nparam, unsigned dim, unsigned extra,
1608 unsigned n_eq, unsigned n_ineq)
1610 return (struct isl_basic_set *)
1611 isl_basic_map_extend((struct isl_basic_map *)base,
1612 nparam, 0, dim, extra, n_eq, n_ineq);
1615 struct isl_basic_set *isl_basic_set_extend_constraints(
1616 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1618 return (struct isl_basic_set *)
1619 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1620 n_eq, n_ineq);
1623 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1625 return (struct isl_basic_set *)
1626 isl_basic_map_cow((struct isl_basic_map *)bset);
1629 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1631 if (!bmap)
1632 return NULL;
1634 if (bmap->ref > 1) {
1635 bmap->ref--;
1636 bmap = isl_basic_map_dup(bmap);
1638 if (bmap) {
1639 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1640 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1642 return bmap;
1645 struct isl_set *isl_set_cow(struct isl_set *set)
1647 if (!set)
1648 return NULL;
1650 if (set->ref == 1)
1651 return set;
1652 set->ref--;
1653 return isl_set_dup(set);
1656 struct isl_map *isl_map_cow(struct isl_map *map)
1658 if (!map)
1659 return NULL;
1661 if (map->ref == 1)
1662 return map;
1663 map->ref--;
1664 return isl_map_dup(map);
1667 static void swap_vars(struct isl_blk blk, isl_int *a,
1668 unsigned a_len, unsigned b_len)
1670 isl_seq_cpy(blk.data, a+a_len, b_len);
1671 isl_seq_cpy(blk.data+b_len, a, a_len);
1672 isl_seq_cpy(a, blk.data, b_len+a_len);
1675 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1676 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1678 int i;
1679 struct isl_blk blk;
1681 if (!bmap)
1682 goto error;
1684 isl_assert(bmap->ctx,
1685 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1687 if (n1 == 0 || n2 == 0)
1688 return bmap;
1690 bmap = isl_basic_map_cow(bmap);
1691 if (!bmap)
1692 return NULL;
1694 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1695 if (isl_blk_is_error(blk))
1696 goto error;
1698 for (i = 0; i < bmap->n_eq; ++i)
1699 swap_vars(blk,
1700 bmap->eq[i] + pos, n1, n2);
1702 for (i = 0; i < bmap->n_ineq; ++i)
1703 swap_vars(blk,
1704 bmap->ineq[i] + pos, n1, n2);
1706 for (i = 0; i < bmap->n_div; ++i)
1707 swap_vars(blk,
1708 bmap->div[i]+1 + pos, n1, n2);
1710 isl_blk_free(bmap->ctx, blk);
1712 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1713 bmap = isl_basic_map_gauss(bmap, NULL);
1714 return isl_basic_map_finalize(bmap);
1715 error:
1716 isl_basic_map_free(bmap);
1717 return NULL;
1720 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1722 int i = 0;
1723 unsigned total;
1724 if (!bmap)
1725 goto error;
1726 total = isl_basic_map_total_dim(bmap);
1727 isl_basic_map_free_div(bmap, bmap->n_div);
1728 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1729 if (bmap->n_eq > 0)
1730 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1731 else {
1732 i = isl_basic_map_alloc_equality(bmap);
1733 if (i < 0)
1734 goto error;
1736 isl_int_set_si(bmap->eq[i][0], 1);
1737 isl_seq_clr(bmap->eq[i]+1, total);
1738 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1739 isl_vec_free(bmap->sample);
1740 bmap->sample = NULL;
1741 return isl_basic_map_finalize(bmap);
1742 error:
1743 isl_basic_map_free(bmap);
1744 return NULL;
1747 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1749 return (struct isl_basic_set *)
1750 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1753 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1754 * of "bmap").
1756 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1758 isl_int *t = bmap->div[a];
1759 bmap->div[a] = bmap->div[b];
1760 bmap->div[b] = t;
1763 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1764 * div definitions accordingly.
1766 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1768 int i;
1769 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1771 swap_div(bmap, a, b);
1773 for (i = 0; i < bmap->n_eq; ++i)
1774 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1776 for (i = 0; i < bmap->n_ineq; ++i)
1777 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1779 for (i = 0; i < bmap->n_div; ++i)
1780 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1781 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1784 /* Eliminate the specified n dimensions starting at first from the
1785 * constraints, without removing the dimensions from the space.
1786 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1788 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1789 enum isl_dim_type type, unsigned first, unsigned n)
1791 int i;
1793 if (!map)
1794 return NULL;
1795 if (n == 0)
1796 return map;
1798 if (first + n > isl_map_dim(map, type) || first + n < first)
1799 isl_die(map->ctx, isl_error_invalid,
1800 "index out of bounds", goto error);
1802 map = isl_map_cow(map);
1803 if (!map)
1804 return NULL;
1806 for (i = 0; i < map->n; ++i) {
1807 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1808 if (!map->p[i])
1809 goto error;
1811 return map;
1812 error:
1813 isl_map_free(map);
1814 return NULL;
1817 /* Eliminate the specified n dimensions starting at first from the
1818 * constraints, without removing the dimensions from the space.
1819 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1821 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1822 enum isl_dim_type type, unsigned first, unsigned n)
1824 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1827 /* Eliminate the specified n dimensions starting at first from the
1828 * constraints, without removing the dimensions from the space.
1829 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1831 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1832 unsigned first, unsigned n)
1834 return isl_set_eliminate(set, isl_dim_set, first, n);
1837 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1838 __isl_take isl_basic_map *bmap)
1840 if (!bmap)
1841 return NULL;
1842 bmap = isl_basic_map_eliminate_vars(bmap,
1843 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1844 if (!bmap)
1845 return NULL;
1846 bmap->n_div = 0;
1847 return isl_basic_map_finalize(bmap);
1850 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1851 __isl_take isl_basic_set *bset)
1853 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1854 (struct isl_basic_map *)bset);
1857 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1859 int i;
1861 if (!map)
1862 return NULL;
1863 if (map->n == 0)
1864 return map;
1866 map = isl_map_cow(map);
1867 if (!map)
1868 return NULL;
1870 for (i = 0; i < map->n; ++i) {
1871 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1872 if (!map->p[i])
1873 goto error;
1875 return map;
1876 error:
1877 isl_map_free(map);
1878 return NULL;
1881 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1883 return isl_map_remove_divs(set);
1886 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1887 enum isl_dim_type type, unsigned first, unsigned n)
1889 if (!bmap)
1890 return NULL;
1891 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1892 goto error);
1893 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1894 return bmap;
1895 bmap = isl_basic_map_eliminate_vars(bmap,
1896 isl_basic_map_offset(bmap, type) - 1 + first, n);
1897 if (!bmap)
1898 return bmap;
1899 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1900 return bmap;
1901 bmap = isl_basic_map_drop(bmap, type, first, n);
1902 return bmap;
1903 error:
1904 isl_basic_map_free(bmap);
1905 return NULL;
1908 /* Return true if the definition of the given div (recursively) involves
1909 * any of the given variables.
1911 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1912 unsigned first, unsigned n)
1914 int i;
1915 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1917 if (isl_int_is_zero(bmap->div[div][0]))
1918 return 0;
1919 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1920 return 1;
1922 for (i = bmap->n_div - 1; i >= 0; --i) {
1923 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1924 continue;
1925 if (div_involves_vars(bmap, i, first, n))
1926 return 1;
1929 return 0;
1932 /* Try and add a lower and/or upper bound on "div" to "bmap"
1933 * based on inequality "i".
1934 * "total" is the total number of variables (excluding the divs).
1935 * "v" is a temporary object that can be used during the calculations.
1936 * If "lb" is set, then a lower bound should be constructed.
1937 * If "ub" is set, then an upper bound should be constructed.
1939 * The calling function has already checked that the inequality does not
1940 * reference "div", but we still need to check that the inequality is
1941 * of the right form. We'll consider the case where we want to construct
1942 * a lower bound. The construction of upper bounds is similar.
1944 * Let "div" be of the form
1946 * q = floor((a + f(x))/d)
1948 * We essentially check if constraint "i" is of the form
1950 * b + f(x) >= 0
1952 * so that we can use it to derive a lower bound on "div".
1953 * However, we allow a slightly more general form
1955 * b + g(x) >= 0
1957 * with the condition that the coefficients of g(x) - f(x) are all
1958 * divisible by d.
1959 * Rewriting this constraint as
1961 * 0 >= -b - g(x)
1963 * adding a + f(x) to both sides and dividing by d, we obtain
1965 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1967 * Taking the floor on both sides, we obtain
1969 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1971 * or
1973 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1975 * In the case of an upper bound, we construct the constraint
1977 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1980 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1981 __isl_take isl_basic_map *bmap, int div, int i,
1982 unsigned total, isl_int v, int lb, int ub)
1984 int j;
1986 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1987 if (lb) {
1988 isl_int_sub(v, bmap->ineq[i][1 + j],
1989 bmap->div[div][1 + 1 + j]);
1990 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1992 if (ub) {
1993 isl_int_add(v, bmap->ineq[i][1 + j],
1994 bmap->div[div][1 + 1 + j]);
1995 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1998 if (!lb && !ub)
1999 return bmap;
2001 bmap = isl_basic_map_cow(bmap);
2002 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2003 if (lb) {
2004 int k = isl_basic_map_alloc_inequality(bmap);
2005 if (k < 0)
2006 goto error;
2007 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2008 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2009 bmap->div[div][1 + j]);
2010 isl_int_cdiv_q(bmap->ineq[k][j],
2011 bmap->ineq[k][j], bmap->div[div][0]);
2013 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2015 if (ub) {
2016 int k = isl_basic_map_alloc_inequality(bmap);
2017 if (k < 0)
2018 goto error;
2019 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2020 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2021 bmap->div[div][1 + j]);
2022 isl_int_fdiv_q(bmap->ineq[k][j],
2023 bmap->ineq[k][j], bmap->div[div][0]);
2025 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2028 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2029 return bmap;
2030 error:
2031 isl_basic_map_free(bmap);
2032 return NULL;
2035 /* This function is called right before "div" is eliminated from "bmap"
2036 * using Fourier-Motzkin.
2037 * Look through the constraints of "bmap" for constraints on the argument
2038 * of the integer division and use them to construct constraints on the
2039 * integer division itself. These constraints can then be combined
2040 * during the Fourier-Motzkin elimination.
2041 * Note that it is only useful to introduce lower bounds on "div"
2042 * if "bmap" already contains upper bounds on "div" as the newly
2043 * introduce lower bounds can then be combined with the pre-existing
2044 * upper bounds. Similarly for upper bounds.
2045 * We therefore first check if "bmap" contains any lower and/or upper bounds
2046 * on "div".
2048 * It is interesting to note that the introduction of these constraints
2049 * can indeed lead to more accurate results, even when compared to
2050 * deriving constraints on the argument of "div" from constraints on "div".
2051 * Consider, for example, the set
2053 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2055 * The second constraint can be rewritten as
2057 * 2 * [(-i-2j+3)/4] + k >= 0
2059 * from which we can derive
2061 * -i - 2j + 3 >= -2k
2063 * or
2065 * i + 2j <= 3 + 2k
2067 * Combined with the first constraint, we obtain
2069 * -3 <= 3 + 2k or k >= -3
2071 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2072 * the first constraint, we obtain
2074 * [(i + 2j)/4] >= [-3/4] = -1
2076 * Combining this constraint with the second constraint, we obtain
2078 * k >= -2
2080 static __isl_give isl_basic_map *insert_bounds_on_div(
2081 __isl_take isl_basic_map *bmap, int div)
2083 int i;
2084 int check_lb, check_ub;
2085 isl_int v;
2086 unsigned total;
2088 if (!bmap)
2089 return NULL;
2091 if (isl_int_is_zero(bmap->div[div][0]))
2092 return bmap;
2094 total = isl_space_dim(bmap->dim, isl_dim_all);
2096 check_lb = 0;
2097 check_ub = 0;
2098 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2099 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2100 if (s > 0)
2101 check_ub = 1;
2102 if (s < 0)
2103 check_lb = 1;
2106 if (!check_lb && !check_ub)
2107 return bmap;
2109 isl_int_init(v);
2111 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2112 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2113 continue;
2115 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2116 check_lb, check_ub);
2119 isl_int_clear(v);
2121 return bmap;
2124 /* Remove all divs (recursively) involving any of the given dimensions
2125 * in their definitions.
2127 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2128 __isl_take isl_basic_map *bmap,
2129 enum isl_dim_type type, unsigned first, unsigned n)
2131 int i;
2133 if (!bmap)
2134 return NULL;
2135 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2136 goto error);
2137 first += isl_basic_map_offset(bmap, type);
2139 for (i = bmap->n_div - 1; i >= 0; --i) {
2140 if (!div_involves_vars(bmap, i, first, n))
2141 continue;
2142 bmap = insert_bounds_on_div(bmap, i);
2143 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2144 if (!bmap)
2145 return NULL;
2146 i = bmap->n_div;
2149 return bmap;
2150 error:
2151 isl_basic_map_free(bmap);
2152 return NULL;
2155 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2156 __isl_take isl_basic_set *bset,
2157 enum isl_dim_type type, unsigned first, unsigned n)
2159 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2162 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2163 enum isl_dim_type type, unsigned first, unsigned n)
2165 int i;
2167 if (!map)
2168 return NULL;
2169 if (map->n == 0)
2170 return map;
2172 map = isl_map_cow(map);
2173 if (!map)
2174 return NULL;
2176 for (i = 0; i < map->n; ++i) {
2177 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2178 type, first, n);
2179 if (!map->p[i])
2180 goto error;
2182 return map;
2183 error:
2184 isl_map_free(map);
2185 return NULL;
2188 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2189 enum isl_dim_type type, unsigned first, unsigned n)
2191 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2192 type, first, n);
2195 /* Does the desciption of "bmap" depend on the specified dimensions?
2196 * We also check whether the dimensions appear in any of the div definitions.
2197 * In principle there is no need for this check. If the dimensions appear
2198 * in a div definition, they also appear in the defining constraints of that
2199 * div.
2201 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2202 enum isl_dim_type type, unsigned first, unsigned n)
2204 int i;
2206 if (!bmap)
2207 return isl_bool_error;
2209 if (first + n > isl_basic_map_dim(bmap, type))
2210 isl_die(bmap->ctx, isl_error_invalid,
2211 "index out of bounds", return isl_bool_error);
2213 first += isl_basic_map_offset(bmap, type);
2214 for (i = 0; i < bmap->n_eq; ++i)
2215 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2216 return isl_bool_true;
2217 for (i = 0; i < bmap->n_ineq; ++i)
2218 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2219 return isl_bool_true;
2220 for (i = 0; i < bmap->n_div; ++i) {
2221 if (isl_int_is_zero(bmap->div[i][0]))
2222 continue;
2223 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2224 return isl_bool_true;
2227 return isl_bool_false;
2230 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2231 enum isl_dim_type type, unsigned first, unsigned n)
2233 int i;
2235 if (!map)
2236 return isl_bool_error;
2238 if (first + n > isl_map_dim(map, type))
2239 isl_die(map->ctx, isl_error_invalid,
2240 "index out of bounds", return isl_bool_error);
2242 for (i = 0; i < map->n; ++i) {
2243 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2244 type, first, n);
2245 if (involves < 0 || involves)
2246 return involves;
2249 return isl_bool_false;
2252 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2253 enum isl_dim_type type, unsigned first, unsigned n)
2255 return isl_basic_map_involves_dims(bset, type, first, n);
2258 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2259 enum isl_dim_type type, unsigned first, unsigned n)
2261 return isl_map_involves_dims(set, type, first, n);
2264 /* Return true if the definition of the given div is unknown or depends
2265 * on unknown divs.
2267 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2269 int i;
2270 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2272 if (isl_int_is_zero(bmap->div[div][0]))
2273 return 1;
2275 for (i = bmap->n_div - 1; i >= 0; --i) {
2276 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2277 continue;
2278 if (div_is_unknown(bmap, i))
2279 return 1;
2282 return 0;
2285 /* Remove all divs that are unknown or defined in terms of unknown divs.
2287 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2288 __isl_take isl_basic_map *bmap)
2290 int i;
2292 if (!bmap)
2293 return NULL;
2295 for (i = bmap->n_div - 1; i >= 0; --i) {
2296 if (!div_is_unknown(bmap, i))
2297 continue;
2298 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2299 if (!bmap)
2300 return NULL;
2301 i = bmap->n_div;
2304 return bmap;
2307 /* Remove all divs that are unknown or defined in terms of unknown divs.
2309 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2310 __isl_take isl_basic_set *bset)
2312 return isl_basic_map_remove_unknown_divs(bset);
2315 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2317 int i;
2319 if (!map)
2320 return NULL;
2321 if (map->n == 0)
2322 return map;
2324 map = isl_map_cow(map);
2325 if (!map)
2326 return NULL;
2328 for (i = 0; i < map->n; ++i) {
2329 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2330 if (!map->p[i])
2331 goto error;
2333 return map;
2334 error:
2335 isl_map_free(map);
2336 return NULL;
2339 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2341 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2344 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2345 __isl_take isl_basic_set *bset,
2346 enum isl_dim_type type, unsigned first, unsigned n)
2348 return (isl_basic_set *)
2349 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2352 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2353 enum isl_dim_type type, unsigned first, unsigned n)
2355 int i;
2357 if (n == 0)
2358 return map;
2360 map = isl_map_cow(map);
2361 if (!map)
2362 return NULL;
2363 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2365 for (i = 0; i < map->n; ++i) {
2366 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2367 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2368 if (!map->p[i])
2369 goto error;
2371 map = isl_map_drop(map, type, first, n);
2372 return map;
2373 error:
2374 isl_map_free(map);
2375 return NULL;
2378 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2379 enum isl_dim_type type, unsigned first, unsigned n)
2381 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2384 /* Project out n inputs starting at first using Fourier-Motzkin */
2385 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2386 unsigned first, unsigned n)
2388 return isl_map_remove_dims(map, isl_dim_in, first, n);
2391 static void dump_term(struct isl_basic_map *bmap,
2392 isl_int c, int pos, FILE *out)
2394 const char *name;
2395 unsigned in = isl_basic_map_n_in(bmap);
2396 unsigned dim = in + isl_basic_map_n_out(bmap);
2397 unsigned nparam = isl_basic_map_n_param(bmap);
2398 if (!pos)
2399 isl_int_print(out, c, 0);
2400 else {
2401 if (!isl_int_is_one(c))
2402 isl_int_print(out, c, 0);
2403 if (pos < 1 + nparam) {
2404 name = isl_space_get_dim_name(bmap->dim,
2405 isl_dim_param, pos - 1);
2406 if (name)
2407 fprintf(out, "%s", name);
2408 else
2409 fprintf(out, "p%d", pos - 1);
2410 } else if (pos < 1 + nparam + in)
2411 fprintf(out, "i%d", pos - 1 - nparam);
2412 else if (pos < 1 + nparam + dim)
2413 fprintf(out, "o%d", pos - 1 - nparam - in);
2414 else
2415 fprintf(out, "e%d", pos - 1 - nparam - dim);
2419 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2420 int sign, FILE *out)
2422 int i;
2423 int first;
2424 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2425 isl_int v;
2427 isl_int_init(v);
2428 for (i = 0, first = 1; i < len; ++i) {
2429 if (isl_int_sgn(c[i]) * sign <= 0)
2430 continue;
2431 if (!first)
2432 fprintf(out, " + ");
2433 first = 0;
2434 isl_int_abs(v, c[i]);
2435 dump_term(bmap, v, i, out);
2437 isl_int_clear(v);
2438 if (first)
2439 fprintf(out, "0");
2442 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2443 const char *op, FILE *out, int indent)
2445 int i;
2447 fprintf(out, "%*s", indent, "");
2449 dump_constraint_sign(bmap, c, 1, out);
2450 fprintf(out, " %s ", op);
2451 dump_constraint_sign(bmap, c, -1, out);
2453 fprintf(out, "\n");
2455 for (i = bmap->n_div; i < bmap->extra; ++i) {
2456 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2457 continue;
2458 fprintf(out, "%*s", indent, "");
2459 fprintf(out, "ERROR: unused div coefficient not zero\n");
2460 abort();
2464 static void dump_constraints(struct isl_basic_map *bmap,
2465 isl_int **c, unsigned n,
2466 const char *op, FILE *out, int indent)
2468 int i;
2470 for (i = 0; i < n; ++i)
2471 dump_constraint(bmap, c[i], op, out, indent);
2474 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2476 int j;
2477 int first = 1;
2478 unsigned total = isl_basic_map_total_dim(bmap);
2480 for (j = 0; j < 1 + total; ++j) {
2481 if (isl_int_is_zero(exp[j]))
2482 continue;
2483 if (!first && isl_int_is_pos(exp[j]))
2484 fprintf(out, "+");
2485 dump_term(bmap, exp[j], j, out);
2486 first = 0;
2490 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2492 int i;
2494 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2495 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2497 for (i = 0; i < bmap->n_div; ++i) {
2498 fprintf(out, "%*s", indent, "");
2499 fprintf(out, "e%d = [(", i);
2500 dump_affine(bmap, bmap->div[i]+1, out);
2501 fprintf(out, ")/");
2502 isl_int_print(out, bmap->div[i][0], 0);
2503 fprintf(out, "]\n");
2507 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2508 FILE *out, int indent)
2510 if (!bset) {
2511 fprintf(out, "null basic set\n");
2512 return;
2515 fprintf(out, "%*s", indent, "");
2516 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2517 bset->ref, bset->dim->nparam, bset->dim->n_out,
2518 bset->extra, bset->flags);
2519 dump((struct isl_basic_map *)bset, out, indent);
2522 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2523 FILE *out, int indent)
2525 if (!bmap) {
2526 fprintf(out, "null basic map\n");
2527 return;
2530 fprintf(out, "%*s", indent, "");
2531 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2532 "flags: %x, n_name: %d\n",
2533 bmap->ref,
2534 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2535 bmap->extra, bmap->flags, bmap->dim->n_id);
2536 dump(bmap, out, indent);
2539 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2541 unsigned total;
2542 if (!bmap)
2543 return -1;
2544 total = isl_basic_map_total_dim(bmap);
2545 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2546 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2547 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2548 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2549 return 0;
2552 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2553 unsigned flags)
2555 struct isl_set *set;
2557 if (!dim)
2558 return NULL;
2559 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2560 isl_assert(dim->ctx, n >= 0, goto error);
2561 set = isl_alloc(dim->ctx, struct isl_set,
2562 sizeof(struct isl_set) +
2563 (n - 1) * sizeof(struct isl_basic_set *));
2564 if (!set)
2565 goto error;
2567 set->ctx = dim->ctx;
2568 isl_ctx_ref(set->ctx);
2569 set->ref = 1;
2570 set->size = n;
2571 set->n = 0;
2572 set->dim = dim;
2573 set->flags = flags;
2574 return set;
2575 error:
2576 isl_space_free(dim);
2577 return NULL;
2580 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2581 unsigned nparam, unsigned dim, int n, unsigned flags)
2583 struct isl_set *set;
2584 isl_space *dims;
2586 dims = isl_space_alloc(ctx, nparam, 0, dim);
2587 if (!dims)
2588 return NULL;
2590 set = isl_set_alloc_space(dims, n, flags);
2591 return set;
2594 /* Make sure "map" has room for at least "n" more basic maps.
2596 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2598 int i;
2599 struct isl_map *grown = NULL;
2601 if (!map)
2602 return NULL;
2603 isl_assert(map->ctx, n >= 0, goto error);
2604 if (map->n + n <= map->size)
2605 return map;
2606 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2607 if (!grown)
2608 goto error;
2609 for (i = 0; i < map->n; ++i) {
2610 grown->p[i] = isl_basic_map_copy(map->p[i]);
2611 if (!grown->p[i])
2612 goto error;
2613 grown->n++;
2615 isl_map_free(map);
2616 return grown;
2617 error:
2618 isl_map_free(grown);
2619 isl_map_free(map);
2620 return NULL;
2623 /* Make sure "set" has room for at least "n" more basic sets.
2625 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2627 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2630 struct isl_set *isl_set_dup(struct isl_set *set)
2632 int i;
2633 struct isl_set *dup;
2635 if (!set)
2636 return NULL;
2638 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2639 if (!dup)
2640 return NULL;
2641 for (i = 0; i < set->n; ++i)
2642 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2643 return dup;
2646 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2648 return isl_map_from_basic_map(bset);
2651 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2653 struct isl_map *map;
2655 if (!bmap)
2656 return NULL;
2658 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2659 return isl_map_add_basic_map(map, bmap);
2662 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2663 __isl_take isl_basic_set *bset)
2665 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2666 (struct isl_basic_map *)bset);
2669 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2671 int i;
2673 if (!set)
2674 return NULL;
2676 if (--set->ref > 0)
2677 return NULL;
2679 isl_ctx_deref(set->ctx);
2680 for (i = 0; i < set->n; ++i)
2681 isl_basic_set_free(set->p[i]);
2682 isl_space_free(set->dim);
2683 free(set);
2685 return NULL;
2688 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2690 int i;
2692 if (!set) {
2693 fprintf(out, "null set\n");
2694 return;
2697 fprintf(out, "%*s", indent, "");
2698 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2699 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2700 set->flags);
2701 for (i = 0; i < set->n; ++i) {
2702 fprintf(out, "%*s", indent, "");
2703 fprintf(out, "basic set %d:\n", i);
2704 isl_basic_set_print_internal(set->p[i], out, indent+4);
2708 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2710 int i;
2712 if (!map) {
2713 fprintf(out, "null map\n");
2714 return;
2717 fprintf(out, "%*s", indent, "");
2718 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2719 "flags: %x, n_name: %d\n",
2720 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2721 map->dim->n_out, map->flags, map->dim->n_id);
2722 for (i = 0; i < map->n; ++i) {
2723 fprintf(out, "%*s", indent, "");
2724 fprintf(out, "basic map %d:\n", i);
2725 isl_basic_map_print_internal(map->p[i], out, indent+4);
2729 struct isl_basic_map *isl_basic_map_intersect_domain(
2730 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2732 struct isl_basic_map *bmap_domain;
2734 if (!bmap || !bset)
2735 goto error;
2737 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2738 bset->dim, isl_dim_param), goto error);
2740 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2741 isl_assert(bset->ctx,
2742 isl_basic_map_compatible_domain(bmap, bset), goto error);
2744 bmap = isl_basic_map_cow(bmap);
2745 if (!bmap)
2746 goto error;
2747 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2748 bset->n_div, bset->n_eq, bset->n_ineq);
2749 bmap_domain = isl_basic_map_from_domain(bset);
2750 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2752 bmap = isl_basic_map_simplify(bmap);
2753 return isl_basic_map_finalize(bmap);
2754 error:
2755 isl_basic_map_free(bmap);
2756 isl_basic_set_free(bset);
2757 return NULL;
2760 struct isl_basic_map *isl_basic_map_intersect_range(
2761 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2763 struct isl_basic_map *bmap_range;
2765 if (!bmap || !bset)
2766 goto error;
2768 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2769 bset->dim, isl_dim_param), goto error);
2771 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2772 isl_assert(bset->ctx,
2773 isl_basic_map_compatible_range(bmap, bset), goto error);
2775 if (isl_basic_set_is_universe(bset)) {
2776 isl_basic_set_free(bset);
2777 return bmap;
2780 bmap = isl_basic_map_cow(bmap);
2781 if (!bmap)
2782 goto error;
2783 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2784 bset->n_div, bset->n_eq, bset->n_ineq);
2785 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2786 bmap = add_constraints(bmap, bmap_range, 0, 0);
2788 bmap = isl_basic_map_simplify(bmap);
2789 return isl_basic_map_finalize(bmap);
2790 error:
2791 isl_basic_map_free(bmap);
2792 isl_basic_set_free(bset);
2793 return NULL;
2796 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2797 __isl_keep isl_vec *vec)
2799 int i;
2800 unsigned total;
2801 isl_int s;
2803 if (!bmap || !vec)
2804 return isl_bool_error;
2806 total = 1 + isl_basic_map_total_dim(bmap);
2807 if (total != vec->size)
2808 return isl_bool_error;
2810 isl_int_init(s);
2812 for (i = 0; i < bmap->n_eq; ++i) {
2813 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2814 if (!isl_int_is_zero(s)) {
2815 isl_int_clear(s);
2816 return isl_bool_false;
2820 for (i = 0; i < bmap->n_ineq; ++i) {
2821 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2822 if (isl_int_is_neg(s)) {
2823 isl_int_clear(s);
2824 return isl_bool_false;
2828 isl_int_clear(s);
2830 return isl_bool_true;
2833 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2834 __isl_keep isl_vec *vec)
2836 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2839 struct isl_basic_map *isl_basic_map_intersect(
2840 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2842 struct isl_vec *sample = NULL;
2844 if (!bmap1 || !bmap2)
2845 goto error;
2847 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2848 bmap2->dim, isl_dim_param), goto error);
2849 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2850 isl_space_dim(bmap1->dim, isl_dim_param) &&
2851 isl_space_dim(bmap2->dim, isl_dim_all) !=
2852 isl_space_dim(bmap2->dim, isl_dim_param))
2853 return isl_basic_map_intersect(bmap2, bmap1);
2855 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2856 isl_space_dim(bmap2->dim, isl_dim_param))
2857 isl_assert(bmap1->ctx,
2858 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2860 if (isl_basic_map_plain_is_empty(bmap1)) {
2861 isl_basic_map_free(bmap2);
2862 return bmap1;
2864 if (isl_basic_map_plain_is_empty(bmap2)) {
2865 isl_basic_map_free(bmap1);
2866 return bmap2;
2869 if (bmap1->sample &&
2870 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2871 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2872 sample = isl_vec_copy(bmap1->sample);
2873 else if (bmap2->sample &&
2874 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2875 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2876 sample = isl_vec_copy(bmap2->sample);
2878 bmap1 = isl_basic_map_cow(bmap1);
2879 if (!bmap1)
2880 goto error;
2881 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2882 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2883 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2885 if (!bmap1)
2886 isl_vec_free(sample);
2887 else if (sample) {
2888 isl_vec_free(bmap1->sample);
2889 bmap1->sample = sample;
2892 bmap1 = isl_basic_map_simplify(bmap1);
2893 return isl_basic_map_finalize(bmap1);
2894 error:
2895 if (sample)
2896 isl_vec_free(sample);
2897 isl_basic_map_free(bmap1);
2898 isl_basic_map_free(bmap2);
2899 return NULL;
2902 struct isl_basic_set *isl_basic_set_intersect(
2903 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2905 return (struct isl_basic_set *)
2906 isl_basic_map_intersect(
2907 (struct isl_basic_map *)bset1,
2908 (struct isl_basic_map *)bset2);
2911 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2912 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2914 return isl_basic_set_intersect(bset1, bset2);
2917 /* Special case of isl_map_intersect, where both map1 and map2
2918 * are convex, without any divs and such that either map1 or map2
2919 * contains a single constraint. This constraint is then simply
2920 * added to the other map.
2922 static __isl_give isl_map *map_intersect_add_constraint(
2923 __isl_take isl_map *map1, __isl_take isl_map *map2)
2925 isl_assert(map1->ctx, map1->n == 1, goto error);
2926 isl_assert(map2->ctx, map1->n == 1, goto error);
2927 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2928 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2930 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2931 return isl_map_intersect(map2, map1);
2933 isl_assert(map2->ctx,
2934 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2936 map1 = isl_map_cow(map1);
2937 if (!map1)
2938 goto error;
2939 if (isl_map_plain_is_empty(map1)) {
2940 isl_map_free(map2);
2941 return map1;
2943 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2944 if (map2->p[0]->n_eq == 1)
2945 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2946 else
2947 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2948 map2->p[0]->ineq[0]);
2950 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2951 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2952 if (!map1->p[0])
2953 goto error;
2955 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2956 isl_basic_map_free(map1->p[0]);
2957 map1->n = 0;
2960 isl_map_free(map2);
2962 return map1;
2963 error:
2964 isl_map_free(map1);
2965 isl_map_free(map2);
2966 return NULL;
2969 /* map2 may be either a parameter domain or a map living in the same
2970 * space as map1.
2972 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2973 __isl_take isl_map *map2)
2975 unsigned flags = 0;
2976 isl_map *result;
2977 int i, j;
2979 if (!map1 || !map2)
2980 goto error;
2982 if ((isl_map_plain_is_empty(map1) ||
2983 isl_map_plain_is_universe(map2)) &&
2984 isl_space_is_equal(map1->dim, map2->dim)) {
2985 isl_map_free(map2);
2986 return map1;
2988 if ((isl_map_plain_is_empty(map2) ||
2989 isl_map_plain_is_universe(map1)) &&
2990 isl_space_is_equal(map1->dim, map2->dim)) {
2991 isl_map_free(map1);
2992 return map2;
2995 if (map1->n == 1 && map2->n == 1 &&
2996 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2997 isl_space_is_equal(map1->dim, map2->dim) &&
2998 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2999 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3000 return map_intersect_add_constraint(map1, map2);
3002 if (isl_space_dim(map2->dim, isl_dim_all) !=
3003 isl_space_dim(map2->dim, isl_dim_param))
3004 isl_assert(map1->ctx,
3005 isl_space_is_equal(map1->dim, map2->dim), goto error);
3007 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3008 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3009 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3011 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3012 map1->n * map2->n, flags);
3013 if (!result)
3014 goto error;
3015 for (i = 0; i < map1->n; ++i)
3016 for (j = 0; j < map2->n; ++j) {
3017 struct isl_basic_map *part;
3018 part = isl_basic_map_intersect(
3019 isl_basic_map_copy(map1->p[i]),
3020 isl_basic_map_copy(map2->p[j]));
3021 if (isl_basic_map_is_empty(part) < 0)
3022 part = isl_basic_map_free(part);
3023 result = isl_map_add_basic_map(result, part);
3024 if (!result)
3025 goto error;
3027 isl_map_free(map1);
3028 isl_map_free(map2);
3029 return result;
3030 error:
3031 isl_map_free(map1);
3032 isl_map_free(map2);
3033 return NULL;
3036 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3037 __isl_take isl_map *map2)
3039 if (!map1 || !map2)
3040 goto error;
3041 if (!isl_space_is_equal(map1->dim, map2->dim))
3042 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3043 "spaces don't match", goto error);
3044 return map_intersect_internal(map1, map2);
3045 error:
3046 isl_map_free(map1);
3047 isl_map_free(map2);
3048 return NULL;
3051 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3052 __isl_take isl_map *map2)
3054 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3057 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3059 return (struct isl_set *)
3060 isl_map_intersect((struct isl_map *)set1,
3061 (struct isl_map *)set2);
3064 /* map_intersect_internal accepts intersections
3065 * with parameter domains, so we can just call that function.
3067 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3068 __isl_take isl_set *params)
3070 return map_intersect_internal(map, params);
3073 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3074 __isl_take isl_map *map2)
3076 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3079 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3080 __isl_take isl_set *params)
3082 return isl_map_intersect_params(set, params);
3085 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3087 isl_space *space;
3088 unsigned pos, n1, n2;
3090 if (!bmap)
3091 return NULL;
3092 bmap = isl_basic_map_cow(bmap);
3093 if (!bmap)
3094 return NULL;
3095 space = isl_space_reverse(isl_space_copy(bmap->dim));
3096 pos = isl_basic_map_offset(bmap, isl_dim_in);
3097 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3098 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3099 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3100 return isl_basic_map_reset_space(bmap, space);
3103 static __isl_give isl_basic_map *basic_map_space_reset(
3104 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3106 isl_space *space;
3108 if (!bmap)
3109 return NULL;
3110 if (!isl_space_is_named_or_nested(bmap->dim, type))
3111 return bmap;
3113 space = isl_basic_map_get_space(bmap);
3114 space = isl_space_reset(space, type);
3115 bmap = isl_basic_map_reset_space(bmap, space);
3116 return bmap;
3119 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3120 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3121 unsigned pos, unsigned n)
3123 isl_space *res_dim;
3124 struct isl_basic_map *res;
3125 struct isl_dim_map *dim_map;
3126 unsigned total, off;
3127 enum isl_dim_type t;
3129 if (n == 0)
3130 return basic_map_space_reset(bmap, type);
3132 if (!bmap)
3133 return NULL;
3135 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3137 total = isl_basic_map_total_dim(bmap) + n;
3138 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3139 off = 0;
3140 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3141 if (t != type) {
3142 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3143 } else {
3144 unsigned size = isl_basic_map_dim(bmap, t);
3145 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3146 0, pos, off);
3147 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3148 pos, size - pos, off + pos + n);
3150 off += isl_space_dim(res_dim, t);
3152 isl_dim_map_div(dim_map, bmap, off);
3154 res = isl_basic_map_alloc_space(res_dim,
3155 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3156 if (isl_basic_map_is_rational(bmap))
3157 res = isl_basic_map_set_rational(res);
3158 if (isl_basic_map_plain_is_empty(bmap)) {
3159 isl_basic_map_free(bmap);
3160 free(dim_map);
3161 return isl_basic_map_set_to_empty(res);
3163 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3164 return isl_basic_map_finalize(res);
3167 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3168 __isl_take isl_basic_set *bset,
3169 enum isl_dim_type type, unsigned pos, unsigned n)
3171 return isl_basic_map_insert_dims(bset, type, pos, n);
3174 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3175 enum isl_dim_type type, unsigned n)
3177 if (!bmap)
3178 return NULL;
3179 return isl_basic_map_insert_dims(bmap, type,
3180 isl_basic_map_dim(bmap, type), n);
3183 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3184 enum isl_dim_type type, unsigned n)
3186 if (!bset)
3187 return NULL;
3188 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3189 return isl_basic_map_add_dims(bset, type, n);
3190 error:
3191 isl_basic_set_free(bset);
3192 return NULL;
3195 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3196 enum isl_dim_type type)
3198 isl_space *space;
3200 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3201 return map;
3203 space = isl_map_get_space(map);
3204 space = isl_space_reset(space, type);
3205 map = isl_map_reset_space(map, space);
3206 return map;
3209 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3210 enum isl_dim_type type, unsigned pos, unsigned n)
3212 int i;
3214 if (n == 0)
3215 return map_space_reset(map, type);
3217 map = isl_map_cow(map);
3218 if (!map)
3219 return NULL;
3221 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3222 if (!map->dim)
3223 goto error;
3225 for (i = 0; i < map->n; ++i) {
3226 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3227 if (!map->p[i])
3228 goto error;
3231 return map;
3232 error:
3233 isl_map_free(map);
3234 return NULL;
3237 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3238 enum isl_dim_type type, unsigned pos, unsigned n)
3240 return isl_map_insert_dims(set, type, pos, n);
3243 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3244 enum isl_dim_type type, unsigned n)
3246 if (!map)
3247 return NULL;
3248 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3251 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3252 enum isl_dim_type type, unsigned n)
3254 if (!set)
3255 return NULL;
3256 isl_assert(set->ctx, type != isl_dim_in, goto error);
3257 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3258 error:
3259 isl_set_free(set);
3260 return NULL;
3263 __isl_give isl_basic_map *isl_basic_map_move_dims(
3264 __isl_take isl_basic_map *bmap,
3265 enum isl_dim_type dst_type, unsigned dst_pos,
3266 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3268 struct isl_dim_map *dim_map;
3269 struct isl_basic_map *res;
3270 enum isl_dim_type t;
3271 unsigned total, off;
3273 if (!bmap)
3274 return NULL;
3275 if (n == 0)
3276 return bmap;
3278 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3279 goto error);
3281 if (dst_type == src_type && dst_pos == src_pos)
3282 return bmap;
3284 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3286 if (pos(bmap->dim, dst_type) + dst_pos ==
3287 pos(bmap->dim, src_type) + src_pos +
3288 ((src_type < dst_type) ? n : 0)) {
3289 bmap = isl_basic_map_cow(bmap);
3290 if (!bmap)
3291 return NULL;
3293 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3294 src_type, src_pos, n);
3295 if (!bmap->dim)
3296 goto error;
3298 bmap = isl_basic_map_finalize(bmap);
3300 return bmap;
3303 total = isl_basic_map_total_dim(bmap);
3304 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3306 off = 0;
3307 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3308 unsigned size = isl_space_dim(bmap->dim, t);
3309 if (t == dst_type) {
3310 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3311 0, dst_pos, off);
3312 off += dst_pos;
3313 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3314 src_pos, n, off);
3315 off += n;
3316 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3317 dst_pos, size - dst_pos, off);
3318 off += size - dst_pos;
3319 } else if (t == src_type) {
3320 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3321 0, src_pos, off);
3322 off += src_pos;
3323 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3324 src_pos + n, size - src_pos - n, off);
3325 off += size - src_pos - n;
3326 } else {
3327 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3328 off += size;
3331 isl_dim_map_div(dim_map, bmap, off);
3333 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3334 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3335 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3336 if (!bmap)
3337 goto error;
3339 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3340 src_type, src_pos, n);
3341 if (!bmap->dim)
3342 goto error;
3344 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3345 bmap = isl_basic_map_gauss(bmap, NULL);
3346 bmap = isl_basic_map_finalize(bmap);
3348 return bmap;
3349 error:
3350 isl_basic_map_free(bmap);
3351 return NULL;
3354 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3355 enum isl_dim_type dst_type, unsigned dst_pos,
3356 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3358 return (isl_basic_set *)isl_basic_map_move_dims(
3359 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3362 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3363 enum isl_dim_type dst_type, unsigned dst_pos,
3364 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3366 if (!set)
3367 return NULL;
3368 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3369 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3370 src_type, src_pos, n);
3371 error:
3372 isl_set_free(set);
3373 return NULL;
3376 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3377 enum isl_dim_type dst_type, unsigned dst_pos,
3378 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3380 int i;
3382 if (!map)
3383 return NULL;
3384 if (n == 0)
3385 return map;
3387 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3388 goto error);
3390 if (dst_type == src_type && dst_pos == src_pos)
3391 return map;
3393 isl_assert(map->ctx, dst_type != src_type, goto error);
3395 map = isl_map_cow(map);
3396 if (!map)
3397 return NULL;
3399 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3400 if (!map->dim)
3401 goto error;
3403 for (i = 0; i < map->n; ++i) {
3404 map->p[i] = isl_basic_map_move_dims(map->p[i],
3405 dst_type, dst_pos,
3406 src_type, src_pos, n);
3407 if (!map->p[i])
3408 goto error;
3411 return map;
3412 error:
3413 isl_map_free(map);
3414 return NULL;
3417 /* Move the specified dimensions to the last columns right before
3418 * the divs. Don't change the dimension specification of bmap.
3419 * That's the responsibility of the caller.
3421 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3422 enum isl_dim_type type, unsigned first, unsigned n)
3424 struct isl_dim_map *dim_map;
3425 struct isl_basic_map *res;
3426 enum isl_dim_type t;
3427 unsigned total, off;
3429 if (!bmap)
3430 return NULL;
3431 if (pos(bmap->dim, type) + first + n ==
3432 1 + isl_space_dim(bmap->dim, isl_dim_all))
3433 return bmap;
3435 total = isl_basic_map_total_dim(bmap);
3436 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3438 off = 0;
3439 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3440 unsigned size = isl_space_dim(bmap->dim, t);
3441 if (t == type) {
3442 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3443 0, first, off);
3444 off += first;
3445 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3446 first, n, total - bmap->n_div - n);
3447 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3448 first + n, size - (first + n), off);
3449 off += size - (first + n);
3450 } else {
3451 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3452 off += size;
3455 isl_dim_map_div(dim_map, bmap, off + n);
3457 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3458 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3459 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3460 return res;
3463 /* Insert "n" rows in the divs of "bmap".
3465 * The number of columns is not changed, which means that the last
3466 * dimensions of "bmap" are being reintepreted as the new divs.
3467 * The space of "bmap" is not adjusted, however, which means
3468 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3469 * from the space of "bmap" is the responsibility of the caller.
3471 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3472 int n)
3474 int i;
3475 size_t row_size;
3476 isl_int **new_div;
3477 isl_int *old;
3479 bmap = isl_basic_map_cow(bmap);
3480 if (!bmap)
3481 return NULL;
3483 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3484 old = bmap->block2.data;
3485 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3486 (bmap->extra + n) * (1 + row_size));
3487 if (!bmap->block2.data)
3488 return isl_basic_map_free(bmap);
3489 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3490 if (!new_div)
3491 return isl_basic_map_free(bmap);
3492 for (i = 0; i < n; ++i) {
3493 new_div[i] = bmap->block2.data +
3494 (bmap->extra + i) * (1 + row_size);
3495 isl_seq_clr(new_div[i], 1 + row_size);
3497 for (i = 0; i < bmap->extra; ++i)
3498 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3499 free(bmap->div);
3500 bmap->div = new_div;
3501 bmap->n_div += n;
3502 bmap->extra += n;
3504 return bmap;
3507 /* Turn the n dimensions of type type, starting at first
3508 * into existentially quantified variables.
3510 __isl_give isl_basic_map *isl_basic_map_project_out(
3511 __isl_take isl_basic_map *bmap,
3512 enum isl_dim_type type, unsigned first, unsigned n)
3514 if (n == 0)
3515 return basic_map_space_reset(bmap, type);
3517 if (!bmap)
3518 return NULL;
3520 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3521 return isl_basic_map_remove_dims(bmap, type, first, n);
3523 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3524 goto error);
3526 bmap = move_last(bmap, type, first, n);
3527 bmap = isl_basic_map_cow(bmap);
3528 bmap = insert_div_rows(bmap, n);
3529 if (!bmap)
3530 return NULL;
3532 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3533 if (!bmap->dim)
3534 goto error;
3535 bmap = isl_basic_map_simplify(bmap);
3536 bmap = isl_basic_map_drop_redundant_divs(bmap);
3537 return isl_basic_map_finalize(bmap);
3538 error:
3539 isl_basic_map_free(bmap);
3540 return NULL;
3543 /* Turn the n dimensions of type type, starting at first
3544 * into existentially quantified variables.
3546 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3547 enum isl_dim_type type, unsigned first, unsigned n)
3549 return (isl_basic_set *)isl_basic_map_project_out(
3550 (isl_basic_map *)bset, type, first, n);
3553 /* Turn the n dimensions of type type, starting at first
3554 * into existentially quantified variables.
3556 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3557 enum isl_dim_type type, unsigned first, unsigned n)
3559 int i;
3561 if (!map)
3562 return NULL;
3564 if (n == 0)
3565 return map_space_reset(map, type);
3567 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3569 map = isl_map_cow(map);
3570 if (!map)
3571 return NULL;
3573 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3574 if (!map->dim)
3575 goto error;
3577 for (i = 0; i < map->n; ++i) {
3578 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3579 if (!map->p[i])
3580 goto error;
3583 return map;
3584 error:
3585 isl_map_free(map);
3586 return NULL;
3589 /* Turn the n dimensions of type type, starting at first
3590 * into existentially quantified variables.
3592 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3593 enum isl_dim_type type, unsigned first, unsigned n)
3595 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3598 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3600 int i, j;
3602 for (i = 0; i < n; ++i) {
3603 j = isl_basic_map_alloc_div(bmap);
3604 if (j < 0)
3605 goto error;
3606 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3608 return bmap;
3609 error:
3610 isl_basic_map_free(bmap);
3611 return NULL;
3614 struct isl_basic_map *isl_basic_map_apply_range(
3615 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3617 isl_space *dim_result = NULL;
3618 struct isl_basic_map *bmap;
3619 unsigned n_in, n_out, n, nparam, total, pos;
3620 struct isl_dim_map *dim_map1, *dim_map2;
3622 if (!bmap1 || !bmap2)
3623 goto error;
3624 if (!isl_space_match(bmap1->dim, isl_dim_param,
3625 bmap2->dim, isl_dim_param))
3626 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3627 "parameters don't match", goto error);
3628 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3629 bmap2->dim, isl_dim_in))
3630 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3631 "spaces don't match", goto error);
3633 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3634 isl_space_copy(bmap2->dim));
3636 n_in = isl_basic_map_n_in(bmap1);
3637 n_out = isl_basic_map_n_out(bmap2);
3638 n = isl_basic_map_n_out(bmap1);
3639 nparam = isl_basic_map_n_param(bmap1);
3641 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3642 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3643 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3644 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3645 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3646 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3647 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3648 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3649 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3650 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3651 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3653 bmap = isl_basic_map_alloc_space(dim_result,
3654 bmap1->n_div + bmap2->n_div + n,
3655 bmap1->n_eq + bmap2->n_eq,
3656 bmap1->n_ineq + bmap2->n_ineq);
3657 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3658 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3659 bmap = add_divs(bmap, n);
3660 bmap = isl_basic_map_simplify(bmap);
3661 bmap = isl_basic_map_drop_redundant_divs(bmap);
3662 return isl_basic_map_finalize(bmap);
3663 error:
3664 isl_basic_map_free(bmap1);
3665 isl_basic_map_free(bmap2);
3666 return NULL;
3669 struct isl_basic_set *isl_basic_set_apply(
3670 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3672 if (!bset || !bmap)
3673 goto error;
3675 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3676 goto error);
3678 return (struct isl_basic_set *)
3679 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3680 error:
3681 isl_basic_set_free(bset);
3682 isl_basic_map_free(bmap);
3683 return NULL;
3686 struct isl_basic_map *isl_basic_map_apply_domain(
3687 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3689 if (!bmap1 || !bmap2)
3690 goto error;
3692 isl_assert(bmap1->ctx,
3693 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3694 isl_assert(bmap1->ctx,
3695 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3696 goto error);
3698 bmap1 = isl_basic_map_reverse(bmap1);
3699 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3700 return isl_basic_map_reverse(bmap1);
3701 error:
3702 isl_basic_map_free(bmap1);
3703 isl_basic_map_free(bmap2);
3704 return NULL;
3707 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3708 * A \cap B -> f(A) + f(B)
3710 struct isl_basic_map *isl_basic_map_sum(
3711 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3713 unsigned n_in, n_out, nparam, total, pos;
3714 struct isl_basic_map *bmap = NULL;
3715 struct isl_dim_map *dim_map1, *dim_map2;
3716 int i;
3718 if (!bmap1 || !bmap2)
3719 goto error;
3721 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3722 goto error);
3724 nparam = isl_basic_map_n_param(bmap1);
3725 n_in = isl_basic_map_n_in(bmap1);
3726 n_out = isl_basic_map_n_out(bmap1);
3728 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3729 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3730 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3731 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3732 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3733 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3734 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3735 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3736 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3737 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3738 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3740 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3741 bmap1->n_div + bmap2->n_div + 2 * n_out,
3742 bmap1->n_eq + bmap2->n_eq + n_out,
3743 bmap1->n_ineq + bmap2->n_ineq);
3744 for (i = 0; i < n_out; ++i) {
3745 int j = isl_basic_map_alloc_equality(bmap);
3746 if (j < 0)
3747 goto error;
3748 isl_seq_clr(bmap->eq[j], 1+total);
3749 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3750 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3751 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3753 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3754 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3755 bmap = add_divs(bmap, 2 * n_out);
3757 bmap = isl_basic_map_simplify(bmap);
3758 return isl_basic_map_finalize(bmap);
3759 error:
3760 isl_basic_map_free(bmap);
3761 isl_basic_map_free(bmap1);
3762 isl_basic_map_free(bmap2);
3763 return NULL;
3766 /* Given two maps A -> f(A) and B -> g(B), construct a map
3767 * A \cap B -> f(A) + f(B)
3769 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3771 struct isl_map *result;
3772 int i, j;
3774 if (!map1 || !map2)
3775 goto error;
3777 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3779 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3780 map1->n * map2->n, 0);
3781 if (!result)
3782 goto error;
3783 for (i = 0; i < map1->n; ++i)
3784 for (j = 0; j < map2->n; ++j) {
3785 struct isl_basic_map *part;
3786 part = isl_basic_map_sum(
3787 isl_basic_map_copy(map1->p[i]),
3788 isl_basic_map_copy(map2->p[j]));
3789 if (isl_basic_map_is_empty(part))
3790 isl_basic_map_free(part);
3791 else
3792 result = isl_map_add_basic_map(result, part);
3793 if (!result)
3794 goto error;
3796 isl_map_free(map1);
3797 isl_map_free(map2);
3798 return result;
3799 error:
3800 isl_map_free(map1);
3801 isl_map_free(map2);
3802 return NULL;
3805 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3806 __isl_take isl_set *set2)
3808 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3811 /* Given a basic map A -> f(A), construct A -> -f(A).
3813 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3815 int i, j;
3816 unsigned off, n;
3818 bmap = isl_basic_map_cow(bmap);
3819 if (!bmap)
3820 return NULL;
3822 n = isl_basic_map_dim(bmap, isl_dim_out);
3823 off = isl_basic_map_offset(bmap, isl_dim_out);
3824 for (i = 0; i < bmap->n_eq; ++i)
3825 for (j = 0; j < n; ++j)
3826 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3827 for (i = 0; i < bmap->n_ineq; ++i)
3828 for (j = 0; j < n; ++j)
3829 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3830 for (i = 0; i < bmap->n_div; ++i)
3831 for (j = 0; j < n; ++j)
3832 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3833 bmap = isl_basic_map_gauss(bmap, NULL);
3834 return isl_basic_map_finalize(bmap);
3837 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3839 return isl_basic_map_neg(bset);
3842 /* Given a map A -> f(A), construct A -> -f(A).
3844 struct isl_map *isl_map_neg(struct isl_map *map)
3846 int i;
3848 map = isl_map_cow(map);
3849 if (!map)
3850 return NULL;
3852 for (i = 0; i < map->n; ++i) {
3853 map->p[i] = isl_basic_map_neg(map->p[i]);
3854 if (!map->p[i])
3855 goto error;
3858 return map;
3859 error:
3860 isl_map_free(map);
3861 return NULL;
3864 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3866 return (isl_set *)isl_map_neg((isl_map *)set);
3869 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3870 * A -> floor(f(A)/d).
3872 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3873 isl_int d)
3875 unsigned n_in, n_out, nparam, total, pos;
3876 struct isl_basic_map *result = NULL;
3877 struct isl_dim_map *dim_map;
3878 int i;
3880 if (!bmap)
3881 return NULL;
3883 nparam = isl_basic_map_n_param(bmap);
3884 n_in = isl_basic_map_n_in(bmap);
3885 n_out = isl_basic_map_n_out(bmap);
3887 total = nparam + n_in + n_out + bmap->n_div + n_out;
3888 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3889 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3890 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3891 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3892 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3894 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3895 bmap->n_div + n_out,
3896 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3897 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3898 result = add_divs(result, n_out);
3899 for (i = 0; i < n_out; ++i) {
3900 int j;
3901 j = isl_basic_map_alloc_inequality(result);
3902 if (j < 0)
3903 goto error;
3904 isl_seq_clr(result->ineq[j], 1+total);
3905 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3906 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3907 j = isl_basic_map_alloc_inequality(result);
3908 if (j < 0)
3909 goto error;
3910 isl_seq_clr(result->ineq[j], 1+total);
3911 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3912 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3913 isl_int_sub_ui(result->ineq[j][0], d, 1);
3916 result = isl_basic_map_simplify(result);
3917 return isl_basic_map_finalize(result);
3918 error:
3919 isl_basic_map_free(result);
3920 return NULL;
3923 /* Given a map A -> f(A) and an integer d, construct a map
3924 * A -> floor(f(A)/d).
3926 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3928 int i;
3930 map = isl_map_cow(map);
3931 if (!map)
3932 return NULL;
3934 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3935 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3936 for (i = 0; i < map->n; ++i) {
3937 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3938 if (!map->p[i])
3939 goto error;
3942 return map;
3943 error:
3944 isl_map_free(map);
3945 return NULL;
3948 /* Given a map A -> f(A) and an integer d, construct a map
3949 * A -> floor(f(A)/d).
3951 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3952 __isl_take isl_val *d)
3954 if (!map || !d)
3955 goto error;
3956 if (!isl_val_is_int(d))
3957 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3958 "expecting integer denominator", goto error);
3959 map = isl_map_floordiv(map, d->n);
3960 isl_val_free(d);
3961 return map;
3962 error:
3963 isl_map_free(map);
3964 isl_val_free(d);
3965 return NULL;
3968 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3970 int i;
3971 unsigned nparam;
3972 unsigned n_in;
3974 i = isl_basic_map_alloc_equality(bmap);
3975 if (i < 0)
3976 goto error;
3977 nparam = isl_basic_map_n_param(bmap);
3978 n_in = isl_basic_map_n_in(bmap);
3979 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3980 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3981 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3982 return isl_basic_map_finalize(bmap);
3983 error:
3984 isl_basic_map_free(bmap);
3985 return NULL;
3988 /* Add a constraints to "bmap" expressing i_pos < o_pos
3990 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3992 int i;
3993 unsigned nparam;
3994 unsigned n_in;
3996 i = isl_basic_map_alloc_inequality(bmap);
3997 if (i < 0)
3998 goto error;
3999 nparam = isl_basic_map_n_param(bmap);
4000 n_in = isl_basic_map_n_in(bmap);
4001 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4002 isl_int_set_si(bmap->ineq[i][0], -1);
4003 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4004 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4005 return isl_basic_map_finalize(bmap);
4006 error:
4007 isl_basic_map_free(bmap);
4008 return NULL;
4011 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4013 static __isl_give isl_basic_map *var_less_or_equal(
4014 __isl_take isl_basic_map *bmap, unsigned pos)
4016 int i;
4017 unsigned nparam;
4018 unsigned n_in;
4020 i = isl_basic_map_alloc_inequality(bmap);
4021 if (i < 0)
4022 goto error;
4023 nparam = isl_basic_map_n_param(bmap);
4024 n_in = isl_basic_map_n_in(bmap);
4025 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4026 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4027 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4028 return isl_basic_map_finalize(bmap);
4029 error:
4030 isl_basic_map_free(bmap);
4031 return NULL;
4034 /* Add a constraints to "bmap" expressing i_pos > o_pos
4036 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4038 int i;
4039 unsigned nparam;
4040 unsigned n_in;
4042 i = isl_basic_map_alloc_inequality(bmap);
4043 if (i < 0)
4044 goto error;
4045 nparam = isl_basic_map_n_param(bmap);
4046 n_in = isl_basic_map_n_in(bmap);
4047 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4048 isl_int_set_si(bmap->ineq[i][0], -1);
4049 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4050 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4051 return isl_basic_map_finalize(bmap);
4052 error:
4053 isl_basic_map_free(bmap);
4054 return NULL;
4057 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4059 static __isl_give isl_basic_map *var_more_or_equal(
4060 __isl_take isl_basic_map *bmap, unsigned pos)
4062 int i;
4063 unsigned nparam;
4064 unsigned n_in;
4066 i = isl_basic_map_alloc_inequality(bmap);
4067 if (i < 0)
4068 goto error;
4069 nparam = isl_basic_map_n_param(bmap);
4070 n_in = isl_basic_map_n_in(bmap);
4071 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4072 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4073 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4074 return isl_basic_map_finalize(bmap);
4075 error:
4076 isl_basic_map_free(bmap);
4077 return NULL;
4080 __isl_give isl_basic_map *isl_basic_map_equal(
4081 __isl_take isl_space *dim, unsigned n_equal)
4083 int i;
4084 struct isl_basic_map *bmap;
4085 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4086 if (!bmap)
4087 return NULL;
4088 for (i = 0; i < n_equal && bmap; ++i)
4089 bmap = var_equal(bmap, i);
4090 return isl_basic_map_finalize(bmap);
4093 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4095 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4096 unsigned pos)
4098 int i;
4099 struct isl_basic_map *bmap;
4100 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4101 if (!bmap)
4102 return NULL;
4103 for (i = 0; i < pos && bmap; ++i)
4104 bmap = var_equal(bmap, i);
4105 if (bmap)
4106 bmap = var_less(bmap, pos);
4107 return isl_basic_map_finalize(bmap);
4110 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4112 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4113 __isl_take isl_space *dim, unsigned pos)
4115 int i;
4116 isl_basic_map *bmap;
4118 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4119 for (i = 0; i < pos; ++i)
4120 bmap = var_equal(bmap, i);
4121 bmap = var_less_or_equal(bmap, pos);
4122 return isl_basic_map_finalize(bmap);
4125 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4127 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4128 unsigned pos)
4130 int i;
4131 struct isl_basic_map *bmap;
4132 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4133 if (!bmap)
4134 return NULL;
4135 for (i = 0; i < pos && bmap; ++i)
4136 bmap = var_equal(bmap, i);
4137 if (bmap)
4138 bmap = var_more(bmap, pos);
4139 return isl_basic_map_finalize(bmap);
4142 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4144 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4145 __isl_take isl_space *dim, unsigned pos)
4147 int i;
4148 isl_basic_map *bmap;
4150 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4151 for (i = 0; i < pos; ++i)
4152 bmap = var_equal(bmap, i);
4153 bmap = var_more_or_equal(bmap, pos);
4154 return isl_basic_map_finalize(bmap);
4157 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4158 unsigned n, int equal)
4160 struct isl_map *map;
4161 int i;
4163 if (n == 0 && equal)
4164 return isl_map_universe(dims);
4166 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4168 for (i = 0; i + 1 < n; ++i)
4169 map = isl_map_add_basic_map(map,
4170 isl_basic_map_less_at(isl_space_copy(dims), i));
4171 if (n > 0) {
4172 if (equal)
4173 map = isl_map_add_basic_map(map,
4174 isl_basic_map_less_or_equal_at(dims, n - 1));
4175 else
4176 map = isl_map_add_basic_map(map,
4177 isl_basic_map_less_at(dims, n - 1));
4178 } else
4179 isl_space_free(dims);
4181 return map;
4184 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4186 if (!dims)
4187 return NULL;
4188 return map_lex_lte_first(dims, dims->n_out, equal);
4191 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4193 return map_lex_lte_first(dim, n, 0);
4196 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4198 return map_lex_lte_first(dim, n, 1);
4201 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4203 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4206 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4208 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4211 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4212 unsigned n, int equal)
4214 struct isl_map *map;
4215 int i;
4217 if (n == 0 && equal)
4218 return isl_map_universe(dims);
4220 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4222 for (i = 0; i + 1 < n; ++i)
4223 map = isl_map_add_basic_map(map,
4224 isl_basic_map_more_at(isl_space_copy(dims), i));
4225 if (n > 0) {
4226 if (equal)
4227 map = isl_map_add_basic_map(map,
4228 isl_basic_map_more_or_equal_at(dims, n - 1));
4229 else
4230 map = isl_map_add_basic_map(map,
4231 isl_basic_map_more_at(dims, n - 1));
4232 } else
4233 isl_space_free(dims);
4235 return map;
4238 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4240 if (!dims)
4241 return NULL;
4242 return map_lex_gte_first(dims, dims->n_out, equal);
4245 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4247 return map_lex_gte_first(dim, n, 0);
4250 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4252 return map_lex_gte_first(dim, n, 1);
4255 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4257 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4260 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4262 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4265 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4266 __isl_take isl_set *set2)
4268 isl_map *map;
4269 map = isl_map_lex_le(isl_set_get_space(set1));
4270 map = isl_map_intersect_domain(map, set1);
4271 map = isl_map_intersect_range(map, set2);
4272 return map;
4275 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4276 __isl_take isl_set *set2)
4278 isl_map *map;
4279 map = isl_map_lex_lt(isl_set_get_space(set1));
4280 map = isl_map_intersect_domain(map, set1);
4281 map = isl_map_intersect_range(map, set2);
4282 return map;
4285 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4286 __isl_take isl_set *set2)
4288 isl_map *map;
4289 map = isl_map_lex_ge(isl_set_get_space(set1));
4290 map = isl_map_intersect_domain(map, set1);
4291 map = isl_map_intersect_range(map, set2);
4292 return map;
4295 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4296 __isl_take isl_set *set2)
4298 isl_map *map;
4299 map = isl_map_lex_gt(isl_set_get_space(set1));
4300 map = isl_map_intersect_domain(map, set1);
4301 map = isl_map_intersect_range(map, set2);
4302 return map;
4305 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4306 __isl_take isl_map *map2)
4308 isl_map *map;
4309 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4310 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4311 map = isl_map_apply_range(map, isl_map_reverse(map2));
4312 return map;
4315 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4316 __isl_take isl_map *map2)
4318 isl_map *map;
4319 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4320 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4321 map = isl_map_apply_range(map, isl_map_reverse(map2));
4322 return map;
4325 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4326 __isl_take isl_map *map2)
4328 isl_map *map;
4329 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4330 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4331 map = isl_map_apply_range(map, isl_map_reverse(map2));
4332 return map;
4335 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4336 __isl_take isl_map *map2)
4338 isl_map *map;
4339 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4340 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4341 map = isl_map_apply_range(map, isl_map_reverse(map2));
4342 return map;
4345 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4346 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4348 struct isl_basic_map *bmap;
4350 bset = isl_basic_set_cow(bset);
4351 if (!bset || !dim)
4352 goto error;
4354 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4355 isl_space_free(bset->dim);
4356 bmap = (struct isl_basic_map *) bset;
4357 bmap->dim = dim;
4358 return isl_basic_map_finalize(bmap);
4359 error:
4360 isl_basic_set_free(bset);
4361 isl_space_free(dim);
4362 return NULL;
4365 /* For a div d = floor(f/m), add the constraint
4367 * f - m d >= 0
4369 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4370 unsigned pos, isl_int *div)
4372 int i;
4373 unsigned total = isl_basic_map_total_dim(bmap);
4375 i = isl_basic_map_alloc_inequality(bmap);
4376 if (i < 0)
4377 return -1;
4378 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4379 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4381 return 0;
4384 /* For a div d = floor(f/m), add the constraint
4386 * -(f-(n-1)) + m d >= 0
4388 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4389 unsigned pos, isl_int *div)
4391 int i;
4392 unsigned total = isl_basic_map_total_dim(bmap);
4394 i = isl_basic_map_alloc_inequality(bmap);
4395 if (i < 0)
4396 return -1;
4397 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4398 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4399 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4400 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4402 return 0;
4405 /* For a div d = floor(f/m), add the constraints
4407 * f - m d >= 0
4408 * -(f-(n-1)) + m d >= 0
4410 * Note that the second constraint is the negation of
4412 * f - m d >= n
4414 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4415 unsigned pos, isl_int *div)
4417 if (add_upper_div_constraint(bmap, pos, div) < 0)
4418 return -1;
4419 if (add_lower_div_constraint(bmap, pos, div) < 0)
4420 return -1;
4421 return 0;
4424 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4425 unsigned pos, isl_int *div)
4427 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4428 pos, div);
4431 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4433 unsigned total = isl_basic_map_total_dim(bmap);
4434 unsigned div_pos = total - bmap->n_div + div;
4436 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4437 bmap->div[div]);
4440 /* For each known div d = floor(f/m), add the constraints
4442 * f - m d >= 0
4443 * -(f-(n-1)) + m d >= 0
4445 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4446 __isl_take isl_basic_map *bmap)
4448 int i;
4449 unsigned n_div;
4451 if (!bmap)
4452 return NULL;
4453 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4454 if (n_div == 0)
4455 return bmap;
4456 bmap = isl_basic_map_cow(bmap);
4457 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4458 if (!bmap)
4459 return NULL;
4460 for (i = 0; i < n_div; ++i) {
4461 if (isl_int_is_zero(bmap->div[i][0]))
4462 continue;
4463 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4464 return isl_basic_map_free(bmap);
4467 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4468 bmap = isl_basic_map_finalize(bmap);
4469 return bmap;
4472 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4474 * In particular, if this div is of the form d = floor(f/m),
4475 * then add the constraint
4477 * f - m d >= 0
4479 * if sign < 0 or the constraint
4481 * -(f-(n-1)) + m d >= 0
4483 * if sign > 0.
4485 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4486 unsigned div, int sign)
4488 unsigned total;
4489 unsigned div_pos;
4491 if (!bmap)
4492 return -1;
4494 total = isl_basic_map_total_dim(bmap);
4495 div_pos = total - bmap->n_div + div;
4497 if (sign < 0)
4498 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4499 else
4500 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4503 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4505 return isl_basic_map_add_div_constraints(bset, div);
4508 struct isl_basic_set *isl_basic_map_underlying_set(
4509 struct isl_basic_map *bmap)
4511 if (!bmap)
4512 goto error;
4513 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4514 bmap->n_div == 0 &&
4515 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4516 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4517 return (struct isl_basic_set *)bmap;
4518 bmap = isl_basic_map_cow(bmap);
4519 if (!bmap)
4520 goto error;
4521 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4522 if (!bmap->dim)
4523 goto error;
4524 bmap->extra -= bmap->n_div;
4525 bmap->n_div = 0;
4526 bmap = isl_basic_map_finalize(bmap);
4527 return (struct isl_basic_set *)bmap;
4528 error:
4529 isl_basic_map_free(bmap);
4530 return NULL;
4533 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4534 __isl_take isl_basic_set *bset)
4536 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4539 /* Replace each element in "list" by the result of applying
4540 * isl_basic_map_underlying_set to the element.
4542 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4543 __isl_take isl_basic_map_list *list)
4545 int i, n;
4547 if (!list)
4548 return NULL;
4550 n = isl_basic_map_list_n_basic_map(list);
4551 for (i = 0; i < n; ++i) {
4552 isl_basic_map *bmap;
4553 isl_basic_set *bset;
4555 bmap = isl_basic_map_list_get_basic_map(list, i);
4556 bset = isl_basic_set_underlying_set(bmap);
4557 list = isl_basic_set_list_set_basic_set(list, i, bset);
4560 return list;
4563 struct isl_basic_map *isl_basic_map_overlying_set(
4564 struct isl_basic_set *bset, struct isl_basic_map *like)
4566 struct isl_basic_map *bmap;
4567 struct isl_ctx *ctx;
4568 unsigned total;
4569 int i;
4571 if (!bset || !like)
4572 goto error;
4573 ctx = bset->ctx;
4574 isl_assert(ctx, bset->n_div == 0, goto error);
4575 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4576 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4577 goto error);
4578 if (like->n_div == 0) {
4579 isl_space *space = isl_basic_map_get_space(like);
4580 isl_basic_map_free(like);
4581 return isl_basic_map_reset_space(bset, space);
4583 bset = isl_basic_set_cow(bset);
4584 if (!bset)
4585 goto error;
4586 total = bset->dim->n_out + bset->extra;
4587 bmap = (struct isl_basic_map *)bset;
4588 isl_space_free(bmap->dim);
4589 bmap->dim = isl_space_copy(like->dim);
4590 if (!bmap->dim)
4591 goto error;
4592 bmap->n_div = like->n_div;
4593 bmap->extra += like->n_div;
4594 if (bmap->extra) {
4595 unsigned ltotal;
4596 isl_int **div;
4597 ltotal = total - bmap->extra + like->extra;
4598 if (ltotal > total)
4599 ltotal = total;
4600 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4601 bmap->extra * (1 + 1 + total));
4602 if (isl_blk_is_error(bmap->block2))
4603 goto error;
4604 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4605 if (!div)
4606 goto error;
4607 bmap->div = div;
4608 for (i = 0; i < bmap->extra; ++i)
4609 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4610 for (i = 0; i < like->n_div; ++i) {
4611 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4612 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4614 bmap = isl_basic_map_add_known_div_constraints(bmap);
4616 isl_basic_map_free(like);
4617 bmap = isl_basic_map_simplify(bmap);
4618 bmap = isl_basic_map_finalize(bmap);
4619 return bmap;
4620 error:
4621 isl_basic_map_free(like);
4622 isl_basic_set_free(bset);
4623 return NULL;
4626 struct isl_basic_set *isl_basic_set_from_underlying_set(
4627 struct isl_basic_set *bset, struct isl_basic_set *like)
4629 return (struct isl_basic_set *)
4630 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4633 struct isl_set *isl_set_from_underlying_set(
4634 struct isl_set *set, struct isl_basic_set *like)
4636 int i;
4638 if (!set || !like)
4639 goto error;
4640 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4641 goto error);
4642 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4643 isl_basic_set_free(like);
4644 return set;
4646 set = isl_set_cow(set);
4647 if (!set)
4648 goto error;
4649 for (i = 0; i < set->n; ++i) {
4650 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4651 isl_basic_set_copy(like));
4652 if (!set->p[i])
4653 goto error;
4655 isl_space_free(set->dim);
4656 set->dim = isl_space_copy(like->dim);
4657 if (!set->dim)
4658 goto error;
4659 isl_basic_set_free(like);
4660 return set;
4661 error:
4662 isl_basic_set_free(like);
4663 isl_set_free(set);
4664 return NULL;
4667 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4669 int i;
4671 map = isl_map_cow(map);
4672 if (!map)
4673 return NULL;
4674 map->dim = isl_space_cow(map->dim);
4675 if (!map->dim)
4676 goto error;
4678 for (i = 1; i < map->n; ++i)
4679 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4680 goto error);
4681 for (i = 0; i < map->n; ++i) {
4682 map->p[i] = (struct isl_basic_map *)
4683 isl_basic_map_underlying_set(map->p[i]);
4684 if (!map->p[i])
4685 goto error;
4687 if (map->n == 0)
4688 map->dim = isl_space_underlying(map->dim, 0);
4689 else {
4690 isl_space_free(map->dim);
4691 map->dim = isl_space_copy(map->p[0]->dim);
4693 if (!map->dim)
4694 goto error;
4695 return (struct isl_set *)map;
4696 error:
4697 isl_map_free(map);
4698 return NULL;
4701 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4703 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4706 __isl_give isl_basic_map *isl_basic_map_reset_space(
4707 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4709 isl_bool equal;
4711 if (!bmap)
4712 goto error;
4713 equal = isl_space_is_equal(bmap->dim, space);
4714 if (equal < 0)
4715 goto error;
4716 if (equal) {
4717 isl_space_free(space);
4718 return bmap;
4720 bmap = isl_basic_map_cow(bmap);
4721 if (!bmap || !space)
4722 goto error;
4724 isl_space_free(bmap->dim);
4725 bmap->dim = space;
4727 bmap = isl_basic_map_finalize(bmap);
4729 return bmap;
4730 error:
4731 isl_basic_map_free(bmap);
4732 isl_space_free(space);
4733 return NULL;
4736 __isl_give isl_basic_set *isl_basic_set_reset_space(
4737 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4739 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4740 dim);
4743 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4744 __isl_take isl_space *dim)
4746 int i;
4748 map = isl_map_cow(map);
4749 if (!map || !dim)
4750 goto error;
4752 for (i = 0; i < map->n; ++i) {
4753 map->p[i] = isl_basic_map_reset_space(map->p[i],
4754 isl_space_copy(dim));
4755 if (!map->p[i])
4756 goto error;
4758 isl_space_free(map->dim);
4759 map->dim = dim;
4761 return map;
4762 error:
4763 isl_map_free(map);
4764 isl_space_free(dim);
4765 return NULL;
4768 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4769 __isl_take isl_space *dim)
4771 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4774 /* Compute the parameter domain of the given basic set.
4776 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4778 isl_space *space;
4779 unsigned n;
4781 if (isl_basic_set_is_params(bset))
4782 return bset;
4784 n = isl_basic_set_dim(bset, isl_dim_set);
4785 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4786 space = isl_basic_set_get_space(bset);
4787 space = isl_space_params(space);
4788 bset = isl_basic_set_reset_space(bset, space);
4789 return bset;
4792 /* Construct a zero-dimensional basic set with the given parameter domain.
4794 __isl_give isl_basic_set *isl_basic_set_from_params(
4795 __isl_take isl_basic_set *bset)
4797 isl_space *space;
4798 space = isl_basic_set_get_space(bset);
4799 space = isl_space_set_from_params(space);
4800 bset = isl_basic_set_reset_space(bset, space);
4801 return bset;
4804 /* Compute the parameter domain of the given set.
4806 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4808 isl_space *space;
4809 unsigned n;
4811 if (isl_set_is_params(set))
4812 return set;
4814 n = isl_set_dim(set, isl_dim_set);
4815 set = isl_set_project_out(set, isl_dim_set, 0, n);
4816 space = isl_set_get_space(set);
4817 space = isl_space_params(space);
4818 set = isl_set_reset_space(set, space);
4819 return set;
4822 /* Construct a zero-dimensional set with the given parameter domain.
4824 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4826 isl_space *space;
4827 space = isl_set_get_space(set);
4828 space = isl_space_set_from_params(space);
4829 set = isl_set_reset_space(set, space);
4830 return set;
4833 /* Compute the parameter domain of the given map.
4835 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4837 isl_space *space;
4838 unsigned n;
4840 n = isl_map_dim(map, isl_dim_in);
4841 map = isl_map_project_out(map, isl_dim_in, 0, n);
4842 n = isl_map_dim(map, isl_dim_out);
4843 map = isl_map_project_out(map, isl_dim_out, 0, n);
4844 space = isl_map_get_space(map);
4845 space = isl_space_params(space);
4846 map = isl_map_reset_space(map, space);
4847 return map;
4850 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4852 isl_space *space;
4853 unsigned n_out;
4855 if (!bmap)
4856 return NULL;
4857 space = isl_space_domain(isl_basic_map_get_space(bmap));
4859 n_out = isl_basic_map_n_out(bmap);
4860 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
4862 return isl_basic_map_reset_space(bmap, space);
4865 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4867 if (!bmap)
4868 return -1;
4869 return isl_space_may_be_set(bmap->dim);
4872 /* Is this basic map actually a set?
4873 * Users should never call this function. Outside of isl,
4874 * the type should indicate whether something is a set or a map.
4876 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4878 if (!bmap)
4879 return -1;
4880 return isl_space_is_set(bmap->dim);
4883 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4885 if (!bmap)
4886 return NULL;
4887 if (isl_basic_map_is_set(bmap))
4888 return bmap;
4889 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4892 __isl_give isl_basic_map *isl_basic_map_domain_map(
4893 __isl_take isl_basic_map *bmap)
4895 int i, k;
4896 isl_space *dim;
4897 isl_basic_map *domain;
4898 int nparam, n_in, n_out;
4899 unsigned total;
4901 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4902 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4903 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4905 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4906 domain = isl_basic_map_universe(dim);
4908 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4909 bmap = isl_basic_map_apply_range(bmap, domain);
4910 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4912 total = isl_basic_map_total_dim(bmap);
4914 for (i = 0; i < n_in; ++i) {
4915 k = isl_basic_map_alloc_equality(bmap);
4916 if (k < 0)
4917 goto error;
4918 isl_seq_clr(bmap->eq[k], 1 + total);
4919 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4920 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4923 bmap = isl_basic_map_gauss(bmap, NULL);
4924 return isl_basic_map_finalize(bmap);
4925 error:
4926 isl_basic_map_free(bmap);
4927 return NULL;
4930 __isl_give isl_basic_map *isl_basic_map_range_map(
4931 __isl_take isl_basic_map *bmap)
4933 int i, k;
4934 isl_space *dim;
4935 isl_basic_map *range;
4936 int nparam, n_in, n_out;
4937 unsigned total;
4939 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4940 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4941 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4943 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4944 range = isl_basic_map_universe(dim);
4946 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4947 bmap = isl_basic_map_apply_range(bmap, range);
4948 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4950 total = isl_basic_map_total_dim(bmap);
4952 for (i = 0; i < n_out; ++i) {
4953 k = isl_basic_map_alloc_equality(bmap);
4954 if (k < 0)
4955 goto error;
4956 isl_seq_clr(bmap->eq[k], 1 + total);
4957 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4958 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4961 bmap = isl_basic_map_gauss(bmap, NULL);
4962 return isl_basic_map_finalize(bmap);
4963 error:
4964 isl_basic_map_free(bmap);
4965 return NULL;
4968 int isl_map_may_be_set(__isl_keep isl_map *map)
4970 if (!map)
4971 return -1;
4972 return isl_space_may_be_set(map->dim);
4975 /* Is this map actually a set?
4976 * Users should never call this function. Outside of isl,
4977 * the type should indicate whether something is a set or a map.
4979 int isl_map_is_set(__isl_keep isl_map *map)
4981 if (!map)
4982 return -1;
4983 return isl_space_is_set(map->dim);
4986 struct isl_set *isl_map_range(struct isl_map *map)
4988 int i;
4989 struct isl_set *set;
4991 if (!map)
4992 goto error;
4993 if (isl_map_is_set(map))
4994 return (isl_set *)map;
4996 map = isl_map_cow(map);
4997 if (!map)
4998 goto error;
5000 set = (struct isl_set *) map;
5001 set->dim = isl_space_range(set->dim);
5002 if (!set->dim)
5003 goto error;
5004 for (i = 0; i < map->n; ++i) {
5005 set->p[i] = isl_basic_map_range(map->p[i]);
5006 if (!set->p[i])
5007 goto error;
5009 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5010 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5011 return set;
5012 error:
5013 isl_map_free(map);
5014 return NULL;
5017 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5019 int i;
5021 map = isl_map_cow(map);
5022 if (!map)
5023 return NULL;
5025 map->dim = isl_space_domain_map(map->dim);
5026 if (!map->dim)
5027 goto error;
5028 for (i = 0; i < map->n; ++i) {
5029 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5030 if (!map->p[i])
5031 goto error;
5033 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5034 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5035 return map;
5036 error:
5037 isl_map_free(map);
5038 return NULL;
5041 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5043 int i;
5044 isl_space *range_dim;
5046 map = isl_map_cow(map);
5047 if (!map)
5048 return NULL;
5050 range_dim = isl_space_range(isl_map_get_space(map));
5051 range_dim = isl_space_from_range(range_dim);
5052 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5053 map->dim = isl_space_join(map->dim, range_dim);
5054 if (!map->dim)
5055 goto error;
5056 for (i = 0; i < map->n; ++i) {
5057 map->p[i] = isl_basic_map_range_map(map->p[i]);
5058 if (!map->p[i])
5059 goto error;
5061 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5062 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5063 return map;
5064 error:
5065 isl_map_free(map);
5066 return NULL;
5069 /* Given a wrapped map of the form A[B -> C],
5070 * return the map A[B -> C] -> B.
5072 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5074 isl_id *id;
5075 isl_map *map;
5077 if (!set)
5078 return NULL;
5079 if (!isl_set_has_tuple_id(set))
5080 return isl_map_domain_map(isl_set_unwrap(set));
5082 id = isl_set_get_tuple_id(set);
5083 map = isl_map_domain_map(isl_set_unwrap(set));
5084 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5086 return map;
5089 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5090 __isl_take isl_space *dim)
5092 int i;
5093 struct isl_map *map = NULL;
5095 set = isl_set_cow(set);
5096 if (!set || !dim)
5097 goto error;
5098 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5099 map = (struct isl_map *)set;
5100 for (i = 0; i < set->n; ++i) {
5101 map->p[i] = isl_basic_map_from_basic_set(
5102 set->p[i], isl_space_copy(dim));
5103 if (!map->p[i])
5104 goto error;
5106 isl_space_free(map->dim);
5107 map->dim = dim;
5108 return map;
5109 error:
5110 isl_space_free(dim);
5111 isl_set_free(set);
5112 return NULL;
5115 __isl_give isl_basic_map *isl_basic_map_from_domain(
5116 __isl_take isl_basic_set *bset)
5118 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5121 __isl_give isl_basic_map *isl_basic_map_from_range(
5122 __isl_take isl_basic_set *bset)
5124 isl_space *space;
5125 space = isl_basic_set_get_space(bset);
5126 space = isl_space_from_range(space);
5127 bset = isl_basic_set_reset_space(bset, space);
5128 return (isl_basic_map *)bset;
5131 /* Create a relation with the given set as range.
5132 * The domain of the created relation is a zero-dimensional
5133 * flat anonymous space.
5135 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5137 isl_space *space;
5138 space = isl_set_get_space(set);
5139 space = isl_space_from_range(space);
5140 set = isl_set_reset_space(set, space);
5141 return (struct isl_map *)set;
5144 /* Create a relation with the given set as domain.
5145 * The range of the created relation is a zero-dimensional
5146 * flat anonymous space.
5148 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5150 return isl_map_reverse(isl_map_from_range(set));
5153 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5154 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5156 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5159 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5160 __isl_take isl_set *range)
5162 return isl_map_apply_range(isl_map_reverse(domain), range);
5165 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5166 unsigned flags)
5168 struct isl_map *map;
5170 if (!dim)
5171 return NULL;
5172 if (n < 0)
5173 isl_die(dim->ctx, isl_error_internal,
5174 "negative number of basic maps", goto error);
5175 map = isl_alloc(dim->ctx, struct isl_map,
5176 sizeof(struct isl_map) +
5177 (n - 1) * sizeof(struct isl_basic_map *));
5178 if (!map)
5179 goto error;
5181 map->ctx = dim->ctx;
5182 isl_ctx_ref(map->ctx);
5183 map->ref = 1;
5184 map->size = n;
5185 map->n = 0;
5186 map->dim = dim;
5187 map->flags = flags;
5188 return map;
5189 error:
5190 isl_space_free(dim);
5191 return NULL;
5194 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5195 unsigned nparam, unsigned in, unsigned out, int n,
5196 unsigned flags)
5198 struct isl_map *map;
5199 isl_space *dims;
5201 dims = isl_space_alloc(ctx, nparam, in, out);
5202 if (!dims)
5203 return NULL;
5205 map = isl_map_alloc_space(dims, n, flags);
5206 return map;
5209 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5211 struct isl_basic_map *bmap;
5212 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5213 bmap = isl_basic_map_set_to_empty(bmap);
5214 return bmap;
5217 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5219 struct isl_basic_set *bset;
5220 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5221 bset = isl_basic_set_set_to_empty(bset);
5222 return bset;
5225 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5227 struct isl_basic_map *bmap;
5228 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5229 bmap = isl_basic_map_finalize(bmap);
5230 return bmap;
5233 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5235 struct isl_basic_set *bset;
5236 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5237 bset = isl_basic_set_finalize(bset);
5238 return bset;
5241 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5243 int i;
5244 unsigned total = isl_space_dim(dim, isl_dim_all);
5245 isl_basic_map *bmap;
5247 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5248 for (i = 0; i < total; ++i) {
5249 int k = isl_basic_map_alloc_inequality(bmap);
5250 if (k < 0)
5251 goto error;
5252 isl_seq_clr(bmap->ineq[k], 1 + total);
5253 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5255 return bmap;
5256 error:
5257 isl_basic_map_free(bmap);
5258 return NULL;
5261 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5263 return isl_basic_map_nat_universe(dim);
5266 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5268 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5271 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5273 return isl_map_nat_universe(dim);
5276 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5278 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5281 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5283 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5286 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5288 struct isl_map *map;
5289 if (!dim)
5290 return NULL;
5291 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5292 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5293 return map;
5296 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5298 struct isl_set *set;
5299 if (!dim)
5300 return NULL;
5301 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5302 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5303 return set;
5306 struct isl_map *isl_map_dup(struct isl_map *map)
5308 int i;
5309 struct isl_map *dup;
5311 if (!map)
5312 return NULL;
5313 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5314 for (i = 0; i < map->n; ++i)
5315 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5316 return dup;
5319 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5320 __isl_take isl_basic_map *bmap)
5322 if (!bmap || !map)
5323 goto error;
5324 if (isl_basic_map_plain_is_empty(bmap)) {
5325 isl_basic_map_free(bmap);
5326 return map;
5328 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5329 isl_assert(map->ctx, map->n < map->size, goto error);
5330 map->p[map->n] = bmap;
5331 map->n++;
5332 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5333 return map;
5334 error:
5335 if (map)
5336 isl_map_free(map);
5337 if (bmap)
5338 isl_basic_map_free(bmap);
5339 return NULL;
5342 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5344 int i;
5346 if (!map)
5347 return NULL;
5349 if (--map->ref > 0)
5350 return NULL;
5352 isl_ctx_deref(map->ctx);
5353 for (i = 0; i < map->n; ++i)
5354 isl_basic_map_free(map->p[i]);
5355 isl_space_free(map->dim);
5356 free(map);
5358 return NULL;
5361 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5362 struct isl_basic_map *bmap, unsigned pos, int value)
5364 int j;
5366 bmap = isl_basic_map_cow(bmap);
5367 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5368 j = isl_basic_map_alloc_equality(bmap);
5369 if (j < 0)
5370 goto error;
5371 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5372 isl_int_set_si(bmap->eq[j][pos], -1);
5373 isl_int_set_si(bmap->eq[j][0], value);
5374 bmap = isl_basic_map_simplify(bmap);
5375 return isl_basic_map_finalize(bmap);
5376 error:
5377 isl_basic_map_free(bmap);
5378 return NULL;
5381 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5382 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5384 int j;
5386 bmap = isl_basic_map_cow(bmap);
5387 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5388 j = isl_basic_map_alloc_equality(bmap);
5389 if (j < 0)
5390 goto error;
5391 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5392 isl_int_set_si(bmap->eq[j][pos], -1);
5393 isl_int_set(bmap->eq[j][0], value);
5394 bmap = isl_basic_map_simplify(bmap);
5395 return isl_basic_map_finalize(bmap);
5396 error:
5397 isl_basic_map_free(bmap);
5398 return NULL;
5401 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5402 enum isl_dim_type type, unsigned pos, int value)
5404 if (!bmap)
5405 return NULL;
5406 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5407 return isl_basic_map_fix_pos_si(bmap,
5408 isl_basic_map_offset(bmap, type) + pos, value);
5409 error:
5410 isl_basic_map_free(bmap);
5411 return NULL;
5414 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5415 enum isl_dim_type type, unsigned pos, isl_int value)
5417 if (!bmap)
5418 return NULL;
5419 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5420 return isl_basic_map_fix_pos(bmap,
5421 isl_basic_map_offset(bmap, type) + pos, value);
5422 error:
5423 isl_basic_map_free(bmap);
5424 return NULL;
5427 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5428 * to be equal to "v".
5430 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5431 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5433 if (!bmap || !v)
5434 goto error;
5435 if (!isl_val_is_int(v))
5436 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5437 "expecting integer value", goto error);
5438 if (pos >= isl_basic_map_dim(bmap, type))
5439 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5440 "index out of bounds", goto error);
5441 pos += isl_basic_map_offset(bmap, type);
5442 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5443 isl_val_free(v);
5444 return bmap;
5445 error:
5446 isl_basic_map_free(bmap);
5447 isl_val_free(v);
5448 return NULL;
5451 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5452 * to be equal to "v".
5454 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5455 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5457 return isl_basic_map_fix_val(bset, type, pos, v);
5460 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5461 enum isl_dim_type type, unsigned pos, int value)
5463 return (struct isl_basic_set *)
5464 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5465 type, pos, value);
5468 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5469 enum isl_dim_type type, unsigned pos, isl_int value)
5471 return (struct isl_basic_set *)
5472 isl_basic_map_fix((struct isl_basic_map *)bset,
5473 type, pos, value);
5476 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5477 unsigned input, int value)
5479 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5482 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5483 unsigned dim, int value)
5485 return (struct isl_basic_set *)
5486 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5487 isl_dim_set, dim, value);
5490 static int remove_if_empty(__isl_keep isl_map *map, int i)
5492 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5494 if (empty < 0)
5495 return -1;
5496 if (!empty)
5497 return 0;
5499 isl_basic_map_free(map->p[i]);
5500 if (i != map->n - 1) {
5501 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5502 map->p[i] = map->p[map->n - 1];
5504 map->n--;
5506 return 0;
5509 /* Perform "fn" on each basic map of "map", where we may not be holding
5510 * the only reference to "map".
5511 * In particular, "fn" should be a semantics preserving operation
5512 * that we want to apply to all copies of "map". We therefore need
5513 * to be careful not to modify "map" in a way that breaks "map"
5514 * in case anything goes wrong.
5516 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5517 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5519 struct isl_basic_map *bmap;
5520 int i;
5522 if (!map)
5523 return NULL;
5525 for (i = map->n - 1; i >= 0; --i) {
5526 bmap = isl_basic_map_copy(map->p[i]);
5527 bmap = fn(bmap);
5528 if (!bmap)
5529 goto error;
5530 isl_basic_map_free(map->p[i]);
5531 map->p[i] = bmap;
5532 if (remove_if_empty(map, i) < 0)
5533 goto error;
5536 return map;
5537 error:
5538 isl_map_free(map);
5539 return NULL;
5542 struct isl_map *isl_map_fix_si(struct isl_map *map,
5543 enum isl_dim_type type, unsigned pos, int value)
5545 int i;
5547 map = isl_map_cow(map);
5548 if (!map)
5549 return NULL;
5551 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5552 for (i = map->n - 1; i >= 0; --i) {
5553 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5554 if (remove_if_empty(map, i) < 0)
5555 goto error;
5557 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5558 return map;
5559 error:
5560 isl_map_free(map);
5561 return NULL;
5564 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5565 enum isl_dim_type type, unsigned pos, int value)
5567 return (struct isl_set *)
5568 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5571 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5572 enum isl_dim_type type, unsigned pos, isl_int value)
5574 int i;
5576 map = isl_map_cow(map);
5577 if (!map)
5578 return NULL;
5580 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5581 for (i = 0; i < map->n; ++i) {
5582 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5583 if (!map->p[i])
5584 goto error;
5586 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5587 return map;
5588 error:
5589 isl_map_free(map);
5590 return NULL;
5593 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5594 enum isl_dim_type type, unsigned pos, isl_int value)
5596 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5599 /* Fix the value of the variable at position "pos" of type "type" of "map"
5600 * to be equal to "v".
5602 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5603 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5605 int i;
5607 map = isl_map_cow(map);
5608 if (!map || !v)
5609 goto error;
5611 if (!isl_val_is_int(v))
5612 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5613 "expecting integer value", goto error);
5614 if (pos >= isl_map_dim(map, type))
5615 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5616 "index out of bounds", goto error);
5617 for (i = map->n - 1; i >= 0; --i) {
5618 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5619 isl_val_copy(v));
5620 if (remove_if_empty(map, i) < 0)
5621 goto error;
5623 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5624 isl_val_free(v);
5625 return map;
5626 error:
5627 isl_map_free(map);
5628 isl_val_free(v);
5629 return NULL;
5632 /* Fix the value of the variable at position "pos" of type "type" of "set"
5633 * to be equal to "v".
5635 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5636 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5638 return isl_map_fix_val(set, type, pos, v);
5641 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5642 unsigned input, int value)
5644 return isl_map_fix_si(map, isl_dim_in, input, value);
5647 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5649 return (struct isl_set *)
5650 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5653 static __isl_give isl_basic_map *basic_map_bound_si(
5654 __isl_take isl_basic_map *bmap,
5655 enum isl_dim_type type, unsigned pos, int value, int upper)
5657 int j;
5659 if (!bmap)
5660 return NULL;
5661 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5662 pos += isl_basic_map_offset(bmap, type);
5663 bmap = isl_basic_map_cow(bmap);
5664 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5665 j = isl_basic_map_alloc_inequality(bmap);
5666 if (j < 0)
5667 goto error;
5668 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5669 if (upper) {
5670 isl_int_set_si(bmap->ineq[j][pos], -1);
5671 isl_int_set_si(bmap->ineq[j][0], value);
5672 } else {
5673 isl_int_set_si(bmap->ineq[j][pos], 1);
5674 isl_int_set_si(bmap->ineq[j][0], -value);
5676 bmap = isl_basic_map_simplify(bmap);
5677 return isl_basic_map_finalize(bmap);
5678 error:
5679 isl_basic_map_free(bmap);
5680 return NULL;
5683 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5684 __isl_take isl_basic_map *bmap,
5685 enum isl_dim_type type, unsigned pos, int value)
5687 return basic_map_bound_si(bmap, type, pos, value, 0);
5690 /* Constrain the values of the given dimension to be no greater than "value".
5692 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5693 __isl_take isl_basic_map *bmap,
5694 enum isl_dim_type type, unsigned pos, int value)
5696 return basic_map_bound_si(bmap, type, pos, value, 1);
5699 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5700 unsigned dim, isl_int value)
5702 int j;
5704 bset = isl_basic_set_cow(bset);
5705 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5706 j = isl_basic_set_alloc_inequality(bset);
5707 if (j < 0)
5708 goto error;
5709 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5710 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5711 isl_int_neg(bset->ineq[j][0], value);
5712 bset = isl_basic_set_simplify(bset);
5713 return isl_basic_set_finalize(bset);
5714 error:
5715 isl_basic_set_free(bset);
5716 return NULL;
5719 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5720 enum isl_dim_type type, unsigned pos, int value, int upper)
5722 int i;
5724 map = isl_map_cow(map);
5725 if (!map)
5726 return NULL;
5728 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5729 for (i = 0; i < map->n; ++i) {
5730 map->p[i] = basic_map_bound_si(map->p[i],
5731 type, pos, value, upper);
5732 if (!map->p[i])
5733 goto error;
5735 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5736 return map;
5737 error:
5738 isl_map_free(map);
5739 return NULL;
5742 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5743 enum isl_dim_type type, unsigned pos, int value)
5745 return map_bound_si(map, type, pos, value, 0);
5748 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5749 enum isl_dim_type type, unsigned pos, int value)
5751 return map_bound_si(map, type, pos, value, 1);
5754 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5755 enum isl_dim_type type, unsigned pos, int value)
5757 return (struct isl_set *)
5758 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5761 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5762 enum isl_dim_type type, unsigned pos, int value)
5764 return isl_map_upper_bound_si(set, type, pos, value);
5767 /* Bound the given variable of "bmap" from below (or above is "upper"
5768 * is set) to "value".
5770 static __isl_give isl_basic_map *basic_map_bound(
5771 __isl_take isl_basic_map *bmap,
5772 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5774 int j;
5776 if (!bmap)
5777 return NULL;
5778 if (pos >= isl_basic_map_dim(bmap, type))
5779 isl_die(bmap->ctx, isl_error_invalid,
5780 "index out of bounds", goto error);
5781 pos += isl_basic_map_offset(bmap, type);
5782 bmap = isl_basic_map_cow(bmap);
5783 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5784 j = isl_basic_map_alloc_inequality(bmap);
5785 if (j < 0)
5786 goto error;
5787 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5788 if (upper) {
5789 isl_int_set_si(bmap->ineq[j][pos], -1);
5790 isl_int_set(bmap->ineq[j][0], value);
5791 } else {
5792 isl_int_set_si(bmap->ineq[j][pos], 1);
5793 isl_int_neg(bmap->ineq[j][0], value);
5795 bmap = isl_basic_map_simplify(bmap);
5796 return isl_basic_map_finalize(bmap);
5797 error:
5798 isl_basic_map_free(bmap);
5799 return NULL;
5802 /* Bound the given variable of "map" from below (or above is "upper"
5803 * is set) to "value".
5805 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5806 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5808 int i;
5810 map = isl_map_cow(map);
5811 if (!map)
5812 return NULL;
5814 if (pos >= isl_map_dim(map, type))
5815 isl_die(map->ctx, isl_error_invalid,
5816 "index out of bounds", goto error);
5817 for (i = map->n - 1; i >= 0; --i) {
5818 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5819 if (remove_if_empty(map, i) < 0)
5820 goto error;
5822 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5823 return map;
5824 error:
5825 isl_map_free(map);
5826 return NULL;
5829 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5830 enum isl_dim_type type, unsigned pos, isl_int value)
5832 return map_bound(map, type, pos, value, 0);
5835 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5836 enum isl_dim_type type, unsigned pos, isl_int value)
5838 return map_bound(map, type, pos, value, 1);
5841 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5842 enum isl_dim_type type, unsigned pos, isl_int value)
5844 return isl_map_lower_bound(set, type, pos, value);
5847 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5848 enum isl_dim_type type, unsigned pos, isl_int value)
5850 return isl_map_upper_bound(set, type, pos, value);
5853 /* Force the values of the variable at position "pos" of type "type" of "set"
5854 * to be no smaller than "value".
5856 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5857 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5859 if (!value)
5860 goto error;
5861 if (!isl_val_is_int(value))
5862 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5863 "expecting integer value", goto error);
5864 set = isl_set_lower_bound(set, type, pos, value->n);
5865 isl_val_free(value);
5866 return set;
5867 error:
5868 isl_val_free(value);
5869 isl_set_free(set);
5870 return NULL;
5873 /* Force the values of the variable at position "pos" of type "type" of "set"
5874 * to be no greater than "value".
5876 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5877 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5879 if (!value)
5880 goto error;
5881 if (!isl_val_is_int(value))
5882 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5883 "expecting integer value", goto error);
5884 set = isl_set_upper_bound(set, type, pos, value->n);
5885 isl_val_free(value);
5886 return set;
5887 error:
5888 isl_val_free(value);
5889 isl_set_free(set);
5890 return NULL;
5893 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5894 isl_int value)
5896 int i;
5898 set = isl_set_cow(set);
5899 if (!set)
5900 return NULL;
5902 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5903 for (i = 0; i < set->n; ++i) {
5904 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5905 if (!set->p[i])
5906 goto error;
5908 return set;
5909 error:
5910 isl_set_free(set);
5911 return NULL;
5914 struct isl_map *isl_map_reverse(struct isl_map *map)
5916 int i;
5918 map = isl_map_cow(map);
5919 if (!map)
5920 return NULL;
5922 map->dim = isl_space_reverse(map->dim);
5923 if (!map->dim)
5924 goto error;
5925 for (i = 0; i < map->n; ++i) {
5926 map->p[i] = isl_basic_map_reverse(map->p[i]);
5927 if (!map->p[i])
5928 goto error;
5930 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5931 return map;
5932 error:
5933 isl_map_free(map);
5934 return NULL;
5937 static struct isl_map *isl_basic_map_partial_lexopt(
5938 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5939 struct isl_set **empty, int max)
5941 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5944 struct isl_map *isl_basic_map_partial_lexmax(
5945 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5946 struct isl_set **empty)
5948 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5951 struct isl_map *isl_basic_map_partial_lexmin(
5952 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5953 struct isl_set **empty)
5955 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5958 struct isl_set *isl_basic_set_partial_lexmin(
5959 struct isl_basic_set *bset, struct isl_basic_set *dom,
5960 struct isl_set **empty)
5962 return (struct isl_set *)
5963 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5964 dom, empty);
5967 struct isl_set *isl_basic_set_partial_lexmax(
5968 struct isl_basic_set *bset, struct isl_basic_set *dom,
5969 struct isl_set **empty)
5971 return (struct isl_set *)
5972 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5973 dom, empty);
5976 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5977 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5978 __isl_give isl_set **empty)
5980 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5983 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5984 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5985 __isl_give isl_set **empty)
5987 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5990 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5991 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5992 __isl_give isl_set **empty)
5994 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5997 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5998 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5999 __isl_give isl_set **empty)
6001 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6004 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6005 __isl_take isl_basic_map *bmap, int max)
6007 isl_basic_set *dom = NULL;
6008 isl_space *dom_space;
6010 if (!bmap)
6011 goto error;
6012 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6013 dom = isl_basic_set_universe(dom_space);
6014 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6015 error:
6016 isl_basic_map_free(bmap);
6017 return NULL;
6020 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6021 __isl_take isl_basic_map *bmap)
6023 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6026 #undef TYPE
6027 #define TYPE isl_pw_multi_aff
6028 #undef SUFFIX
6029 #define SUFFIX _pw_multi_aff
6030 #undef EMPTY
6031 #define EMPTY isl_pw_multi_aff_empty
6032 #undef ADD
6033 #define ADD isl_pw_multi_aff_union_add
6034 #include "isl_map_lexopt_templ.c"
6036 /* Given a map "map", compute the lexicographically minimal
6037 * (or maximal) image element for each domain element in dom,
6038 * in the form of an isl_pw_multi_aff.
6039 * Set *empty to those elements in dom that do not have an image element.
6041 * We first compute the lexicographically minimal or maximal element
6042 * in the first basic map. This results in a partial solution "res"
6043 * and a subset "todo" of dom that still need to be handled.
6044 * We then consider each of the remaining maps in "map" and successively
6045 * update both "res" and "todo".
6047 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6048 __isl_take isl_map *map, __isl_take isl_set *dom,
6049 __isl_give isl_set **empty, int max)
6051 int i;
6052 isl_pw_multi_aff *res;
6053 isl_set *todo;
6055 if (!map || !dom)
6056 goto error;
6058 if (isl_map_plain_is_empty(map)) {
6059 if (empty)
6060 *empty = dom;
6061 else
6062 isl_set_free(dom);
6063 return isl_pw_multi_aff_from_map(map);
6066 res = basic_map_partial_lexopt_pw_multi_aff(
6067 isl_basic_map_copy(map->p[0]),
6068 isl_set_copy(dom), &todo, max);
6070 for (i = 1; i < map->n; ++i) {
6071 isl_pw_multi_aff *res_i;
6072 isl_set *todo_i;
6074 res_i = basic_map_partial_lexopt_pw_multi_aff(
6075 isl_basic_map_copy(map->p[i]),
6076 isl_set_copy(dom), &todo_i, max);
6078 if (max)
6079 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6080 else
6081 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6083 todo = isl_set_intersect(todo, todo_i);
6086 isl_set_free(dom);
6087 isl_map_free(map);
6089 if (empty)
6090 *empty = todo;
6091 else
6092 isl_set_free(todo);
6094 return res;
6095 error:
6096 if (empty)
6097 *empty = NULL;
6098 isl_set_free(dom);
6099 isl_map_free(map);
6100 return NULL;
6103 #undef TYPE
6104 #define TYPE isl_map
6105 #undef SUFFIX
6106 #define SUFFIX
6107 #undef EMPTY
6108 #define EMPTY isl_map_empty
6109 #undef ADD
6110 #define ADD isl_map_union_disjoint
6111 #include "isl_map_lexopt_templ.c"
6113 /* Given a map "map", compute the lexicographically minimal
6114 * (or maximal) image element for each domain element in dom.
6115 * Set *empty to those elements in dom that do not have an image element.
6117 * We first compute the lexicographically minimal or maximal element
6118 * in the first basic map. This results in a partial solution "res"
6119 * and a subset "todo" of dom that still need to be handled.
6120 * We then consider each of the remaining maps in "map" and successively
6121 * update both "res" and "todo".
6123 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6124 * Assume we are computing the lexicographical maximum.
6125 * We first compute the lexicographically maximal element in basic map i.
6126 * This results in a partial solution res_i and a subset todo_i.
6127 * Then we combine these results with those obtain for the first k basic maps
6128 * to obtain a result that is valid for the first k+1 basic maps.
6129 * In particular, the set where there is no solution is the set where
6130 * there is no solution for the first k basic maps and also no solution
6131 * for the ith basic map, i.e.,
6133 * todo^i = todo^k * todo_i
6135 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6136 * solutions, arbitrarily breaking ties in favor of res^k.
6137 * That is, when res^k(a) >= res_i(a), we pick res^k and
6138 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6139 * the lexicographic order.)
6140 * In practice, we compute
6142 * res^k * (res_i . "<=")
6144 * and
6146 * res_i * (res^k . "<")
6148 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6149 * where only one of res^k and res_i provides a solution and we simply pick
6150 * that one, i.e.,
6152 * res^k * todo_i
6153 * and
6154 * res_i * todo^k
6156 * Note that we only compute these intersections when dom(res^k) intersects
6157 * dom(res_i). Otherwise, the only effect of these intersections is to
6158 * potentially break up res^k and res_i into smaller pieces.
6159 * We want to avoid such splintering as much as possible.
6160 * In fact, an earlier implementation of this function would look for
6161 * better results in the domain of res^k and for extra results in todo^k,
6162 * but this would always result in a splintering according to todo^k,
6163 * even when the domain of basic map i is disjoint from the domains of
6164 * the previous basic maps.
6166 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6167 __isl_take isl_map *map, __isl_take isl_set *dom,
6168 __isl_give isl_set **empty, int max)
6170 int i;
6171 struct isl_map *res;
6172 struct isl_set *todo;
6174 if (!map || !dom)
6175 goto error;
6177 if (isl_map_plain_is_empty(map)) {
6178 if (empty)
6179 *empty = dom;
6180 else
6181 isl_set_free(dom);
6182 return map;
6185 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6186 isl_set_copy(dom), &todo, max);
6188 for (i = 1; i < map->n; ++i) {
6189 isl_map *lt, *le;
6190 isl_map *res_i;
6191 isl_set *todo_i;
6192 isl_space *dim = isl_space_range(isl_map_get_space(res));
6194 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6195 isl_set_copy(dom), &todo_i, max);
6197 if (max) {
6198 lt = isl_map_lex_lt(isl_space_copy(dim));
6199 le = isl_map_lex_le(dim);
6200 } else {
6201 lt = isl_map_lex_gt(isl_space_copy(dim));
6202 le = isl_map_lex_ge(dim);
6204 lt = isl_map_apply_range(isl_map_copy(res), lt);
6205 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6206 le = isl_map_apply_range(isl_map_copy(res_i), le);
6207 le = isl_map_intersect(le, isl_map_copy(res));
6209 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6210 res = isl_map_intersect_domain(res,
6211 isl_set_copy(todo_i));
6212 res_i = isl_map_intersect_domain(res_i,
6213 isl_set_copy(todo));
6216 res = isl_map_union_disjoint(res, res_i);
6217 res = isl_map_union_disjoint(res, lt);
6218 res = isl_map_union_disjoint(res, le);
6220 todo = isl_set_intersect(todo, todo_i);
6223 isl_set_free(dom);
6224 isl_map_free(map);
6226 if (empty)
6227 *empty = todo;
6228 else
6229 isl_set_free(todo);
6231 return res;
6232 error:
6233 if (empty)
6234 *empty = NULL;
6235 isl_set_free(dom);
6236 isl_map_free(map);
6237 return NULL;
6240 __isl_give isl_map *isl_map_partial_lexmax(
6241 __isl_take isl_map *map, __isl_take isl_set *dom,
6242 __isl_give isl_set **empty)
6244 return isl_map_partial_lexopt(map, dom, empty, 1);
6247 __isl_give isl_map *isl_map_partial_lexmin(
6248 __isl_take isl_map *map, __isl_take isl_set *dom,
6249 __isl_give isl_set **empty)
6251 return isl_map_partial_lexopt(map, dom, empty, 0);
6254 __isl_give isl_set *isl_set_partial_lexmin(
6255 __isl_take isl_set *set, __isl_take isl_set *dom,
6256 __isl_give isl_set **empty)
6258 return (struct isl_set *)
6259 isl_map_partial_lexmin((struct isl_map *)set,
6260 dom, empty);
6263 __isl_give isl_set *isl_set_partial_lexmax(
6264 __isl_take isl_set *set, __isl_take isl_set *dom,
6265 __isl_give isl_set **empty)
6267 return (struct isl_set *)
6268 isl_map_partial_lexmax((struct isl_map *)set,
6269 dom, empty);
6272 /* Compute the lexicographic minimum (or maximum if "max" is set)
6273 * of "bmap" over its domain.
6275 * Since we are not interested in the part of the domain space where
6276 * there is no solution, we initialize the domain to those constraints
6277 * of "bmap" that only involve the parameters and the input dimensions.
6278 * This relieves the parametric programming engine from detecting those
6279 * inequalities and transferring them to the context. More importantly,
6280 * it ensures that those inequalities are transferred first and not
6281 * intermixed with inequalities that actually split the domain.
6283 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6285 int n_div;
6286 int n_out;
6287 isl_basic_map *copy;
6288 isl_basic_set *dom;
6290 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6291 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6292 copy = isl_basic_map_copy(bmap);
6293 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6294 isl_dim_div, 0, n_div);
6295 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6296 isl_dim_out, 0, n_out);
6297 dom = isl_basic_map_domain(copy);
6298 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6301 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6303 return isl_basic_map_lexopt(bmap, 0);
6306 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6308 return isl_basic_map_lexopt(bmap, 1);
6311 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6313 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6316 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6318 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6321 /* Extract the first and only affine expression from list
6322 * and then add it to *pwaff with the given dom.
6323 * This domain is known to be disjoint from other domains
6324 * because of the way isl_basic_map_foreach_lexmax works.
6326 static int update_dim_opt(__isl_take isl_basic_set *dom,
6327 __isl_take isl_aff_list *list, void *user)
6329 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6330 isl_aff *aff;
6331 isl_pw_aff **pwaff = user;
6332 isl_pw_aff *pwaff_i;
6334 if (!list)
6335 goto error;
6336 if (isl_aff_list_n_aff(list) != 1)
6337 isl_die(ctx, isl_error_internal,
6338 "expecting single element list", goto error);
6340 aff = isl_aff_list_get_aff(list, 0);
6341 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6343 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6345 isl_aff_list_free(list);
6347 return 0;
6348 error:
6349 isl_basic_set_free(dom);
6350 isl_aff_list_free(list);
6351 return -1;
6354 /* Given a basic map with one output dimension, compute the minimum or
6355 * maximum of that dimension as an isl_pw_aff.
6357 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6358 * call update_dim_opt on each leaf of the result.
6360 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6361 int max)
6363 isl_space *dim = isl_basic_map_get_space(bmap);
6364 isl_pw_aff *pwaff;
6365 int r;
6367 dim = isl_space_from_domain(isl_space_domain(dim));
6368 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6369 pwaff = isl_pw_aff_empty(dim);
6371 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6372 if (r < 0)
6373 return isl_pw_aff_free(pwaff);
6375 return pwaff;
6378 /* Compute the minimum or maximum of the given output dimension
6379 * as a function of the parameters and the input dimensions,
6380 * but independently of the other output dimensions.
6382 * We first project out the other output dimension and then compute
6383 * the "lexicographic" maximum in each basic map, combining the results
6384 * using isl_pw_aff_union_max.
6386 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6387 int max)
6389 int i;
6390 isl_pw_aff *pwaff;
6391 unsigned n_out;
6393 n_out = isl_map_dim(map, isl_dim_out);
6394 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6395 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6396 if (!map)
6397 return NULL;
6399 if (map->n == 0) {
6400 isl_space *dim = isl_map_get_space(map);
6401 isl_map_free(map);
6402 return isl_pw_aff_empty(dim);
6405 pwaff = basic_map_dim_opt(map->p[0], max);
6406 for (i = 1; i < map->n; ++i) {
6407 isl_pw_aff *pwaff_i;
6409 pwaff_i = basic_map_dim_opt(map->p[i], max);
6410 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6413 isl_map_free(map);
6415 return pwaff;
6418 /* Compute the maximum of the given output dimension as a function of the
6419 * parameters and input dimensions, but independently of
6420 * the other output dimensions.
6422 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6424 return map_dim_opt(map, pos, 1);
6427 /* Compute the minimum or maximum of the given set dimension
6428 * as a function of the parameters,
6429 * but independently of the other set dimensions.
6431 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6432 int max)
6434 return map_dim_opt(set, pos, max);
6437 /* Compute the maximum of the given set dimension as a function of the
6438 * parameters, but independently of the other set dimensions.
6440 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6442 return set_dim_opt(set, pos, 1);
6445 /* Compute the minimum of the given set dimension as a function of the
6446 * parameters, but independently of the other set dimensions.
6448 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6450 return set_dim_opt(set, pos, 0);
6453 /* Apply a preimage specified by "mat" on the parameters of "bset".
6454 * bset is assumed to have only parameters and divs.
6456 static struct isl_basic_set *basic_set_parameter_preimage(
6457 struct isl_basic_set *bset, struct isl_mat *mat)
6459 unsigned nparam;
6461 if (!bset || !mat)
6462 goto error;
6464 bset->dim = isl_space_cow(bset->dim);
6465 if (!bset->dim)
6466 goto error;
6468 nparam = isl_basic_set_dim(bset, isl_dim_param);
6470 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6472 bset->dim->nparam = 0;
6473 bset->dim->n_out = nparam;
6474 bset = isl_basic_set_preimage(bset, mat);
6475 if (bset) {
6476 bset->dim->nparam = bset->dim->n_out;
6477 bset->dim->n_out = 0;
6479 return bset;
6480 error:
6481 isl_mat_free(mat);
6482 isl_basic_set_free(bset);
6483 return NULL;
6486 /* Apply a preimage specified by "mat" on the parameters of "set".
6487 * set is assumed to have only parameters and divs.
6489 static struct isl_set *set_parameter_preimage(
6490 struct isl_set *set, struct isl_mat *mat)
6492 isl_space *dim = NULL;
6493 unsigned nparam;
6495 if (!set || !mat)
6496 goto error;
6498 dim = isl_space_copy(set->dim);
6499 dim = isl_space_cow(dim);
6500 if (!dim)
6501 goto error;
6503 nparam = isl_set_dim(set, isl_dim_param);
6505 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6507 dim->nparam = 0;
6508 dim->n_out = nparam;
6509 isl_set_reset_space(set, dim);
6510 set = isl_set_preimage(set, mat);
6511 if (!set)
6512 goto error2;
6513 dim = isl_space_copy(set->dim);
6514 dim = isl_space_cow(dim);
6515 if (!dim)
6516 goto error2;
6517 dim->nparam = dim->n_out;
6518 dim->n_out = 0;
6519 isl_set_reset_space(set, dim);
6520 return set;
6521 error:
6522 isl_space_free(dim);
6523 isl_mat_free(mat);
6524 error2:
6525 isl_set_free(set);
6526 return NULL;
6529 /* Intersect the basic set "bset" with the affine space specified by the
6530 * equalities in "eq".
6532 static struct isl_basic_set *basic_set_append_equalities(
6533 struct isl_basic_set *bset, struct isl_mat *eq)
6535 int i, k;
6536 unsigned len;
6538 if (!bset || !eq)
6539 goto error;
6541 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6542 eq->n_row, 0);
6543 if (!bset)
6544 goto error;
6546 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6547 for (i = 0; i < eq->n_row; ++i) {
6548 k = isl_basic_set_alloc_equality(bset);
6549 if (k < 0)
6550 goto error;
6551 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6552 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6554 isl_mat_free(eq);
6556 bset = isl_basic_set_gauss(bset, NULL);
6557 bset = isl_basic_set_finalize(bset);
6559 return bset;
6560 error:
6561 isl_mat_free(eq);
6562 isl_basic_set_free(bset);
6563 return NULL;
6566 /* Intersect the set "set" with the affine space specified by the
6567 * equalities in "eq".
6569 static struct isl_set *set_append_equalities(struct isl_set *set,
6570 struct isl_mat *eq)
6572 int i;
6574 if (!set || !eq)
6575 goto error;
6577 for (i = 0; i < set->n; ++i) {
6578 set->p[i] = basic_set_append_equalities(set->p[i],
6579 isl_mat_copy(eq));
6580 if (!set->p[i])
6581 goto error;
6583 isl_mat_free(eq);
6584 return set;
6585 error:
6586 isl_mat_free(eq);
6587 isl_set_free(set);
6588 return NULL;
6591 /* Given a basic set "bset" that only involves parameters and existentially
6592 * quantified variables, return the index of the first equality
6593 * that only involves parameters. If there is no such equality then
6594 * return bset->n_eq.
6596 * This function assumes that isl_basic_set_gauss has been called on "bset".
6598 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6600 int i, j;
6601 unsigned nparam, n_div;
6603 if (!bset)
6604 return -1;
6606 nparam = isl_basic_set_dim(bset, isl_dim_param);
6607 n_div = isl_basic_set_dim(bset, isl_dim_div);
6609 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6610 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6611 ++i;
6614 return i;
6617 /* Compute an explicit representation for the existentially quantified
6618 * variables in "bset" by computing the "minimal value" of the set
6619 * variables. Since there are no set variables, the computation of
6620 * the minimal value essentially computes an explicit representation
6621 * of the non-empty part(s) of "bset".
6623 * The input only involves parameters and existentially quantified variables.
6624 * All equalities among parameters have been removed.
6626 * Since the existentially quantified variables in the result are in general
6627 * going to be different from those in the input, we first replace
6628 * them by the minimal number of variables based on their equalities.
6629 * This should simplify the parametric integer programming.
6631 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6633 isl_morph *morph1, *morph2;
6634 isl_set *set;
6635 unsigned n;
6637 if (!bset)
6638 return NULL;
6639 if (bset->n_eq == 0)
6640 return isl_basic_set_lexmin(bset);
6642 morph1 = isl_basic_set_parameter_compression(bset);
6643 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6644 bset = isl_basic_set_lift(bset);
6645 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6646 bset = isl_morph_basic_set(morph2, bset);
6647 n = isl_basic_set_dim(bset, isl_dim_set);
6648 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6650 set = isl_basic_set_lexmin(bset);
6652 set = isl_morph_set(isl_morph_inverse(morph1), set);
6654 return set;
6657 /* Project the given basic set onto its parameter domain, possibly introducing
6658 * new, explicit, existential variables in the constraints.
6659 * The input has parameters and (possibly implicit) existential variables.
6660 * The output has the same parameters, but only
6661 * explicit existentially quantified variables.
6663 * The actual projection is performed by pip, but pip doesn't seem
6664 * to like equalities very much, so we first remove the equalities
6665 * among the parameters by performing a variable compression on
6666 * the parameters. Afterward, an inverse transformation is performed
6667 * and the equalities among the parameters are inserted back in.
6669 * The variable compression on the parameters may uncover additional
6670 * equalities that were only implicit before. We therefore check
6671 * if there are any new parameter equalities in the result and
6672 * if so recurse. The removal of parameter equalities is required
6673 * for the parameter compression performed by base_compute_divs.
6675 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6677 int i;
6678 struct isl_mat *eq;
6679 struct isl_mat *T, *T2;
6680 struct isl_set *set;
6681 unsigned nparam;
6683 bset = isl_basic_set_cow(bset);
6684 if (!bset)
6685 return NULL;
6687 if (bset->n_eq == 0)
6688 return base_compute_divs(bset);
6690 bset = isl_basic_set_gauss(bset, NULL);
6691 if (!bset)
6692 return NULL;
6693 if (isl_basic_set_plain_is_empty(bset))
6694 return isl_set_from_basic_set(bset);
6696 i = first_parameter_equality(bset);
6697 if (i == bset->n_eq)
6698 return base_compute_divs(bset);
6700 nparam = isl_basic_set_dim(bset, isl_dim_param);
6701 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6702 0, 1 + nparam);
6703 eq = isl_mat_cow(eq);
6704 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6705 if (T && T->n_col == 0) {
6706 isl_mat_free(T);
6707 isl_mat_free(T2);
6708 isl_mat_free(eq);
6709 bset = isl_basic_set_set_to_empty(bset);
6710 return isl_set_from_basic_set(bset);
6712 bset = basic_set_parameter_preimage(bset, T);
6714 i = first_parameter_equality(bset);
6715 if (!bset)
6716 set = NULL;
6717 else if (i == bset->n_eq)
6718 set = base_compute_divs(bset);
6719 else
6720 set = parameter_compute_divs(bset);
6721 set = set_parameter_preimage(set, T2);
6722 set = set_append_equalities(set, eq);
6723 return set;
6726 /* Insert the divs from "ls" before those of "bmap".
6728 * The number of columns is not changed, which means that the last
6729 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6730 * The caller is responsible for removing the same number of dimensions
6731 * from the space of "bmap".
6733 static __isl_give isl_basic_map *insert_divs_from_local_space(
6734 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6736 int i;
6737 int n_div;
6738 int old_n_div;
6740 n_div = isl_local_space_dim(ls, isl_dim_div);
6741 if (n_div == 0)
6742 return bmap;
6744 old_n_div = bmap->n_div;
6745 bmap = insert_div_rows(bmap, n_div);
6746 if (!bmap)
6747 return NULL;
6749 for (i = 0; i < n_div; ++i) {
6750 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6751 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6754 return bmap;
6757 /* Replace the space of "bmap" by the space and divs of "ls".
6759 * If "ls" has any divs, then we simplify the result since we may
6760 * have discovered some additional equalities that could simplify
6761 * the div expressions.
6763 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6764 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6766 int n_div;
6768 bmap = isl_basic_map_cow(bmap);
6769 if (!bmap || !ls)
6770 goto error;
6772 n_div = isl_local_space_dim(ls, isl_dim_div);
6773 bmap = insert_divs_from_local_space(bmap, ls);
6774 if (!bmap)
6775 goto error;
6777 isl_space_free(bmap->dim);
6778 bmap->dim = isl_local_space_get_space(ls);
6779 if (!bmap->dim)
6780 goto error;
6782 isl_local_space_free(ls);
6783 if (n_div > 0)
6784 bmap = isl_basic_map_simplify(bmap);
6785 bmap = isl_basic_map_finalize(bmap);
6786 return bmap;
6787 error:
6788 isl_basic_map_free(bmap);
6789 isl_local_space_free(ls);
6790 return NULL;
6793 /* Replace the space of "map" by the space and divs of "ls".
6795 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6796 __isl_take isl_local_space *ls)
6798 int i;
6800 map = isl_map_cow(map);
6801 if (!map || !ls)
6802 goto error;
6804 for (i = 0; i < map->n; ++i) {
6805 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6806 isl_local_space_copy(ls));
6807 if (!map->p[i])
6808 goto error;
6810 isl_space_free(map->dim);
6811 map->dim = isl_local_space_get_space(ls);
6812 if (!map->dim)
6813 goto error;
6815 isl_local_space_free(ls);
6816 return map;
6817 error:
6818 isl_local_space_free(ls);
6819 isl_map_free(map);
6820 return NULL;
6823 /* Compute an explicit representation for the existentially
6824 * quantified variables for which do not know any explicit representation yet.
6826 * We first sort the existentially quantified variables so that the
6827 * existentially quantified variables for which we already have an explicit
6828 * representation are placed before those for which we do not.
6829 * The input dimensions, the output dimensions and the existentially
6830 * quantified variables for which we already have an explicit
6831 * representation are then turned into parameters.
6832 * compute_divs returns a map with the same parameters and
6833 * no input or output dimensions and the dimension specification
6834 * is reset to that of the input, including the existentially quantified
6835 * variables for which we already had an explicit representation.
6837 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6839 struct isl_basic_set *bset;
6840 struct isl_set *set;
6841 struct isl_map *map;
6842 isl_space *dim;
6843 isl_local_space *ls;
6844 unsigned nparam;
6845 unsigned n_in;
6846 unsigned n_out;
6847 unsigned n_known;
6848 int i;
6850 bmap = isl_basic_map_sort_divs(bmap);
6851 bmap = isl_basic_map_cow(bmap);
6852 if (!bmap)
6853 return NULL;
6855 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6856 if (isl_int_is_zero(bmap->div[n_known][0]))
6857 break;
6859 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6860 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6861 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6862 dim = isl_space_set_alloc(bmap->ctx,
6863 nparam + n_in + n_out + n_known, 0);
6864 if (!dim)
6865 goto error;
6867 ls = isl_basic_map_get_local_space(bmap);
6868 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6869 n_known, bmap->n_div - n_known);
6870 if (n_known > 0) {
6871 for (i = n_known; i < bmap->n_div; ++i)
6872 swap_div(bmap, i - n_known, i);
6873 bmap->n_div -= n_known;
6874 bmap->extra -= n_known;
6876 bmap = isl_basic_map_reset_space(bmap, dim);
6877 bset = (struct isl_basic_set *)bmap;
6879 set = parameter_compute_divs(bset);
6880 map = (struct isl_map *)set;
6881 map = replace_space_by_local_space(map, ls);
6883 return map;
6884 error:
6885 isl_basic_map_free(bmap);
6886 return NULL;
6889 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6891 int i;
6892 unsigned off;
6894 if (!bmap)
6895 return -1;
6897 off = isl_space_dim(bmap->dim, isl_dim_all);
6898 for (i = 0; i < bmap->n_div; ++i) {
6899 if (isl_int_is_zero(bmap->div[i][0]))
6900 return 0;
6901 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6902 return -1);
6904 return 1;
6907 static int map_divs_known(__isl_keep isl_map *map)
6909 int i;
6911 if (!map)
6912 return -1;
6914 for (i = 0; i < map->n; ++i) {
6915 int known = isl_basic_map_divs_known(map->p[i]);
6916 if (known <= 0)
6917 return known;
6920 return 1;
6923 /* If bmap contains any unknown divs, then compute explicit
6924 * expressions for them. However, this computation may be
6925 * quite expensive, so first try to remove divs that aren't
6926 * strictly needed.
6928 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6930 int known;
6931 struct isl_map *map;
6933 known = isl_basic_map_divs_known(bmap);
6934 if (known < 0)
6935 goto error;
6936 if (known)
6937 return isl_map_from_basic_map(bmap);
6939 bmap = isl_basic_map_drop_redundant_divs(bmap);
6941 known = isl_basic_map_divs_known(bmap);
6942 if (known < 0)
6943 goto error;
6944 if (known)
6945 return isl_map_from_basic_map(bmap);
6947 map = compute_divs(bmap);
6948 return map;
6949 error:
6950 isl_basic_map_free(bmap);
6951 return NULL;
6954 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6956 int i;
6957 int known;
6958 struct isl_map *res;
6960 if (!map)
6961 return NULL;
6962 if (map->n == 0)
6963 return map;
6965 known = map_divs_known(map);
6966 if (known < 0) {
6967 isl_map_free(map);
6968 return NULL;
6970 if (known)
6971 return map;
6973 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6974 for (i = 1 ; i < map->n; ++i) {
6975 struct isl_map *r2;
6976 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6977 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6978 res = isl_map_union_disjoint(res, r2);
6979 else
6980 res = isl_map_union(res, r2);
6982 isl_map_free(map);
6984 return res;
6987 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6989 return (struct isl_set *)
6990 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6993 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6995 return (struct isl_set *)
6996 isl_map_compute_divs((struct isl_map *)set);
6999 struct isl_set *isl_map_domain(struct isl_map *map)
7001 int i;
7002 struct isl_set *set;
7004 if (!map)
7005 goto error;
7007 map = isl_map_cow(map);
7008 if (!map)
7009 return NULL;
7011 set = (struct isl_set *)map;
7012 set->dim = isl_space_domain(set->dim);
7013 if (!set->dim)
7014 goto error;
7015 for (i = 0; i < map->n; ++i) {
7016 set->p[i] = isl_basic_map_domain(map->p[i]);
7017 if (!set->p[i])
7018 goto error;
7020 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7021 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7022 return set;
7023 error:
7024 isl_map_free(map);
7025 return NULL;
7028 /* Return the union of "map1" and "map2", where we assume for now that
7029 * "map1" and "map2" are disjoint. Note that the basic maps inside
7030 * "map1" or "map2" may not be disjoint from each other.
7031 * Also note that this function is also called from isl_map_union,
7032 * which takes care of handling the situation where "map1" and "map2"
7033 * may not be disjoint.
7035 * If one of the inputs is empty, we can simply return the other input.
7036 * Similarly, if one of the inputs is universal, then it is equal to the union.
7038 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7039 __isl_take isl_map *map2)
7041 int i;
7042 unsigned flags = 0;
7043 struct isl_map *map = NULL;
7044 int is_universe;
7046 if (!map1 || !map2)
7047 goto error;
7049 if (!isl_space_is_equal(map1->dim, map2->dim))
7050 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7051 "spaces don't match", goto error);
7053 if (map1->n == 0) {
7054 isl_map_free(map1);
7055 return map2;
7057 if (map2->n == 0) {
7058 isl_map_free(map2);
7059 return map1;
7062 is_universe = isl_map_plain_is_universe(map1);
7063 if (is_universe < 0)
7064 goto error;
7065 if (is_universe) {
7066 isl_map_free(map2);
7067 return map1;
7070 is_universe = isl_map_plain_is_universe(map2);
7071 if (is_universe < 0)
7072 goto error;
7073 if (is_universe) {
7074 isl_map_free(map1);
7075 return map2;
7078 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7079 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7080 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7082 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7083 map1->n + map2->n, flags);
7084 if (!map)
7085 goto error;
7086 for (i = 0; i < map1->n; ++i) {
7087 map = isl_map_add_basic_map(map,
7088 isl_basic_map_copy(map1->p[i]));
7089 if (!map)
7090 goto error;
7092 for (i = 0; i < map2->n; ++i) {
7093 map = isl_map_add_basic_map(map,
7094 isl_basic_map_copy(map2->p[i]));
7095 if (!map)
7096 goto error;
7098 isl_map_free(map1);
7099 isl_map_free(map2);
7100 return map;
7101 error:
7102 isl_map_free(map);
7103 isl_map_free(map1);
7104 isl_map_free(map2);
7105 return NULL;
7108 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7109 * guaranteed to be disjoint by the caller.
7111 * Note that this functions is called from within isl_map_make_disjoint,
7112 * so we have to be careful not to touch the constraints of the inputs
7113 * in any way.
7115 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7116 __isl_take isl_map *map2)
7118 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7121 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7122 * not be disjoint. The parameters are assumed to have been aligned.
7124 * We currently simply call map_union_disjoint, the internal operation
7125 * of which does not really depend on the inputs being disjoint.
7126 * If the result contains more than one basic map, then we clear
7127 * the disjoint flag since the result may contain basic maps from
7128 * both inputs and these are not guaranteed to be disjoint.
7130 * As a special case, if "map1" and "map2" are obviously equal,
7131 * then we simply return "map1".
7133 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7134 __isl_take isl_map *map2)
7136 int equal;
7138 if (!map1 || !map2)
7139 goto error;
7141 equal = isl_map_plain_is_equal(map1, map2);
7142 if (equal < 0)
7143 goto error;
7144 if (equal) {
7145 isl_map_free(map2);
7146 return map1;
7149 map1 = map_union_disjoint(map1, map2);
7150 if (!map1)
7151 return NULL;
7152 if (map1->n > 1)
7153 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7154 return map1;
7155 error:
7156 isl_map_free(map1);
7157 isl_map_free(map2);
7158 return NULL;
7161 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7162 * not be disjoint.
7164 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7165 __isl_take isl_map *map2)
7167 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7170 struct isl_set *isl_set_union_disjoint(
7171 struct isl_set *set1, struct isl_set *set2)
7173 return (struct isl_set *)
7174 isl_map_union_disjoint(
7175 (struct isl_map *)set1, (struct isl_map *)set2);
7178 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7180 return (struct isl_set *)
7181 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7184 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7185 * the results.
7187 * "map" and "set" are assumed to be compatible and non-NULL.
7189 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7190 __isl_take isl_set *set,
7191 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7192 __isl_take isl_basic_set *bset))
7194 unsigned flags = 0;
7195 struct isl_map *result;
7196 int i, j;
7198 if (isl_set_plain_is_universe(set)) {
7199 isl_set_free(set);
7200 return map;
7203 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7204 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7205 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7207 result = isl_map_alloc_space(isl_space_copy(map->dim),
7208 map->n * set->n, flags);
7209 for (i = 0; result && i < map->n; ++i)
7210 for (j = 0; j < set->n; ++j) {
7211 result = isl_map_add_basic_map(result,
7212 fn(isl_basic_map_copy(map->p[i]),
7213 isl_basic_set_copy(set->p[j])));
7214 if (!result)
7215 break;
7218 isl_map_free(map);
7219 isl_set_free(set);
7220 return result;
7223 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7224 __isl_take isl_set *set)
7226 if (!map || !set)
7227 goto error;
7229 if (!isl_map_compatible_range(map, set))
7230 isl_die(set->ctx, isl_error_invalid,
7231 "incompatible spaces", goto error);
7233 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7234 error:
7235 isl_map_free(map);
7236 isl_set_free(set);
7237 return NULL;
7240 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7241 __isl_take isl_set *set)
7243 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7246 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7247 __isl_take isl_set *set)
7249 if (!map || !set)
7250 goto error;
7252 if (!isl_map_compatible_domain(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_domain);
7257 error:
7258 isl_map_free(map);
7259 isl_set_free(set);
7260 return NULL;
7263 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7264 __isl_take isl_set *set)
7266 return isl_map_align_params_map_map_and(map, set,
7267 &map_intersect_domain);
7270 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7271 __isl_take isl_map *map2)
7273 if (!map1 || !map2)
7274 goto error;
7275 map1 = isl_map_reverse(map1);
7276 map1 = isl_map_apply_range(map1, map2);
7277 return isl_map_reverse(map1);
7278 error:
7279 isl_map_free(map1);
7280 isl_map_free(map2);
7281 return NULL;
7284 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7285 __isl_take isl_map *map2)
7287 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7290 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7291 __isl_take isl_map *map2)
7293 isl_space *dim_result;
7294 struct isl_map *result;
7295 int i, j;
7297 if (!map1 || !map2)
7298 goto error;
7300 dim_result = isl_space_join(isl_space_copy(map1->dim),
7301 isl_space_copy(map2->dim));
7303 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7304 if (!result)
7305 goto error;
7306 for (i = 0; i < map1->n; ++i)
7307 for (j = 0; j < map2->n; ++j) {
7308 result = isl_map_add_basic_map(result,
7309 isl_basic_map_apply_range(
7310 isl_basic_map_copy(map1->p[i]),
7311 isl_basic_map_copy(map2->p[j])));
7312 if (!result)
7313 goto error;
7315 isl_map_free(map1);
7316 isl_map_free(map2);
7317 if (result && result->n <= 1)
7318 ISL_F_SET(result, ISL_MAP_DISJOINT);
7319 return result;
7320 error:
7321 isl_map_free(map1);
7322 isl_map_free(map2);
7323 return NULL;
7326 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7327 __isl_take isl_map *map2)
7329 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7333 * returns range - domain
7335 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7337 isl_space *target_space;
7338 struct isl_basic_set *bset;
7339 unsigned dim;
7340 unsigned nparam;
7341 int i;
7343 if (!bmap)
7344 goto error;
7345 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7346 bmap->dim, isl_dim_out),
7347 goto error);
7348 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7349 dim = isl_basic_map_n_in(bmap);
7350 nparam = isl_basic_map_n_param(bmap);
7351 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7352 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7353 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7354 for (i = 0; i < dim; ++i) {
7355 int j = isl_basic_map_alloc_equality(bmap);
7356 if (j < 0) {
7357 bmap = isl_basic_map_free(bmap);
7358 break;
7360 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7361 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7362 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7363 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7365 bset = isl_basic_map_domain(bmap);
7366 bset = isl_basic_set_reset_space(bset, target_space);
7367 return bset;
7368 error:
7369 isl_basic_map_free(bmap);
7370 return NULL;
7374 * returns range - domain
7376 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7378 int i;
7379 isl_space *dim;
7380 struct isl_set *result;
7382 if (!map)
7383 return NULL;
7385 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7386 map->dim, isl_dim_out),
7387 goto error);
7388 dim = isl_map_get_space(map);
7389 dim = isl_space_domain(dim);
7390 result = isl_set_alloc_space(dim, map->n, 0);
7391 if (!result)
7392 goto error;
7393 for (i = 0; i < map->n; ++i)
7394 result = isl_set_add_basic_set(result,
7395 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7396 isl_map_free(map);
7397 return result;
7398 error:
7399 isl_map_free(map);
7400 return NULL;
7404 * returns [domain -> range] -> range - domain
7406 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7407 __isl_take isl_basic_map *bmap)
7409 int i, k;
7410 isl_space *dim;
7411 isl_basic_map *domain;
7412 int nparam, n;
7413 unsigned total;
7415 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7416 bmap->dim, isl_dim_out))
7417 isl_die(bmap->ctx, isl_error_invalid,
7418 "domain and range don't match", goto error);
7420 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7421 n = isl_basic_map_dim(bmap, isl_dim_in);
7423 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7424 domain = isl_basic_map_universe(dim);
7426 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7427 bmap = isl_basic_map_apply_range(bmap, domain);
7428 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7430 total = isl_basic_map_total_dim(bmap);
7432 for (i = 0; i < n; ++i) {
7433 k = isl_basic_map_alloc_equality(bmap);
7434 if (k < 0)
7435 goto error;
7436 isl_seq_clr(bmap->eq[k], 1 + total);
7437 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7438 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7439 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7442 bmap = isl_basic_map_gauss(bmap, NULL);
7443 return isl_basic_map_finalize(bmap);
7444 error:
7445 isl_basic_map_free(bmap);
7446 return NULL;
7450 * returns [domain -> range] -> range - domain
7452 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7454 int i;
7455 isl_space *domain_dim;
7457 if (!map)
7458 return NULL;
7460 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7461 map->dim, isl_dim_out))
7462 isl_die(map->ctx, isl_error_invalid,
7463 "domain and range don't match", goto error);
7465 map = isl_map_cow(map);
7466 if (!map)
7467 return NULL;
7469 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7470 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7471 map->dim = isl_space_join(map->dim, domain_dim);
7472 if (!map->dim)
7473 goto error;
7474 for (i = 0; i < map->n; ++i) {
7475 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7476 if (!map->p[i])
7477 goto error;
7479 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7480 return map;
7481 error:
7482 isl_map_free(map);
7483 return NULL;
7486 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7488 struct isl_basic_map *bmap;
7489 unsigned nparam;
7490 unsigned dim;
7491 int i;
7493 if (!dims)
7494 return NULL;
7496 nparam = dims->nparam;
7497 dim = dims->n_out;
7498 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7499 if (!bmap)
7500 goto error;
7502 for (i = 0; i < dim; ++i) {
7503 int j = isl_basic_map_alloc_equality(bmap);
7504 if (j < 0)
7505 goto error;
7506 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7507 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7508 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7510 return isl_basic_map_finalize(bmap);
7511 error:
7512 isl_basic_map_free(bmap);
7513 return NULL;
7516 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7518 if (!dim)
7519 return NULL;
7520 if (dim->n_in != dim->n_out)
7521 isl_die(dim->ctx, isl_error_invalid,
7522 "number of input and output dimensions needs to be "
7523 "the same", goto error);
7524 return basic_map_identity(dim);
7525 error:
7526 isl_space_free(dim);
7527 return NULL;
7530 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7532 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7535 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7537 isl_space *dim = isl_set_get_space(set);
7538 isl_map *id;
7539 id = isl_map_identity(isl_space_map_from_set(dim));
7540 return isl_map_intersect_range(id, set);
7543 /* Construct a basic set with all set dimensions having only non-negative
7544 * values.
7546 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7547 __isl_take isl_space *space)
7549 int i;
7550 unsigned nparam;
7551 unsigned dim;
7552 struct isl_basic_set *bset;
7554 if (!space)
7555 return NULL;
7556 nparam = space->nparam;
7557 dim = space->n_out;
7558 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7559 if (!bset)
7560 return NULL;
7561 for (i = 0; i < dim; ++i) {
7562 int k = isl_basic_set_alloc_inequality(bset);
7563 if (k < 0)
7564 goto error;
7565 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7566 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7568 return bset;
7569 error:
7570 isl_basic_set_free(bset);
7571 return NULL;
7574 /* Construct the half-space x_pos >= 0.
7576 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7577 int pos)
7579 int k;
7580 isl_basic_set *nonneg;
7582 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7583 k = isl_basic_set_alloc_inequality(nonneg);
7584 if (k < 0)
7585 goto error;
7586 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7587 isl_int_set_si(nonneg->ineq[k][pos], 1);
7589 return isl_basic_set_finalize(nonneg);
7590 error:
7591 isl_basic_set_free(nonneg);
7592 return NULL;
7595 /* Construct the half-space x_pos <= -1.
7597 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7599 int k;
7600 isl_basic_set *neg;
7602 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7603 k = isl_basic_set_alloc_inequality(neg);
7604 if (k < 0)
7605 goto error;
7606 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7607 isl_int_set_si(neg->ineq[k][0], -1);
7608 isl_int_set_si(neg->ineq[k][pos], -1);
7610 return isl_basic_set_finalize(neg);
7611 error:
7612 isl_basic_set_free(neg);
7613 return NULL;
7616 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7617 enum isl_dim_type type, unsigned first, unsigned n)
7619 int i;
7620 unsigned offset;
7621 isl_basic_set *nonneg;
7622 isl_basic_set *neg;
7624 if (!set)
7625 return NULL;
7626 if (n == 0)
7627 return set;
7629 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7631 offset = pos(set->dim, type);
7632 for (i = 0; i < n; ++i) {
7633 nonneg = nonneg_halfspace(isl_set_get_space(set),
7634 offset + first + i);
7635 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7637 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7640 return set;
7641 error:
7642 isl_set_free(set);
7643 return NULL;
7646 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7647 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7648 void *user)
7650 isl_set *half;
7652 if (!set)
7653 return -1;
7654 if (isl_set_plain_is_empty(set)) {
7655 isl_set_free(set);
7656 return 0;
7658 if (first == len)
7659 return fn(set, signs, user);
7661 signs[first] = 1;
7662 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7663 1 + first));
7664 half = isl_set_intersect(half, isl_set_copy(set));
7665 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7666 goto error;
7668 signs[first] = -1;
7669 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7670 1 + first));
7671 half = isl_set_intersect(half, set);
7672 return foreach_orthant(half, signs, first + 1, len, fn, user);
7673 error:
7674 isl_set_free(set);
7675 return -1;
7678 /* Call "fn" on the intersections of "set" with each of the orthants
7679 * (except for obviously empty intersections). The orthant is identified
7680 * by the signs array, with each entry having value 1 or -1 according
7681 * to the sign of the corresponding variable.
7683 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7684 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7685 void *user)
7687 unsigned nparam;
7688 unsigned nvar;
7689 int *signs;
7690 int r;
7692 if (!set)
7693 return -1;
7694 if (isl_set_plain_is_empty(set))
7695 return 0;
7697 nparam = isl_set_dim(set, isl_dim_param);
7698 nvar = isl_set_dim(set, isl_dim_set);
7700 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7702 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7703 fn, user);
7705 free(signs);
7707 return r;
7710 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7712 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7715 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7716 __isl_keep isl_basic_map *bmap2)
7718 int is_subset;
7719 struct isl_map *map1;
7720 struct isl_map *map2;
7722 if (!bmap1 || !bmap2)
7723 return isl_bool_error;
7725 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7726 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7728 is_subset = isl_map_is_subset(map1, map2);
7730 isl_map_free(map1);
7731 isl_map_free(map2);
7733 return is_subset;
7736 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7737 __isl_keep isl_basic_set *bset2)
7739 return isl_basic_map_is_subset(bset1, bset2);
7742 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7743 __isl_keep isl_basic_map *bmap2)
7745 isl_bool is_subset;
7747 if (!bmap1 || !bmap2)
7748 return isl_bool_error;
7749 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7750 if (is_subset != isl_bool_true)
7751 return is_subset;
7752 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7753 return is_subset;
7756 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7757 __isl_keep isl_basic_set *bset2)
7759 return isl_basic_map_is_equal(
7760 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7763 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7765 int i;
7766 int is_empty;
7768 if (!map)
7769 return isl_bool_error;
7770 for (i = 0; i < map->n; ++i) {
7771 is_empty = isl_basic_map_is_empty(map->p[i]);
7772 if (is_empty < 0)
7773 return isl_bool_error;
7774 if (!is_empty)
7775 return isl_bool_false;
7777 return isl_bool_true;
7780 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7782 return map ? map->n == 0 : isl_bool_error;
7785 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7787 return set ? set->n == 0 : isl_bool_error;
7790 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7792 return isl_map_is_empty((struct isl_map *)set);
7795 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7797 if (!map1 || !map2)
7798 return -1;
7800 return isl_space_is_equal(map1->dim, map2->dim);
7803 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7805 if (!set1 || !set2)
7806 return -1;
7808 return isl_space_is_equal(set1->dim, set2->dim);
7811 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7813 isl_bool is_subset;
7815 if (!map1 || !map2)
7816 return isl_bool_error;
7817 is_subset = isl_map_is_subset(map1, map2);
7818 if (is_subset != isl_bool_true)
7819 return is_subset;
7820 is_subset = isl_map_is_subset(map2, map1);
7821 return is_subset;
7824 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7826 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7829 isl_bool isl_basic_map_is_strict_subset(
7830 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7832 isl_bool is_subset;
7834 if (!bmap1 || !bmap2)
7835 return isl_bool_error;
7836 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7837 if (is_subset != isl_bool_true)
7838 return is_subset;
7839 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7840 if (is_subset == isl_bool_error)
7841 return is_subset;
7842 return !is_subset;
7845 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7846 __isl_keep isl_map *map2)
7848 isl_bool is_subset;
7850 if (!map1 || !map2)
7851 return isl_bool_error;
7852 is_subset = isl_map_is_subset(map1, map2);
7853 if (is_subset != isl_bool_true)
7854 return is_subset;
7855 is_subset = isl_map_is_subset(map2, map1);
7856 if (is_subset == isl_bool_error)
7857 return is_subset;
7858 return !is_subset;
7861 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7862 __isl_keep isl_set *set2)
7864 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7867 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
7869 if (!bmap)
7870 return isl_bool_error;
7871 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7874 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
7876 if (!bset)
7877 return isl_bool_error;
7878 return bset->n_eq == 0 && bset->n_ineq == 0;
7881 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
7883 int i;
7885 if (!map)
7886 return isl_bool_error;
7888 for (i = 0; i < map->n; ++i) {
7889 isl_bool r = isl_basic_map_is_universe(map->p[i]);
7890 if (r < 0 || r)
7891 return r;
7894 return isl_bool_false;
7897 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
7899 return isl_map_plain_is_universe((isl_map *) set);
7902 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
7904 struct isl_basic_set *bset = NULL;
7905 struct isl_vec *sample = NULL;
7906 isl_bool empty;
7907 unsigned total;
7909 if (!bmap)
7910 return isl_bool_error;
7912 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7913 return isl_bool_true;
7915 if (isl_basic_map_is_universe(bmap))
7916 return isl_bool_false;
7918 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7919 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7920 copy = isl_basic_map_remove_redundancies(copy);
7921 empty = isl_basic_map_plain_is_empty(copy);
7922 isl_basic_map_free(copy);
7923 return empty;
7926 total = 1 + isl_basic_map_total_dim(bmap);
7927 if (bmap->sample && bmap->sample->size == total) {
7928 int contains = isl_basic_map_contains(bmap, bmap->sample);
7929 if (contains < 0)
7930 return isl_bool_error;
7931 if (contains)
7932 return isl_bool_false;
7934 isl_vec_free(bmap->sample);
7935 bmap->sample = NULL;
7936 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7937 if (!bset)
7938 return isl_bool_error;
7939 sample = isl_basic_set_sample_vec(bset);
7940 if (!sample)
7941 return isl_bool_error;
7942 empty = sample->size == 0;
7943 isl_vec_free(bmap->sample);
7944 bmap->sample = sample;
7945 if (empty)
7946 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7948 return empty;
7951 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7953 if (!bmap)
7954 return isl_bool_error;
7955 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7958 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7960 if (!bset)
7961 return isl_bool_error;
7962 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7965 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
7967 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7970 struct isl_map *isl_basic_map_union(
7971 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7973 struct isl_map *map;
7974 if (!bmap1 || !bmap2)
7975 goto error;
7977 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7979 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7980 if (!map)
7981 goto error;
7982 map = isl_map_add_basic_map(map, bmap1);
7983 map = isl_map_add_basic_map(map, bmap2);
7984 return map;
7985 error:
7986 isl_basic_map_free(bmap1);
7987 isl_basic_map_free(bmap2);
7988 return NULL;
7991 struct isl_set *isl_basic_set_union(
7992 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7994 return (struct isl_set *)isl_basic_map_union(
7995 (struct isl_basic_map *)bset1,
7996 (struct isl_basic_map *)bset2);
7999 /* Order divs such that any div only depends on previous divs */
8000 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8002 int i;
8003 unsigned off;
8005 if (!bmap)
8006 return NULL;
8008 off = isl_space_dim(bmap->dim, isl_dim_all);
8010 for (i = 0; i < bmap->n_div; ++i) {
8011 int pos;
8012 if (isl_int_is_zero(bmap->div[i][0]))
8013 continue;
8014 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8015 bmap->n_div-i);
8016 if (pos == -1)
8017 continue;
8018 isl_basic_map_swap_div(bmap, i, i + pos);
8019 --i;
8021 return bmap;
8024 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8026 return (struct isl_basic_set *)
8027 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8030 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8032 int i;
8034 if (!map)
8035 return 0;
8037 for (i = 0; i < map->n; ++i) {
8038 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8039 if (!map->p[i])
8040 goto error;
8043 return map;
8044 error:
8045 isl_map_free(map);
8046 return NULL;
8049 /* Apply the expansion computed by isl_merge_divs.
8050 * The expansion itself is given by "exp" while the resulting
8051 * list of divs is given by "div".
8053 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8054 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8056 int i, j;
8057 int n_div;
8059 bset = isl_basic_set_cow(bset);
8060 if (!bset || !div)
8061 goto error;
8063 if (div->n_row < bset->n_div)
8064 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8065 "not an expansion", goto error);
8067 n_div = bset->n_div;
8068 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8069 div->n_row - n_div, 0,
8070 2 * (div->n_row - n_div));
8072 for (i = n_div; i < div->n_row; ++i)
8073 if (isl_basic_set_alloc_div(bset) < 0)
8074 goto error;
8076 j = n_div - 1;
8077 for (i = div->n_row - 1; i >= 0; --i) {
8078 if (j >= 0 && exp[j] == i) {
8079 if (i != j)
8080 isl_basic_map_swap_div(bset, i, j);
8081 j--;
8082 } else {
8083 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8084 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8085 goto error;
8089 isl_mat_free(div);
8090 return bset;
8091 error:
8092 isl_basic_set_free(bset);
8093 isl_mat_free(div);
8094 return NULL;
8097 /* Look for a div in dst that corresponds to the div "div" in src.
8098 * The divs before "div" in src and dst are assumed to be the same.
8100 * Returns -1 if no corresponding div was found and the position
8101 * of the corresponding div in dst otherwise.
8103 static int find_div(struct isl_basic_map *dst,
8104 struct isl_basic_map *src, unsigned div)
8106 int i;
8108 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8110 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8111 for (i = div; i < dst->n_div; ++i)
8112 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8113 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8114 dst->n_div - div) == -1)
8115 return i;
8116 return -1;
8119 /* Align the divs of "dst" to those of "src", adding divs from "src"
8120 * if needed. That is, make sure that the first src->n_div divs
8121 * of the result are equal to those of src.
8123 * The result is not finalized as by design it will have redundant
8124 * divs if any divs from "src" were copied.
8126 __isl_give isl_basic_map *isl_basic_map_align_divs(
8127 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8129 int i;
8130 int known, extended;
8131 unsigned total;
8133 if (!dst || !src)
8134 return isl_basic_map_free(dst);
8136 if (src->n_div == 0)
8137 return dst;
8139 known = isl_basic_map_divs_known(src);
8140 if (known < 0)
8141 return isl_basic_map_free(dst);
8142 if (!known)
8143 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8144 "some src divs are unknown",
8145 return isl_basic_map_free(dst));
8147 src = isl_basic_map_order_divs(src);
8149 extended = 0;
8150 total = isl_space_dim(src->dim, isl_dim_all);
8151 for (i = 0; i < src->n_div; ++i) {
8152 int j = find_div(dst, src, i);
8153 if (j < 0) {
8154 if (!extended) {
8155 int extra = src->n_div - i;
8156 dst = isl_basic_map_cow(dst);
8157 if (!dst)
8158 return NULL;
8159 dst = isl_basic_map_extend_space(dst,
8160 isl_space_copy(dst->dim),
8161 extra, 0, 2 * extra);
8162 extended = 1;
8164 j = isl_basic_map_alloc_div(dst);
8165 if (j < 0)
8166 return isl_basic_map_free(dst);
8167 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8168 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8169 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8170 return isl_basic_map_free(dst);
8172 if (j != i)
8173 isl_basic_map_swap_div(dst, i, j);
8175 return dst;
8178 struct isl_basic_set *isl_basic_set_align_divs(
8179 struct isl_basic_set *dst, struct isl_basic_set *src)
8181 return (struct isl_basic_set *)isl_basic_map_align_divs(
8182 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8185 struct isl_map *isl_map_align_divs(struct isl_map *map)
8187 int i;
8189 if (!map)
8190 return NULL;
8191 if (map->n == 0)
8192 return map;
8193 map = isl_map_compute_divs(map);
8194 map = isl_map_cow(map);
8195 if (!map)
8196 return NULL;
8198 for (i = 1; i < map->n; ++i)
8199 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8200 for (i = 1; i < map->n; ++i) {
8201 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8202 if (!map->p[i])
8203 return isl_map_free(map);
8206 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8207 return map;
8210 struct isl_set *isl_set_align_divs(struct isl_set *set)
8212 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8215 /* Align the divs of the basic maps in "map" to those
8216 * of the basic maps in "list", as well as to the other basic maps in "map".
8217 * The elements in "list" are assumed to have known divs.
8219 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8220 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8222 int i, n;
8224 map = isl_map_compute_divs(map);
8225 map = isl_map_cow(map);
8226 if (!map || !list)
8227 return isl_map_free(map);
8228 if (map->n == 0)
8229 return map;
8231 n = isl_basic_map_list_n_basic_map(list);
8232 for (i = 0; i < n; ++i) {
8233 isl_basic_map *bmap;
8235 bmap = isl_basic_map_list_get_basic_map(list, i);
8236 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8237 isl_basic_map_free(bmap);
8239 if (!map->p[0])
8240 return isl_map_free(map);
8242 return isl_map_align_divs(map);
8245 /* Align the divs of each element of "list" to those of "bmap".
8246 * Both "bmap" and the elements of "list" are assumed to have known divs.
8248 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8249 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8251 int i, n;
8253 if (!list || !bmap)
8254 return isl_basic_map_list_free(list);
8256 n = isl_basic_map_list_n_basic_map(list);
8257 for (i = 0; i < n; ++i) {
8258 isl_basic_map *bmap_i;
8260 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8261 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8262 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8265 return list;
8268 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8269 __isl_take isl_map *map)
8271 if (!set || !map)
8272 goto error;
8273 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8274 map = isl_map_intersect_domain(map, set);
8275 set = isl_map_range(map);
8276 return set;
8277 error:
8278 isl_set_free(set);
8279 isl_map_free(map);
8280 return NULL;
8283 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8284 __isl_take isl_map *map)
8286 return isl_map_align_params_map_map_and(set, map, &set_apply);
8289 /* There is no need to cow as removing empty parts doesn't change
8290 * the meaning of the set.
8292 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8294 int i;
8296 if (!map)
8297 return NULL;
8299 for (i = map->n - 1; i >= 0; --i)
8300 remove_if_empty(map, i);
8302 return map;
8305 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8307 return (struct isl_set *)
8308 isl_map_remove_empty_parts((struct isl_map *)set);
8311 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8313 struct isl_basic_map *bmap;
8314 if (!map || map->n == 0)
8315 return NULL;
8316 bmap = map->p[map->n-1];
8317 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8318 return isl_basic_map_copy(bmap);
8321 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8323 return (struct isl_basic_set *)
8324 isl_map_copy_basic_map((struct isl_map *)set);
8327 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8328 __isl_keep isl_basic_map *bmap)
8330 int i;
8332 if (!map || !bmap)
8333 goto error;
8334 for (i = map->n-1; i >= 0; --i) {
8335 if (map->p[i] != bmap)
8336 continue;
8337 map = isl_map_cow(map);
8338 if (!map)
8339 goto error;
8340 isl_basic_map_free(map->p[i]);
8341 if (i != map->n-1) {
8342 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8343 map->p[i] = map->p[map->n-1];
8345 map->n--;
8346 return map;
8348 return map;
8349 error:
8350 isl_map_free(map);
8351 return NULL;
8354 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8355 struct isl_basic_set *bset)
8357 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8358 (struct isl_basic_map *)bset);
8361 /* Given two basic sets bset1 and bset2, compute the maximal difference
8362 * between the values of dimension pos in bset1 and those in bset2
8363 * for any common value of the parameters and dimensions preceding pos.
8365 static enum isl_lp_result basic_set_maximal_difference_at(
8366 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8367 int pos, isl_int *opt)
8369 isl_space *dims;
8370 struct isl_basic_map *bmap1 = NULL;
8371 struct isl_basic_map *bmap2 = NULL;
8372 struct isl_ctx *ctx;
8373 struct isl_vec *obj;
8374 unsigned total;
8375 unsigned nparam;
8376 unsigned dim1, dim2;
8377 enum isl_lp_result res;
8379 if (!bset1 || !bset2)
8380 return isl_lp_error;
8382 nparam = isl_basic_set_n_param(bset1);
8383 dim1 = isl_basic_set_n_dim(bset1);
8384 dim2 = isl_basic_set_n_dim(bset2);
8385 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8386 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8387 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8388 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8389 if (!bmap1 || !bmap2)
8390 goto error;
8391 bmap1 = isl_basic_map_cow(bmap1);
8392 bmap1 = isl_basic_map_extend(bmap1, nparam,
8393 pos, (dim1 - pos) + (dim2 - pos),
8394 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8395 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8396 if (!bmap1)
8397 goto error2;
8398 total = isl_basic_map_total_dim(bmap1);
8399 ctx = bmap1->ctx;
8400 obj = isl_vec_alloc(ctx, 1 + total);
8401 if (!obj)
8402 goto error2;
8403 isl_seq_clr(obj->block.data, 1 + total);
8404 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8405 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8406 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8407 opt, NULL, NULL);
8408 isl_basic_map_free(bmap1);
8409 isl_vec_free(obj);
8410 return res;
8411 error:
8412 isl_basic_map_free(bmap2);
8413 error2:
8414 isl_basic_map_free(bmap1);
8415 return isl_lp_error;
8418 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8419 * for any common value of the parameters and dimensions preceding pos
8420 * in both basic sets, the values of dimension pos in bset1 are
8421 * smaller or larger than those in bset2.
8423 * Returns
8424 * 1 if bset1 follows bset2
8425 * -1 if bset1 precedes bset2
8426 * 0 if bset1 and bset2 are incomparable
8427 * -2 if some error occurred.
8429 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8430 struct isl_basic_set *bset2, int pos)
8432 isl_int opt;
8433 enum isl_lp_result res;
8434 int cmp;
8436 isl_int_init(opt);
8438 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8440 if (res == isl_lp_empty)
8441 cmp = 0;
8442 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8443 res == isl_lp_unbounded)
8444 cmp = 1;
8445 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8446 cmp = -1;
8447 else
8448 cmp = -2;
8450 isl_int_clear(opt);
8451 return cmp;
8454 /* Given two basic sets bset1 and bset2, check whether
8455 * for any common value of the parameters and dimensions preceding pos
8456 * there is a value of dimension pos in bset1 that is larger
8457 * than a value of the same dimension in bset2.
8459 * Return
8460 * 1 if there exists such a pair
8461 * 0 if there is no such pair, but there is a pair of equal values
8462 * -1 otherwise
8463 * -2 if some error occurred.
8465 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8466 __isl_keep isl_basic_set *bset2, int pos)
8468 isl_int opt;
8469 enum isl_lp_result res;
8470 int cmp;
8472 isl_int_init(opt);
8474 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8476 if (res == isl_lp_empty)
8477 cmp = -1;
8478 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8479 res == isl_lp_unbounded)
8480 cmp = 1;
8481 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8482 cmp = -1;
8483 else if (res == isl_lp_ok)
8484 cmp = 0;
8485 else
8486 cmp = -2;
8488 isl_int_clear(opt);
8489 return cmp;
8492 /* Given two sets set1 and set2, check whether
8493 * for any common value of the parameters and dimensions preceding pos
8494 * there is a value of dimension pos in set1 that is larger
8495 * than a value of the same dimension in set2.
8497 * Return
8498 * 1 if there exists such a pair
8499 * 0 if there is no such pair, but there is a pair of equal values
8500 * -1 otherwise
8501 * -2 if some error occurred.
8503 int isl_set_follows_at(__isl_keep isl_set *set1,
8504 __isl_keep isl_set *set2, int pos)
8506 int i, j;
8507 int follows = -1;
8509 if (!set1 || !set2)
8510 return -2;
8512 for (i = 0; i < set1->n; ++i)
8513 for (j = 0; j < set2->n; ++j) {
8514 int f;
8515 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8516 if (f == 1 || f == -2)
8517 return f;
8518 if (f > follows)
8519 follows = f;
8522 return follows;
8525 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8526 unsigned pos, isl_int *val)
8528 int i;
8529 int d;
8530 unsigned total;
8532 if (!bmap)
8533 return -1;
8534 total = isl_basic_map_total_dim(bmap);
8535 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8536 for (; d+1 > pos; --d)
8537 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8538 break;
8539 if (d != pos)
8540 continue;
8541 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8542 return 0;
8543 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8544 return 0;
8545 if (!isl_int_is_one(bmap->eq[i][1+d]))
8546 return 0;
8547 if (val)
8548 isl_int_neg(*val, bmap->eq[i][0]);
8549 return 1;
8551 return 0;
8554 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8555 unsigned pos, isl_int *val)
8557 int i;
8558 isl_int v;
8559 isl_int tmp;
8560 int fixed;
8562 if (!map)
8563 return -1;
8564 if (map->n == 0)
8565 return 0;
8566 if (map->n == 1)
8567 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8568 isl_int_init(v);
8569 isl_int_init(tmp);
8570 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8571 for (i = 1; fixed == 1 && i < map->n; ++i) {
8572 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8573 if (fixed == 1 && isl_int_ne(tmp, v))
8574 fixed = 0;
8576 if (val)
8577 isl_int_set(*val, v);
8578 isl_int_clear(tmp);
8579 isl_int_clear(v);
8580 return fixed;
8583 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8584 unsigned pos, isl_int *val)
8586 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8587 pos, val);
8590 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8591 isl_int *val)
8593 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8596 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8597 enum isl_dim_type type, unsigned pos, isl_int *val)
8599 if (pos >= isl_basic_map_dim(bmap, type))
8600 return -1;
8601 return isl_basic_map_plain_has_fixed_var(bmap,
8602 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8605 /* If "bmap" obviously lies on a hyperplane where the given dimension
8606 * has a fixed value, then return that value.
8607 * Otherwise return NaN.
8609 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8610 __isl_keep isl_basic_map *bmap,
8611 enum isl_dim_type type, unsigned pos)
8613 isl_ctx *ctx;
8614 isl_val *v;
8615 int fixed;
8617 if (!bmap)
8618 return NULL;
8619 ctx = isl_basic_map_get_ctx(bmap);
8620 v = isl_val_alloc(ctx);
8621 if (!v)
8622 return NULL;
8623 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8624 if (fixed < 0)
8625 return isl_val_free(v);
8626 if (fixed) {
8627 isl_int_set_si(v->d, 1);
8628 return v;
8630 isl_val_free(v);
8631 return isl_val_nan(ctx);
8634 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8635 enum isl_dim_type type, unsigned pos, isl_int *val)
8637 if (pos >= isl_map_dim(map, type))
8638 return -1;
8639 return isl_map_plain_has_fixed_var(map,
8640 map_offset(map, type) - 1 + pos, val);
8643 /* If "map" obviously lies on a hyperplane where the given dimension
8644 * has a fixed value, then return that value.
8645 * Otherwise return NaN.
8647 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8648 enum isl_dim_type type, unsigned pos)
8650 isl_ctx *ctx;
8651 isl_val *v;
8652 int fixed;
8654 if (!map)
8655 return NULL;
8656 ctx = isl_map_get_ctx(map);
8657 v = isl_val_alloc(ctx);
8658 if (!v)
8659 return NULL;
8660 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8661 if (fixed < 0)
8662 return isl_val_free(v);
8663 if (fixed) {
8664 isl_int_set_si(v->d, 1);
8665 return v;
8667 isl_val_free(v);
8668 return isl_val_nan(ctx);
8671 /* If "set" obviously lies on a hyperplane where the given dimension
8672 * has a fixed value, then return that value.
8673 * Otherwise return NaN.
8675 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8676 enum isl_dim_type type, unsigned pos)
8678 return isl_map_plain_get_val_if_fixed(set, type, pos);
8681 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8682 enum isl_dim_type type, unsigned pos, isl_int *val)
8684 return isl_map_plain_is_fixed(set, type, pos, val);
8687 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8688 * then return this fixed value in *val.
8690 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8691 unsigned dim, isl_int *val)
8693 return isl_basic_set_plain_has_fixed_var(bset,
8694 isl_basic_set_n_param(bset) + dim, val);
8697 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8698 * then return this fixed value in *val.
8700 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8701 unsigned dim, isl_int *val)
8703 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8706 /* Check if input variable in has fixed value and if so and if val is not NULL,
8707 * then return this fixed value in *val.
8709 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8710 unsigned in, isl_int *val)
8712 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8715 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8716 * and if val is not NULL, then return this lower bound in *val.
8718 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8719 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8721 int i, i_eq = -1, i_ineq = -1;
8722 isl_int *c;
8723 unsigned total;
8724 unsigned nparam;
8726 if (!bset)
8727 return -1;
8728 total = isl_basic_set_total_dim(bset);
8729 nparam = isl_basic_set_n_param(bset);
8730 for (i = 0; i < bset->n_eq; ++i) {
8731 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8732 continue;
8733 if (i_eq != -1)
8734 return 0;
8735 i_eq = i;
8737 for (i = 0; i < bset->n_ineq; ++i) {
8738 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8739 continue;
8740 if (i_eq != -1 || i_ineq != -1)
8741 return 0;
8742 i_ineq = i;
8744 if (i_eq == -1 && i_ineq == -1)
8745 return 0;
8746 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8747 /* The coefficient should always be one due to normalization. */
8748 if (!isl_int_is_one(c[1+nparam+dim]))
8749 return 0;
8750 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8751 return 0;
8752 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8753 total - nparam - dim - 1) != -1)
8754 return 0;
8755 if (val)
8756 isl_int_neg(*val, c[0]);
8757 return 1;
8760 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8761 unsigned dim, isl_int *val)
8763 int i;
8764 isl_int v;
8765 isl_int tmp;
8766 int fixed;
8768 if (!set)
8769 return -1;
8770 if (set->n == 0)
8771 return 0;
8772 if (set->n == 1)
8773 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8774 dim, val);
8775 isl_int_init(v);
8776 isl_int_init(tmp);
8777 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8778 dim, &v);
8779 for (i = 1; fixed == 1 && i < set->n; ++i) {
8780 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8781 dim, &tmp);
8782 if (fixed == 1 && isl_int_ne(tmp, v))
8783 fixed = 0;
8785 if (val)
8786 isl_int_set(*val, v);
8787 isl_int_clear(tmp);
8788 isl_int_clear(v);
8789 return fixed;
8792 /* uset_gist depends on constraints without existentially quantified
8793 * variables sorting first.
8795 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8797 isl_int **c1 = (isl_int **) p1;
8798 isl_int **c2 = (isl_int **) p2;
8799 int l1, l2;
8800 unsigned size = *(unsigned *) arg;
8802 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8803 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8805 if (l1 != l2)
8806 return l1 - l2;
8808 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8811 static struct isl_basic_map *isl_basic_map_sort_constraints(
8812 struct isl_basic_map *bmap)
8814 unsigned total;
8816 if (!bmap)
8817 return NULL;
8818 if (bmap->n_ineq == 0)
8819 return bmap;
8820 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8821 return bmap;
8822 total = isl_basic_map_total_dim(bmap);
8823 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
8824 &sort_constraint_cmp, &total) < 0)
8825 return isl_basic_map_free(bmap);
8826 return bmap;
8829 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8830 __isl_take isl_basic_set *bset)
8832 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8833 (struct isl_basic_map *)bset);
8836 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8838 if (!bmap)
8839 return NULL;
8840 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8841 return bmap;
8842 bmap = isl_basic_map_remove_redundancies(bmap);
8843 bmap = isl_basic_map_sort_constraints(bmap);
8844 if (bmap)
8845 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8846 return bmap;
8849 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8851 return (struct isl_basic_set *)isl_basic_map_normalize(
8852 (struct isl_basic_map *)bset);
8855 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8856 const __isl_keep isl_basic_map *bmap2)
8858 int i, cmp;
8859 unsigned total;
8861 if (!bmap1 || !bmap2)
8862 return -1;
8864 if (bmap1 == bmap2)
8865 return 0;
8866 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8867 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8868 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8869 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8870 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8871 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8872 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8873 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8874 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8875 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8876 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8877 return 0;
8878 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8879 return 1;
8880 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8881 return -1;
8882 if (bmap1->n_eq != bmap2->n_eq)
8883 return bmap1->n_eq - bmap2->n_eq;
8884 if (bmap1->n_ineq != bmap2->n_ineq)
8885 return bmap1->n_ineq - bmap2->n_ineq;
8886 if (bmap1->n_div != bmap2->n_div)
8887 return bmap1->n_div - bmap2->n_div;
8888 total = isl_basic_map_total_dim(bmap1);
8889 for (i = 0; i < bmap1->n_eq; ++i) {
8890 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8891 if (cmp)
8892 return cmp;
8894 for (i = 0; i < bmap1->n_ineq; ++i) {
8895 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8896 if (cmp)
8897 return cmp;
8899 for (i = 0; i < bmap1->n_div; ++i) {
8900 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8901 if (cmp)
8902 return cmp;
8904 return 0;
8907 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8908 const __isl_keep isl_basic_set *bset2)
8910 return isl_basic_map_plain_cmp(bset1, bset2);
8913 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8915 int i, cmp;
8917 if (set1 == set2)
8918 return 0;
8919 if (set1->n != set2->n)
8920 return set1->n - set2->n;
8922 for (i = 0; i < set1->n; ++i) {
8923 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8924 if (cmp)
8925 return cmp;
8928 return 0;
8931 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8932 __isl_keep isl_basic_map *bmap2)
8934 if (!bmap1 || !bmap2)
8935 return isl_bool_error;
8936 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8939 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8940 __isl_keep isl_basic_set *bset2)
8942 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8943 (isl_basic_map *)bset2);
8946 static int qsort_bmap_cmp(const void *p1, const void *p2)
8948 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8949 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8951 return isl_basic_map_plain_cmp(bmap1, bmap2);
8954 /* Sort the basic maps of "map" and remove duplicate basic maps.
8956 * While removing basic maps, we make sure that the basic maps remain
8957 * sorted because isl_map_normalize expects the basic maps of the result
8958 * to be sorted.
8960 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
8962 int i, j;
8964 map = isl_map_remove_empty_parts(map);
8965 if (!map)
8966 return NULL;
8967 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8968 for (i = map->n - 1; i >= 1; --i) {
8969 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
8970 continue;
8971 isl_basic_map_free(map->p[i-1]);
8972 for (j = i; j < map->n; ++j)
8973 map->p[j - 1] = map->p[j];
8974 map->n--;
8977 return map;
8980 /* Remove obvious duplicates among the basic maps of "map".
8982 * Unlike isl_map_normalize, this function does not remove redundant
8983 * constraints and only removes duplicates that have exactly the same
8984 * constraints in the input. It does sort the constraints and
8985 * the basic maps to ease the detection of duplicates.
8987 * If "map" has already been normalized or if the basic maps are
8988 * disjoint, then there can be no duplicates.
8990 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
8992 int i;
8993 isl_basic_map *bmap;
8995 if (!map)
8996 return NULL;
8997 if (map->n <= 1)
8998 return map;
8999 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9000 return map;
9001 for (i = 0; i < map->n; ++i) {
9002 bmap = isl_basic_map_copy(map->p[i]);
9003 bmap = isl_basic_map_sort_constraints(bmap);
9004 if (!bmap)
9005 return isl_map_free(map);
9006 isl_basic_map_free(map->p[i]);
9007 map->p[i] = bmap;
9010 map = sort_and_remove_duplicates(map);
9011 return map;
9014 /* We normalize in place, but if anything goes wrong we need
9015 * to return NULL, so we need to make sure we don't change the
9016 * meaning of any possible other copies of map.
9018 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9020 int i;
9021 struct isl_basic_map *bmap;
9023 if (!map)
9024 return NULL;
9025 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9026 return map;
9027 for (i = 0; i < map->n; ++i) {
9028 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9029 if (!bmap)
9030 goto error;
9031 isl_basic_map_free(map->p[i]);
9032 map->p[i] = bmap;
9035 map = sort_and_remove_duplicates(map);
9036 if (map)
9037 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9038 return map;
9039 error:
9040 isl_map_free(map);
9041 return NULL;
9044 struct isl_set *isl_set_normalize(struct isl_set *set)
9046 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9049 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9050 __isl_keep isl_map *map2)
9052 int i;
9053 isl_bool equal;
9055 if (!map1 || !map2)
9056 return isl_bool_error;
9058 if (map1 == map2)
9059 return isl_bool_true;
9060 if (!isl_space_is_equal(map1->dim, map2->dim))
9061 return isl_bool_false;
9063 map1 = isl_map_copy(map1);
9064 map2 = isl_map_copy(map2);
9065 map1 = isl_map_normalize(map1);
9066 map2 = isl_map_normalize(map2);
9067 if (!map1 || !map2)
9068 goto error;
9069 equal = map1->n == map2->n;
9070 for (i = 0; equal && i < map1->n; ++i) {
9071 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9072 if (equal < 0)
9073 goto error;
9075 isl_map_free(map1);
9076 isl_map_free(map2);
9077 return equal;
9078 error:
9079 isl_map_free(map1);
9080 isl_map_free(map2);
9081 return isl_bool_error;
9084 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9085 __isl_keep isl_set *set2)
9087 return isl_map_plain_is_equal((struct isl_map *)set1,
9088 (struct isl_map *)set2);
9091 /* Return an interval that ranges from min to max (inclusive)
9093 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9094 isl_int min, isl_int max)
9096 int k;
9097 struct isl_basic_set *bset = NULL;
9099 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9100 if (!bset)
9101 goto error;
9103 k = isl_basic_set_alloc_inequality(bset);
9104 if (k < 0)
9105 goto error;
9106 isl_int_set_si(bset->ineq[k][1], 1);
9107 isl_int_neg(bset->ineq[k][0], min);
9109 k = isl_basic_set_alloc_inequality(bset);
9110 if (k < 0)
9111 goto error;
9112 isl_int_set_si(bset->ineq[k][1], -1);
9113 isl_int_set(bset->ineq[k][0], max);
9115 return bset;
9116 error:
9117 isl_basic_set_free(bset);
9118 return NULL;
9121 /* Return the basic maps in "map" as a list.
9123 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9124 __isl_keep isl_map *map)
9126 int i;
9127 isl_ctx *ctx;
9128 isl_basic_map_list *list;
9130 if (!map)
9131 return NULL;
9132 ctx = isl_map_get_ctx(map);
9133 list = isl_basic_map_list_alloc(ctx, map->n);
9135 for (i = 0; i < map->n; ++i) {
9136 isl_basic_map *bmap;
9138 bmap = isl_basic_map_copy(map->p[i]);
9139 list = isl_basic_map_list_add(list, bmap);
9142 return list;
9145 /* Return the intersection of the elements in the non-empty list "list".
9146 * All elements are assumed to live in the same space.
9148 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9149 __isl_take isl_basic_map_list *list)
9151 int i, n;
9152 isl_basic_map *bmap;
9154 if (!list)
9155 return NULL;
9156 n = isl_basic_map_list_n_basic_map(list);
9157 if (n < 1)
9158 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9159 "expecting non-empty list", goto error);
9161 bmap = isl_basic_map_list_get_basic_map(list, 0);
9162 for (i = 1; i < n; ++i) {
9163 isl_basic_map *bmap_i;
9165 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9166 bmap = isl_basic_map_intersect(bmap, bmap_i);
9169 isl_basic_map_list_free(list);
9170 return bmap;
9171 error:
9172 isl_basic_map_list_free(list);
9173 return NULL;
9176 /* Return the intersection of the elements in the non-empty list "list".
9177 * All elements are assumed to live in the same space.
9179 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9180 __isl_take isl_basic_set_list *list)
9182 return isl_basic_map_list_intersect(list);
9185 /* Return the Cartesian product of the basic sets in list (in the given order).
9187 __isl_give isl_basic_set *isl_basic_set_list_product(
9188 __isl_take struct isl_basic_set_list *list)
9190 int i;
9191 unsigned dim;
9192 unsigned nparam;
9193 unsigned extra;
9194 unsigned n_eq;
9195 unsigned n_ineq;
9196 struct isl_basic_set *product = NULL;
9198 if (!list)
9199 goto error;
9200 isl_assert(list->ctx, list->n > 0, goto error);
9201 isl_assert(list->ctx, list->p[0], goto error);
9202 nparam = isl_basic_set_n_param(list->p[0]);
9203 dim = isl_basic_set_n_dim(list->p[0]);
9204 extra = list->p[0]->n_div;
9205 n_eq = list->p[0]->n_eq;
9206 n_ineq = list->p[0]->n_ineq;
9207 for (i = 1; i < list->n; ++i) {
9208 isl_assert(list->ctx, list->p[i], goto error);
9209 isl_assert(list->ctx,
9210 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9211 dim += isl_basic_set_n_dim(list->p[i]);
9212 extra += list->p[i]->n_div;
9213 n_eq += list->p[i]->n_eq;
9214 n_ineq += list->p[i]->n_ineq;
9216 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9217 n_eq, n_ineq);
9218 if (!product)
9219 goto error;
9220 dim = 0;
9221 for (i = 0; i < list->n; ++i) {
9222 isl_basic_set_add_constraints(product,
9223 isl_basic_set_copy(list->p[i]), dim);
9224 dim += isl_basic_set_n_dim(list->p[i]);
9226 isl_basic_set_list_free(list);
9227 return product;
9228 error:
9229 isl_basic_set_free(product);
9230 isl_basic_set_list_free(list);
9231 return NULL;
9234 struct isl_basic_map *isl_basic_map_product(
9235 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9237 isl_space *dim_result = NULL;
9238 struct isl_basic_map *bmap;
9239 unsigned in1, in2, out1, out2, nparam, total, pos;
9240 struct isl_dim_map *dim_map1, *dim_map2;
9242 if (!bmap1 || !bmap2)
9243 goto error;
9245 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9246 bmap2->dim, isl_dim_param), goto error);
9247 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9248 isl_space_copy(bmap2->dim));
9250 in1 = isl_basic_map_n_in(bmap1);
9251 in2 = isl_basic_map_n_in(bmap2);
9252 out1 = isl_basic_map_n_out(bmap1);
9253 out2 = isl_basic_map_n_out(bmap2);
9254 nparam = isl_basic_map_n_param(bmap1);
9256 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9257 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9258 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9259 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9260 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9261 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9262 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9263 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9264 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9265 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9266 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9268 bmap = isl_basic_map_alloc_space(dim_result,
9269 bmap1->n_div + bmap2->n_div,
9270 bmap1->n_eq + bmap2->n_eq,
9271 bmap1->n_ineq + bmap2->n_ineq);
9272 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9273 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9274 bmap = isl_basic_map_simplify(bmap);
9275 return isl_basic_map_finalize(bmap);
9276 error:
9277 isl_basic_map_free(bmap1);
9278 isl_basic_map_free(bmap2);
9279 return NULL;
9282 __isl_give isl_basic_map *isl_basic_map_flat_product(
9283 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9285 isl_basic_map *prod;
9287 prod = isl_basic_map_product(bmap1, bmap2);
9288 prod = isl_basic_map_flatten(prod);
9289 return prod;
9292 __isl_give isl_basic_set *isl_basic_set_flat_product(
9293 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9295 return isl_basic_map_flat_range_product(bset1, bset2);
9298 __isl_give isl_basic_map *isl_basic_map_domain_product(
9299 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9301 isl_space *space_result = NULL;
9302 isl_basic_map *bmap;
9303 unsigned in1, in2, out, nparam, total, pos;
9304 struct isl_dim_map *dim_map1, *dim_map2;
9306 if (!bmap1 || !bmap2)
9307 goto error;
9309 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9310 isl_space_copy(bmap2->dim));
9312 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9313 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9314 out = isl_basic_map_dim(bmap1, isl_dim_out);
9315 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9317 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9318 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9319 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9320 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9321 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9322 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9323 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9324 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9325 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9326 isl_dim_map_div(dim_map1, bmap1, pos += out);
9327 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9329 bmap = isl_basic_map_alloc_space(space_result,
9330 bmap1->n_div + bmap2->n_div,
9331 bmap1->n_eq + bmap2->n_eq,
9332 bmap1->n_ineq + bmap2->n_ineq);
9333 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9334 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9335 bmap = isl_basic_map_simplify(bmap);
9336 return isl_basic_map_finalize(bmap);
9337 error:
9338 isl_basic_map_free(bmap1);
9339 isl_basic_map_free(bmap2);
9340 return NULL;
9343 __isl_give isl_basic_map *isl_basic_map_range_product(
9344 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9346 isl_space *dim_result = NULL;
9347 isl_basic_map *bmap;
9348 unsigned in, out1, out2, nparam, total, pos;
9349 struct isl_dim_map *dim_map1, *dim_map2;
9351 if (!bmap1 || !bmap2)
9352 goto error;
9354 if (!isl_space_match(bmap1->dim, isl_dim_param,
9355 bmap2->dim, isl_dim_param))
9356 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9357 "parameters don't match", goto error);
9359 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9360 isl_space_copy(bmap2->dim));
9362 in = isl_basic_map_dim(bmap1, isl_dim_in);
9363 out1 = isl_basic_map_n_out(bmap1);
9364 out2 = isl_basic_map_n_out(bmap2);
9365 nparam = isl_basic_map_n_param(bmap1);
9367 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9368 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9369 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9370 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9371 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9372 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9373 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9374 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9375 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9376 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9377 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9379 bmap = isl_basic_map_alloc_space(dim_result,
9380 bmap1->n_div + bmap2->n_div,
9381 bmap1->n_eq + bmap2->n_eq,
9382 bmap1->n_ineq + bmap2->n_ineq);
9383 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9384 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9385 bmap = isl_basic_map_simplify(bmap);
9386 return isl_basic_map_finalize(bmap);
9387 error:
9388 isl_basic_map_free(bmap1);
9389 isl_basic_map_free(bmap2);
9390 return NULL;
9393 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9394 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9396 isl_basic_map *prod;
9398 prod = isl_basic_map_range_product(bmap1, bmap2);
9399 prod = isl_basic_map_flatten_range(prod);
9400 return prod;
9403 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9404 * and collect the results.
9405 * The result live in the space obtained by calling "space_product"
9406 * on the spaces of "map1" and "map2".
9407 * If "remove_duplicates" is set then the result may contain duplicates
9408 * (even if the inputs do not) and so we try and remove the obvious
9409 * duplicates.
9411 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9412 __isl_take isl_map *map2,
9413 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9414 __isl_take isl_space *right),
9415 __isl_give isl_basic_map *(*basic_map_product)(
9416 __isl_take isl_basic_map *left,
9417 __isl_take isl_basic_map *right),
9418 int remove_duplicates)
9420 unsigned flags = 0;
9421 struct isl_map *result;
9422 int i, j;
9424 if (!map1 || !map2)
9425 goto error;
9427 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9428 map2->dim, isl_dim_param), goto error);
9430 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9431 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9432 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9434 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9435 isl_space_copy(map2->dim)),
9436 map1->n * map2->n, flags);
9437 if (!result)
9438 goto error;
9439 for (i = 0; i < map1->n; ++i)
9440 for (j = 0; j < map2->n; ++j) {
9441 struct isl_basic_map *part;
9442 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9443 isl_basic_map_copy(map2->p[j]));
9444 if (isl_basic_map_is_empty(part))
9445 isl_basic_map_free(part);
9446 else
9447 result = isl_map_add_basic_map(result, part);
9448 if (!result)
9449 goto error;
9451 if (remove_duplicates)
9452 result = isl_map_remove_obvious_duplicates(result);
9453 isl_map_free(map1);
9454 isl_map_free(map2);
9455 return result;
9456 error:
9457 isl_map_free(map1);
9458 isl_map_free(map2);
9459 return NULL;
9462 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9464 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9465 __isl_take isl_map *map2)
9467 return map_product(map1, map2, &isl_space_product,
9468 &isl_basic_map_product, 0);
9471 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9472 __isl_take isl_map *map2)
9474 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9477 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9479 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9480 __isl_take isl_map *map2)
9482 isl_map *prod;
9484 prod = isl_map_product(map1, map2);
9485 prod = isl_map_flatten(prod);
9486 return prod;
9489 /* Given two set A and B, construct its Cartesian product A x B.
9491 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9493 return isl_map_range_product(set1, set2);
9496 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9497 __isl_take isl_set *set2)
9499 return isl_map_flat_range_product(set1, set2);
9502 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9504 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9505 __isl_take isl_map *map2)
9507 return map_product(map1, map2, &isl_space_domain_product,
9508 &isl_basic_map_domain_product, 1);
9511 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9513 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9514 __isl_take isl_map *map2)
9516 return map_product(map1, map2, &isl_space_range_product,
9517 &isl_basic_map_range_product, 1);
9520 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9521 __isl_take isl_map *map2)
9523 return isl_map_align_params_map_map_and(map1, map2,
9524 &map_domain_product_aligned);
9527 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9528 __isl_take isl_map *map2)
9530 return isl_map_align_params_map_map_and(map1, map2,
9531 &map_range_product_aligned);
9534 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9536 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9538 isl_space *space;
9539 int total1, keep1, total2, keep2;
9541 if (!map)
9542 return NULL;
9543 if (!isl_space_domain_is_wrapping(map->dim) ||
9544 !isl_space_range_is_wrapping(map->dim))
9545 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9546 "not a product", return isl_map_free(map));
9548 space = isl_map_get_space(map);
9549 total1 = isl_space_dim(space, isl_dim_in);
9550 total2 = isl_space_dim(space, isl_dim_out);
9551 space = isl_space_factor_domain(space);
9552 keep1 = isl_space_dim(space, isl_dim_in);
9553 keep2 = isl_space_dim(space, isl_dim_out);
9554 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9555 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9556 map = isl_map_reset_space(map, space);
9558 return map;
9561 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9563 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9565 isl_space *space;
9566 int total1, keep1, total2, keep2;
9568 if (!map)
9569 return NULL;
9570 if (!isl_space_domain_is_wrapping(map->dim) ||
9571 !isl_space_range_is_wrapping(map->dim))
9572 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9573 "not a product", return isl_map_free(map));
9575 space = isl_map_get_space(map);
9576 total1 = isl_space_dim(space, isl_dim_in);
9577 total2 = isl_space_dim(space, isl_dim_out);
9578 space = isl_space_factor_range(space);
9579 keep1 = isl_space_dim(space, isl_dim_in);
9580 keep2 = isl_space_dim(space, isl_dim_out);
9581 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9582 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9583 map = isl_map_reset_space(map, space);
9585 return map;
9588 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9590 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9592 isl_space *space;
9593 int total, keep;
9595 if (!map)
9596 return NULL;
9597 if (!isl_space_domain_is_wrapping(map->dim))
9598 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9599 "domain is not a product", return isl_map_free(map));
9601 space = isl_map_get_space(map);
9602 total = isl_space_dim(space, isl_dim_in);
9603 space = isl_space_domain_factor_domain(space);
9604 keep = isl_space_dim(space, isl_dim_in);
9605 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9606 map = isl_map_reset_space(map, space);
9608 return map;
9611 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9613 __isl_give isl_map *isl_map_domain_factor_range(__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_range(space);
9627 keep = isl_space_dim(space, isl_dim_in);
9628 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9629 map = isl_map_reset_space(map, space);
9631 return map;
9634 /* Given a map A -> [B -> C], extract the map A -> B.
9636 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9638 isl_space *space;
9639 int total, keep;
9641 if (!map)
9642 return NULL;
9643 if (!isl_space_range_is_wrapping(map->dim))
9644 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9645 "range is not a product", return isl_map_free(map));
9647 space = isl_map_get_space(map);
9648 total = isl_space_dim(space, isl_dim_out);
9649 space = isl_space_range_factor_domain(space);
9650 keep = isl_space_dim(space, isl_dim_out);
9651 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9652 map = isl_map_reset_space(map, space);
9654 return map;
9657 /* Given a map A -> [B -> C], extract the map A -> C.
9659 __isl_give isl_map *isl_map_range_factor_range(__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_range(space);
9673 keep = isl_space_dim(space, isl_dim_out);
9674 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9675 map = isl_map_reset_space(map, space);
9677 return map;
9680 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9682 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9683 __isl_take isl_map *map2)
9685 isl_map *prod;
9687 prod = isl_map_domain_product(map1, map2);
9688 prod = isl_map_flatten_domain(prod);
9689 return prod;
9692 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9694 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9695 __isl_take isl_map *map2)
9697 isl_map *prod;
9699 prod = isl_map_range_product(map1, map2);
9700 prod = isl_map_flatten_range(prod);
9701 return prod;
9704 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9706 int i;
9707 uint32_t hash = isl_hash_init();
9708 unsigned total;
9710 if (!bmap)
9711 return 0;
9712 bmap = isl_basic_map_copy(bmap);
9713 bmap = isl_basic_map_normalize(bmap);
9714 if (!bmap)
9715 return 0;
9716 total = isl_basic_map_total_dim(bmap);
9717 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9718 for (i = 0; i < bmap->n_eq; ++i) {
9719 uint32_t c_hash;
9720 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9721 isl_hash_hash(hash, c_hash);
9723 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9724 for (i = 0; i < bmap->n_ineq; ++i) {
9725 uint32_t c_hash;
9726 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9727 isl_hash_hash(hash, c_hash);
9729 isl_hash_byte(hash, bmap->n_div & 0xFF);
9730 for (i = 0; i < bmap->n_div; ++i) {
9731 uint32_t c_hash;
9732 if (isl_int_is_zero(bmap->div[i][0]))
9733 continue;
9734 isl_hash_byte(hash, i & 0xFF);
9735 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9736 isl_hash_hash(hash, c_hash);
9738 isl_basic_map_free(bmap);
9739 return hash;
9742 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9744 return isl_basic_map_get_hash((isl_basic_map *)bset);
9747 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9749 int i;
9750 uint32_t hash;
9752 if (!map)
9753 return 0;
9754 map = isl_map_copy(map);
9755 map = isl_map_normalize(map);
9756 if (!map)
9757 return 0;
9759 hash = isl_hash_init();
9760 for (i = 0; i < map->n; ++i) {
9761 uint32_t bmap_hash;
9762 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9763 isl_hash_hash(hash, bmap_hash);
9766 isl_map_free(map);
9768 return hash;
9771 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9773 return isl_map_get_hash((isl_map *)set);
9776 /* Check if the value for dimension dim is completely determined
9777 * by the values of the other parameters and variables.
9778 * That is, check if dimension dim is involved in an equality.
9780 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9782 int i;
9783 unsigned nparam;
9785 if (!bset)
9786 return -1;
9787 nparam = isl_basic_set_n_param(bset);
9788 for (i = 0; i < bset->n_eq; ++i)
9789 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9790 return 1;
9791 return 0;
9794 /* Check if the value for dimension dim is completely determined
9795 * by the values of the other parameters and variables.
9796 * That is, check if dimension dim is involved in an equality
9797 * for each of the subsets.
9799 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9801 int i;
9803 if (!set)
9804 return -1;
9805 for (i = 0; i < set->n; ++i) {
9806 int unique;
9807 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9808 if (unique != 1)
9809 return unique;
9811 return 1;
9814 /* Return the number of basic maps in the (current) representation of "map".
9816 int isl_map_n_basic_map(__isl_keep isl_map *map)
9818 return map ? map->n : 0;
9821 int isl_set_n_basic_set(__isl_keep isl_set *set)
9823 return set ? set->n : 0;
9826 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
9827 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9829 int i;
9831 if (!map)
9832 return isl_stat_error;
9834 for (i = 0; i < map->n; ++i)
9835 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9836 return isl_stat_error;
9838 return isl_stat_ok;
9841 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
9842 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9844 int i;
9846 if (!set)
9847 return isl_stat_error;
9849 for (i = 0; i < set->n; ++i)
9850 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9851 return isl_stat_error;
9853 return isl_stat_ok;
9856 /* Return a list of basic sets, the union of which is equal to "set".
9858 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9859 __isl_keep isl_set *set)
9861 int i;
9862 isl_basic_set_list *list;
9864 if (!set)
9865 return NULL;
9867 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
9868 for (i = 0; i < set->n; ++i) {
9869 isl_basic_set *bset;
9871 bset = isl_basic_set_copy(set->p[i]);
9872 list = isl_basic_set_list_add(list, bset);
9875 return list;
9878 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9880 isl_space *dim;
9882 if (!bset)
9883 return NULL;
9885 bset = isl_basic_set_cow(bset);
9886 if (!bset)
9887 return NULL;
9889 dim = isl_basic_set_get_space(bset);
9890 dim = isl_space_lift(dim, bset->n_div);
9891 if (!dim)
9892 goto error;
9893 isl_space_free(bset->dim);
9894 bset->dim = dim;
9895 bset->extra -= bset->n_div;
9896 bset->n_div = 0;
9898 bset = isl_basic_set_finalize(bset);
9900 return bset;
9901 error:
9902 isl_basic_set_free(bset);
9903 return NULL;
9906 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9908 int i;
9909 isl_space *dim;
9910 unsigned n_div;
9912 set = isl_set_align_divs(set);
9914 if (!set)
9915 return NULL;
9917 set = isl_set_cow(set);
9918 if (!set)
9919 return NULL;
9921 n_div = set->p[0]->n_div;
9922 dim = isl_set_get_space(set);
9923 dim = isl_space_lift(dim, n_div);
9924 if (!dim)
9925 goto error;
9926 isl_space_free(set->dim);
9927 set->dim = dim;
9929 for (i = 0; i < set->n; ++i) {
9930 set->p[i] = isl_basic_set_lift(set->p[i]);
9931 if (!set->p[i])
9932 goto error;
9935 return set;
9936 error:
9937 isl_set_free(set);
9938 return NULL;
9941 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9943 isl_space *dim;
9944 struct isl_basic_map *bmap;
9945 unsigned n_set;
9946 unsigned n_div;
9947 unsigned n_param;
9948 unsigned total;
9949 int i, k, l;
9951 set = isl_set_align_divs(set);
9953 if (!set)
9954 return NULL;
9956 dim = isl_set_get_space(set);
9957 if (set->n == 0 || set->p[0]->n_div == 0) {
9958 isl_set_free(set);
9959 return isl_map_identity(isl_space_map_from_set(dim));
9962 n_div = set->p[0]->n_div;
9963 dim = isl_space_map_from_set(dim);
9964 n_param = isl_space_dim(dim, isl_dim_param);
9965 n_set = isl_space_dim(dim, isl_dim_in);
9966 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9967 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9968 for (i = 0; i < n_set; ++i)
9969 bmap = var_equal(bmap, i);
9971 total = n_param + n_set + n_set + n_div;
9972 for (i = 0; i < n_div; ++i) {
9973 k = isl_basic_map_alloc_inequality(bmap);
9974 if (k < 0)
9975 goto error;
9976 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9977 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9978 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9979 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9980 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9981 set->p[0]->div[i][0]);
9983 l = isl_basic_map_alloc_inequality(bmap);
9984 if (l < 0)
9985 goto error;
9986 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9987 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9988 set->p[0]->div[i][0]);
9989 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9992 isl_set_free(set);
9993 bmap = isl_basic_map_simplify(bmap);
9994 bmap = isl_basic_map_finalize(bmap);
9995 return isl_map_from_basic_map(bmap);
9996 error:
9997 isl_set_free(set);
9998 isl_basic_map_free(bmap);
9999 return NULL;
10002 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10004 unsigned dim;
10005 int size = 0;
10007 if (!bset)
10008 return -1;
10010 dim = isl_basic_set_total_dim(bset);
10011 size += bset->n_eq * (1 + dim);
10012 size += bset->n_ineq * (1 + dim);
10013 size += bset->n_div * (2 + dim);
10015 return size;
10018 int isl_set_size(__isl_keep isl_set *set)
10020 int i;
10021 int size = 0;
10023 if (!set)
10024 return -1;
10026 for (i = 0; i < set->n; ++i)
10027 size += isl_basic_set_size(set->p[i]);
10029 return size;
10032 /* Check if there is any lower bound (if lower == 0) and/or upper
10033 * bound (if upper == 0) on the specified dim.
10035 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10036 enum isl_dim_type type, unsigned pos, int lower, int upper)
10038 int i;
10040 if (!bmap)
10041 return isl_bool_error;
10043 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10044 return isl_bool_error);
10046 pos += isl_basic_map_offset(bmap, type);
10048 for (i = 0; i < bmap->n_div; ++i) {
10049 if (isl_int_is_zero(bmap->div[i][0]))
10050 continue;
10051 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10052 return isl_bool_true;
10055 for (i = 0; i < bmap->n_eq; ++i)
10056 if (!isl_int_is_zero(bmap->eq[i][pos]))
10057 return isl_bool_true;
10059 for (i = 0; i < bmap->n_ineq; ++i) {
10060 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10061 if (sgn > 0)
10062 lower = 1;
10063 if (sgn < 0)
10064 upper = 1;
10067 return lower && upper;
10070 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10071 enum isl_dim_type type, unsigned pos)
10073 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10076 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10077 enum isl_dim_type type, unsigned pos)
10079 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10082 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10083 enum isl_dim_type type, unsigned pos)
10085 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10088 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10089 enum isl_dim_type type, unsigned pos)
10091 int i;
10093 if (!map)
10094 return -1;
10096 for (i = 0; i < map->n; ++i) {
10097 int bounded;
10098 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10099 if (bounded < 0 || !bounded)
10100 return bounded;
10103 return 1;
10106 /* Return 1 if the specified dim is involved in both an upper bound
10107 * and a lower bound.
10109 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10110 enum isl_dim_type type, unsigned pos)
10112 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10115 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10117 static isl_bool has_any_bound(__isl_keep isl_map *map,
10118 enum isl_dim_type type, unsigned pos,
10119 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10120 enum isl_dim_type type, unsigned pos))
10122 int i;
10124 if (!map)
10125 return isl_bool_error;
10127 for (i = 0; i < map->n; ++i) {
10128 isl_bool bounded;
10129 bounded = fn(map->p[i], type, pos);
10130 if (bounded < 0 || bounded)
10131 return bounded;
10134 return isl_bool_false;
10137 /* Return 1 if the specified dim is involved in any lower bound.
10139 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10140 enum isl_dim_type type, unsigned pos)
10142 return has_any_bound(set, type, pos,
10143 &isl_basic_map_dim_has_lower_bound);
10146 /* Return 1 if the specified dim is involved in any upper bound.
10148 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10149 enum isl_dim_type type, unsigned pos)
10151 return has_any_bound(set, type, pos,
10152 &isl_basic_map_dim_has_upper_bound);
10155 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10157 static isl_bool has_bound(__isl_keep isl_map *map,
10158 enum isl_dim_type type, unsigned pos,
10159 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10160 enum isl_dim_type type, unsigned pos))
10162 int i;
10164 if (!map)
10165 return isl_bool_error;
10167 for (i = 0; i < map->n; ++i) {
10168 isl_bool bounded;
10169 bounded = fn(map->p[i], type, pos);
10170 if (bounded < 0 || !bounded)
10171 return bounded;
10174 return isl_bool_true;
10177 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10179 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10180 enum isl_dim_type type, unsigned pos)
10182 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10185 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10187 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10188 enum isl_dim_type type, unsigned pos)
10190 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10193 /* For each of the "n" variables starting at "first", determine
10194 * the sign of the variable and put the results in the first "n"
10195 * elements of the array "signs".
10196 * Sign
10197 * 1 means that the variable is non-negative
10198 * -1 means that the variable is non-positive
10199 * 0 means the variable attains both positive and negative values.
10201 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10202 unsigned first, unsigned n, int *signs)
10204 isl_vec *bound = NULL;
10205 struct isl_tab *tab = NULL;
10206 struct isl_tab_undo *snap;
10207 int i;
10209 if (!bset || !signs)
10210 return -1;
10212 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10213 tab = isl_tab_from_basic_set(bset, 0);
10214 if (!bound || !tab)
10215 goto error;
10217 isl_seq_clr(bound->el, bound->size);
10218 isl_int_set_si(bound->el[0], -1);
10220 snap = isl_tab_snap(tab);
10221 for (i = 0; i < n; ++i) {
10222 int empty;
10224 isl_int_set_si(bound->el[1 + first + i], -1);
10225 if (isl_tab_add_ineq(tab, bound->el) < 0)
10226 goto error;
10227 empty = tab->empty;
10228 isl_int_set_si(bound->el[1 + first + i], 0);
10229 if (isl_tab_rollback(tab, snap) < 0)
10230 goto error;
10232 if (empty) {
10233 signs[i] = 1;
10234 continue;
10237 isl_int_set_si(bound->el[1 + first + i], 1);
10238 if (isl_tab_add_ineq(tab, bound->el) < 0)
10239 goto error;
10240 empty = tab->empty;
10241 isl_int_set_si(bound->el[1 + first + i], 0);
10242 if (isl_tab_rollback(tab, snap) < 0)
10243 goto error;
10245 signs[i] = empty ? -1 : 0;
10248 isl_tab_free(tab);
10249 isl_vec_free(bound);
10250 return 0;
10251 error:
10252 isl_tab_free(tab);
10253 isl_vec_free(bound);
10254 return -1;
10257 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10258 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10260 if (!bset || !signs)
10261 return -1;
10262 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10263 return -1);
10265 first += pos(bset->dim, type) - 1;
10266 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10269 /* Is it possible for the integer division "div" to depend (possibly
10270 * indirectly) on any output dimensions?
10272 * If the div is undefined, then we conservatively assume that it
10273 * may depend on them.
10274 * Otherwise, we check if it actually depends on them or on any integer
10275 * divisions that may depend on them.
10277 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10279 int i;
10280 unsigned n_out, o_out;
10281 unsigned n_div, o_div;
10283 if (isl_int_is_zero(bmap->div[div][0]))
10284 return 1;
10286 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10287 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10289 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10290 return 1;
10292 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10293 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10295 for (i = 0; i < n_div; ++i) {
10296 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10297 continue;
10298 if (div_may_involve_output(bmap, i))
10299 return 1;
10302 return 0;
10305 /* Return the index of the equality of "bmap" that defines
10306 * the output dimension "pos" in terms of earlier dimensions.
10307 * The equality may also involve integer divisions, as long
10308 * as those integer divisions are defined in terms of
10309 * parameters or input dimensions.
10310 * Return bmap->n_eq if there is no such equality.
10311 * Return -1 on error.
10313 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10314 int pos)
10316 int j, k;
10317 unsigned n_out, o_out;
10318 unsigned n_div, o_div;
10320 if (!bmap)
10321 return -1;
10323 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10324 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10325 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10326 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10328 for (j = 0; j < bmap->n_eq; ++j) {
10329 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10330 continue;
10331 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10332 n_out - (pos + 1)) != -1)
10333 continue;
10334 for (k = 0; k < n_div; ++k) {
10335 if (isl_int_is_zero(bmap->eq[j][o_div + k]))
10336 continue;
10337 if (div_may_involve_output(bmap, k))
10338 break;
10340 if (k >= n_div)
10341 return j;
10344 return bmap->n_eq;
10347 /* Check if the given basic map is obviously single-valued.
10348 * In particular, for each output dimension, check that there is
10349 * an equality that defines the output dimension in terms of
10350 * earlier dimensions.
10352 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10354 int i;
10355 unsigned n_out;
10357 if (!bmap)
10358 return isl_bool_error;
10360 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10362 for (i = 0; i < n_out; ++i) {
10363 int eq;
10365 eq = isl_basic_map_output_defining_equality(bmap, i);
10366 if (eq < 0)
10367 return isl_bool_error;
10368 if (eq >= bmap->n_eq)
10369 return isl_bool_false;
10372 return isl_bool_true;
10375 /* Check if the given basic map is single-valued.
10376 * We simply compute
10378 * M \circ M^-1
10380 * and check if the result is a subset of the identity mapping.
10382 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10384 isl_space *space;
10385 isl_basic_map *test;
10386 isl_basic_map *id;
10387 isl_bool sv;
10389 sv = isl_basic_map_plain_is_single_valued(bmap);
10390 if (sv < 0 || sv)
10391 return sv;
10393 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10394 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10396 space = isl_basic_map_get_space(bmap);
10397 space = isl_space_map_from_set(isl_space_range(space));
10398 id = isl_basic_map_identity(space);
10400 sv = isl_basic_map_is_subset(test, id);
10402 isl_basic_map_free(test);
10403 isl_basic_map_free(id);
10405 return sv;
10408 /* Check if the given map is obviously single-valued.
10410 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10412 if (!map)
10413 return isl_bool_error;
10414 if (map->n == 0)
10415 return isl_bool_true;
10416 if (map->n >= 2)
10417 return isl_bool_false;
10419 return isl_basic_map_plain_is_single_valued(map->p[0]);
10422 /* Check if the given map is single-valued.
10423 * We simply compute
10425 * M \circ M^-1
10427 * and check if the result is a subset of the identity mapping.
10429 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10431 isl_space *dim;
10432 isl_map *test;
10433 isl_map *id;
10434 isl_bool sv;
10436 sv = isl_map_plain_is_single_valued(map);
10437 if (sv < 0 || sv)
10438 return sv;
10440 test = isl_map_reverse(isl_map_copy(map));
10441 test = isl_map_apply_range(test, isl_map_copy(map));
10443 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10444 id = isl_map_identity(dim);
10446 sv = isl_map_is_subset(test, id);
10448 isl_map_free(test);
10449 isl_map_free(id);
10451 return sv;
10454 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10456 isl_bool in;
10458 map = isl_map_copy(map);
10459 map = isl_map_reverse(map);
10460 in = isl_map_is_single_valued(map);
10461 isl_map_free(map);
10463 return in;
10466 /* Check if the given map is obviously injective.
10468 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10470 isl_bool in;
10472 map = isl_map_copy(map);
10473 map = isl_map_reverse(map);
10474 in = isl_map_plain_is_single_valued(map);
10475 isl_map_free(map);
10477 return in;
10480 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10482 isl_bool sv;
10484 sv = isl_map_is_single_valued(map);
10485 if (sv < 0 || !sv)
10486 return sv;
10488 return isl_map_is_injective(map);
10491 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10493 return isl_map_is_single_valued((isl_map *)set);
10496 int isl_map_is_translation(__isl_keep isl_map *map)
10498 int ok;
10499 isl_set *delta;
10501 delta = isl_map_deltas(isl_map_copy(map));
10502 ok = isl_set_is_singleton(delta);
10503 isl_set_free(delta);
10505 return ok;
10508 static int unique(isl_int *p, unsigned pos, unsigned len)
10510 if (isl_seq_first_non_zero(p, pos) != -1)
10511 return 0;
10512 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10513 return 0;
10514 return 1;
10517 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10519 int i, j;
10520 unsigned nvar;
10521 unsigned ovar;
10523 if (!bset)
10524 return -1;
10526 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10527 return 0;
10529 nvar = isl_basic_set_dim(bset, isl_dim_set);
10530 ovar = isl_space_offset(bset->dim, isl_dim_set);
10531 for (j = 0; j < nvar; ++j) {
10532 int lower = 0, upper = 0;
10533 for (i = 0; i < bset->n_eq; ++i) {
10534 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10535 continue;
10536 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10537 return 0;
10538 break;
10540 if (i < bset->n_eq)
10541 continue;
10542 for (i = 0; i < bset->n_ineq; ++i) {
10543 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10544 continue;
10545 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10546 return 0;
10547 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10548 lower = 1;
10549 else
10550 upper = 1;
10552 if (!lower || !upper)
10553 return 0;
10556 return 1;
10559 int isl_set_is_box(__isl_keep isl_set *set)
10561 if (!set)
10562 return -1;
10563 if (set->n != 1)
10564 return 0;
10566 return isl_basic_set_is_box(set->p[0]);
10569 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10571 if (!bset)
10572 return isl_bool_error;
10574 return isl_space_is_wrapping(bset->dim);
10577 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10579 if (!set)
10580 return isl_bool_error;
10582 return isl_space_is_wrapping(set->dim);
10585 /* Is the domain of "map" a wrapped relation?
10587 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10589 if (!map)
10590 return isl_bool_error;
10592 return isl_space_domain_is_wrapping(map->dim);
10595 /* Is the range of "map" a wrapped relation?
10597 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
10599 if (!map)
10600 return isl_bool_error;
10602 return isl_space_range_is_wrapping(map->dim);
10605 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10607 bmap = isl_basic_map_cow(bmap);
10608 if (!bmap)
10609 return NULL;
10611 bmap->dim = isl_space_wrap(bmap->dim);
10612 if (!bmap->dim)
10613 goto error;
10615 bmap = isl_basic_map_finalize(bmap);
10617 return (isl_basic_set *)bmap;
10618 error:
10619 isl_basic_map_free(bmap);
10620 return NULL;
10623 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10625 int i;
10627 map = isl_map_cow(map);
10628 if (!map)
10629 return NULL;
10631 for (i = 0; i < map->n; ++i) {
10632 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10633 if (!map->p[i])
10634 goto error;
10636 map->dim = isl_space_wrap(map->dim);
10637 if (!map->dim)
10638 goto error;
10640 return (isl_set *)map;
10641 error:
10642 isl_map_free(map);
10643 return NULL;
10646 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10648 bset = isl_basic_set_cow(bset);
10649 if (!bset)
10650 return NULL;
10652 bset->dim = isl_space_unwrap(bset->dim);
10653 if (!bset->dim)
10654 goto error;
10656 bset = isl_basic_set_finalize(bset);
10658 return (isl_basic_map *)bset;
10659 error:
10660 isl_basic_set_free(bset);
10661 return NULL;
10664 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10666 int i;
10668 if (!set)
10669 return NULL;
10671 if (!isl_set_is_wrapping(set))
10672 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10673 goto error);
10675 set = isl_set_cow(set);
10676 if (!set)
10677 return NULL;
10679 for (i = 0; i < set->n; ++i) {
10680 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10681 if (!set->p[i])
10682 goto error;
10685 set->dim = isl_space_unwrap(set->dim);
10686 if (!set->dim)
10687 goto error;
10689 return (isl_map *)set;
10690 error:
10691 isl_set_free(set);
10692 return NULL;
10695 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10696 enum isl_dim_type type)
10698 if (!bmap)
10699 return NULL;
10701 if (!isl_space_is_named_or_nested(bmap->dim, type))
10702 return bmap;
10704 bmap = isl_basic_map_cow(bmap);
10705 if (!bmap)
10706 return NULL;
10708 bmap->dim = isl_space_reset(bmap->dim, type);
10709 if (!bmap->dim)
10710 goto error;
10712 bmap = isl_basic_map_finalize(bmap);
10714 return bmap;
10715 error:
10716 isl_basic_map_free(bmap);
10717 return NULL;
10720 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10721 enum isl_dim_type type)
10723 int i;
10725 if (!map)
10726 return NULL;
10728 if (!isl_space_is_named_or_nested(map->dim, type))
10729 return map;
10731 map = isl_map_cow(map);
10732 if (!map)
10733 return NULL;
10735 for (i = 0; i < map->n; ++i) {
10736 map->p[i] = isl_basic_map_reset(map->p[i], type);
10737 if (!map->p[i])
10738 goto error;
10740 map->dim = isl_space_reset(map->dim, type);
10741 if (!map->dim)
10742 goto error;
10744 return map;
10745 error:
10746 isl_map_free(map);
10747 return NULL;
10750 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10752 if (!bmap)
10753 return NULL;
10755 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10756 return bmap;
10758 bmap = isl_basic_map_cow(bmap);
10759 if (!bmap)
10760 return NULL;
10762 bmap->dim = isl_space_flatten(bmap->dim);
10763 if (!bmap->dim)
10764 goto error;
10766 bmap = isl_basic_map_finalize(bmap);
10768 return bmap;
10769 error:
10770 isl_basic_map_free(bmap);
10771 return NULL;
10774 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10776 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10779 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10780 __isl_take isl_basic_map *bmap)
10782 if (!bmap)
10783 return NULL;
10785 if (!bmap->dim->nested[0])
10786 return bmap;
10788 bmap = isl_basic_map_cow(bmap);
10789 if (!bmap)
10790 return NULL;
10792 bmap->dim = isl_space_flatten_domain(bmap->dim);
10793 if (!bmap->dim)
10794 goto error;
10796 bmap = isl_basic_map_finalize(bmap);
10798 return bmap;
10799 error:
10800 isl_basic_map_free(bmap);
10801 return NULL;
10804 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10805 __isl_take isl_basic_map *bmap)
10807 if (!bmap)
10808 return NULL;
10810 if (!bmap->dim->nested[1])
10811 return bmap;
10813 bmap = isl_basic_map_cow(bmap);
10814 if (!bmap)
10815 return NULL;
10817 bmap->dim = isl_space_flatten_range(bmap->dim);
10818 if (!bmap->dim)
10819 goto error;
10821 bmap = isl_basic_map_finalize(bmap);
10823 return bmap;
10824 error:
10825 isl_basic_map_free(bmap);
10826 return NULL;
10829 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10831 int i;
10833 if (!map)
10834 return NULL;
10836 if (!map->dim->nested[0] && !map->dim->nested[1])
10837 return map;
10839 map = isl_map_cow(map);
10840 if (!map)
10841 return NULL;
10843 for (i = 0; i < map->n; ++i) {
10844 map->p[i] = isl_basic_map_flatten(map->p[i]);
10845 if (!map->p[i])
10846 goto error;
10848 map->dim = isl_space_flatten(map->dim);
10849 if (!map->dim)
10850 goto error;
10852 return map;
10853 error:
10854 isl_map_free(map);
10855 return NULL;
10858 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10860 return (isl_set *)isl_map_flatten((isl_map *)set);
10863 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10865 isl_space *dim, *flat_dim;
10866 isl_map *map;
10868 dim = isl_set_get_space(set);
10869 flat_dim = isl_space_flatten(isl_space_copy(dim));
10870 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10871 map = isl_map_intersect_domain(map, set);
10873 return map;
10876 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10878 int i;
10880 if (!map)
10881 return NULL;
10883 if (!map->dim->nested[0])
10884 return map;
10886 map = isl_map_cow(map);
10887 if (!map)
10888 return NULL;
10890 for (i = 0; i < map->n; ++i) {
10891 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10892 if (!map->p[i])
10893 goto error;
10895 map->dim = isl_space_flatten_domain(map->dim);
10896 if (!map->dim)
10897 goto error;
10899 return map;
10900 error:
10901 isl_map_free(map);
10902 return NULL;
10905 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10907 int i;
10909 if (!map)
10910 return NULL;
10912 if (!map->dim->nested[1])
10913 return map;
10915 map = isl_map_cow(map);
10916 if (!map)
10917 return NULL;
10919 for (i = 0; i < map->n; ++i) {
10920 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10921 if (!map->p[i])
10922 goto error;
10924 map->dim = isl_space_flatten_range(map->dim);
10925 if (!map->dim)
10926 goto error;
10928 return map;
10929 error:
10930 isl_map_free(map);
10931 return NULL;
10934 /* Reorder the dimensions of "bmap" according to the given dim_map
10935 * and set the dimension specification to "dim".
10937 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10938 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10940 isl_basic_map *res;
10941 unsigned flags;
10943 bmap = isl_basic_map_cow(bmap);
10944 if (!bmap || !dim || !dim_map)
10945 goto error;
10947 flags = bmap->flags;
10948 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10949 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10950 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10951 res = isl_basic_map_alloc_space(dim,
10952 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10953 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10954 if (res)
10955 res->flags = flags;
10956 res = isl_basic_map_finalize(res);
10957 return res;
10958 error:
10959 free(dim_map);
10960 isl_basic_map_free(bmap);
10961 isl_space_free(dim);
10962 return NULL;
10965 /* Reorder the dimensions of "map" according to given reordering.
10967 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10968 __isl_take isl_reordering *r)
10970 int i;
10971 struct isl_dim_map *dim_map;
10973 map = isl_map_cow(map);
10974 dim_map = isl_dim_map_from_reordering(r);
10975 if (!map || !r || !dim_map)
10976 goto error;
10978 for (i = 0; i < map->n; ++i) {
10979 struct isl_dim_map *dim_map_i;
10981 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10983 map->p[i] = isl_basic_map_realign(map->p[i],
10984 isl_space_copy(r->dim), dim_map_i);
10986 if (!map->p[i])
10987 goto error;
10990 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10992 isl_reordering_free(r);
10993 free(dim_map);
10994 return map;
10995 error:
10996 free(dim_map);
10997 isl_map_free(map);
10998 isl_reordering_free(r);
10999 return NULL;
11002 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11003 __isl_take isl_reordering *r)
11005 return (isl_set *)isl_map_realign((isl_map *)set, r);
11008 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11009 __isl_take isl_space *model)
11011 isl_ctx *ctx;
11013 if (!map || !model)
11014 goto error;
11016 ctx = isl_space_get_ctx(model);
11017 if (!isl_space_has_named_params(model))
11018 isl_die(ctx, isl_error_invalid,
11019 "model has unnamed parameters", goto error);
11020 if (!isl_space_has_named_params(map->dim))
11021 isl_die(ctx, isl_error_invalid,
11022 "relation has unnamed parameters", goto error);
11023 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11024 isl_reordering *exp;
11026 model = isl_space_drop_dims(model, isl_dim_in,
11027 0, isl_space_dim(model, isl_dim_in));
11028 model = isl_space_drop_dims(model, isl_dim_out,
11029 0, isl_space_dim(model, isl_dim_out));
11030 exp = isl_parameter_alignment_reordering(map->dim, model);
11031 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11032 map = isl_map_realign(map, exp);
11035 isl_space_free(model);
11036 return map;
11037 error:
11038 isl_space_free(model);
11039 isl_map_free(map);
11040 return NULL;
11043 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11044 __isl_take isl_space *model)
11046 return isl_map_align_params(set, model);
11049 /* Align the parameters of "bmap" to those of "model", introducing
11050 * additional parameters if needed.
11052 __isl_give isl_basic_map *isl_basic_map_align_params(
11053 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11055 isl_ctx *ctx;
11057 if (!bmap || !model)
11058 goto error;
11060 ctx = isl_space_get_ctx(model);
11061 if (!isl_space_has_named_params(model))
11062 isl_die(ctx, isl_error_invalid,
11063 "model has unnamed parameters", goto error);
11064 if (!isl_space_has_named_params(bmap->dim))
11065 isl_die(ctx, isl_error_invalid,
11066 "relation has unnamed parameters", goto error);
11067 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11068 isl_reordering *exp;
11069 struct isl_dim_map *dim_map;
11071 model = isl_space_drop_dims(model, isl_dim_in,
11072 0, isl_space_dim(model, isl_dim_in));
11073 model = isl_space_drop_dims(model, isl_dim_out,
11074 0, isl_space_dim(model, isl_dim_out));
11075 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11076 exp = isl_reordering_extend_space(exp,
11077 isl_basic_map_get_space(bmap));
11078 dim_map = isl_dim_map_from_reordering(exp);
11079 bmap = isl_basic_map_realign(bmap,
11080 exp ? isl_space_copy(exp->dim) : NULL,
11081 isl_dim_map_extend(dim_map, bmap));
11082 isl_reordering_free(exp);
11083 free(dim_map);
11086 isl_space_free(model);
11087 return bmap;
11088 error:
11089 isl_space_free(model);
11090 isl_basic_map_free(bmap);
11091 return NULL;
11094 /* Align the parameters of "bset" to those of "model", introducing
11095 * additional parameters if needed.
11097 __isl_give isl_basic_set *isl_basic_set_align_params(
11098 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11100 return isl_basic_map_align_params(bset, model);
11103 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11104 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11105 enum isl_dim_type c2, enum isl_dim_type c3,
11106 enum isl_dim_type c4, enum isl_dim_type c5)
11108 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11109 struct isl_mat *mat;
11110 int i, j, k;
11111 int pos;
11113 if (!bmap)
11114 return NULL;
11115 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11116 isl_basic_map_total_dim(bmap) + 1);
11117 if (!mat)
11118 return NULL;
11119 for (i = 0; i < bmap->n_eq; ++i)
11120 for (j = 0, pos = 0; j < 5; ++j) {
11121 int off = isl_basic_map_offset(bmap, c[j]);
11122 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11123 isl_int_set(mat->row[i][pos],
11124 bmap->eq[i][off + k]);
11125 ++pos;
11129 return mat;
11132 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11133 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11134 enum isl_dim_type c2, enum isl_dim_type c3,
11135 enum isl_dim_type c4, enum isl_dim_type c5)
11137 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11138 struct isl_mat *mat;
11139 int i, j, k;
11140 int pos;
11142 if (!bmap)
11143 return NULL;
11144 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11145 isl_basic_map_total_dim(bmap) + 1);
11146 if (!mat)
11147 return NULL;
11148 for (i = 0; i < bmap->n_ineq; ++i)
11149 for (j = 0, pos = 0; j < 5; ++j) {
11150 int off = isl_basic_map_offset(bmap, c[j]);
11151 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11152 isl_int_set(mat->row[i][pos],
11153 bmap->ineq[i][off + k]);
11154 ++pos;
11158 return mat;
11161 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11162 __isl_take isl_space *dim,
11163 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11164 enum isl_dim_type c2, enum isl_dim_type c3,
11165 enum isl_dim_type c4, enum isl_dim_type c5)
11167 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11168 isl_basic_map *bmap;
11169 unsigned total;
11170 unsigned extra;
11171 int i, j, k, l;
11172 int pos;
11174 if (!dim || !eq || !ineq)
11175 goto error;
11177 if (eq->n_col != ineq->n_col)
11178 isl_die(dim->ctx, isl_error_invalid,
11179 "equalities and inequalities matrices should have "
11180 "same number of columns", goto error);
11182 total = 1 + isl_space_dim(dim, isl_dim_all);
11184 if (eq->n_col < total)
11185 isl_die(dim->ctx, isl_error_invalid,
11186 "number of columns too small", goto error);
11188 extra = eq->n_col - total;
11190 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11191 eq->n_row, ineq->n_row);
11192 if (!bmap)
11193 goto error;
11194 for (i = 0; i < extra; ++i) {
11195 k = isl_basic_map_alloc_div(bmap);
11196 if (k < 0)
11197 goto error;
11198 isl_int_set_si(bmap->div[k][0], 0);
11200 for (i = 0; i < eq->n_row; ++i) {
11201 l = isl_basic_map_alloc_equality(bmap);
11202 if (l < 0)
11203 goto error;
11204 for (j = 0, pos = 0; j < 5; ++j) {
11205 int off = isl_basic_map_offset(bmap, c[j]);
11206 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11207 isl_int_set(bmap->eq[l][off + k],
11208 eq->row[i][pos]);
11209 ++pos;
11213 for (i = 0; i < ineq->n_row; ++i) {
11214 l = isl_basic_map_alloc_inequality(bmap);
11215 if (l < 0)
11216 goto error;
11217 for (j = 0, pos = 0; j < 5; ++j) {
11218 int off = isl_basic_map_offset(bmap, c[j]);
11219 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11220 isl_int_set(bmap->ineq[l][off + k],
11221 ineq->row[i][pos]);
11222 ++pos;
11227 isl_space_free(dim);
11228 isl_mat_free(eq);
11229 isl_mat_free(ineq);
11231 bmap = isl_basic_map_simplify(bmap);
11232 return isl_basic_map_finalize(bmap);
11233 error:
11234 isl_space_free(dim);
11235 isl_mat_free(eq);
11236 isl_mat_free(ineq);
11237 return NULL;
11240 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11241 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11242 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11244 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11245 c1, c2, c3, c4, isl_dim_in);
11248 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11249 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11250 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11252 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11253 c1, c2, c3, c4, isl_dim_in);
11256 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11257 __isl_take isl_space *dim,
11258 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11259 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11261 return (isl_basic_set*)
11262 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11263 c1, c2, c3, c4, isl_dim_in);
11266 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11268 if (!bmap)
11269 return isl_bool_error;
11271 return isl_space_can_zip(bmap->dim);
11274 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11276 if (!map)
11277 return isl_bool_error;
11279 return isl_space_can_zip(map->dim);
11282 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11283 * (A -> C) -> (B -> D).
11285 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11287 unsigned pos;
11288 unsigned n1;
11289 unsigned n2;
11291 if (!bmap)
11292 return NULL;
11294 if (!isl_basic_map_can_zip(bmap))
11295 isl_die(bmap->ctx, isl_error_invalid,
11296 "basic map cannot be zipped", goto error);
11297 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11298 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11299 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11300 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11301 bmap = isl_basic_map_cow(bmap);
11302 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11303 if (!bmap)
11304 return NULL;
11305 bmap->dim = isl_space_zip(bmap->dim);
11306 if (!bmap->dim)
11307 goto error;
11308 return bmap;
11309 error:
11310 isl_basic_map_free(bmap);
11311 return NULL;
11314 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11315 * (A -> C) -> (B -> D).
11317 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11319 int i;
11321 if (!map)
11322 return NULL;
11324 if (!isl_map_can_zip(map))
11325 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11326 goto error);
11328 map = isl_map_cow(map);
11329 if (!map)
11330 return NULL;
11332 for (i = 0; i < map->n; ++i) {
11333 map->p[i] = isl_basic_map_zip(map->p[i]);
11334 if (!map->p[i])
11335 goto error;
11338 map->dim = isl_space_zip(map->dim);
11339 if (!map->dim)
11340 goto error;
11342 return map;
11343 error:
11344 isl_map_free(map);
11345 return NULL;
11348 /* Can we apply isl_basic_map_curry to "bmap"?
11349 * That is, does it have a nested relation in its domain?
11351 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11353 if (!bmap)
11354 return isl_bool_error;
11356 return isl_space_can_curry(bmap->dim);
11359 /* Can we apply isl_map_curry to "map"?
11360 * That is, does it have a nested relation in its domain?
11362 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11364 if (!map)
11365 return isl_bool_error;
11367 return isl_space_can_curry(map->dim);
11370 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11371 * A -> (B -> C).
11373 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11376 if (!bmap)
11377 return NULL;
11379 if (!isl_basic_map_can_curry(bmap))
11380 isl_die(bmap->ctx, isl_error_invalid,
11381 "basic map cannot be curried", goto error);
11382 bmap = isl_basic_map_cow(bmap);
11383 if (!bmap)
11384 return NULL;
11385 bmap->dim = isl_space_curry(bmap->dim);
11386 if (!bmap->dim)
11387 goto error;
11388 return bmap;
11389 error:
11390 isl_basic_map_free(bmap);
11391 return NULL;
11394 /* Given a map (A -> B) -> C, return the corresponding map
11395 * A -> (B -> C).
11397 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11399 int i;
11401 if (!map)
11402 return NULL;
11404 if (!isl_map_can_curry(map))
11405 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11406 goto error);
11408 map = isl_map_cow(map);
11409 if (!map)
11410 return NULL;
11412 for (i = 0; i < map->n; ++i) {
11413 map->p[i] = isl_basic_map_curry(map->p[i]);
11414 if (!map->p[i])
11415 goto error;
11418 map->dim = isl_space_curry(map->dim);
11419 if (!map->dim)
11420 goto error;
11422 return map;
11423 error:
11424 isl_map_free(map);
11425 return NULL;
11428 /* Can we apply isl_basic_map_uncurry to "bmap"?
11429 * That is, does it have a nested relation in its domain?
11431 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11433 if (!bmap)
11434 return isl_bool_error;
11436 return isl_space_can_uncurry(bmap->dim);
11439 /* Can we apply isl_map_uncurry to "map"?
11440 * That is, does it have a nested relation in its domain?
11442 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11444 if (!map)
11445 return isl_bool_error;
11447 return isl_space_can_uncurry(map->dim);
11450 /* Given a basic map A -> (B -> C), return the corresponding basic map
11451 * (A -> B) -> C.
11453 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11456 if (!bmap)
11457 return NULL;
11459 if (!isl_basic_map_can_uncurry(bmap))
11460 isl_die(bmap->ctx, isl_error_invalid,
11461 "basic map cannot be uncurried",
11462 return isl_basic_map_free(bmap));
11463 bmap = isl_basic_map_cow(bmap);
11464 if (!bmap)
11465 return NULL;
11466 bmap->dim = isl_space_uncurry(bmap->dim);
11467 if (!bmap->dim)
11468 return isl_basic_map_free(bmap);
11469 return bmap;
11472 /* Given a map A -> (B -> C), return the corresponding map
11473 * (A -> B) -> C.
11475 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11477 int i;
11479 if (!map)
11480 return NULL;
11482 if (!isl_map_can_uncurry(map))
11483 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11484 return isl_map_free(map));
11486 map = isl_map_cow(map);
11487 if (!map)
11488 return NULL;
11490 for (i = 0; i < map->n; ++i) {
11491 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11492 if (!map->p[i])
11493 return isl_map_free(map);
11496 map->dim = isl_space_uncurry(map->dim);
11497 if (!map->dim)
11498 return isl_map_free(map);
11500 return map;
11503 /* Construct a basic map mapping the domain of the affine expression
11504 * to a one-dimensional range prescribed by the affine expression.
11506 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11508 int k;
11509 int pos;
11510 isl_local_space *ls;
11511 isl_basic_map *bmap;
11513 if (!aff)
11514 return NULL;
11516 ls = isl_aff_get_local_space(aff);
11517 bmap = isl_basic_map_from_local_space(ls);
11518 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11519 k = isl_basic_map_alloc_equality(bmap);
11520 if (k < 0)
11521 goto error;
11523 pos = isl_basic_map_offset(bmap, isl_dim_out);
11524 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11525 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11526 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11527 aff->v->size - (pos + 1));
11529 isl_aff_free(aff);
11530 bmap = isl_basic_map_finalize(bmap);
11531 return bmap;
11532 error:
11533 isl_aff_free(aff);
11534 isl_basic_map_free(bmap);
11535 return NULL;
11538 /* Construct a map mapping the domain of the affine expression
11539 * to a one-dimensional range prescribed by the affine expression.
11541 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11543 isl_basic_map *bmap;
11545 bmap = isl_basic_map_from_aff(aff);
11546 return isl_map_from_basic_map(bmap);
11549 /* Construct a basic map mapping the domain the multi-affine expression
11550 * to its range, with each dimension in the range equated to the
11551 * corresponding affine expression.
11553 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11554 __isl_take isl_multi_aff *maff)
11556 int i;
11557 isl_space *space;
11558 isl_basic_map *bmap;
11560 if (!maff)
11561 return NULL;
11563 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11564 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11565 "invalid space", goto error);
11567 space = isl_space_domain(isl_multi_aff_get_space(maff));
11568 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11570 for (i = 0; i < maff->n; ++i) {
11571 isl_aff *aff;
11572 isl_basic_map *bmap_i;
11574 aff = isl_aff_copy(maff->p[i]);
11575 bmap_i = isl_basic_map_from_aff(aff);
11577 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11580 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11582 isl_multi_aff_free(maff);
11583 return bmap;
11584 error:
11585 isl_multi_aff_free(maff);
11586 return NULL;
11589 /* Construct a map mapping the domain the multi-affine expression
11590 * to its range, with each dimension in the range equated to the
11591 * corresponding affine expression.
11593 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11595 isl_basic_map *bmap;
11597 bmap = isl_basic_map_from_multi_aff(maff);
11598 return isl_map_from_basic_map(bmap);
11601 /* Construct a basic map mapping a domain in the given space to
11602 * to an n-dimensional range, with n the number of elements in the list,
11603 * where each coordinate in the range is prescribed by the
11604 * corresponding affine expression.
11605 * The domains of all affine expressions in the list are assumed to match
11606 * domain_dim.
11608 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11609 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11611 int i;
11612 isl_space *dim;
11613 isl_basic_map *bmap;
11615 if (!list)
11616 return NULL;
11618 dim = isl_space_from_domain(domain_dim);
11619 bmap = isl_basic_map_universe(dim);
11621 for (i = 0; i < list->n; ++i) {
11622 isl_aff *aff;
11623 isl_basic_map *bmap_i;
11625 aff = isl_aff_copy(list->p[i]);
11626 bmap_i = isl_basic_map_from_aff(aff);
11628 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11631 isl_aff_list_free(list);
11632 return bmap;
11635 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11636 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11638 return isl_map_equate(set, type1, pos1, type2, pos2);
11641 /* Construct a basic map where the given dimensions are equal to each other.
11643 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11644 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11646 isl_basic_map *bmap = NULL;
11647 int i;
11649 if (!space)
11650 return NULL;
11652 if (pos1 >= isl_space_dim(space, type1))
11653 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11654 "index out of bounds", goto error);
11655 if (pos2 >= isl_space_dim(space, type2))
11656 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11657 "index out of bounds", goto error);
11659 if (type1 == type2 && pos1 == pos2)
11660 return isl_basic_map_universe(space);
11662 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11663 i = isl_basic_map_alloc_equality(bmap);
11664 if (i < 0)
11665 goto error;
11666 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11667 pos1 += isl_basic_map_offset(bmap, type1);
11668 pos2 += isl_basic_map_offset(bmap, type2);
11669 isl_int_set_si(bmap->eq[i][pos1], -1);
11670 isl_int_set_si(bmap->eq[i][pos2], 1);
11671 bmap = isl_basic_map_finalize(bmap);
11672 isl_space_free(space);
11673 return bmap;
11674 error:
11675 isl_space_free(space);
11676 isl_basic_map_free(bmap);
11677 return NULL;
11680 /* Add a constraint imposing that the given two dimensions are equal.
11682 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11683 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11685 isl_basic_map *eq;
11687 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11689 bmap = isl_basic_map_intersect(bmap, eq);
11691 return bmap;
11694 /* Add a constraint imposing that the given two dimensions are equal.
11696 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11697 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11699 isl_basic_map *bmap;
11701 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11703 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11705 return map;
11708 /* Add a constraint imposing that the given two dimensions have opposite values.
11710 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11711 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11713 isl_basic_map *bmap = NULL;
11714 int i;
11716 if (!map)
11717 return NULL;
11719 if (pos1 >= isl_map_dim(map, type1))
11720 isl_die(map->ctx, isl_error_invalid,
11721 "index out of bounds", goto error);
11722 if (pos2 >= isl_map_dim(map, type2))
11723 isl_die(map->ctx, isl_error_invalid,
11724 "index out of bounds", goto error);
11726 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11727 i = isl_basic_map_alloc_equality(bmap);
11728 if (i < 0)
11729 goto error;
11730 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11731 pos1 += isl_basic_map_offset(bmap, type1);
11732 pos2 += isl_basic_map_offset(bmap, type2);
11733 isl_int_set_si(bmap->eq[i][pos1], 1);
11734 isl_int_set_si(bmap->eq[i][pos2], 1);
11735 bmap = isl_basic_map_finalize(bmap);
11737 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11739 return map;
11740 error:
11741 isl_basic_map_free(bmap);
11742 isl_map_free(map);
11743 return NULL;
11746 /* Construct a constraint imposing that the value of the first dimension is
11747 * greater than or equal to that of the second.
11749 static __isl_give isl_constraint *constraint_order_ge(
11750 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11751 enum isl_dim_type type2, int pos2)
11753 isl_constraint *c;
11755 if (!space)
11756 return NULL;
11758 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
11760 if (pos1 >= isl_constraint_dim(c, type1))
11761 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11762 "index out of bounds", return isl_constraint_free(c));
11763 if (pos2 >= isl_constraint_dim(c, type2))
11764 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11765 "index out of bounds", return isl_constraint_free(c));
11767 if (type1 == type2 && pos1 == pos2)
11768 return c;
11770 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11771 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11773 return c;
11776 /* Add a constraint imposing that the value of the first dimension is
11777 * greater than or equal to that of the second.
11779 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11780 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11782 isl_constraint *c;
11783 isl_space *space;
11785 if (type1 == type2 && pos1 == pos2)
11786 return bmap;
11787 space = isl_basic_map_get_space(bmap);
11788 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11789 bmap = isl_basic_map_add_constraint(bmap, c);
11791 return bmap;
11794 /* Add a constraint imposing that the value of the first dimension is
11795 * greater than or equal to that of the second.
11797 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11798 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11800 isl_constraint *c;
11801 isl_space *space;
11803 if (type1 == type2 && pos1 == pos2)
11804 return map;
11805 space = isl_map_get_space(map);
11806 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11807 map = isl_map_add_constraint(map, c);
11809 return map;
11812 /* Add a constraint imposing that the value of the first dimension is
11813 * less than or equal to that of the second.
11815 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11816 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11818 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11821 /* Construct a basic map where the value of the first dimension is
11822 * greater than that of the second.
11824 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11825 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11827 isl_basic_map *bmap = NULL;
11828 int i;
11830 if (!space)
11831 return NULL;
11833 if (pos1 >= isl_space_dim(space, type1))
11834 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11835 "index out of bounds", goto error);
11836 if (pos2 >= isl_space_dim(space, type2))
11837 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11838 "index out of bounds", goto error);
11840 if (type1 == type2 && pos1 == pos2)
11841 return isl_basic_map_empty(space);
11843 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11844 i = isl_basic_map_alloc_inequality(bmap);
11845 if (i < 0)
11846 return isl_basic_map_free(bmap);
11847 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11848 pos1 += isl_basic_map_offset(bmap, type1);
11849 pos2 += isl_basic_map_offset(bmap, type2);
11850 isl_int_set_si(bmap->ineq[i][pos1], 1);
11851 isl_int_set_si(bmap->ineq[i][pos2], -1);
11852 isl_int_set_si(bmap->ineq[i][0], -1);
11853 bmap = isl_basic_map_finalize(bmap);
11855 return bmap;
11856 error:
11857 isl_space_free(space);
11858 isl_basic_map_free(bmap);
11859 return NULL;
11862 /* Add a constraint imposing that the value of the first dimension is
11863 * greater than that of the second.
11865 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11866 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11868 isl_basic_map *gt;
11870 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11872 bmap = isl_basic_map_intersect(bmap, gt);
11874 return bmap;
11877 /* Add a constraint imposing that the value of the first dimension is
11878 * greater than that of the second.
11880 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11881 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11883 isl_basic_map *bmap;
11885 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11887 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11889 return map;
11892 /* Add a constraint imposing that the value of the first dimension is
11893 * smaller than that of the second.
11895 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11896 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11898 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11901 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11902 int pos)
11904 isl_aff *div;
11905 isl_local_space *ls;
11907 if (!bmap)
11908 return NULL;
11910 if (!isl_basic_map_divs_known(bmap))
11911 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11912 "some divs are unknown", return NULL);
11914 ls = isl_basic_map_get_local_space(bmap);
11915 div = isl_local_space_get_div(ls, pos);
11916 isl_local_space_free(ls);
11918 return div;
11921 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11922 int pos)
11924 return isl_basic_map_get_div(bset, pos);
11927 /* Plug in "subs" for dimension "type", "pos" of "bset".
11929 * Let i be the dimension to replace and let "subs" be of the form
11931 * f/d
11933 * Any integer division with a non-zero coefficient for i,
11935 * floor((a i + g)/m)
11937 * is replaced by
11939 * floor((a f + d g)/(m d))
11941 * Constraints of the form
11943 * a i + g
11945 * are replaced by
11947 * a f + d g
11949 * We currently require that "subs" is an integral expression.
11950 * Handling rational expressions may require us to add stride constraints
11951 * as we do in isl_basic_set_preimage_multi_aff.
11953 __isl_give isl_basic_set *isl_basic_set_substitute(
11954 __isl_take isl_basic_set *bset,
11955 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11957 int i;
11958 isl_int v;
11959 isl_ctx *ctx;
11961 if (bset && isl_basic_set_plain_is_empty(bset))
11962 return bset;
11964 bset = isl_basic_set_cow(bset);
11965 if (!bset || !subs)
11966 goto error;
11968 ctx = isl_basic_set_get_ctx(bset);
11969 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11970 isl_die(ctx, isl_error_invalid,
11971 "spaces don't match", goto error);
11972 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11973 isl_die(ctx, isl_error_unsupported,
11974 "cannot handle divs yet", goto error);
11975 if (!isl_int_is_one(subs->v->el[0]))
11976 isl_die(ctx, isl_error_invalid,
11977 "can only substitute integer expressions", goto error);
11979 pos += isl_basic_set_offset(bset, type);
11981 isl_int_init(v);
11983 for (i = 0; i < bset->n_eq; ++i) {
11984 if (isl_int_is_zero(bset->eq[i][pos]))
11985 continue;
11986 isl_int_set(v, bset->eq[i][pos]);
11987 isl_int_set_si(bset->eq[i][pos], 0);
11988 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11989 v, subs->v->el + 1, subs->v->size - 1);
11992 for (i = 0; i < bset->n_ineq; ++i) {
11993 if (isl_int_is_zero(bset->ineq[i][pos]))
11994 continue;
11995 isl_int_set(v, bset->ineq[i][pos]);
11996 isl_int_set_si(bset->ineq[i][pos], 0);
11997 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11998 v, subs->v->el + 1, subs->v->size - 1);
12001 for (i = 0; i < bset->n_div; ++i) {
12002 if (isl_int_is_zero(bset->div[i][1 + pos]))
12003 continue;
12004 isl_int_set(v, bset->div[i][1 + pos]);
12005 isl_int_set_si(bset->div[i][1 + pos], 0);
12006 isl_seq_combine(bset->div[i] + 1,
12007 subs->v->el[0], bset->div[i] + 1,
12008 v, subs->v->el + 1, subs->v->size - 1);
12009 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12012 isl_int_clear(v);
12014 bset = isl_basic_set_simplify(bset);
12015 return isl_basic_set_finalize(bset);
12016 error:
12017 isl_basic_set_free(bset);
12018 return NULL;
12021 /* Plug in "subs" for dimension "type", "pos" of "set".
12023 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12024 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12026 int i;
12028 if (set && isl_set_plain_is_empty(set))
12029 return set;
12031 set = isl_set_cow(set);
12032 if (!set || !subs)
12033 goto error;
12035 for (i = set->n - 1; i >= 0; --i) {
12036 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12037 if (remove_if_empty(set, i) < 0)
12038 goto error;
12041 return set;
12042 error:
12043 isl_set_free(set);
12044 return NULL;
12047 /* Check if the range of "ma" is compatible with the domain or range
12048 * (depending on "type") of "bmap".
12049 * Return -1 if anything is wrong.
12051 static int check_basic_map_compatible_range_multi_aff(
12052 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12053 __isl_keep isl_multi_aff *ma)
12055 int m;
12056 isl_space *ma_space;
12058 ma_space = isl_multi_aff_get_space(ma);
12060 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12061 if (m < 0)
12062 goto error;
12063 if (!m)
12064 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12065 "parameters don't match", goto error);
12066 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12067 if (m < 0)
12068 goto error;
12069 if (!m)
12070 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12071 "spaces don't match", goto error);
12073 isl_space_free(ma_space);
12074 return m;
12075 error:
12076 isl_space_free(ma_space);
12077 return -1;
12080 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12081 * coefficients before the transformed range of dimensions,
12082 * the "n_after" coefficients after the transformed range of dimensions
12083 * and the coefficients of the other divs in "bmap".
12085 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12086 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12088 int i;
12089 int n_param;
12090 int n_set;
12091 isl_local_space *ls;
12093 if (n_div == 0)
12094 return 0;
12096 ls = isl_aff_get_domain_local_space(ma->p[0]);
12097 if (!ls)
12098 return -1;
12100 n_param = isl_local_space_dim(ls, isl_dim_param);
12101 n_set = isl_local_space_dim(ls, isl_dim_set);
12102 for (i = 0; i < n_div; ++i) {
12103 int o_bmap = 0, o_ls = 0;
12105 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12106 o_bmap += 1 + 1 + n_param;
12107 o_ls += 1 + 1 + n_param;
12108 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12109 o_bmap += n_before;
12110 isl_seq_cpy(bmap->div[i] + o_bmap,
12111 ls->div->row[i] + o_ls, n_set);
12112 o_bmap += n_set;
12113 o_ls += n_set;
12114 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12115 o_bmap += n_after;
12116 isl_seq_cpy(bmap->div[i] + o_bmap,
12117 ls->div->row[i] + o_ls, n_div);
12118 o_bmap += n_div;
12119 o_ls += n_div;
12120 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12121 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12122 goto error;
12125 isl_local_space_free(ls);
12126 return 0;
12127 error:
12128 isl_local_space_free(ls);
12129 return -1;
12132 /* How many stride constraints does "ma" enforce?
12133 * That is, how many of the affine expressions have a denominator
12134 * different from one?
12136 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12138 int i;
12139 int strides = 0;
12141 for (i = 0; i < ma->n; ++i)
12142 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12143 strides++;
12145 return strides;
12148 /* For each affine expression in ma of the form
12150 * x_i = (f_i y + h_i)/m_i
12152 * with m_i different from one, add a constraint to "bmap"
12153 * of the form
12155 * f_i y + h_i = m_i alpha_i
12157 * with alpha_i an additional existentially quantified variable.
12159 static __isl_give isl_basic_map *add_ma_strides(
12160 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12161 int n_before, int n_after)
12163 int i, k;
12164 int div;
12165 int total;
12166 int n_param;
12167 int n_in;
12168 int n_div;
12170 total = isl_basic_map_total_dim(bmap);
12171 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12172 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12173 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12174 for (i = 0; i < ma->n; ++i) {
12175 int o_bmap = 0, o_ma = 1;
12177 if (isl_int_is_one(ma->p[i]->v->el[0]))
12178 continue;
12179 div = isl_basic_map_alloc_div(bmap);
12180 k = isl_basic_map_alloc_equality(bmap);
12181 if (div < 0 || k < 0)
12182 goto error;
12183 isl_int_set_si(bmap->div[div][0], 0);
12184 isl_seq_cpy(bmap->eq[k] + o_bmap,
12185 ma->p[i]->v->el + o_ma, 1 + n_param);
12186 o_bmap += 1 + n_param;
12187 o_ma += 1 + n_param;
12188 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12189 o_bmap += n_before;
12190 isl_seq_cpy(bmap->eq[k] + o_bmap,
12191 ma->p[i]->v->el + o_ma, n_in);
12192 o_bmap += n_in;
12193 o_ma += n_in;
12194 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12195 o_bmap += n_after;
12196 isl_seq_cpy(bmap->eq[k] + o_bmap,
12197 ma->p[i]->v->el + o_ma, n_div);
12198 o_bmap += n_div;
12199 o_ma += n_div;
12200 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12201 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12202 total++;
12205 return bmap;
12206 error:
12207 isl_basic_map_free(bmap);
12208 return NULL;
12211 /* Replace the domain or range space (depending on "type) of "space" by "set".
12213 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12214 enum isl_dim_type type, __isl_take isl_space *set)
12216 if (type == isl_dim_in) {
12217 space = isl_space_range(space);
12218 space = isl_space_map_from_domain_and_range(set, space);
12219 } else {
12220 space = isl_space_domain(space);
12221 space = isl_space_map_from_domain_and_range(space, set);
12224 return space;
12227 /* Compute the preimage of the domain or range (depending on "type")
12228 * of "bmap" under the function represented by "ma".
12229 * In other words, plug in "ma" in the domain or range of "bmap".
12230 * The result is a basic map that lives in the same space as "bmap"
12231 * except that the domain or range has been replaced by
12232 * the domain space of "ma".
12234 * If bmap is represented by
12236 * A(p) + S u + B x + T v + C(divs) >= 0,
12238 * where u and x are input and output dimensions if type == isl_dim_out
12239 * while x and v are input and output dimensions if type == isl_dim_in,
12240 * and ma is represented by
12242 * x = D(p) + F(y) + G(divs')
12244 * then the result is
12246 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12248 * The divs in the input set are similarly adjusted.
12249 * In particular
12251 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12253 * becomes
12255 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12256 * B_i G(divs') + c_i(divs))/n_i)
12258 * If bmap is not a rational map and if F(y) involves any denominators
12260 * x_i = (f_i y + h_i)/m_i
12262 * then additional constraints are added to ensure that we only
12263 * map back integer points. That is we enforce
12265 * f_i y + h_i = m_i alpha_i
12267 * with alpha_i an additional existentially quantified variable.
12269 * We first copy over the divs from "ma".
12270 * Then we add the modified constraints and divs from "bmap".
12271 * Finally, we add the stride constraints, if needed.
12273 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12274 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12275 __isl_take isl_multi_aff *ma)
12277 int i, k;
12278 isl_space *space;
12279 isl_basic_map *res = NULL;
12280 int n_before, n_after, n_div_bmap, n_div_ma;
12281 isl_int f, c1, c2, g;
12282 int rational, strides;
12284 isl_int_init(f);
12285 isl_int_init(c1);
12286 isl_int_init(c2);
12287 isl_int_init(g);
12289 ma = isl_multi_aff_align_divs(ma);
12290 if (!bmap || !ma)
12291 goto error;
12292 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12293 goto error;
12295 if (type == isl_dim_in) {
12296 n_before = 0;
12297 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12298 } else {
12299 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12300 n_after = 0;
12302 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12303 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12305 space = isl_multi_aff_get_domain_space(ma);
12306 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12307 rational = isl_basic_map_is_rational(bmap);
12308 strides = rational ? 0 : multi_aff_strides(ma);
12309 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12310 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12311 if (rational)
12312 res = isl_basic_map_set_rational(res);
12314 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12315 if (isl_basic_map_alloc_div(res) < 0)
12316 goto error;
12318 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12319 goto error;
12321 for (i = 0; i < bmap->n_eq; ++i) {
12322 k = isl_basic_map_alloc_equality(res);
12323 if (k < 0)
12324 goto error;
12325 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12326 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12329 for (i = 0; i < bmap->n_ineq; ++i) {
12330 k = isl_basic_map_alloc_inequality(res);
12331 if (k < 0)
12332 goto error;
12333 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12334 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12337 for (i = 0; i < bmap->n_div; ++i) {
12338 if (isl_int_is_zero(bmap->div[i][0])) {
12339 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12340 continue;
12342 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12343 n_before, n_after, n_div_ma, n_div_bmap,
12344 f, c1, c2, g, 1);
12347 if (strides)
12348 res = add_ma_strides(res, ma, n_before, n_after);
12350 isl_int_clear(f);
12351 isl_int_clear(c1);
12352 isl_int_clear(c2);
12353 isl_int_clear(g);
12354 isl_basic_map_free(bmap);
12355 isl_multi_aff_free(ma);
12356 res = isl_basic_set_simplify(res);
12357 return isl_basic_map_finalize(res);
12358 error:
12359 isl_int_clear(f);
12360 isl_int_clear(c1);
12361 isl_int_clear(c2);
12362 isl_int_clear(g);
12363 isl_basic_map_free(bmap);
12364 isl_multi_aff_free(ma);
12365 isl_basic_map_free(res);
12366 return NULL;
12369 /* Compute the preimage of "bset" under the function represented by "ma".
12370 * In other words, plug in "ma" in "bset". The result is a basic set
12371 * that lives in the domain space of "ma".
12373 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12374 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12376 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12379 /* Compute the preimage of the domain of "bmap" under the function
12380 * represented by "ma".
12381 * In other words, plug in "ma" in the domain of "bmap".
12382 * The result is a basic map that lives in the same space as "bmap"
12383 * except that the domain has been replaced by the domain space of "ma".
12385 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12386 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12388 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12391 /* Compute the preimage of the range of "bmap" under the function
12392 * represented by "ma".
12393 * In other words, plug in "ma" in the range of "bmap".
12394 * The result is a basic map that lives in the same space as "bmap"
12395 * except that the range has been replaced by the domain space of "ma".
12397 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12398 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12400 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12403 /* Check if the range of "ma" is compatible with the domain or range
12404 * (depending on "type") of "map".
12405 * Return -1 if anything is wrong.
12407 static int check_map_compatible_range_multi_aff(
12408 __isl_keep isl_map *map, enum isl_dim_type type,
12409 __isl_keep isl_multi_aff *ma)
12411 int m;
12412 isl_space *ma_space;
12414 ma_space = isl_multi_aff_get_space(ma);
12415 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12416 isl_space_free(ma_space);
12417 if (m >= 0 && !m)
12418 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12419 "spaces don't match", return -1);
12420 return m;
12423 /* Compute the preimage of the domain or range (depending on "type")
12424 * of "map" under the function represented by "ma".
12425 * In other words, plug in "ma" in the domain or range of "map".
12426 * The result is a map that lives in the same space as "map"
12427 * except that the domain or range has been replaced by
12428 * the domain space of "ma".
12430 * The parameters are assumed to have been aligned.
12432 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12433 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12435 int i;
12436 isl_space *space;
12438 map = isl_map_cow(map);
12439 ma = isl_multi_aff_align_divs(ma);
12440 if (!map || !ma)
12441 goto error;
12442 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12443 goto error;
12445 for (i = 0; i < map->n; ++i) {
12446 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12447 isl_multi_aff_copy(ma));
12448 if (!map->p[i])
12449 goto error;
12452 space = isl_multi_aff_get_domain_space(ma);
12453 space = isl_space_set(isl_map_get_space(map), type, space);
12455 isl_space_free(map->dim);
12456 map->dim = space;
12457 if (!map->dim)
12458 goto error;
12460 isl_multi_aff_free(ma);
12461 if (map->n > 1)
12462 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12463 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12464 return map;
12465 error:
12466 isl_multi_aff_free(ma);
12467 isl_map_free(map);
12468 return NULL;
12471 /* Compute the preimage of the domain or range (depending on "type")
12472 * of "map" under the function represented by "ma".
12473 * In other words, plug in "ma" in the domain or range of "map".
12474 * The result is a map that lives in the same space as "map"
12475 * except that the domain or range has been replaced by
12476 * the domain space of "ma".
12478 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12479 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12481 if (!map || !ma)
12482 goto error;
12484 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12485 return map_preimage_multi_aff(map, type, ma);
12487 if (!isl_space_has_named_params(map->dim) ||
12488 !isl_space_has_named_params(ma->space))
12489 isl_die(map->ctx, isl_error_invalid,
12490 "unaligned unnamed parameters", goto error);
12491 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12492 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12494 return map_preimage_multi_aff(map, type, ma);
12495 error:
12496 isl_multi_aff_free(ma);
12497 return isl_map_free(map);
12500 /* Compute the preimage of "set" under the function represented by "ma".
12501 * In other words, plug in "ma" in "set". The result is a set
12502 * that lives in the domain space of "ma".
12504 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12505 __isl_take isl_multi_aff *ma)
12507 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12510 /* Compute the preimage of the domain of "map" under the function
12511 * represented by "ma".
12512 * In other words, plug in "ma" in the domain of "map".
12513 * The result is a map that lives in the same space as "map"
12514 * except that the domain has been replaced by the domain space of "ma".
12516 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12517 __isl_take isl_multi_aff *ma)
12519 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12522 /* Compute the preimage of the range of "map" under the function
12523 * represented by "ma".
12524 * In other words, plug in "ma" in the range of "map".
12525 * The result is a map that lives in the same space as "map"
12526 * except that the range has been replaced by the domain space of "ma".
12528 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12529 __isl_take isl_multi_aff *ma)
12531 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12534 /* Compute the preimage of "map" under the function represented by "pma".
12535 * In other words, plug in "pma" in the domain or range of "map".
12536 * The result is a map that lives in the same space as "map",
12537 * except that the space of type "type" has been replaced by
12538 * the domain space of "pma".
12540 * The parameters of "map" and "pma" are assumed to have been aligned.
12542 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12543 __isl_take isl_map *map, enum isl_dim_type type,
12544 __isl_take isl_pw_multi_aff *pma)
12546 int i;
12547 isl_map *res;
12549 if (!pma)
12550 goto error;
12552 if (pma->n == 0) {
12553 isl_pw_multi_aff_free(pma);
12554 res = isl_map_empty(isl_map_get_space(map));
12555 isl_map_free(map);
12556 return res;
12559 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12560 isl_multi_aff_copy(pma->p[0].maff));
12561 if (type == isl_dim_in)
12562 res = isl_map_intersect_domain(res,
12563 isl_map_copy(pma->p[0].set));
12564 else
12565 res = isl_map_intersect_range(res,
12566 isl_map_copy(pma->p[0].set));
12568 for (i = 1; i < pma->n; ++i) {
12569 isl_map *res_i;
12571 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12572 isl_multi_aff_copy(pma->p[i].maff));
12573 if (type == isl_dim_in)
12574 res_i = isl_map_intersect_domain(res_i,
12575 isl_map_copy(pma->p[i].set));
12576 else
12577 res_i = isl_map_intersect_range(res_i,
12578 isl_map_copy(pma->p[i].set));
12579 res = isl_map_union(res, res_i);
12582 isl_pw_multi_aff_free(pma);
12583 isl_map_free(map);
12584 return res;
12585 error:
12586 isl_pw_multi_aff_free(pma);
12587 isl_map_free(map);
12588 return NULL;
12591 /* Compute the preimage of "map" under the function represented by "pma".
12592 * In other words, plug in "pma" in the domain or range of "map".
12593 * The result is a map that lives in the same space as "map",
12594 * except that the space of type "type" has been replaced by
12595 * the domain space of "pma".
12597 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12598 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12600 if (!map || !pma)
12601 goto error;
12603 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12604 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12606 if (!isl_space_has_named_params(map->dim) ||
12607 !isl_space_has_named_params(pma->dim))
12608 isl_die(map->ctx, isl_error_invalid,
12609 "unaligned unnamed parameters", goto error);
12610 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12611 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12613 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12614 error:
12615 isl_pw_multi_aff_free(pma);
12616 return isl_map_free(map);
12619 /* Compute the preimage of "set" under the function represented by "pma".
12620 * In other words, plug in "pma" in "set". The result is a set
12621 * that lives in the domain space of "pma".
12623 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12624 __isl_take isl_pw_multi_aff *pma)
12626 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12629 /* Compute the preimage of the domain of "map" under the function
12630 * represented by "pma".
12631 * In other words, plug in "pma" in the domain of "map".
12632 * The result is a map that lives in the same space as "map",
12633 * except that domain space has been replaced by the domain space of "pma".
12635 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12636 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12638 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12641 /* Compute the preimage of the range of "map" under the function
12642 * represented by "pma".
12643 * In other words, plug in "pma" in the range of "map".
12644 * The result is a map that lives in the same space as "map",
12645 * except that range space has been replaced by the domain space of "pma".
12647 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12648 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12650 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12653 /* Compute the preimage of "map" under the function represented by "mpa".
12654 * In other words, plug in "mpa" in the domain or range of "map".
12655 * The result is a map that lives in the same space as "map",
12656 * except that the space of type "type" has been replaced by
12657 * the domain space of "mpa".
12659 * If the map does not involve any constraints that refer to the
12660 * dimensions of the substituted space, then the only possible
12661 * effect of "mpa" on the map is to map the space to a different space.
12662 * We create a separate isl_multi_aff to effectuate this change
12663 * in order to avoid spurious splitting of the map along the pieces
12664 * of "mpa".
12666 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12667 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12669 int n;
12670 isl_pw_multi_aff *pma;
12672 if (!map || !mpa)
12673 goto error;
12675 n = isl_map_dim(map, type);
12676 if (!isl_map_involves_dims(map, type, 0, n)) {
12677 isl_space *space;
12678 isl_multi_aff *ma;
12680 space = isl_multi_pw_aff_get_space(mpa);
12681 isl_multi_pw_aff_free(mpa);
12682 ma = isl_multi_aff_zero(space);
12683 return isl_map_preimage_multi_aff(map, type, ma);
12686 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12687 return isl_map_preimage_pw_multi_aff(map, type, pma);
12688 error:
12689 isl_map_free(map);
12690 isl_multi_pw_aff_free(mpa);
12691 return NULL;
12694 /* Compute the preimage of "map" under the function represented by "mpa".
12695 * In other words, plug in "mpa" in the domain "map".
12696 * The result is a map that lives in the same space as "map",
12697 * except that domain space has been replaced by the domain space of "mpa".
12699 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12700 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12702 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12705 /* Compute the preimage of "set" by the function represented by "mpa".
12706 * In other words, plug in "mpa" in "set".
12708 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12709 __isl_take isl_multi_pw_aff *mpa)
12711 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);