isl_map_simplify.c uset_gist_full: detect equalities in the tableau
[isl.git] / isl_map.c
bloba4762f43a8c53c4fbf38d955791faeeb3c77799c
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 (bmap1->sample &&
2861 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2862 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2863 sample = isl_vec_copy(bmap1->sample);
2864 else if (bmap2->sample &&
2865 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2866 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2867 sample = isl_vec_copy(bmap2->sample);
2869 bmap1 = isl_basic_map_cow(bmap1);
2870 if (!bmap1)
2871 goto error;
2872 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2873 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2874 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2876 if (!bmap1)
2877 isl_vec_free(sample);
2878 else if (sample) {
2879 isl_vec_free(bmap1->sample);
2880 bmap1->sample = sample;
2883 bmap1 = isl_basic_map_simplify(bmap1);
2884 return isl_basic_map_finalize(bmap1);
2885 error:
2886 if (sample)
2887 isl_vec_free(sample);
2888 isl_basic_map_free(bmap1);
2889 isl_basic_map_free(bmap2);
2890 return NULL;
2893 struct isl_basic_set *isl_basic_set_intersect(
2894 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2896 return (struct isl_basic_set *)
2897 isl_basic_map_intersect(
2898 (struct isl_basic_map *)bset1,
2899 (struct isl_basic_map *)bset2);
2902 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2903 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2905 return isl_basic_set_intersect(bset1, bset2);
2908 /* Special case of isl_map_intersect, where both map1 and map2
2909 * are convex, without any divs and such that either map1 or map2
2910 * contains a single constraint. This constraint is then simply
2911 * added to the other map.
2913 static __isl_give isl_map *map_intersect_add_constraint(
2914 __isl_take isl_map *map1, __isl_take isl_map *map2)
2916 isl_assert(map1->ctx, map1->n == 1, goto error);
2917 isl_assert(map2->ctx, map1->n == 1, goto error);
2918 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2919 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2921 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2922 return isl_map_intersect(map2, map1);
2924 isl_assert(map2->ctx,
2925 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2927 map1 = isl_map_cow(map1);
2928 if (!map1)
2929 goto error;
2930 if (isl_map_plain_is_empty(map1)) {
2931 isl_map_free(map2);
2932 return map1;
2934 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2935 if (map2->p[0]->n_eq == 1)
2936 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2937 else
2938 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2939 map2->p[0]->ineq[0]);
2941 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2942 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2943 if (!map1->p[0])
2944 goto error;
2946 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2947 isl_basic_map_free(map1->p[0]);
2948 map1->n = 0;
2951 isl_map_free(map2);
2953 return map1;
2954 error:
2955 isl_map_free(map1);
2956 isl_map_free(map2);
2957 return NULL;
2960 /* map2 may be either a parameter domain or a map living in the same
2961 * space as map1.
2963 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2964 __isl_take isl_map *map2)
2966 unsigned flags = 0;
2967 isl_map *result;
2968 int i, j;
2970 if (!map1 || !map2)
2971 goto error;
2973 if ((isl_map_plain_is_empty(map1) ||
2974 isl_map_plain_is_universe(map2)) &&
2975 isl_space_is_equal(map1->dim, map2->dim)) {
2976 isl_map_free(map2);
2977 return map1;
2979 if ((isl_map_plain_is_empty(map2) ||
2980 isl_map_plain_is_universe(map1)) &&
2981 isl_space_is_equal(map1->dim, map2->dim)) {
2982 isl_map_free(map1);
2983 return map2;
2986 if (map1->n == 1 && map2->n == 1 &&
2987 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2988 isl_space_is_equal(map1->dim, map2->dim) &&
2989 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2990 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2991 return map_intersect_add_constraint(map1, map2);
2993 if (isl_space_dim(map2->dim, isl_dim_all) !=
2994 isl_space_dim(map2->dim, isl_dim_param))
2995 isl_assert(map1->ctx,
2996 isl_space_is_equal(map1->dim, map2->dim), goto error);
2998 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2999 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3000 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3002 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3003 map1->n * map2->n, flags);
3004 if (!result)
3005 goto error;
3006 for (i = 0; i < map1->n; ++i)
3007 for (j = 0; j < map2->n; ++j) {
3008 struct isl_basic_map *part;
3009 part = isl_basic_map_intersect(
3010 isl_basic_map_copy(map1->p[i]),
3011 isl_basic_map_copy(map2->p[j]));
3012 if (isl_basic_map_is_empty(part) < 0)
3013 part = isl_basic_map_free(part);
3014 result = isl_map_add_basic_map(result, part);
3015 if (!result)
3016 goto error;
3018 isl_map_free(map1);
3019 isl_map_free(map2);
3020 return result;
3021 error:
3022 isl_map_free(map1);
3023 isl_map_free(map2);
3024 return NULL;
3027 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3028 __isl_take isl_map *map2)
3030 if (!map1 || !map2)
3031 goto error;
3032 if (!isl_space_is_equal(map1->dim, map2->dim))
3033 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3034 "spaces don't match", goto error);
3035 return map_intersect_internal(map1, map2);
3036 error:
3037 isl_map_free(map1);
3038 isl_map_free(map2);
3039 return NULL;
3042 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3043 __isl_take isl_map *map2)
3045 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3048 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3050 return (struct isl_set *)
3051 isl_map_intersect((struct isl_map *)set1,
3052 (struct isl_map *)set2);
3055 /* map_intersect_internal accepts intersections
3056 * with parameter domains, so we can just call that function.
3058 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3059 __isl_take isl_set *params)
3061 return map_intersect_internal(map, params);
3064 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3065 __isl_take isl_map *map2)
3067 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3070 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3071 __isl_take isl_set *params)
3073 return isl_map_intersect_params(set, params);
3076 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3078 isl_space *space;
3079 unsigned pos, n1, n2;
3081 if (!bmap)
3082 return NULL;
3083 bmap = isl_basic_map_cow(bmap);
3084 if (!bmap)
3085 return NULL;
3086 space = isl_space_reverse(isl_space_copy(bmap->dim));
3087 pos = isl_basic_map_offset(bmap, isl_dim_in);
3088 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3089 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3090 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3091 return isl_basic_map_reset_space(bmap, space);
3094 static __isl_give isl_basic_map *basic_map_space_reset(
3095 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3097 isl_space *space;
3099 if (!bmap)
3100 return NULL;
3101 if (!isl_space_is_named_or_nested(bmap->dim, type))
3102 return bmap;
3104 space = isl_basic_map_get_space(bmap);
3105 space = isl_space_reset(space, type);
3106 bmap = isl_basic_map_reset_space(bmap, space);
3107 return bmap;
3110 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3111 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3112 unsigned pos, unsigned n)
3114 isl_space *res_dim;
3115 struct isl_basic_map *res;
3116 struct isl_dim_map *dim_map;
3117 unsigned total, off;
3118 enum isl_dim_type t;
3120 if (n == 0)
3121 return basic_map_space_reset(bmap, type);
3123 if (!bmap)
3124 return NULL;
3126 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3128 total = isl_basic_map_total_dim(bmap) + n;
3129 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3130 off = 0;
3131 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3132 if (t != type) {
3133 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3134 } else {
3135 unsigned size = isl_basic_map_dim(bmap, t);
3136 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3137 0, pos, off);
3138 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3139 pos, size - pos, off + pos + n);
3141 off += isl_space_dim(res_dim, t);
3143 isl_dim_map_div(dim_map, bmap, off);
3145 res = isl_basic_map_alloc_space(res_dim,
3146 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3147 if (isl_basic_map_is_rational(bmap))
3148 res = isl_basic_map_set_rational(res);
3149 if (isl_basic_map_plain_is_empty(bmap)) {
3150 isl_basic_map_free(bmap);
3151 free(dim_map);
3152 return isl_basic_map_set_to_empty(res);
3154 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3155 return isl_basic_map_finalize(res);
3158 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3159 __isl_take isl_basic_set *bset,
3160 enum isl_dim_type type, unsigned pos, unsigned n)
3162 return isl_basic_map_insert_dims(bset, type, pos, n);
3165 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3166 enum isl_dim_type type, unsigned n)
3168 if (!bmap)
3169 return NULL;
3170 return isl_basic_map_insert_dims(bmap, type,
3171 isl_basic_map_dim(bmap, type), n);
3174 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3175 enum isl_dim_type type, unsigned n)
3177 if (!bset)
3178 return NULL;
3179 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3180 return isl_basic_map_add_dims(bset, type, n);
3181 error:
3182 isl_basic_set_free(bset);
3183 return NULL;
3186 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3187 enum isl_dim_type type)
3189 isl_space *space;
3191 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3192 return map;
3194 space = isl_map_get_space(map);
3195 space = isl_space_reset(space, type);
3196 map = isl_map_reset_space(map, space);
3197 return map;
3200 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3201 enum isl_dim_type type, unsigned pos, unsigned n)
3203 int i;
3205 if (n == 0)
3206 return map_space_reset(map, type);
3208 map = isl_map_cow(map);
3209 if (!map)
3210 return NULL;
3212 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3213 if (!map->dim)
3214 goto error;
3216 for (i = 0; i < map->n; ++i) {
3217 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3218 if (!map->p[i])
3219 goto error;
3222 return map;
3223 error:
3224 isl_map_free(map);
3225 return NULL;
3228 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3229 enum isl_dim_type type, unsigned pos, unsigned n)
3231 return isl_map_insert_dims(set, type, pos, n);
3234 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3235 enum isl_dim_type type, unsigned n)
3237 if (!map)
3238 return NULL;
3239 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3242 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3243 enum isl_dim_type type, unsigned n)
3245 if (!set)
3246 return NULL;
3247 isl_assert(set->ctx, type != isl_dim_in, goto error);
3248 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3249 error:
3250 isl_set_free(set);
3251 return NULL;
3254 __isl_give isl_basic_map *isl_basic_map_move_dims(
3255 __isl_take isl_basic_map *bmap,
3256 enum isl_dim_type dst_type, unsigned dst_pos,
3257 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3259 struct isl_dim_map *dim_map;
3260 struct isl_basic_map *res;
3261 enum isl_dim_type t;
3262 unsigned total, off;
3264 if (!bmap)
3265 return NULL;
3266 if (n == 0)
3267 return bmap;
3269 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3270 goto error);
3272 if (dst_type == src_type && dst_pos == src_pos)
3273 return bmap;
3275 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3277 if (pos(bmap->dim, dst_type) + dst_pos ==
3278 pos(bmap->dim, src_type) + src_pos +
3279 ((src_type < dst_type) ? n : 0)) {
3280 bmap = isl_basic_map_cow(bmap);
3281 if (!bmap)
3282 return NULL;
3284 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3285 src_type, src_pos, n);
3286 if (!bmap->dim)
3287 goto error;
3289 bmap = isl_basic_map_finalize(bmap);
3291 return bmap;
3294 total = isl_basic_map_total_dim(bmap);
3295 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3297 off = 0;
3298 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3299 unsigned size = isl_space_dim(bmap->dim, t);
3300 if (t == dst_type) {
3301 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3302 0, dst_pos, off);
3303 off += dst_pos;
3304 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3305 src_pos, n, off);
3306 off += n;
3307 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3308 dst_pos, size - dst_pos, off);
3309 off += size - dst_pos;
3310 } else if (t == src_type) {
3311 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3312 0, src_pos, off);
3313 off += src_pos;
3314 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3315 src_pos + n, size - src_pos - n, off);
3316 off += size - src_pos - n;
3317 } else {
3318 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3319 off += size;
3322 isl_dim_map_div(dim_map, bmap, off);
3324 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3325 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3326 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3327 if (!bmap)
3328 goto error;
3330 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3331 src_type, src_pos, n);
3332 if (!bmap->dim)
3333 goto error;
3335 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3336 bmap = isl_basic_map_gauss(bmap, NULL);
3337 bmap = isl_basic_map_finalize(bmap);
3339 return bmap;
3340 error:
3341 isl_basic_map_free(bmap);
3342 return NULL;
3345 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3346 enum isl_dim_type dst_type, unsigned dst_pos,
3347 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3349 return (isl_basic_set *)isl_basic_map_move_dims(
3350 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3353 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3354 enum isl_dim_type dst_type, unsigned dst_pos,
3355 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3357 if (!set)
3358 return NULL;
3359 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3360 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3361 src_type, src_pos, n);
3362 error:
3363 isl_set_free(set);
3364 return NULL;
3367 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3368 enum isl_dim_type dst_type, unsigned dst_pos,
3369 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3371 int i;
3373 if (!map)
3374 return NULL;
3375 if (n == 0)
3376 return map;
3378 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3379 goto error);
3381 if (dst_type == src_type && dst_pos == src_pos)
3382 return map;
3384 isl_assert(map->ctx, dst_type != src_type, goto error);
3386 map = isl_map_cow(map);
3387 if (!map)
3388 return NULL;
3390 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3391 if (!map->dim)
3392 goto error;
3394 for (i = 0; i < map->n; ++i) {
3395 map->p[i] = isl_basic_map_move_dims(map->p[i],
3396 dst_type, dst_pos,
3397 src_type, src_pos, n);
3398 if (!map->p[i])
3399 goto error;
3402 return map;
3403 error:
3404 isl_map_free(map);
3405 return NULL;
3408 /* Move the specified dimensions to the last columns right before
3409 * the divs. Don't change the dimension specification of bmap.
3410 * That's the responsibility of the caller.
3412 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3413 enum isl_dim_type type, unsigned first, unsigned n)
3415 struct isl_dim_map *dim_map;
3416 struct isl_basic_map *res;
3417 enum isl_dim_type t;
3418 unsigned total, off;
3420 if (!bmap)
3421 return NULL;
3422 if (pos(bmap->dim, type) + first + n ==
3423 1 + isl_space_dim(bmap->dim, isl_dim_all))
3424 return bmap;
3426 total = isl_basic_map_total_dim(bmap);
3427 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3429 off = 0;
3430 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3431 unsigned size = isl_space_dim(bmap->dim, t);
3432 if (t == type) {
3433 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3434 0, first, off);
3435 off += first;
3436 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3437 first, n, total - bmap->n_div - n);
3438 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3439 first + n, size - (first + n), off);
3440 off += size - (first + n);
3441 } else {
3442 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3443 off += size;
3446 isl_dim_map_div(dim_map, bmap, off + n);
3448 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3449 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3450 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3451 return res;
3454 /* Insert "n" rows in the divs of "bmap".
3456 * The number of columns is not changed, which means that the last
3457 * dimensions of "bmap" are being reintepreted as the new divs.
3458 * The space of "bmap" is not adjusted, however, which means
3459 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3460 * from the space of "bmap" is the responsibility of the caller.
3462 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3463 int n)
3465 int i;
3466 size_t row_size;
3467 isl_int **new_div;
3468 isl_int *old;
3470 bmap = isl_basic_map_cow(bmap);
3471 if (!bmap)
3472 return NULL;
3474 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3475 old = bmap->block2.data;
3476 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3477 (bmap->extra + n) * (1 + row_size));
3478 if (!bmap->block2.data)
3479 return isl_basic_map_free(bmap);
3480 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3481 if (!new_div)
3482 return isl_basic_map_free(bmap);
3483 for (i = 0; i < n; ++i) {
3484 new_div[i] = bmap->block2.data +
3485 (bmap->extra + i) * (1 + row_size);
3486 isl_seq_clr(new_div[i], 1 + row_size);
3488 for (i = 0; i < bmap->extra; ++i)
3489 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3490 free(bmap->div);
3491 bmap->div = new_div;
3492 bmap->n_div += n;
3493 bmap->extra += n;
3495 return bmap;
3498 /* Turn the n dimensions of type type, starting at first
3499 * into existentially quantified variables.
3501 __isl_give isl_basic_map *isl_basic_map_project_out(
3502 __isl_take isl_basic_map *bmap,
3503 enum isl_dim_type type, unsigned first, unsigned n)
3505 if (n == 0)
3506 return basic_map_space_reset(bmap, type);
3508 if (!bmap)
3509 return NULL;
3511 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3512 return isl_basic_map_remove_dims(bmap, type, first, n);
3514 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3515 goto error);
3517 bmap = move_last(bmap, type, first, n);
3518 bmap = isl_basic_map_cow(bmap);
3519 bmap = insert_div_rows(bmap, n);
3520 if (!bmap)
3521 return NULL;
3523 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3524 if (!bmap->dim)
3525 goto error;
3526 bmap = isl_basic_map_simplify(bmap);
3527 bmap = isl_basic_map_drop_redundant_divs(bmap);
3528 return isl_basic_map_finalize(bmap);
3529 error:
3530 isl_basic_map_free(bmap);
3531 return NULL;
3534 /* Turn the n dimensions of type type, starting at first
3535 * into existentially quantified variables.
3537 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3538 enum isl_dim_type type, unsigned first, unsigned n)
3540 return (isl_basic_set *)isl_basic_map_project_out(
3541 (isl_basic_map *)bset, type, first, n);
3544 /* Turn the n dimensions of type type, starting at first
3545 * into existentially quantified variables.
3547 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3548 enum isl_dim_type type, unsigned first, unsigned n)
3550 int i;
3552 if (!map)
3553 return NULL;
3555 if (n == 0)
3556 return map_space_reset(map, type);
3558 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3560 map = isl_map_cow(map);
3561 if (!map)
3562 return NULL;
3564 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3565 if (!map->dim)
3566 goto error;
3568 for (i = 0; i < map->n; ++i) {
3569 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3570 if (!map->p[i])
3571 goto error;
3574 return map;
3575 error:
3576 isl_map_free(map);
3577 return NULL;
3580 /* Turn the n dimensions of type type, starting at first
3581 * into existentially quantified variables.
3583 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3584 enum isl_dim_type type, unsigned first, unsigned n)
3586 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3589 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3591 int i, j;
3593 for (i = 0; i < n; ++i) {
3594 j = isl_basic_map_alloc_div(bmap);
3595 if (j < 0)
3596 goto error;
3597 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3599 return bmap;
3600 error:
3601 isl_basic_map_free(bmap);
3602 return NULL;
3605 struct isl_basic_map *isl_basic_map_apply_range(
3606 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3608 isl_space *dim_result = NULL;
3609 struct isl_basic_map *bmap;
3610 unsigned n_in, n_out, n, nparam, total, pos;
3611 struct isl_dim_map *dim_map1, *dim_map2;
3613 if (!bmap1 || !bmap2)
3614 goto error;
3615 if (!isl_space_match(bmap1->dim, isl_dim_param,
3616 bmap2->dim, isl_dim_param))
3617 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3618 "parameters don't match", goto error);
3619 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3620 bmap2->dim, isl_dim_in))
3621 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3622 "spaces don't match", goto error);
3624 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3625 isl_space_copy(bmap2->dim));
3627 n_in = isl_basic_map_n_in(bmap1);
3628 n_out = isl_basic_map_n_out(bmap2);
3629 n = isl_basic_map_n_out(bmap1);
3630 nparam = isl_basic_map_n_param(bmap1);
3632 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3633 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3634 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3635 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3636 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3637 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3638 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3639 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3640 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3641 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3642 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3644 bmap = isl_basic_map_alloc_space(dim_result,
3645 bmap1->n_div + bmap2->n_div + n,
3646 bmap1->n_eq + bmap2->n_eq,
3647 bmap1->n_ineq + bmap2->n_ineq);
3648 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3649 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3650 bmap = add_divs(bmap, n);
3651 bmap = isl_basic_map_simplify(bmap);
3652 bmap = isl_basic_map_drop_redundant_divs(bmap);
3653 return isl_basic_map_finalize(bmap);
3654 error:
3655 isl_basic_map_free(bmap1);
3656 isl_basic_map_free(bmap2);
3657 return NULL;
3660 struct isl_basic_set *isl_basic_set_apply(
3661 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3663 if (!bset || !bmap)
3664 goto error;
3666 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3667 goto error);
3669 return (struct isl_basic_set *)
3670 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3671 error:
3672 isl_basic_set_free(bset);
3673 isl_basic_map_free(bmap);
3674 return NULL;
3677 struct isl_basic_map *isl_basic_map_apply_domain(
3678 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3680 if (!bmap1 || !bmap2)
3681 goto error;
3683 isl_assert(bmap1->ctx,
3684 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3685 isl_assert(bmap1->ctx,
3686 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3687 goto error);
3689 bmap1 = isl_basic_map_reverse(bmap1);
3690 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3691 return isl_basic_map_reverse(bmap1);
3692 error:
3693 isl_basic_map_free(bmap1);
3694 isl_basic_map_free(bmap2);
3695 return NULL;
3698 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3699 * A \cap B -> f(A) + f(B)
3701 struct isl_basic_map *isl_basic_map_sum(
3702 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3704 unsigned n_in, n_out, nparam, total, pos;
3705 struct isl_basic_map *bmap = NULL;
3706 struct isl_dim_map *dim_map1, *dim_map2;
3707 int i;
3709 if (!bmap1 || !bmap2)
3710 goto error;
3712 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3713 goto error);
3715 nparam = isl_basic_map_n_param(bmap1);
3716 n_in = isl_basic_map_n_in(bmap1);
3717 n_out = isl_basic_map_n_out(bmap1);
3719 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3720 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3721 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3722 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3723 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3724 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3725 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3726 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3727 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3728 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3729 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3731 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3732 bmap1->n_div + bmap2->n_div + 2 * n_out,
3733 bmap1->n_eq + bmap2->n_eq + n_out,
3734 bmap1->n_ineq + bmap2->n_ineq);
3735 for (i = 0; i < n_out; ++i) {
3736 int j = isl_basic_map_alloc_equality(bmap);
3737 if (j < 0)
3738 goto error;
3739 isl_seq_clr(bmap->eq[j], 1+total);
3740 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3741 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3742 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3744 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3745 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3746 bmap = add_divs(bmap, 2 * n_out);
3748 bmap = isl_basic_map_simplify(bmap);
3749 return isl_basic_map_finalize(bmap);
3750 error:
3751 isl_basic_map_free(bmap);
3752 isl_basic_map_free(bmap1);
3753 isl_basic_map_free(bmap2);
3754 return NULL;
3757 /* Given two maps A -> f(A) and B -> g(B), construct a map
3758 * A \cap B -> f(A) + f(B)
3760 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3762 struct isl_map *result;
3763 int i, j;
3765 if (!map1 || !map2)
3766 goto error;
3768 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3770 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3771 map1->n * map2->n, 0);
3772 if (!result)
3773 goto error;
3774 for (i = 0; i < map1->n; ++i)
3775 for (j = 0; j < map2->n; ++j) {
3776 struct isl_basic_map *part;
3777 part = isl_basic_map_sum(
3778 isl_basic_map_copy(map1->p[i]),
3779 isl_basic_map_copy(map2->p[j]));
3780 if (isl_basic_map_is_empty(part))
3781 isl_basic_map_free(part);
3782 else
3783 result = isl_map_add_basic_map(result, part);
3784 if (!result)
3785 goto error;
3787 isl_map_free(map1);
3788 isl_map_free(map2);
3789 return result;
3790 error:
3791 isl_map_free(map1);
3792 isl_map_free(map2);
3793 return NULL;
3796 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3797 __isl_take isl_set *set2)
3799 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3802 /* Given a basic map A -> f(A), construct A -> -f(A).
3804 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3806 int i, j;
3807 unsigned off, n;
3809 bmap = isl_basic_map_cow(bmap);
3810 if (!bmap)
3811 return NULL;
3813 n = isl_basic_map_dim(bmap, isl_dim_out);
3814 off = isl_basic_map_offset(bmap, isl_dim_out);
3815 for (i = 0; i < bmap->n_eq; ++i)
3816 for (j = 0; j < n; ++j)
3817 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3818 for (i = 0; i < bmap->n_ineq; ++i)
3819 for (j = 0; j < n; ++j)
3820 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3821 for (i = 0; i < bmap->n_div; ++i)
3822 for (j = 0; j < n; ++j)
3823 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3824 bmap = isl_basic_map_gauss(bmap, NULL);
3825 return isl_basic_map_finalize(bmap);
3828 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3830 return isl_basic_map_neg(bset);
3833 /* Given a map A -> f(A), construct A -> -f(A).
3835 struct isl_map *isl_map_neg(struct isl_map *map)
3837 int i;
3839 map = isl_map_cow(map);
3840 if (!map)
3841 return NULL;
3843 for (i = 0; i < map->n; ++i) {
3844 map->p[i] = isl_basic_map_neg(map->p[i]);
3845 if (!map->p[i])
3846 goto error;
3849 return map;
3850 error:
3851 isl_map_free(map);
3852 return NULL;
3855 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3857 return (isl_set *)isl_map_neg((isl_map *)set);
3860 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3861 * A -> floor(f(A)/d).
3863 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3864 isl_int d)
3866 unsigned n_in, n_out, nparam, total, pos;
3867 struct isl_basic_map *result = NULL;
3868 struct isl_dim_map *dim_map;
3869 int i;
3871 if (!bmap)
3872 return NULL;
3874 nparam = isl_basic_map_n_param(bmap);
3875 n_in = isl_basic_map_n_in(bmap);
3876 n_out = isl_basic_map_n_out(bmap);
3878 total = nparam + n_in + n_out + bmap->n_div + n_out;
3879 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3880 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3881 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3882 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3883 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3885 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3886 bmap->n_div + n_out,
3887 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3888 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3889 result = add_divs(result, n_out);
3890 for (i = 0; i < n_out; ++i) {
3891 int j;
3892 j = isl_basic_map_alloc_inequality(result);
3893 if (j < 0)
3894 goto error;
3895 isl_seq_clr(result->ineq[j], 1+total);
3896 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3897 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3898 j = isl_basic_map_alloc_inequality(result);
3899 if (j < 0)
3900 goto error;
3901 isl_seq_clr(result->ineq[j], 1+total);
3902 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3903 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3904 isl_int_sub_ui(result->ineq[j][0], d, 1);
3907 result = isl_basic_map_simplify(result);
3908 return isl_basic_map_finalize(result);
3909 error:
3910 isl_basic_map_free(result);
3911 return NULL;
3914 /* Given a map A -> f(A) and an integer d, construct a map
3915 * A -> floor(f(A)/d).
3917 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3919 int i;
3921 map = isl_map_cow(map);
3922 if (!map)
3923 return NULL;
3925 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3926 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3927 for (i = 0; i < map->n; ++i) {
3928 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3929 if (!map->p[i])
3930 goto error;
3933 return map;
3934 error:
3935 isl_map_free(map);
3936 return NULL;
3939 /* Given a map A -> f(A) and an integer d, construct a map
3940 * A -> floor(f(A)/d).
3942 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3943 __isl_take isl_val *d)
3945 if (!map || !d)
3946 goto error;
3947 if (!isl_val_is_int(d))
3948 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3949 "expecting integer denominator", goto error);
3950 map = isl_map_floordiv(map, d->n);
3951 isl_val_free(d);
3952 return map;
3953 error:
3954 isl_map_free(map);
3955 isl_val_free(d);
3956 return NULL;
3959 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3961 int i;
3962 unsigned nparam;
3963 unsigned n_in;
3965 i = isl_basic_map_alloc_equality(bmap);
3966 if (i < 0)
3967 goto error;
3968 nparam = isl_basic_map_n_param(bmap);
3969 n_in = isl_basic_map_n_in(bmap);
3970 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3971 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3972 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3973 return isl_basic_map_finalize(bmap);
3974 error:
3975 isl_basic_map_free(bmap);
3976 return NULL;
3979 /* Add a constraints to "bmap" expressing i_pos < o_pos
3981 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3983 int i;
3984 unsigned nparam;
3985 unsigned n_in;
3987 i = isl_basic_map_alloc_inequality(bmap);
3988 if (i < 0)
3989 goto error;
3990 nparam = isl_basic_map_n_param(bmap);
3991 n_in = isl_basic_map_n_in(bmap);
3992 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3993 isl_int_set_si(bmap->ineq[i][0], -1);
3994 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3995 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3996 return isl_basic_map_finalize(bmap);
3997 error:
3998 isl_basic_map_free(bmap);
3999 return NULL;
4002 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4004 static __isl_give isl_basic_map *var_less_or_equal(
4005 __isl_take isl_basic_map *bmap, unsigned pos)
4007 int i;
4008 unsigned nparam;
4009 unsigned n_in;
4011 i = isl_basic_map_alloc_inequality(bmap);
4012 if (i < 0)
4013 goto error;
4014 nparam = isl_basic_map_n_param(bmap);
4015 n_in = isl_basic_map_n_in(bmap);
4016 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4017 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4018 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4019 return isl_basic_map_finalize(bmap);
4020 error:
4021 isl_basic_map_free(bmap);
4022 return NULL;
4025 /* Add a constraints to "bmap" expressing i_pos > o_pos
4027 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4029 int i;
4030 unsigned nparam;
4031 unsigned n_in;
4033 i = isl_basic_map_alloc_inequality(bmap);
4034 if (i < 0)
4035 goto error;
4036 nparam = isl_basic_map_n_param(bmap);
4037 n_in = isl_basic_map_n_in(bmap);
4038 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4039 isl_int_set_si(bmap->ineq[i][0], -1);
4040 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4041 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4042 return isl_basic_map_finalize(bmap);
4043 error:
4044 isl_basic_map_free(bmap);
4045 return NULL;
4048 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4050 static __isl_give isl_basic_map *var_more_or_equal(
4051 __isl_take isl_basic_map *bmap, unsigned pos)
4053 int i;
4054 unsigned nparam;
4055 unsigned n_in;
4057 i = isl_basic_map_alloc_inequality(bmap);
4058 if (i < 0)
4059 goto error;
4060 nparam = isl_basic_map_n_param(bmap);
4061 n_in = isl_basic_map_n_in(bmap);
4062 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4063 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4064 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4065 return isl_basic_map_finalize(bmap);
4066 error:
4067 isl_basic_map_free(bmap);
4068 return NULL;
4071 __isl_give isl_basic_map *isl_basic_map_equal(
4072 __isl_take isl_space *dim, unsigned n_equal)
4074 int i;
4075 struct isl_basic_map *bmap;
4076 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4077 if (!bmap)
4078 return NULL;
4079 for (i = 0; i < n_equal && bmap; ++i)
4080 bmap = var_equal(bmap, i);
4081 return isl_basic_map_finalize(bmap);
4084 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4086 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4087 unsigned pos)
4089 int i;
4090 struct isl_basic_map *bmap;
4091 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4092 if (!bmap)
4093 return NULL;
4094 for (i = 0; i < pos && bmap; ++i)
4095 bmap = var_equal(bmap, i);
4096 if (bmap)
4097 bmap = var_less(bmap, pos);
4098 return isl_basic_map_finalize(bmap);
4101 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4103 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4104 __isl_take isl_space *dim, unsigned pos)
4106 int i;
4107 isl_basic_map *bmap;
4109 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4110 for (i = 0; i < pos; ++i)
4111 bmap = var_equal(bmap, i);
4112 bmap = var_less_or_equal(bmap, pos);
4113 return isl_basic_map_finalize(bmap);
4116 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4118 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4119 unsigned pos)
4121 int i;
4122 struct isl_basic_map *bmap;
4123 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4124 if (!bmap)
4125 return NULL;
4126 for (i = 0; i < pos && bmap; ++i)
4127 bmap = var_equal(bmap, i);
4128 if (bmap)
4129 bmap = var_more(bmap, pos);
4130 return isl_basic_map_finalize(bmap);
4133 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4135 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4136 __isl_take isl_space *dim, unsigned pos)
4138 int i;
4139 isl_basic_map *bmap;
4141 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4142 for (i = 0; i < pos; ++i)
4143 bmap = var_equal(bmap, i);
4144 bmap = var_more_or_equal(bmap, pos);
4145 return isl_basic_map_finalize(bmap);
4148 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4149 unsigned n, int equal)
4151 struct isl_map *map;
4152 int i;
4154 if (n == 0 && equal)
4155 return isl_map_universe(dims);
4157 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4159 for (i = 0; i + 1 < n; ++i)
4160 map = isl_map_add_basic_map(map,
4161 isl_basic_map_less_at(isl_space_copy(dims), i));
4162 if (n > 0) {
4163 if (equal)
4164 map = isl_map_add_basic_map(map,
4165 isl_basic_map_less_or_equal_at(dims, n - 1));
4166 else
4167 map = isl_map_add_basic_map(map,
4168 isl_basic_map_less_at(dims, n - 1));
4169 } else
4170 isl_space_free(dims);
4172 return map;
4175 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4177 if (!dims)
4178 return NULL;
4179 return map_lex_lte_first(dims, dims->n_out, equal);
4182 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4184 return map_lex_lte_first(dim, n, 0);
4187 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4189 return map_lex_lte_first(dim, n, 1);
4192 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4194 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4197 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4199 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4202 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4203 unsigned n, int equal)
4205 struct isl_map *map;
4206 int i;
4208 if (n == 0 && equal)
4209 return isl_map_universe(dims);
4211 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4213 for (i = 0; i + 1 < n; ++i)
4214 map = isl_map_add_basic_map(map,
4215 isl_basic_map_more_at(isl_space_copy(dims), i));
4216 if (n > 0) {
4217 if (equal)
4218 map = isl_map_add_basic_map(map,
4219 isl_basic_map_more_or_equal_at(dims, n - 1));
4220 else
4221 map = isl_map_add_basic_map(map,
4222 isl_basic_map_more_at(dims, n - 1));
4223 } else
4224 isl_space_free(dims);
4226 return map;
4229 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4231 if (!dims)
4232 return NULL;
4233 return map_lex_gte_first(dims, dims->n_out, equal);
4236 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4238 return map_lex_gte_first(dim, n, 0);
4241 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4243 return map_lex_gte_first(dim, n, 1);
4246 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4248 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4251 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4253 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4256 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4257 __isl_take isl_set *set2)
4259 isl_map *map;
4260 map = isl_map_lex_le(isl_set_get_space(set1));
4261 map = isl_map_intersect_domain(map, set1);
4262 map = isl_map_intersect_range(map, set2);
4263 return map;
4266 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4267 __isl_take isl_set *set2)
4269 isl_map *map;
4270 map = isl_map_lex_lt(isl_set_get_space(set1));
4271 map = isl_map_intersect_domain(map, set1);
4272 map = isl_map_intersect_range(map, set2);
4273 return map;
4276 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4277 __isl_take isl_set *set2)
4279 isl_map *map;
4280 map = isl_map_lex_ge(isl_set_get_space(set1));
4281 map = isl_map_intersect_domain(map, set1);
4282 map = isl_map_intersect_range(map, set2);
4283 return map;
4286 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4287 __isl_take isl_set *set2)
4289 isl_map *map;
4290 map = isl_map_lex_gt(isl_set_get_space(set1));
4291 map = isl_map_intersect_domain(map, set1);
4292 map = isl_map_intersect_range(map, set2);
4293 return map;
4296 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4297 __isl_take isl_map *map2)
4299 isl_map *map;
4300 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4301 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4302 map = isl_map_apply_range(map, isl_map_reverse(map2));
4303 return map;
4306 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4307 __isl_take isl_map *map2)
4309 isl_map *map;
4310 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4311 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4312 map = isl_map_apply_range(map, isl_map_reverse(map2));
4313 return map;
4316 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4317 __isl_take isl_map *map2)
4319 isl_map *map;
4320 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4321 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4322 map = isl_map_apply_range(map, isl_map_reverse(map2));
4323 return map;
4326 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4327 __isl_take isl_map *map2)
4329 isl_map *map;
4330 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4331 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4332 map = isl_map_apply_range(map, isl_map_reverse(map2));
4333 return map;
4336 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4337 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4339 struct isl_basic_map *bmap;
4341 bset = isl_basic_set_cow(bset);
4342 if (!bset || !dim)
4343 goto error;
4345 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4346 isl_space_free(bset->dim);
4347 bmap = (struct isl_basic_map *) bset;
4348 bmap->dim = dim;
4349 return isl_basic_map_finalize(bmap);
4350 error:
4351 isl_basic_set_free(bset);
4352 isl_space_free(dim);
4353 return NULL;
4356 /* For a div d = floor(f/m), add the constraint
4358 * f - m d >= 0
4360 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4361 unsigned pos, isl_int *div)
4363 int i;
4364 unsigned total = isl_basic_map_total_dim(bmap);
4366 i = isl_basic_map_alloc_inequality(bmap);
4367 if (i < 0)
4368 return -1;
4369 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4370 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4372 return 0;
4375 /* For a div d = floor(f/m), add the constraint
4377 * -(f-(n-1)) + m d >= 0
4379 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4380 unsigned pos, isl_int *div)
4382 int i;
4383 unsigned total = isl_basic_map_total_dim(bmap);
4385 i = isl_basic_map_alloc_inequality(bmap);
4386 if (i < 0)
4387 return -1;
4388 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4389 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4390 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4391 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4393 return 0;
4396 /* For a div d = floor(f/m), add the constraints
4398 * f - m d >= 0
4399 * -(f-(n-1)) + m d >= 0
4401 * Note that the second constraint is the negation of
4403 * f - m d >= n
4405 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4406 unsigned pos, isl_int *div)
4408 if (add_upper_div_constraint(bmap, pos, div) < 0)
4409 return -1;
4410 if (add_lower_div_constraint(bmap, pos, div) < 0)
4411 return -1;
4412 return 0;
4415 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4416 unsigned pos, isl_int *div)
4418 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4419 pos, div);
4422 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4424 unsigned total = isl_basic_map_total_dim(bmap);
4425 unsigned div_pos = total - bmap->n_div + div;
4427 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4428 bmap->div[div]);
4431 /* For each known div d = floor(f/m), add the constraints
4433 * f - m d >= 0
4434 * -(f-(n-1)) + m d >= 0
4436 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4437 __isl_take isl_basic_map *bmap)
4439 int i;
4440 unsigned n_div;
4442 if (!bmap)
4443 return NULL;
4444 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4445 if (n_div == 0)
4446 return bmap;
4447 bmap = isl_basic_map_cow(bmap);
4448 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4449 if (!bmap)
4450 return NULL;
4451 for (i = 0; i < n_div; ++i) {
4452 if (isl_int_is_zero(bmap->div[i][0]))
4453 continue;
4454 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4455 return isl_basic_map_free(bmap);
4458 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4459 bmap = isl_basic_map_finalize(bmap);
4460 return bmap;
4463 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4465 * In particular, if this div is of the form d = floor(f/m),
4466 * then add the constraint
4468 * f - m d >= 0
4470 * if sign < 0 or the constraint
4472 * -(f-(n-1)) + m d >= 0
4474 * if sign > 0.
4476 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4477 unsigned div, int sign)
4479 unsigned total;
4480 unsigned div_pos;
4482 if (!bmap)
4483 return -1;
4485 total = isl_basic_map_total_dim(bmap);
4486 div_pos = total - bmap->n_div + div;
4488 if (sign < 0)
4489 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4490 else
4491 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4494 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4496 return isl_basic_map_add_div_constraints(bset, div);
4499 struct isl_basic_set *isl_basic_map_underlying_set(
4500 struct isl_basic_map *bmap)
4502 if (!bmap)
4503 goto error;
4504 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4505 bmap->n_div == 0 &&
4506 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4507 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4508 return (struct isl_basic_set *)bmap;
4509 bmap = isl_basic_map_cow(bmap);
4510 if (!bmap)
4511 goto error;
4512 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4513 if (!bmap->dim)
4514 goto error;
4515 bmap->extra -= bmap->n_div;
4516 bmap->n_div = 0;
4517 bmap = isl_basic_map_finalize(bmap);
4518 return (struct isl_basic_set *)bmap;
4519 error:
4520 isl_basic_map_free(bmap);
4521 return NULL;
4524 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4525 __isl_take isl_basic_set *bset)
4527 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4530 /* Replace each element in "list" by the result of applying
4531 * isl_basic_map_underlying_set to the element.
4533 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4534 __isl_take isl_basic_map_list *list)
4536 int i, n;
4538 if (!list)
4539 return NULL;
4541 n = isl_basic_map_list_n_basic_map(list);
4542 for (i = 0; i < n; ++i) {
4543 isl_basic_map *bmap;
4544 isl_basic_set *bset;
4546 bmap = isl_basic_map_list_get_basic_map(list, i);
4547 bset = isl_basic_set_underlying_set(bmap);
4548 list = isl_basic_set_list_set_basic_set(list, i, bset);
4551 return list;
4554 struct isl_basic_map *isl_basic_map_overlying_set(
4555 struct isl_basic_set *bset, struct isl_basic_map *like)
4557 struct isl_basic_map *bmap;
4558 struct isl_ctx *ctx;
4559 unsigned total;
4560 int i;
4562 if (!bset || !like)
4563 goto error;
4564 ctx = bset->ctx;
4565 isl_assert(ctx, bset->n_div == 0, goto error);
4566 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4567 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4568 goto error);
4569 if (like->n_div == 0) {
4570 isl_space *space = isl_basic_map_get_space(like);
4571 isl_basic_map_free(like);
4572 return isl_basic_map_reset_space(bset, space);
4574 bset = isl_basic_set_cow(bset);
4575 if (!bset)
4576 goto error;
4577 total = bset->dim->n_out + bset->extra;
4578 bmap = (struct isl_basic_map *)bset;
4579 isl_space_free(bmap->dim);
4580 bmap->dim = isl_space_copy(like->dim);
4581 if (!bmap->dim)
4582 goto error;
4583 bmap->n_div = like->n_div;
4584 bmap->extra += like->n_div;
4585 if (bmap->extra) {
4586 unsigned ltotal;
4587 isl_int **div;
4588 ltotal = total - bmap->extra + like->extra;
4589 if (ltotal > total)
4590 ltotal = total;
4591 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4592 bmap->extra * (1 + 1 + total));
4593 if (isl_blk_is_error(bmap->block2))
4594 goto error;
4595 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4596 if (!div)
4597 goto error;
4598 bmap->div = div;
4599 for (i = 0; i < bmap->extra; ++i)
4600 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4601 for (i = 0; i < like->n_div; ++i) {
4602 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4603 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4605 bmap = isl_basic_map_add_known_div_constraints(bmap);
4607 isl_basic_map_free(like);
4608 bmap = isl_basic_map_simplify(bmap);
4609 bmap = isl_basic_map_finalize(bmap);
4610 return bmap;
4611 error:
4612 isl_basic_map_free(like);
4613 isl_basic_set_free(bset);
4614 return NULL;
4617 struct isl_basic_set *isl_basic_set_from_underlying_set(
4618 struct isl_basic_set *bset, struct isl_basic_set *like)
4620 return (struct isl_basic_set *)
4621 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4624 struct isl_set *isl_set_from_underlying_set(
4625 struct isl_set *set, struct isl_basic_set *like)
4627 int i;
4629 if (!set || !like)
4630 goto error;
4631 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4632 goto error);
4633 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4634 isl_basic_set_free(like);
4635 return set;
4637 set = isl_set_cow(set);
4638 if (!set)
4639 goto error;
4640 for (i = 0; i < set->n; ++i) {
4641 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4642 isl_basic_set_copy(like));
4643 if (!set->p[i])
4644 goto error;
4646 isl_space_free(set->dim);
4647 set->dim = isl_space_copy(like->dim);
4648 if (!set->dim)
4649 goto error;
4650 isl_basic_set_free(like);
4651 return set;
4652 error:
4653 isl_basic_set_free(like);
4654 isl_set_free(set);
4655 return NULL;
4658 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4660 int i;
4662 map = isl_map_cow(map);
4663 if (!map)
4664 return NULL;
4665 map->dim = isl_space_cow(map->dim);
4666 if (!map->dim)
4667 goto error;
4669 for (i = 1; i < map->n; ++i)
4670 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4671 goto error);
4672 for (i = 0; i < map->n; ++i) {
4673 map->p[i] = (struct isl_basic_map *)
4674 isl_basic_map_underlying_set(map->p[i]);
4675 if (!map->p[i])
4676 goto error;
4678 if (map->n == 0)
4679 map->dim = isl_space_underlying(map->dim, 0);
4680 else {
4681 isl_space_free(map->dim);
4682 map->dim = isl_space_copy(map->p[0]->dim);
4684 if (!map->dim)
4685 goto error;
4686 return (struct isl_set *)map;
4687 error:
4688 isl_map_free(map);
4689 return NULL;
4692 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4694 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4697 __isl_give isl_basic_map *isl_basic_map_reset_space(
4698 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4700 isl_bool equal;
4702 if (!bmap)
4703 goto error;
4704 equal = isl_space_is_equal(bmap->dim, space);
4705 if (equal < 0)
4706 goto error;
4707 if (equal) {
4708 isl_space_free(space);
4709 return bmap;
4711 bmap = isl_basic_map_cow(bmap);
4712 if (!bmap || !space)
4713 goto error;
4715 isl_space_free(bmap->dim);
4716 bmap->dim = space;
4718 bmap = isl_basic_map_finalize(bmap);
4720 return bmap;
4721 error:
4722 isl_basic_map_free(bmap);
4723 isl_space_free(space);
4724 return NULL;
4727 __isl_give isl_basic_set *isl_basic_set_reset_space(
4728 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4730 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4731 dim);
4734 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4735 __isl_take isl_space *dim)
4737 int i;
4739 map = isl_map_cow(map);
4740 if (!map || !dim)
4741 goto error;
4743 for (i = 0; i < map->n; ++i) {
4744 map->p[i] = isl_basic_map_reset_space(map->p[i],
4745 isl_space_copy(dim));
4746 if (!map->p[i])
4747 goto error;
4749 isl_space_free(map->dim);
4750 map->dim = dim;
4752 return map;
4753 error:
4754 isl_map_free(map);
4755 isl_space_free(dim);
4756 return NULL;
4759 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4760 __isl_take isl_space *dim)
4762 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4765 /* Compute the parameter domain of the given basic set.
4767 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4769 isl_space *space;
4770 unsigned n;
4772 if (isl_basic_set_is_params(bset))
4773 return bset;
4775 n = isl_basic_set_dim(bset, isl_dim_set);
4776 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4777 space = isl_basic_set_get_space(bset);
4778 space = isl_space_params(space);
4779 bset = isl_basic_set_reset_space(bset, space);
4780 return bset;
4783 /* Construct a zero-dimensional basic set with the given parameter domain.
4785 __isl_give isl_basic_set *isl_basic_set_from_params(
4786 __isl_take isl_basic_set *bset)
4788 isl_space *space;
4789 space = isl_basic_set_get_space(bset);
4790 space = isl_space_set_from_params(space);
4791 bset = isl_basic_set_reset_space(bset, space);
4792 return bset;
4795 /* Compute the parameter domain of the given set.
4797 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4799 isl_space *space;
4800 unsigned n;
4802 if (isl_set_is_params(set))
4803 return set;
4805 n = isl_set_dim(set, isl_dim_set);
4806 set = isl_set_project_out(set, isl_dim_set, 0, n);
4807 space = isl_set_get_space(set);
4808 space = isl_space_params(space);
4809 set = isl_set_reset_space(set, space);
4810 return set;
4813 /* Construct a zero-dimensional set with the given parameter domain.
4815 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4817 isl_space *space;
4818 space = isl_set_get_space(set);
4819 space = isl_space_set_from_params(space);
4820 set = isl_set_reset_space(set, space);
4821 return set;
4824 /* Compute the parameter domain of the given map.
4826 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4828 isl_space *space;
4829 unsigned n;
4831 n = isl_map_dim(map, isl_dim_in);
4832 map = isl_map_project_out(map, isl_dim_in, 0, n);
4833 n = isl_map_dim(map, isl_dim_out);
4834 map = isl_map_project_out(map, isl_dim_out, 0, n);
4835 space = isl_map_get_space(map);
4836 space = isl_space_params(space);
4837 map = isl_map_reset_space(map, space);
4838 return map;
4841 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4843 isl_space *space;
4844 unsigned n_out;
4846 if (!bmap)
4847 return NULL;
4848 space = isl_space_domain(isl_basic_map_get_space(bmap));
4850 n_out = isl_basic_map_n_out(bmap);
4851 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
4853 return isl_basic_map_reset_space(bmap, space);
4856 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4858 if (!bmap)
4859 return -1;
4860 return isl_space_may_be_set(bmap->dim);
4863 /* Is this basic map actually a set?
4864 * Users should never call this function. Outside of isl,
4865 * the type should indicate whether something is a set or a map.
4867 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4869 if (!bmap)
4870 return -1;
4871 return isl_space_is_set(bmap->dim);
4874 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4876 if (!bmap)
4877 return NULL;
4878 if (isl_basic_map_is_set(bmap))
4879 return bmap;
4880 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4883 __isl_give isl_basic_map *isl_basic_map_domain_map(
4884 __isl_take isl_basic_map *bmap)
4886 int i, k;
4887 isl_space *dim;
4888 isl_basic_map *domain;
4889 int nparam, n_in, n_out;
4890 unsigned total;
4892 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4893 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4894 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4896 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4897 domain = isl_basic_map_universe(dim);
4899 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4900 bmap = isl_basic_map_apply_range(bmap, domain);
4901 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4903 total = isl_basic_map_total_dim(bmap);
4905 for (i = 0; i < n_in; ++i) {
4906 k = isl_basic_map_alloc_equality(bmap);
4907 if (k < 0)
4908 goto error;
4909 isl_seq_clr(bmap->eq[k], 1 + total);
4910 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4911 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4914 bmap = isl_basic_map_gauss(bmap, NULL);
4915 return isl_basic_map_finalize(bmap);
4916 error:
4917 isl_basic_map_free(bmap);
4918 return NULL;
4921 __isl_give isl_basic_map *isl_basic_map_range_map(
4922 __isl_take isl_basic_map *bmap)
4924 int i, k;
4925 isl_space *dim;
4926 isl_basic_map *range;
4927 int nparam, n_in, n_out;
4928 unsigned total;
4930 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4931 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4932 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4934 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4935 range = isl_basic_map_universe(dim);
4937 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4938 bmap = isl_basic_map_apply_range(bmap, range);
4939 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4941 total = isl_basic_map_total_dim(bmap);
4943 for (i = 0; i < n_out; ++i) {
4944 k = isl_basic_map_alloc_equality(bmap);
4945 if (k < 0)
4946 goto error;
4947 isl_seq_clr(bmap->eq[k], 1 + total);
4948 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4949 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4952 bmap = isl_basic_map_gauss(bmap, NULL);
4953 return isl_basic_map_finalize(bmap);
4954 error:
4955 isl_basic_map_free(bmap);
4956 return NULL;
4959 int isl_map_may_be_set(__isl_keep isl_map *map)
4961 if (!map)
4962 return -1;
4963 return isl_space_may_be_set(map->dim);
4966 /* Is this map actually a set?
4967 * Users should never call this function. Outside of isl,
4968 * the type should indicate whether something is a set or a map.
4970 int isl_map_is_set(__isl_keep isl_map *map)
4972 if (!map)
4973 return -1;
4974 return isl_space_is_set(map->dim);
4977 struct isl_set *isl_map_range(struct isl_map *map)
4979 int i;
4980 struct isl_set *set;
4982 if (!map)
4983 goto error;
4984 if (isl_map_is_set(map))
4985 return (isl_set *)map;
4987 map = isl_map_cow(map);
4988 if (!map)
4989 goto error;
4991 set = (struct isl_set *) map;
4992 set->dim = isl_space_range(set->dim);
4993 if (!set->dim)
4994 goto error;
4995 for (i = 0; i < map->n; ++i) {
4996 set->p[i] = isl_basic_map_range(map->p[i]);
4997 if (!set->p[i])
4998 goto error;
5000 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5001 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5002 return set;
5003 error:
5004 isl_map_free(map);
5005 return NULL;
5008 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5010 int i;
5012 map = isl_map_cow(map);
5013 if (!map)
5014 return NULL;
5016 map->dim = isl_space_domain_map(map->dim);
5017 if (!map->dim)
5018 goto error;
5019 for (i = 0; i < map->n; ++i) {
5020 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5021 if (!map->p[i])
5022 goto error;
5024 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5025 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5026 return map;
5027 error:
5028 isl_map_free(map);
5029 return NULL;
5032 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5034 int i;
5035 isl_space *range_dim;
5037 map = isl_map_cow(map);
5038 if (!map)
5039 return NULL;
5041 range_dim = isl_space_range(isl_map_get_space(map));
5042 range_dim = isl_space_from_range(range_dim);
5043 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5044 map->dim = isl_space_join(map->dim, range_dim);
5045 if (!map->dim)
5046 goto error;
5047 for (i = 0; i < map->n; ++i) {
5048 map->p[i] = isl_basic_map_range_map(map->p[i]);
5049 if (!map->p[i])
5050 goto error;
5052 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5053 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5054 return map;
5055 error:
5056 isl_map_free(map);
5057 return NULL;
5060 /* Given a wrapped map of the form A[B -> C],
5061 * return the map A[B -> C] -> B.
5063 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5065 isl_id *id;
5066 isl_map *map;
5068 if (!set)
5069 return NULL;
5070 if (!isl_set_has_tuple_id(set))
5071 return isl_map_domain_map(isl_set_unwrap(set));
5073 id = isl_set_get_tuple_id(set);
5074 map = isl_map_domain_map(isl_set_unwrap(set));
5075 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5077 return map;
5080 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5081 __isl_take isl_space *dim)
5083 int i;
5084 struct isl_map *map = NULL;
5086 set = isl_set_cow(set);
5087 if (!set || !dim)
5088 goto error;
5089 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5090 map = (struct isl_map *)set;
5091 for (i = 0; i < set->n; ++i) {
5092 map->p[i] = isl_basic_map_from_basic_set(
5093 set->p[i], isl_space_copy(dim));
5094 if (!map->p[i])
5095 goto error;
5097 isl_space_free(map->dim);
5098 map->dim = dim;
5099 return map;
5100 error:
5101 isl_space_free(dim);
5102 isl_set_free(set);
5103 return NULL;
5106 __isl_give isl_basic_map *isl_basic_map_from_domain(
5107 __isl_take isl_basic_set *bset)
5109 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5112 __isl_give isl_basic_map *isl_basic_map_from_range(
5113 __isl_take isl_basic_set *bset)
5115 isl_space *space;
5116 space = isl_basic_set_get_space(bset);
5117 space = isl_space_from_range(space);
5118 bset = isl_basic_set_reset_space(bset, space);
5119 return (isl_basic_map *)bset;
5122 /* Create a relation with the given set as range.
5123 * The domain of the created relation is a zero-dimensional
5124 * flat anonymous space.
5126 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5128 isl_space *space;
5129 space = isl_set_get_space(set);
5130 space = isl_space_from_range(space);
5131 set = isl_set_reset_space(set, space);
5132 return (struct isl_map *)set;
5135 /* Create a relation with the given set as domain.
5136 * The range of the created relation is a zero-dimensional
5137 * flat anonymous space.
5139 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5141 return isl_map_reverse(isl_map_from_range(set));
5144 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5145 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5147 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5150 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5151 __isl_take isl_set *range)
5153 return isl_map_apply_range(isl_map_reverse(domain), range);
5156 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5157 unsigned flags)
5159 struct isl_map *map;
5161 if (!dim)
5162 return NULL;
5163 if (n < 0)
5164 isl_die(dim->ctx, isl_error_internal,
5165 "negative number of basic maps", goto error);
5166 map = isl_alloc(dim->ctx, struct isl_map,
5167 sizeof(struct isl_map) +
5168 (n - 1) * sizeof(struct isl_basic_map *));
5169 if (!map)
5170 goto error;
5172 map->ctx = dim->ctx;
5173 isl_ctx_ref(map->ctx);
5174 map->ref = 1;
5175 map->size = n;
5176 map->n = 0;
5177 map->dim = dim;
5178 map->flags = flags;
5179 return map;
5180 error:
5181 isl_space_free(dim);
5182 return NULL;
5185 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5186 unsigned nparam, unsigned in, unsigned out, int n,
5187 unsigned flags)
5189 struct isl_map *map;
5190 isl_space *dims;
5192 dims = isl_space_alloc(ctx, nparam, in, out);
5193 if (!dims)
5194 return NULL;
5196 map = isl_map_alloc_space(dims, n, flags);
5197 return map;
5200 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5202 struct isl_basic_map *bmap;
5203 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5204 bmap = isl_basic_map_set_to_empty(bmap);
5205 return bmap;
5208 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5210 struct isl_basic_set *bset;
5211 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5212 bset = isl_basic_set_set_to_empty(bset);
5213 return bset;
5216 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5218 struct isl_basic_map *bmap;
5219 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5220 bmap = isl_basic_map_finalize(bmap);
5221 return bmap;
5224 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5226 struct isl_basic_set *bset;
5227 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5228 bset = isl_basic_set_finalize(bset);
5229 return bset;
5232 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5234 int i;
5235 unsigned total = isl_space_dim(dim, isl_dim_all);
5236 isl_basic_map *bmap;
5238 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5239 for (i = 0; i < total; ++i) {
5240 int k = isl_basic_map_alloc_inequality(bmap);
5241 if (k < 0)
5242 goto error;
5243 isl_seq_clr(bmap->ineq[k], 1 + total);
5244 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5246 return bmap;
5247 error:
5248 isl_basic_map_free(bmap);
5249 return NULL;
5252 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5254 return isl_basic_map_nat_universe(dim);
5257 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5259 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5262 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5264 return isl_map_nat_universe(dim);
5267 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5269 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5272 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5274 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5277 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5279 struct isl_map *map;
5280 if (!dim)
5281 return NULL;
5282 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5283 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5284 return map;
5287 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5289 struct isl_set *set;
5290 if (!dim)
5291 return NULL;
5292 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5293 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5294 return set;
5297 struct isl_map *isl_map_dup(struct isl_map *map)
5299 int i;
5300 struct isl_map *dup;
5302 if (!map)
5303 return NULL;
5304 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5305 for (i = 0; i < map->n; ++i)
5306 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5307 return dup;
5310 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5311 __isl_take isl_basic_map *bmap)
5313 if (!bmap || !map)
5314 goto error;
5315 if (isl_basic_map_plain_is_empty(bmap)) {
5316 isl_basic_map_free(bmap);
5317 return map;
5319 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5320 isl_assert(map->ctx, map->n < map->size, goto error);
5321 map->p[map->n] = bmap;
5322 map->n++;
5323 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5324 return map;
5325 error:
5326 if (map)
5327 isl_map_free(map);
5328 if (bmap)
5329 isl_basic_map_free(bmap);
5330 return NULL;
5333 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5335 int i;
5337 if (!map)
5338 return NULL;
5340 if (--map->ref > 0)
5341 return NULL;
5343 isl_ctx_deref(map->ctx);
5344 for (i = 0; i < map->n; ++i)
5345 isl_basic_map_free(map->p[i]);
5346 isl_space_free(map->dim);
5347 free(map);
5349 return NULL;
5352 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5353 struct isl_basic_map *bmap, unsigned pos, int value)
5355 int j;
5357 bmap = isl_basic_map_cow(bmap);
5358 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5359 j = isl_basic_map_alloc_equality(bmap);
5360 if (j < 0)
5361 goto error;
5362 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5363 isl_int_set_si(bmap->eq[j][pos], -1);
5364 isl_int_set_si(bmap->eq[j][0], value);
5365 bmap = isl_basic_map_simplify(bmap);
5366 return isl_basic_map_finalize(bmap);
5367 error:
5368 isl_basic_map_free(bmap);
5369 return NULL;
5372 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5373 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5375 int j;
5377 bmap = isl_basic_map_cow(bmap);
5378 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5379 j = isl_basic_map_alloc_equality(bmap);
5380 if (j < 0)
5381 goto error;
5382 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5383 isl_int_set_si(bmap->eq[j][pos], -1);
5384 isl_int_set(bmap->eq[j][0], value);
5385 bmap = isl_basic_map_simplify(bmap);
5386 return isl_basic_map_finalize(bmap);
5387 error:
5388 isl_basic_map_free(bmap);
5389 return NULL;
5392 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5393 enum isl_dim_type type, unsigned pos, int value)
5395 if (!bmap)
5396 return NULL;
5397 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5398 return isl_basic_map_fix_pos_si(bmap,
5399 isl_basic_map_offset(bmap, type) + pos, value);
5400 error:
5401 isl_basic_map_free(bmap);
5402 return NULL;
5405 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5406 enum isl_dim_type type, unsigned pos, isl_int value)
5408 if (!bmap)
5409 return NULL;
5410 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5411 return isl_basic_map_fix_pos(bmap,
5412 isl_basic_map_offset(bmap, type) + pos, value);
5413 error:
5414 isl_basic_map_free(bmap);
5415 return NULL;
5418 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5419 * to be equal to "v".
5421 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5422 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5424 if (!bmap || !v)
5425 goto error;
5426 if (!isl_val_is_int(v))
5427 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5428 "expecting integer value", goto error);
5429 if (pos >= isl_basic_map_dim(bmap, type))
5430 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5431 "index out of bounds", goto error);
5432 pos += isl_basic_map_offset(bmap, type);
5433 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5434 isl_val_free(v);
5435 return bmap;
5436 error:
5437 isl_basic_map_free(bmap);
5438 isl_val_free(v);
5439 return NULL;
5442 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5443 * to be equal to "v".
5445 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5446 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5448 return isl_basic_map_fix_val(bset, type, pos, v);
5451 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5452 enum isl_dim_type type, unsigned pos, int value)
5454 return (struct isl_basic_set *)
5455 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5456 type, pos, value);
5459 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5460 enum isl_dim_type type, unsigned pos, isl_int value)
5462 return (struct isl_basic_set *)
5463 isl_basic_map_fix((struct isl_basic_map *)bset,
5464 type, pos, value);
5467 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5468 unsigned input, int value)
5470 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5473 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5474 unsigned dim, int value)
5476 return (struct isl_basic_set *)
5477 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5478 isl_dim_set, dim, value);
5481 static int remove_if_empty(__isl_keep isl_map *map, int i)
5483 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5485 if (empty < 0)
5486 return -1;
5487 if (!empty)
5488 return 0;
5490 isl_basic_map_free(map->p[i]);
5491 if (i != map->n - 1) {
5492 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5493 map->p[i] = map->p[map->n - 1];
5495 map->n--;
5497 return 0;
5500 /* Perform "fn" on each basic map of "map", where we may not be holding
5501 * the only reference to "map".
5502 * In particular, "fn" should be a semantics preserving operation
5503 * that we want to apply to all copies of "map". We therefore need
5504 * to be careful not to modify "map" in a way that breaks "map"
5505 * in case anything goes wrong.
5507 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5508 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5510 struct isl_basic_map *bmap;
5511 int i;
5513 if (!map)
5514 return NULL;
5516 for (i = map->n - 1; i >= 0; --i) {
5517 bmap = isl_basic_map_copy(map->p[i]);
5518 bmap = fn(bmap);
5519 if (!bmap)
5520 goto error;
5521 isl_basic_map_free(map->p[i]);
5522 map->p[i] = bmap;
5523 if (remove_if_empty(map, i) < 0)
5524 goto error;
5527 return map;
5528 error:
5529 isl_map_free(map);
5530 return NULL;
5533 struct isl_map *isl_map_fix_si(struct isl_map *map,
5534 enum isl_dim_type type, unsigned pos, int value)
5536 int i;
5538 map = isl_map_cow(map);
5539 if (!map)
5540 return NULL;
5542 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5543 for (i = map->n - 1; i >= 0; --i) {
5544 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5545 if (remove_if_empty(map, i) < 0)
5546 goto error;
5548 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5549 return map;
5550 error:
5551 isl_map_free(map);
5552 return NULL;
5555 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5556 enum isl_dim_type type, unsigned pos, int value)
5558 return (struct isl_set *)
5559 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5562 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5563 enum isl_dim_type type, unsigned pos, isl_int value)
5565 int i;
5567 map = isl_map_cow(map);
5568 if (!map)
5569 return NULL;
5571 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5572 for (i = 0; i < map->n; ++i) {
5573 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5574 if (!map->p[i])
5575 goto error;
5577 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5578 return map;
5579 error:
5580 isl_map_free(map);
5581 return NULL;
5584 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5585 enum isl_dim_type type, unsigned pos, isl_int value)
5587 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5590 /* Fix the value of the variable at position "pos" of type "type" of "map"
5591 * to be equal to "v".
5593 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5594 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5596 int i;
5598 map = isl_map_cow(map);
5599 if (!map || !v)
5600 goto error;
5602 if (!isl_val_is_int(v))
5603 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5604 "expecting integer value", goto error);
5605 if (pos >= isl_map_dim(map, type))
5606 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5607 "index out of bounds", goto error);
5608 for (i = map->n - 1; i >= 0; --i) {
5609 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5610 isl_val_copy(v));
5611 if (remove_if_empty(map, i) < 0)
5612 goto error;
5614 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5615 isl_val_free(v);
5616 return map;
5617 error:
5618 isl_map_free(map);
5619 isl_val_free(v);
5620 return NULL;
5623 /* Fix the value of the variable at position "pos" of type "type" of "set"
5624 * to be equal to "v".
5626 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5627 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5629 return isl_map_fix_val(set, type, pos, v);
5632 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5633 unsigned input, int value)
5635 return isl_map_fix_si(map, isl_dim_in, input, value);
5638 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5640 return (struct isl_set *)
5641 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5644 static __isl_give isl_basic_map *basic_map_bound_si(
5645 __isl_take isl_basic_map *bmap,
5646 enum isl_dim_type type, unsigned pos, int value, int upper)
5648 int j;
5650 if (!bmap)
5651 return NULL;
5652 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5653 pos += isl_basic_map_offset(bmap, type);
5654 bmap = isl_basic_map_cow(bmap);
5655 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5656 j = isl_basic_map_alloc_inequality(bmap);
5657 if (j < 0)
5658 goto error;
5659 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5660 if (upper) {
5661 isl_int_set_si(bmap->ineq[j][pos], -1);
5662 isl_int_set_si(bmap->ineq[j][0], value);
5663 } else {
5664 isl_int_set_si(bmap->ineq[j][pos], 1);
5665 isl_int_set_si(bmap->ineq[j][0], -value);
5667 bmap = isl_basic_map_simplify(bmap);
5668 return isl_basic_map_finalize(bmap);
5669 error:
5670 isl_basic_map_free(bmap);
5671 return NULL;
5674 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5675 __isl_take isl_basic_map *bmap,
5676 enum isl_dim_type type, unsigned pos, int value)
5678 return basic_map_bound_si(bmap, type, pos, value, 0);
5681 /* Constrain the values of the given dimension to be no greater than "value".
5683 __isl_give isl_basic_map *isl_basic_map_upper_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, 1);
5690 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5691 unsigned dim, isl_int value)
5693 int j;
5695 bset = isl_basic_set_cow(bset);
5696 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5697 j = isl_basic_set_alloc_inequality(bset);
5698 if (j < 0)
5699 goto error;
5700 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5701 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5702 isl_int_neg(bset->ineq[j][0], value);
5703 bset = isl_basic_set_simplify(bset);
5704 return isl_basic_set_finalize(bset);
5705 error:
5706 isl_basic_set_free(bset);
5707 return NULL;
5710 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5711 enum isl_dim_type type, unsigned pos, int value, int upper)
5713 int i;
5715 map = isl_map_cow(map);
5716 if (!map)
5717 return NULL;
5719 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5720 for (i = 0; i < map->n; ++i) {
5721 map->p[i] = basic_map_bound_si(map->p[i],
5722 type, pos, value, upper);
5723 if (!map->p[i])
5724 goto error;
5726 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5727 return map;
5728 error:
5729 isl_map_free(map);
5730 return NULL;
5733 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5734 enum isl_dim_type type, unsigned pos, int value)
5736 return map_bound_si(map, type, pos, value, 0);
5739 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5740 enum isl_dim_type type, unsigned pos, int value)
5742 return map_bound_si(map, type, pos, value, 1);
5745 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5746 enum isl_dim_type type, unsigned pos, int value)
5748 return (struct isl_set *)
5749 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5752 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5753 enum isl_dim_type type, unsigned pos, int value)
5755 return isl_map_upper_bound_si(set, type, pos, value);
5758 /* Bound the given variable of "bmap" from below (or above is "upper"
5759 * is set) to "value".
5761 static __isl_give isl_basic_map *basic_map_bound(
5762 __isl_take isl_basic_map *bmap,
5763 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5765 int j;
5767 if (!bmap)
5768 return NULL;
5769 if (pos >= isl_basic_map_dim(bmap, type))
5770 isl_die(bmap->ctx, isl_error_invalid,
5771 "index out of bounds", goto error);
5772 pos += isl_basic_map_offset(bmap, type);
5773 bmap = isl_basic_map_cow(bmap);
5774 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5775 j = isl_basic_map_alloc_inequality(bmap);
5776 if (j < 0)
5777 goto error;
5778 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5779 if (upper) {
5780 isl_int_set_si(bmap->ineq[j][pos], -1);
5781 isl_int_set(bmap->ineq[j][0], value);
5782 } else {
5783 isl_int_set_si(bmap->ineq[j][pos], 1);
5784 isl_int_neg(bmap->ineq[j][0], value);
5786 bmap = isl_basic_map_simplify(bmap);
5787 return isl_basic_map_finalize(bmap);
5788 error:
5789 isl_basic_map_free(bmap);
5790 return NULL;
5793 /* Bound the given variable of "map" from below (or above is "upper"
5794 * is set) to "value".
5796 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5797 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5799 int i;
5801 map = isl_map_cow(map);
5802 if (!map)
5803 return NULL;
5805 if (pos >= isl_map_dim(map, type))
5806 isl_die(map->ctx, isl_error_invalid,
5807 "index out of bounds", goto error);
5808 for (i = map->n - 1; i >= 0; --i) {
5809 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5810 if (remove_if_empty(map, i) < 0)
5811 goto error;
5813 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5814 return map;
5815 error:
5816 isl_map_free(map);
5817 return NULL;
5820 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5821 enum isl_dim_type type, unsigned pos, isl_int value)
5823 return map_bound(map, type, pos, value, 0);
5826 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5827 enum isl_dim_type type, unsigned pos, isl_int value)
5829 return map_bound(map, type, pos, value, 1);
5832 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5833 enum isl_dim_type type, unsigned pos, isl_int value)
5835 return isl_map_lower_bound(set, type, pos, value);
5838 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5839 enum isl_dim_type type, unsigned pos, isl_int value)
5841 return isl_map_upper_bound(set, type, pos, value);
5844 /* Force the values of the variable at position "pos" of type "type" of "set"
5845 * to be no smaller than "value".
5847 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5848 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5850 if (!value)
5851 goto error;
5852 if (!isl_val_is_int(value))
5853 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5854 "expecting integer value", goto error);
5855 set = isl_set_lower_bound(set, type, pos, value->n);
5856 isl_val_free(value);
5857 return set;
5858 error:
5859 isl_val_free(value);
5860 isl_set_free(set);
5861 return NULL;
5864 /* Force the values of the variable at position "pos" of type "type" of "set"
5865 * to be no greater than "value".
5867 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5868 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5870 if (!value)
5871 goto error;
5872 if (!isl_val_is_int(value))
5873 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5874 "expecting integer value", goto error);
5875 set = isl_set_upper_bound(set, type, pos, value->n);
5876 isl_val_free(value);
5877 return set;
5878 error:
5879 isl_val_free(value);
5880 isl_set_free(set);
5881 return NULL;
5884 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5885 isl_int value)
5887 int i;
5889 set = isl_set_cow(set);
5890 if (!set)
5891 return NULL;
5893 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5894 for (i = 0; i < set->n; ++i) {
5895 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5896 if (!set->p[i])
5897 goto error;
5899 return set;
5900 error:
5901 isl_set_free(set);
5902 return NULL;
5905 struct isl_map *isl_map_reverse(struct isl_map *map)
5907 int i;
5909 map = isl_map_cow(map);
5910 if (!map)
5911 return NULL;
5913 map->dim = isl_space_reverse(map->dim);
5914 if (!map->dim)
5915 goto error;
5916 for (i = 0; i < map->n; ++i) {
5917 map->p[i] = isl_basic_map_reverse(map->p[i]);
5918 if (!map->p[i])
5919 goto error;
5921 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5922 return map;
5923 error:
5924 isl_map_free(map);
5925 return NULL;
5928 static struct isl_map *isl_basic_map_partial_lexopt(
5929 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5930 struct isl_set **empty, int max)
5932 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5935 struct isl_map *isl_basic_map_partial_lexmax(
5936 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5937 struct isl_set **empty)
5939 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5942 struct isl_map *isl_basic_map_partial_lexmin(
5943 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5944 struct isl_set **empty)
5946 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5949 struct isl_set *isl_basic_set_partial_lexmin(
5950 struct isl_basic_set *bset, struct isl_basic_set *dom,
5951 struct isl_set **empty)
5953 return (struct isl_set *)
5954 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5955 dom, empty);
5958 struct isl_set *isl_basic_set_partial_lexmax(
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_lexmax((struct isl_basic_map *)bset,
5964 dom, empty);
5967 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5968 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5969 __isl_give isl_set **empty)
5971 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5974 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5975 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5976 __isl_give isl_set **empty)
5978 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5981 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5982 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5983 __isl_give isl_set **empty)
5985 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5988 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5989 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5990 __isl_give isl_set **empty)
5992 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5995 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5996 __isl_take isl_basic_map *bmap, int max)
5998 isl_basic_set *dom = NULL;
5999 isl_space *dom_space;
6001 if (!bmap)
6002 goto error;
6003 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6004 dom = isl_basic_set_universe(dom_space);
6005 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6006 error:
6007 isl_basic_map_free(bmap);
6008 return NULL;
6011 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6012 __isl_take isl_basic_map *bmap)
6014 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6017 #undef TYPE
6018 #define TYPE isl_pw_multi_aff
6019 #undef SUFFIX
6020 #define SUFFIX _pw_multi_aff
6021 #undef EMPTY
6022 #define EMPTY isl_pw_multi_aff_empty
6023 #undef ADD
6024 #define ADD isl_pw_multi_aff_union_add
6025 #include "isl_map_lexopt_templ.c"
6027 /* Given a map "map", compute the lexicographically minimal
6028 * (or maximal) image element for each domain element in dom,
6029 * in the form of an isl_pw_multi_aff.
6030 * Set *empty to those elements in dom that do not have an image element.
6032 * We first compute the lexicographically minimal or maximal element
6033 * in the first basic map. This results in a partial solution "res"
6034 * and a subset "todo" of dom that still need to be handled.
6035 * We then consider each of the remaining maps in "map" and successively
6036 * update both "res" and "todo".
6038 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6039 __isl_take isl_map *map, __isl_take isl_set *dom,
6040 __isl_give isl_set **empty, int max)
6042 int i;
6043 isl_pw_multi_aff *res;
6044 isl_set *todo;
6046 if (!map || !dom)
6047 goto error;
6049 if (isl_map_plain_is_empty(map)) {
6050 if (empty)
6051 *empty = dom;
6052 else
6053 isl_set_free(dom);
6054 return isl_pw_multi_aff_from_map(map);
6057 res = basic_map_partial_lexopt_pw_multi_aff(
6058 isl_basic_map_copy(map->p[0]),
6059 isl_set_copy(dom), &todo, max);
6061 for (i = 1; i < map->n; ++i) {
6062 isl_pw_multi_aff *res_i;
6063 isl_set *todo_i;
6065 res_i = basic_map_partial_lexopt_pw_multi_aff(
6066 isl_basic_map_copy(map->p[i]),
6067 isl_set_copy(dom), &todo_i, max);
6069 if (max)
6070 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6071 else
6072 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6074 todo = isl_set_intersect(todo, todo_i);
6077 isl_set_free(dom);
6078 isl_map_free(map);
6080 if (empty)
6081 *empty = todo;
6082 else
6083 isl_set_free(todo);
6085 return res;
6086 error:
6087 if (empty)
6088 *empty = NULL;
6089 isl_set_free(dom);
6090 isl_map_free(map);
6091 return NULL;
6094 #undef TYPE
6095 #define TYPE isl_map
6096 #undef SUFFIX
6097 #define SUFFIX
6098 #undef EMPTY
6099 #define EMPTY isl_map_empty
6100 #undef ADD
6101 #define ADD isl_map_union_disjoint
6102 #include "isl_map_lexopt_templ.c"
6104 /* Given a map "map", compute the lexicographically minimal
6105 * (or maximal) image element for each domain element in dom.
6106 * Set *empty to those elements in dom that do not have an image element.
6108 * We first compute the lexicographically minimal or maximal element
6109 * in the first basic map. This results in a partial solution "res"
6110 * and a subset "todo" of dom that still need to be handled.
6111 * We then consider each of the remaining maps in "map" and successively
6112 * update both "res" and "todo".
6114 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6115 * Assume we are computing the lexicographical maximum.
6116 * We first compute the lexicographically maximal element in basic map i.
6117 * This results in a partial solution res_i and a subset todo_i.
6118 * Then we combine these results with those obtain for the first k basic maps
6119 * to obtain a result that is valid for the first k+1 basic maps.
6120 * In particular, the set where there is no solution is the set where
6121 * there is no solution for the first k basic maps and also no solution
6122 * for the ith basic map, i.e.,
6124 * todo^i = todo^k * todo_i
6126 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6127 * solutions, arbitrarily breaking ties in favor of res^k.
6128 * That is, when res^k(a) >= res_i(a), we pick res^k and
6129 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6130 * the lexicographic order.)
6131 * In practice, we compute
6133 * res^k * (res_i . "<=")
6135 * and
6137 * res_i * (res^k . "<")
6139 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6140 * where only one of res^k and res_i provides a solution and we simply pick
6141 * that one, i.e.,
6143 * res^k * todo_i
6144 * and
6145 * res_i * todo^k
6147 * Note that we only compute these intersections when dom(res^k) intersects
6148 * dom(res_i). Otherwise, the only effect of these intersections is to
6149 * potentially break up res^k and res_i into smaller pieces.
6150 * We want to avoid such splintering as much as possible.
6151 * In fact, an earlier implementation of this function would look for
6152 * better results in the domain of res^k and for extra results in todo^k,
6153 * but this would always result in a splintering according to todo^k,
6154 * even when the domain of basic map i is disjoint from the domains of
6155 * the previous basic maps.
6157 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6158 __isl_take isl_map *map, __isl_take isl_set *dom,
6159 __isl_give isl_set **empty, int max)
6161 int i;
6162 struct isl_map *res;
6163 struct isl_set *todo;
6165 if (!map || !dom)
6166 goto error;
6168 if (isl_map_plain_is_empty(map)) {
6169 if (empty)
6170 *empty = dom;
6171 else
6172 isl_set_free(dom);
6173 return map;
6176 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6177 isl_set_copy(dom), &todo, max);
6179 for (i = 1; i < map->n; ++i) {
6180 isl_map *lt, *le;
6181 isl_map *res_i;
6182 isl_set *todo_i;
6183 isl_space *dim = isl_space_range(isl_map_get_space(res));
6185 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6186 isl_set_copy(dom), &todo_i, max);
6188 if (max) {
6189 lt = isl_map_lex_lt(isl_space_copy(dim));
6190 le = isl_map_lex_le(dim);
6191 } else {
6192 lt = isl_map_lex_gt(isl_space_copy(dim));
6193 le = isl_map_lex_ge(dim);
6195 lt = isl_map_apply_range(isl_map_copy(res), lt);
6196 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6197 le = isl_map_apply_range(isl_map_copy(res_i), le);
6198 le = isl_map_intersect(le, isl_map_copy(res));
6200 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6201 res = isl_map_intersect_domain(res,
6202 isl_set_copy(todo_i));
6203 res_i = isl_map_intersect_domain(res_i,
6204 isl_set_copy(todo));
6207 res = isl_map_union_disjoint(res, res_i);
6208 res = isl_map_union_disjoint(res, lt);
6209 res = isl_map_union_disjoint(res, le);
6211 todo = isl_set_intersect(todo, todo_i);
6214 isl_set_free(dom);
6215 isl_map_free(map);
6217 if (empty)
6218 *empty = todo;
6219 else
6220 isl_set_free(todo);
6222 return res;
6223 error:
6224 if (empty)
6225 *empty = NULL;
6226 isl_set_free(dom);
6227 isl_map_free(map);
6228 return NULL;
6231 __isl_give isl_map *isl_map_partial_lexmax(
6232 __isl_take isl_map *map, __isl_take isl_set *dom,
6233 __isl_give isl_set **empty)
6235 return isl_map_partial_lexopt(map, dom, empty, 1);
6238 __isl_give isl_map *isl_map_partial_lexmin(
6239 __isl_take isl_map *map, __isl_take isl_set *dom,
6240 __isl_give isl_set **empty)
6242 return isl_map_partial_lexopt(map, dom, empty, 0);
6245 __isl_give isl_set *isl_set_partial_lexmin(
6246 __isl_take isl_set *set, __isl_take isl_set *dom,
6247 __isl_give isl_set **empty)
6249 return (struct isl_set *)
6250 isl_map_partial_lexmin((struct isl_map *)set,
6251 dom, empty);
6254 __isl_give isl_set *isl_set_partial_lexmax(
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_lexmax((struct isl_map *)set,
6260 dom, empty);
6263 /* Compute the lexicographic minimum (or maximum if "max" is set)
6264 * of "bmap" over its domain.
6266 * Since we are not interested in the part of the domain space where
6267 * there is no solution, we initialize the domain to those constraints
6268 * of "bmap" that only involve the parameters and the input dimensions.
6269 * This relieves the parametric programming engine from detecting those
6270 * inequalities and transferring them to the context. More importantly,
6271 * it ensures that those inequalities are transferred first and not
6272 * intermixed with inequalities that actually split the domain.
6274 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6276 int n_div;
6277 int n_out;
6278 isl_basic_map *copy;
6279 isl_basic_set *dom;
6281 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6282 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6283 copy = isl_basic_map_copy(bmap);
6284 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6285 isl_dim_div, 0, n_div);
6286 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6287 isl_dim_out, 0, n_out);
6288 dom = isl_basic_map_domain(copy);
6289 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6292 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6294 return isl_basic_map_lexopt(bmap, 0);
6297 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6299 return isl_basic_map_lexopt(bmap, 1);
6302 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6304 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6307 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6309 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6312 /* Extract the first and only affine expression from list
6313 * and then add it to *pwaff with the given dom.
6314 * This domain is known to be disjoint from other domains
6315 * because of the way isl_basic_map_foreach_lexmax works.
6317 static int update_dim_opt(__isl_take isl_basic_set *dom,
6318 __isl_take isl_aff_list *list, void *user)
6320 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6321 isl_aff *aff;
6322 isl_pw_aff **pwaff = user;
6323 isl_pw_aff *pwaff_i;
6325 if (!list)
6326 goto error;
6327 if (isl_aff_list_n_aff(list) != 1)
6328 isl_die(ctx, isl_error_internal,
6329 "expecting single element list", goto error);
6331 aff = isl_aff_list_get_aff(list, 0);
6332 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6334 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6336 isl_aff_list_free(list);
6338 return 0;
6339 error:
6340 isl_basic_set_free(dom);
6341 isl_aff_list_free(list);
6342 return -1;
6345 /* Given a basic map with one output dimension, compute the minimum or
6346 * maximum of that dimension as an isl_pw_aff.
6348 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6349 * call update_dim_opt on each leaf of the result.
6351 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6352 int max)
6354 isl_space *dim = isl_basic_map_get_space(bmap);
6355 isl_pw_aff *pwaff;
6356 int r;
6358 dim = isl_space_from_domain(isl_space_domain(dim));
6359 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6360 pwaff = isl_pw_aff_empty(dim);
6362 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6363 if (r < 0)
6364 return isl_pw_aff_free(pwaff);
6366 return pwaff;
6369 /* Compute the minimum or maximum of the given output dimension
6370 * as a function of the parameters and the input dimensions,
6371 * but independently of the other output dimensions.
6373 * We first project out the other output dimension and then compute
6374 * the "lexicographic" maximum in each basic map, combining the results
6375 * using isl_pw_aff_union_max.
6377 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6378 int max)
6380 int i;
6381 isl_pw_aff *pwaff;
6382 unsigned n_out;
6384 n_out = isl_map_dim(map, isl_dim_out);
6385 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6386 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6387 if (!map)
6388 return NULL;
6390 if (map->n == 0) {
6391 isl_space *dim = isl_map_get_space(map);
6392 isl_map_free(map);
6393 return isl_pw_aff_empty(dim);
6396 pwaff = basic_map_dim_opt(map->p[0], max);
6397 for (i = 1; i < map->n; ++i) {
6398 isl_pw_aff *pwaff_i;
6400 pwaff_i = basic_map_dim_opt(map->p[i], max);
6401 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6404 isl_map_free(map);
6406 return pwaff;
6409 /* Compute the maximum of the given output dimension as a function of the
6410 * parameters and input dimensions, but independently of
6411 * the other output dimensions.
6413 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6415 return map_dim_opt(map, pos, 1);
6418 /* Compute the minimum or maximum of the given set dimension
6419 * as a function of the parameters,
6420 * but independently of the other set dimensions.
6422 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6423 int max)
6425 return map_dim_opt(set, pos, max);
6428 /* Compute the maximum of the given set dimension as a function of the
6429 * parameters, but independently of the other set dimensions.
6431 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6433 return set_dim_opt(set, pos, 1);
6436 /* Compute the minimum of the given set dimension as a function of the
6437 * parameters, but independently of the other set dimensions.
6439 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6441 return set_dim_opt(set, pos, 0);
6444 /* Apply a preimage specified by "mat" on the parameters of "bset".
6445 * bset is assumed to have only parameters and divs.
6447 static struct isl_basic_set *basic_set_parameter_preimage(
6448 struct isl_basic_set *bset, struct isl_mat *mat)
6450 unsigned nparam;
6452 if (!bset || !mat)
6453 goto error;
6455 bset->dim = isl_space_cow(bset->dim);
6456 if (!bset->dim)
6457 goto error;
6459 nparam = isl_basic_set_dim(bset, isl_dim_param);
6461 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6463 bset->dim->nparam = 0;
6464 bset->dim->n_out = nparam;
6465 bset = isl_basic_set_preimage(bset, mat);
6466 if (bset) {
6467 bset->dim->nparam = bset->dim->n_out;
6468 bset->dim->n_out = 0;
6470 return bset;
6471 error:
6472 isl_mat_free(mat);
6473 isl_basic_set_free(bset);
6474 return NULL;
6477 /* Apply a preimage specified by "mat" on the parameters of "set".
6478 * set is assumed to have only parameters and divs.
6480 static struct isl_set *set_parameter_preimage(
6481 struct isl_set *set, struct isl_mat *mat)
6483 isl_space *dim = NULL;
6484 unsigned nparam;
6486 if (!set || !mat)
6487 goto error;
6489 dim = isl_space_copy(set->dim);
6490 dim = isl_space_cow(dim);
6491 if (!dim)
6492 goto error;
6494 nparam = isl_set_dim(set, isl_dim_param);
6496 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6498 dim->nparam = 0;
6499 dim->n_out = nparam;
6500 isl_set_reset_space(set, dim);
6501 set = isl_set_preimage(set, mat);
6502 if (!set)
6503 goto error2;
6504 dim = isl_space_copy(set->dim);
6505 dim = isl_space_cow(dim);
6506 if (!dim)
6507 goto error2;
6508 dim->nparam = dim->n_out;
6509 dim->n_out = 0;
6510 isl_set_reset_space(set, dim);
6511 return set;
6512 error:
6513 isl_space_free(dim);
6514 isl_mat_free(mat);
6515 error2:
6516 isl_set_free(set);
6517 return NULL;
6520 /* Intersect the basic set "bset" with the affine space specified by the
6521 * equalities in "eq".
6523 static struct isl_basic_set *basic_set_append_equalities(
6524 struct isl_basic_set *bset, struct isl_mat *eq)
6526 int i, k;
6527 unsigned len;
6529 if (!bset || !eq)
6530 goto error;
6532 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6533 eq->n_row, 0);
6534 if (!bset)
6535 goto error;
6537 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6538 for (i = 0; i < eq->n_row; ++i) {
6539 k = isl_basic_set_alloc_equality(bset);
6540 if (k < 0)
6541 goto error;
6542 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6543 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6545 isl_mat_free(eq);
6547 bset = isl_basic_set_gauss(bset, NULL);
6548 bset = isl_basic_set_finalize(bset);
6550 return bset;
6551 error:
6552 isl_mat_free(eq);
6553 isl_basic_set_free(bset);
6554 return NULL;
6557 /* Intersect the set "set" with the affine space specified by the
6558 * equalities in "eq".
6560 static struct isl_set *set_append_equalities(struct isl_set *set,
6561 struct isl_mat *eq)
6563 int i;
6565 if (!set || !eq)
6566 goto error;
6568 for (i = 0; i < set->n; ++i) {
6569 set->p[i] = basic_set_append_equalities(set->p[i],
6570 isl_mat_copy(eq));
6571 if (!set->p[i])
6572 goto error;
6574 isl_mat_free(eq);
6575 return set;
6576 error:
6577 isl_mat_free(eq);
6578 isl_set_free(set);
6579 return NULL;
6582 /* Given a basic set "bset" that only involves parameters and existentially
6583 * quantified variables, return the index of the first equality
6584 * that only involves parameters. If there is no such equality then
6585 * return bset->n_eq.
6587 * This function assumes that isl_basic_set_gauss has been called on "bset".
6589 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6591 int i, j;
6592 unsigned nparam, n_div;
6594 if (!bset)
6595 return -1;
6597 nparam = isl_basic_set_dim(bset, isl_dim_param);
6598 n_div = isl_basic_set_dim(bset, isl_dim_div);
6600 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6601 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6602 ++i;
6605 return i;
6608 /* Compute an explicit representation for the existentially quantified
6609 * variables in "bset" by computing the "minimal value" of the set
6610 * variables. Since there are no set variables, the computation of
6611 * the minimal value essentially computes an explicit representation
6612 * of the non-empty part(s) of "bset".
6614 * The input only involves parameters and existentially quantified variables.
6615 * All equalities among parameters have been removed.
6617 * Since the existentially quantified variables in the result are in general
6618 * going to be different from those in the input, we first replace
6619 * them by the minimal number of variables based on their equalities.
6620 * This should simplify the parametric integer programming.
6622 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6624 isl_morph *morph1, *morph2;
6625 isl_set *set;
6626 unsigned n;
6628 if (!bset)
6629 return NULL;
6630 if (bset->n_eq == 0)
6631 return isl_basic_set_lexmin(bset);
6633 morph1 = isl_basic_set_parameter_compression(bset);
6634 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6635 bset = isl_basic_set_lift(bset);
6636 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6637 bset = isl_morph_basic_set(morph2, bset);
6638 n = isl_basic_set_dim(bset, isl_dim_set);
6639 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6641 set = isl_basic_set_lexmin(bset);
6643 set = isl_morph_set(isl_morph_inverse(morph1), set);
6645 return set;
6648 /* Project the given basic set onto its parameter domain, possibly introducing
6649 * new, explicit, existential variables in the constraints.
6650 * The input has parameters and (possibly implicit) existential variables.
6651 * The output has the same parameters, but only
6652 * explicit existentially quantified variables.
6654 * The actual projection is performed by pip, but pip doesn't seem
6655 * to like equalities very much, so we first remove the equalities
6656 * among the parameters by performing a variable compression on
6657 * the parameters. Afterward, an inverse transformation is performed
6658 * and the equalities among the parameters are inserted back in.
6660 * The variable compression on the parameters may uncover additional
6661 * equalities that were only implicit before. We therefore check
6662 * if there are any new parameter equalities in the result and
6663 * if so recurse. The removal of parameter equalities is required
6664 * for the parameter compression performed by base_compute_divs.
6666 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6668 int i;
6669 struct isl_mat *eq;
6670 struct isl_mat *T, *T2;
6671 struct isl_set *set;
6672 unsigned nparam;
6674 bset = isl_basic_set_cow(bset);
6675 if (!bset)
6676 return NULL;
6678 if (bset->n_eq == 0)
6679 return base_compute_divs(bset);
6681 bset = isl_basic_set_gauss(bset, NULL);
6682 if (!bset)
6683 return NULL;
6684 if (isl_basic_set_plain_is_empty(bset))
6685 return isl_set_from_basic_set(bset);
6687 i = first_parameter_equality(bset);
6688 if (i == bset->n_eq)
6689 return base_compute_divs(bset);
6691 nparam = isl_basic_set_dim(bset, isl_dim_param);
6692 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6693 0, 1 + nparam);
6694 eq = isl_mat_cow(eq);
6695 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6696 if (T && T->n_col == 0) {
6697 isl_mat_free(T);
6698 isl_mat_free(T2);
6699 isl_mat_free(eq);
6700 bset = isl_basic_set_set_to_empty(bset);
6701 return isl_set_from_basic_set(bset);
6703 bset = basic_set_parameter_preimage(bset, T);
6705 i = first_parameter_equality(bset);
6706 if (!bset)
6707 set = NULL;
6708 else if (i == bset->n_eq)
6709 set = base_compute_divs(bset);
6710 else
6711 set = parameter_compute_divs(bset);
6712 set = set_parameter_preimage(set, T2);
6713 set = set_append_equalities(set, eq);
6714 return set;
6717 /* Insert the divs from "ls" before those of "bmap".
6719 * The number of columns is not changed, which means that the last
6720 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6721 * The caller is responsible for removing the same number of dimensions
6722 * from the space of "bmap".
6724 static __isl_give isl_basic_map *insert_divs_from_local_space(
6725 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6727 int i;
6728 int n_div;
6729 int old_n_div;
6731 n_div = isl_local_space_dim(ls, isl_dim_div);
6732 if (n_div == 0)
6733 return bmap;
6735 old_n_div = bmap->n_div;
6736 bmap = insert_div_rows(bmap, n_div);
6737 if (!bmap)
6738 return NULL;
6740 for (i = 0; i < n_div; ++i) {
6741 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6742 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6745 return bmap;
6748 /* Replace the space of "bmap" by the space and divs of "ls".
6750 * If "ls" has any divs, then we simplify the result since we may
6751 * have discovered some additional equalities that could simplify
6752 * the div expressions.
6754 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6755 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6757 int n_div;
6759 bmap = isl_basic_map_cow(bmap);
6760 if (!bmap || !ls)
6761 goto error;
6763 n_div = isl_local_space_dim(ls, isl_dim_div);
6764 bmap = insert_divs_from_local_space(bmap, ls);
6765 if (!bmap)
6766 goto error;
6768 isl_space_free(bmap->dim);
6769 bmap->dim = isl_local_space_get_space(ls);
6770 if (!bmap->dim)
6771 goto error;
6773 isl_local_space_free(ls);
6774 if (n_div > 0)
6775 bmap = isl_basic_map_simplify(bmap);
6776 bmap = isl_basic_map_finalize(bmap);
6777 return bmap;
6778 error:
6779 isl_basic_map_free(bmap);
6780 isl_local_space_free(ls);
6781 return NULL;
6784 /* Replace the space of "map" by the space and divs of "ls".
6786 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6787 __isl_take isl_local_space *ls)
6789 int i;
6791 map = isl_map_cow(map);
6792 if (!map || !ls)
6793 goto error;
6795 for (i = 0; i < map->n; ++i) {
6796 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6797 isl_local_space_copy(ls));
6798 if (!map->p[i])
6799 goto error;
6801 isl_space_free(map->dim);
6802 map->dim = isl_local_space_get_space(ls);
6803 if (!map->dim)
6804 goto error;
6806 isl_local_space_free(ls);
6807 return map;
6808 error:
6809 isl_local_space_free(ls);
6810 isl_map_free(map);
6811 return NULL;
6814 /* Compute an explicit representation for the existentially
6815 * quantified variables for which do not know any explicit representation yet.
6817 * We first sort the existentially quantified variables so that the
6818 * existentially quantified variables for which we already have an explicit
6819 * representation are placed before those for which we do not.
6820 * The input dimensions, the output dimensions and the existentially
6821 * quantified variables for which we already have an explicit
6822 * representation are then turned into parameters.
6823 * compute_divs returns a map with the same parameters and
6824 * no input or output dimensions and the dimension specification
6825 * is reset to that of the input, including the existentially quantified
6826 * variables for which we already had an explicit representation.
6828 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6830 struct isl_basic_set *bset;
6831 struct isl_set *set;
6832 struct isl_map *map;
6833 isl_space *dim;
6834 isl_local_space *ls;
6835 unsigned nparam;
6836 unsigned n_in;
6837 unsigned n_out;
6838 unsigned n_known;
6839 int i;
6841 bmap = isl_basic_map_sort_divs(bmap);
6842 bmap = isl_basic_map_cow(bmap);
6843 if (!bmap)
6844 return NULL;
6846 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6847 if (isl_int_is_zero(bmap->div[n_known][0]))
6848 break;
6850 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6851 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6852 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6853 dim = isl_space_set_alloc(bmap->ctx,
6854 nparam + n_in + n_out + n_known, 0);
6855 if (!dim)
6856 goto error;
6858 ls = isl_basic_map_get_local_space(bmap);
6859 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6860 n_known, bmap->n_div - n_known);
6861 if (n_known > 0) {
6862 for (i = n_known; i < bmap->n_div; ++i)
6863 swap_div(bmap, i - n_known, i);
6864 bmap->n_div -= n_known;
6865 bmap->extra -= n_known;
6867 bmap = isl_basic_map_reset_space(bmap, dim);
6868 bset = (struct isl_basic_set *)bmap;
6870 set = parameter_compute_divs(bset);
6871 map = (struct isl_map *)set;
6872 map = replace_space_by_local_space(map, ls);
6874 return map;
6875 error:
6876 isl_basic_map_free(bmap);
6877 return NULL;
6880 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6882 int i;
6883 unsigned off;
6885 if (!bmap)
6886 return -1;
6888 off = isl_space_dim(bmap->dim, isl_dim_all);
6889 for (i = 0; i < bmap->n_div; ++i) {
6890 if (isl_int_is_zero(bmap->div[i][0]))
6891 return 0;
6892 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6893 return -1);
6895 return 1;
6898 static int map_divs_known(__isl_keep isl_map *map)
6900 int i;
6902 if (!map)
6903 return -1;
6905 for (i = 0; i < map->n; ++i) {
6906 int known = isl_basic_map_divs_known(map->p[i]);
6907 if (known <= 0)
6908 return known;
6911 return 1;
6914 /* If bmap contains any unknown divs, then compute explicit
6915 * expressions for them. However, this computation may be
6916 * quite expensive, so first try to remove divs that aren't
6917 * strictly needed.
6919 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6921 int known;
6922 struct isl_map *map;
6924 known = isl_basic_map_divs_known(bmap);
6925 if (known < 0)
6926 goto error;
6927 if (known)
6928 return isl_map_from_basic_map(bmap);
6930 bmap = isl_basic_map_drop_redundant_divs(bmap);
6932 known = isl_basic_map_divs_known(bmap);
6933 if (known < 0)
6934 goto error;
6935 if (known)
6936 return isl_map_from_basic_map(bmap);
6938 map = compute_divs(bmap);
6939 return map;
6940 error:
6941 isl_basic_map_free(bmap);
6942 return NULL;
6945 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6947 int i;
6948 int known;
6949 struct isl_map *res;
6951 if (!map)
6952 return NULL;
6953 if (map->n == 0)
6954 return map;
6956 known = map_divs_known(map);
6957 if (known < 0) {
6958 isl_map_free(map);
6959 return NULL;
6961 if (known)
6962 return map;
6964 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6965 for (i = 1 ; i < map->n; ++i) {
6966 struct isl_map *r2;
6967 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6968 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6969 res = isl_map_union_disjoint(res, r2);
6970 else
6971 res = isl_map_union(res, r2);
6973 isl_map_free(map);
6975 return res;
6978 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6980 return (struct isl_set *)
6981 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6984 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6986 return (struct isl_set *)
6987 isl_map_compute_divs((struct isl_map *)set);
6990 struct isl_set *isl_map_domain(struct isl_map *map)
6992 int i;
6993 struct isl_set *set;
6995 if (!map)
6996 goto error;
6998 map = isl_map_cow(map);
6999 if (!map)
7000 return NULL;
7002 set = (struct isl_set *)map;
7003 set->dim = isl_space_domain(set->dim);
7004 if (!set->dim)
7005 goto error;
7006 for (i = 0; i < map->n; ++i) {
7007 set->p[i] = isl_basic_map_domain(map->p[i]);
7008 if (!set->p[i])
7009 goto error;
7011 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7012 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7013 return set;
7014 error:
7015 isl_map_free(map);
7016 return NULL;
7019 /* Return the union of "map1" and "map2", where we assume for now that
7020 * "map1" and "map2" are disjoint. Note that the basic maps inside
7021 * "map1" or "map2" may not be disjoint from each other.
7022 * Also note that this function is also called from isl_map_union,
7023 * which takes care of handling the situation where "map1" and "map2"
7024 * may not be disjoint.
7026 * If one of the inputs is empty, we can simply return the other input.
7027 * Similarly, if one of the inputs is universal, then it is equal to the union.
7029 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7030 __isl_take isl_map *map2)
7032 int i;
7033 unsigned flags = 0;
7034 struct isl_map *map = NULL;
7035 int is_universe;
7037 if (!map1 || !map2)
7038 goto error;
7040 if (!isl_space_is_equal(map1->dim, map2->dim))
7041 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7042 "spaces don't match", goto error);
7044 if (map1->n == 0) {
7045 isl_map_free(map1);
7046 return map2;
7048 if (map2->n == 0) {
7049 isl_map_free(map2);
7050 return map1;
7053 is_universe = isl_map_plain_is_universe(map1);
7054 if (is_universe < 0)
7055 goto error;
7056 if (is_universe) {
7057 isl_map_free(map2);
7058 return map1;
7061 is_universe = isl_map_plain_is_universe(map2);
7062 if (is_universe < 0)
7063 goto error;
7064 if (is_universe) {
7065 isl_map_free(map1);
7066 return map2;
7069 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7070 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7071 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7073 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7074 map1->n + map2->n, flags);
7075 if (!map)
7076 goto error;
7077 for (i = 0; i < map1->n; ++i) {
7078 map = isl_map_add_basic_map(map,
7079 isl_basic_map_copy(map1->p[i]));
7080 if (!map)
7081 goto error;
7083 for (i = 0; i < map2->n; ++i) {
7084 map = isl_map_add_basic_map(map,
7085 isl_basic_map_copy(map2->p[i]));
7086 if (!map)
7087 goto error;
7089 isl_map_free(map1);
7090 isl_map_free(map2);
7091 return map;
7092 error:
7093 isl_map_free(map);
7094 isl_map_free(map1);
7095 isl_map_free(map2);
7096 return NULL;
7099 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7100 * guaranteed to be disjoint by the caller.
7102 * Note that this functions is called from within isl_map_make_disjoint,
7103 * so we have to be careful not to touch the constraints of the inputs
7104 * in any way.
7106 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7107 __isl_take isl_map *map2)
7109 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7112 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7113 * not be disjoint. The parameters are assumed to have been aligned.
7115 * We currently simply call map_union_disjoint, the internal operation
7116 * of which does not really depend on the inputs being disjoint.
7117 * If the result contains more than one basic map, then we clear
7118 * the disjoint flag since the result may contain basic maps from
7119 * both inputs and these are not guaranteed to be disjoint.
7121 * As a special case, if "map1" and "map2" are obviously equal,
7122 * then we simply return "map1".
7124 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7125 __isl_take isl_map *map2)
7127 int equal;
7129 if (!map1 || !map2)
7130 goto error;
7132 equal = isl_map_plain_is_equal(map1, map2);
7133 if (equal < 0)
7134 goto error;
7135 if (equal) {
7136 isl_map_free(map2);
7137 return map1;
7140 map1 = map_union_disjoint(map1, map2);
7141 if (!map1)
7142 return NULL;
7143 if (map1->n > 1)
7144 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7145 return map1;
7146 error:
7147 isl_map_free(map1);
7148 isl_map_free(map2);
7149 return NULL;
7152 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7153 * not be disjoint.
7155 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7156 __isl_take isl_map *map2)
7158 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7161 struct isl_set *isl_set_union_disjoint(
7162 struct isl_set *set1, struct isl_set *set2)
7164 return (struct isl_set *)
7165 isl_map_union_disjoint(
7166 (struct isl_map *)set1, (struct isl_map *)set2);
7169 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7171 return (struct isl_set *)
7172 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7175 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7176 * the results.
7178 * "map" and "set" are assumed to be compatible and non-NULL.
7180 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7181 __isl_take isl_set *set,
7182 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7183 __isl_take isl_basic_set *bset))
7185 unsigned flags = 0;
7186 struct isl_map *result;
7187 int i, j;
7189 if (isl_set_plain_is_universe(set)) {
7190 isl_set_free(set);
7191 return map;
7194 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7195 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7196 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7198 result = isl_map_alloc_space(isl_space_copy(map->dim),
7199 map->n * set->n, flags);
7200 for (i = 0; result && i < map->n; ++i)
7201 for (j = 0; j < set->n; ++j) {
7202 result = isl_map_add_basic_map(result,
7203 fn(isl_basic_map_copy(map->p[i]),
7204 isl_basic_set_copy(set->p[j])));
7205 if (!result)
7206 break;
7209 isl_map_free(map);
7210 isl_set_free(set);
7211 return result;
7214 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7215 __isl_take isl_set *set)
7217 if (!map || !set)
7218 goto error;
7220 if (!isl_map_compatible_range(map, set))
7221 isl_die(set->ctx, isl_error_invalid,
7222 "incompatible spaces", goto error);
7224 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7225 error:
7226 isl_map_free(map);
7227 isl_set_free(set);
7228 return NULL;
7231 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7232 __isl_take isl_set *set)
7234 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7237 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7238 __isl_take isl_set *set)
7240 if (!map || !set)
7241 goto error;
7243 if (!isl_map_compatible_domain(map, set))
7244 isl_die(set->ctx, isl_error_invalid,
7245 "incompatible spaces", goto error);
7247 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7248 error:
7249 isl_map_free(map);
7250 isl_set_free(set);
7251 return NULL;
7254 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7255 __isl_take isl_set *set)
7257 return isl_map_align_params_map_map_and(map, set,
7258 &map_intersect_domain);
7261 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7262 __isl_take isl_map *map2)
7264 if (!map1 || !map2)
7265 goto error;
7266 map1 = isl_map_reverse(map1);
7267 map1 = isl_map_apply_range(map1, map2);
7268 return isl_map_reverse(map1);
7269 error:
7270 isl_map_free(map1);
7271 isl_map_free(map2);
7272 return NULL;
7275 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7276 __isl_take isl_map *map2)
7278 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7281 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7282 __isl_take isl_map *map2)
7284 isl_space *dim_result;
7285 struct isl_map *result;
7286 int i, j;
7288 if (!map1 || !map2)
7289 goto error;
7291 dim_result = isl_space_join(isl_space_copy(map1->dim),
7292 isl_space_copy(map2->dim));
7294 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7295 if (!result)
7296 goto error;
7297 for (i = 0; i < map1->n; ++i)
7298 for (j = 0; j < map2->n; ++j) {
7299 result = isl_map_add_basic_map(result,
7300 isl_basic_map_apply_range(
7301 isl_basic_map_copy(map1->p[i]),
7302 isl_basic_map_copy(map2->p[j])));
7303 if (!result)
7304 goto error;
7306 isl_map_free(map1);
7307 isl_map_free(map2);
7308 if (result && result->n <= 1)
7309 ISL_F_SET(result, ISL_MAP_DISJOINT);
7310 return result;
7311 error:
7312 isl_map_free(map1);
7313 isl_map_free(map2);
7314 return NULL;
7317 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7318 __isl_take isl_map *map2)
7320 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7324 * returns range - domain
7326 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7328 isl_space *target_space;
7329 struct isl_basic_set *bset;
7330 unsigned dim;
7331 unsigned nparam;
7332 int i;
7334 if (!bmap)
7335 goto error;
7336 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7337 bmap->dim, isl_dim_out),
7338 goto error);
7339 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7340 dim = isl_basic_map_n_in(bmap);
7341 nparam = isl_basic_map_n_param(bmap);
7342 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7343 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7344 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7345 for (i = 0; i < dim; ++i) {
7346 int j = isl_basic_map_alloc_equality(bmap);
7347 if (j < 0) {
7348 bmap = isl_basic_map_free(bmap);
7349 break;
7351 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7352 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7353 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7354 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7356 bset = isl_basic_map_domain(bmap);
7357 bset = isl_basic_set_reset_space(bset, target_space);
7358 return bset;
7359 error:
7360 isl_basic_map_free(bmap);
7361 return NULL;
7365 * returns range - domain
7367 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7369 int i;
7370 isl_space *dim;
7371 struct isl_set *result;
7373 if (!map)
7374 return NULL;
7376 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7377 map->dim, isl_dim_out),
7378 goto error);
7379 dim = isl_map_get_space(map);
7380 dim = isl_space_domain(dim);
7381 result = isl_set_alloc_space(dim, map->n, 0);
7382 if (!result)
7383 goto error;
7384 for (i = 0; i < map->n; ++i)
7385 result = isl_set_add_basic_set(result,
7386 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7387 isl_map_free(map);
7388 return result;
7389 error:
7390 isl_map_free(map);
7391 return NULL;
7395 * returns [domain -> range] -> range - domain
7397 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7398 __isl_take isl_basic_map *bmap)
7400 int i, k;
7401 isl_space *dim;
7402 isl_basic_map *domain;
7403 int nparam, n;
7404 unsigned total;
7406 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7407 bmap->dim, isl_dim_out))
7408 isl_die(bmap->ctx, isl_error_invalid,
7409 "domain and range don't match", goto error);
7411 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7412 n = isl_basic_map_dim(bmap, isl_dim_in);
7414 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7415 domain = isl_basic_map_universe(dim);
7417 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7418 bmap = isl_basic_map_apply_range(bmap, domain);
7419 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7421 total = isl_basic_map_total_dim(bmap);
7423 for (i = 0; i < n; ++i) {
7424 k = isl_basic_map_alloc_equality(bmap);
7425 if (k < 0)
7426 goto error;
7427 isl_seq_clr(bmap->eq[k], 1 + total);
7428 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7429 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7430 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7433 bmap = isl_basic_map_gauss(bmap, NULL);
7434 return isl_basic_map_finalize(bmap);
7435 error:
7436 isl_basic_map_free(bmap);
7437 return NULL;
7441 * returns [domain -> range] -> range - domain
7443 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7445 int i;
7446 isl_space *domain_dim;
7448 if (!map)
7449 return NULL;
7451 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7452 map->dim, isl_dim_out))
7453 isl_die(map->ctx, isl_error_invalid,
7454 "domain and range don't match", goto error);
7456 map = isl_map_cow(map);
7457 if (!map)
7458 return NULL;
7460 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7461 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7462 map->dim = isl_space_join(map->dim, domain_dim);
7463 if (!map->dim)
7464 goto error;
7465 for (i = 0; i < map->n; ++i) {
7466 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7467 if (!map->p[i])
7468 goto error;
7470 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7471 return map;
7472 error:
7473 isl_map_free(map);
7474 return NULL;
7477 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7479 struct isl_basic_map *bmap;
7480 unsigned nparam;
7481 unsigned dim;
7482 int i;
7484 if (!dims)
7485 return NULL;
7487 nparam = dims->nparam;
7488 dim = dims->n_out;
7489 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7490 if (!bmap)
7491 goto error;
7493 for (i = 0; i < dim; ++i) {
7494 int j = isl_basic_map_alloc_equality(bmap);
7495 if (j < 0)
7496 goto error;
7497 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7498 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7499 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7501 return isl_basic_map_finalize(bmap);
7502 error:
7503 isl_basic_map_free(bmap);
7504 return NULL;
7507 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7509 if (!dim)
7510 return NULL;
7511 if (dim->n_in != dim->n_out)
7512 isl_die(dim->ctx, isl_error_invalid,
7513 "number of input and output dimensions needs to be "
7514 "the same", goto error);
7515 return basic_map_identity(dim);
7516 error:
7517 isl_space_free(dim);
7518 return NULL;
7521 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7523 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7526 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7528 isl_space *dim = isl_set_get_space(set);
7529 isl_map *id;
7530 id = isl_map_identity(isl_space_map_from_set(dim));
7531 return isl_map_intersect_range(id, set);
7534 /* Construct a basic set with all set dimensions having only non-negative
7535 * values.
7537 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7538 __isl_take isl_space *space)
7540 int i;
7541 unsigned nparam;
7542 unsigned dim;
7543 struct isl_basic_set *bset;
7545 if (!space)
7546 return NULL;
7547 nparam = space->nparam;
7548 dim = space->n_out;
7549 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7550 if (!bset)
7551 return NULL;
7552 for (i = 0; i < dim; ++i) {
7553 int k = isl_basic_set_alloc_inequality(bset);
7554 if (k < 0)
7555 goto error;
7556 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7557 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7559 return bset;
7560 error:
7561 isl_basic_set_free(bset);
7562 return NULL;
7565 /* Construct the half-space x_pos >= 0.
7567 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7568 int pos)
7570 int k;
7571 isl_basic_set *nonneg;
7573 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7574 k = isl_basic_set_alloc_inequality(nonneg);
7575 if (k < 0)
7576 goto error;
7577 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7578 isl_int_set_si(nonneg->ineq[k][pos], 1);
7580 return isl_basic_set_finalize(nonneg);
7581 error:
7582 isl_basic_set_free(nonneg);
7583 return NULL;
7586 /* Construct the half-space x_pos <= -1.
7588 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7590 int k;
7591 isl_basic_set *neg;
7593 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7594 k = isl_basic_set_alloc_inequality(neg);
7595 if (k < 0)
7596 goto error;
7597 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7598 isl_int_set_si(neg->ineq[k][0], -1);
7599 isl_int_set_si(neg->ineq[k][pos], -1);
7601 return isl_basic_set_finalize(neg);
7602 error:
7603 isl_basic_set_free(neg);
7604 return NULL;
7607 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7608 enum isl_dim_type type, unsigned first, unsigned n)
7610 int i;
7611 unsigned offset;
7612 isl_basic_set *nonneg;
7613 isl_basic_set *neg;
7615 if (!set)
7616 return NULL;
7617 if (n == 0)
7618 return set;
7620 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7622 offset = pos(set->dim, type);
7623 for (i = 0; i < n; ++i) {
7624 nonneg = nonneg_halfspace(isl_set_get_space(set),
7625 offset + first + i);
7626 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7628 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7631 return set;
7632 error:
7633 isl_set_free(set);
7634 return NULL;
7637 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7638 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7639 void *user)
7641 isl_set *half;
7643 if (!set)
7644 return -1;
7645 if (isl_set_plain_is_empty(set)) {
7646 isl_set_free(set);
7647 return 0;
7649 if (first == len)
7650 return fn(set, signs, user);
7652 signs[first] = 1;
7653 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7654 1 + first));
7655 half = isl_set_intersect(half, isl_set_copy(set));
7656 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7657 goto error;
7659 signs[first] = -1;
7660 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7661 1 + first));
7662 half = isl_set_intersect(half, set);
7663 return foreach_orthant(half, signs, first + 1, len, fn, user);
7664 error:
7665 isl_set_free(set);
7666 return -1;
7669 /* Call "fn" on the intersections of "set" with each of the orthants
7670 * (except for obviously empty intersections). The orthant is identified
7671 * by the signs array, with each entry having value 1 or -1 according
7672 * to the sign of the corresponding variable.
7674 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7675 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7676 void *user)
7678 unsigned nparam;
7679 unsigned nvar;
7680 int *signs;
7681 int r;
7683 if (!set)
7684 return -1;
7685 if (isl_set_plain_is_empty(set))
7686 return 0;
7688 nparam = isl_set_dim(set, isl_dim_param);
7689 nvar = isl_set_dim(set, isl_dim_set);
7691 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7693 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7694 fn, user);
7696 free(signs);
7698 return r;
7701 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7703 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7706 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7707 __isl_keep isl_basic_map *bmap2)
7709 int is_subset;
7710 struct isl_map *map1;
7711 struct isl_map *map2;
7713 if (!bmap1 || !bmap2)
7714 return isl_bool_error;
7716 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7717 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7719 is_subset = isl_map_is_subset(map1, map2);
7721 isl_map_free(map1);
7722 isl_map_free(map2);
7724 return is_subset;
7727 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7728 __isl_keep isl_basic_set *bset2)
7730 return isl_basic_map_is_subset(bset1, bset2);
7733 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7734 __isl_keep isl_basic_map *bmap2)
7736 isl_bool is_subset;
7738 if (!bmap1 || !bmap2)
7739 return isl_bool_error;
7740 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7741 if (is_subset != isl_bool_true)
7742 return is_subset;
7743 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7744 return is_subset;
7747 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7748 __isl_keep isl_basic_set *bset2)
7750 return isl_basic_map_is_equal(
7751 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7754 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7756 int i;
7757 int is_empty;
7759 if (!map)
7760 return isl_bool_error;
7761 for (i = 0; i < map->n; ++i) {
7762 is_empty = isl_basic_map_is_empty(map->p[i]);
7763 if (is_empty < 0)
7764 return isl_bool_error;
7765 if (!is_empty)
7766 return isl_bool_false;
7768 return isl_bool_true;
7771 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7773 return map ? map->n == 0 : isl_bool_error;
7776 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7778 return set ? set->n == 0 : isl_bool_error;
7781 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7783 return isl_map_is_empty((struct isl_map *)set);
7786 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7788 if (!map1 || !map2)
7789 return -1;
7791 return isl_space_is_equal(map1->dim, map2->dim);
7794 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7796 if (!set1 || !set2)
7797 return -1;
7799 return isl_space_is_equal(set1->dim, set2->dim);
7802 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7804 isl_bool is_subset;
7806 if (!map1 || !map2)
7807 return isl_bool_error;
7808 is_subset = isl_map_is_subset(map1, map2);
7809 if (is_subset != isl_bool_true)
7810 return is_subset;
7811 is_subset = isl_map_is_subset(map2, map1);
7812 return is_subset;
7815 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7817 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7820 isl_bool isl_basic_map_is_strict_subset(
7821 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7823 isl_bool is_subset;
7825 if (!bmap1 || !bmap2)
7826 return isl_bool_error;
7827 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7828 if (is_subset != isl_bool_true)
7829 return is_subset;
7830 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7831 if (is_subset == isl_bool_error)
7832 return is_subset;
7833 return !is_subset;
7836 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7837 __isl_keep isl_map *map2)
7839 isl_bool is_subset;
7841 if (!map1 || !map2)
7842 return isl_bool_error;
7843 is_subset = isl_map_is_subset(map1, map2);
7844 if (is_subset != isl_bool_true)
7845 return is_subset;
7846 is_subset = isl_map_is_subset(map2, map1);
7847 if (is_subset == isl_bool_error)
7848 return is_subset;
7849 return !is_subset;
7852 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7853 __isl_keep isl_set *set2)
7855 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7858 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
7860 if (!bmap)
7861 return isl_bool_error;
7862 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7865 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
7867 if (!bset)
7868 return isl_bool_error;
7869 return bset->n_eq == 0 && bset->n_ineq == 0;
7872 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
7874 int i;
7876 if (!map)
7877 return isl_bool_error;
7879 for (i = 0; i < map->n; ++i) {
7880 isl_bool r = isl_basic_map_is_universe(map->p[i]);
7881 if (r < 0 || r)
7882 return r;
7885 return isl_bool_false;
7888 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
7890 return isl_map_plain_is_universe((isl_map *) set);
7893 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
7895 struct isl_basic_set *bset = NULL;
7896 struct isl_vec *sample = NULL;
7897 isl_bool empty;
7898 unsigned total;
7900 if (!bmap)
7901 return isl_bool_error;
7903 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7904 return isl_bool_true;
7906 if (isl_basic_map_is_universe(bmap))
7907 return isl_bool_false;
7909 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7910 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7911 copy = isl_basic_map_remove_redundancies(copy);
7912 empty = isl_basic_map_plain_is_empty(copy);
7913 isl_basic_map_free(copy);
7914 return empty;
7917 total = 1 + isl_basic_map_total_dim(bmap);
7918 if (bmap->sample && bmap->sample->size == total) {
7919 int contains = isl_basic_map_contains(bmap, bmap->sample);
7920 if (contains < 0)
7921 return isl_bool_error;
7922 if (contains)
7923 return isl_bool_false;
7925 isl_vec_free(bmap->sample);
7926 bmap->sample = NULL;
7927 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7928 if (!bset)
7929 return isl_bool_error;
7930 sample = isl_basic_set_sample_vec(bset);
7931 if (!sample)
7932 return isl_bool_error;
7933 empty = sample->size == 0;
7934 isl_vec_free(bmap->sample);
7935 bmap->sample = sample;
7936 if (empty)
7937 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7939 return empty;
7942 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7944 if (!bmap)
7945 return isl_bool_error;
7946 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7949 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7951 if (!bset)
7952 return isl_bool_error;
7953 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7956 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
7958 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7961 struct isl_map *isl_basic_map_union(
7962 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7964 struct isl_map *map;
7965 if (!bmap1 || !bmap2)
7966 goto error;
7968 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7970 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7971 if (!map)
7972 goto error;
7973 map = isl_map_add_basic_map(map, bmap1);
7974 map = isl_map_add_basic_map(map, bmap2);
7975 return map;
7976 error:
7977 isl_basic_map_free(bmap1);
7978 isl_basic_map_free(bmap2);
7979 return NULL;
7982 struct isl_set *isl_basic_set_union(
7983 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7985 return (struct isl_set *)isl_basic_map_union(
7986 (struct isl_basic_map *)bset1,
7987 (struct isl_basic_map *)bset2);
7990 /* Order divs such that any div only depends on previous divs */
7991 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7993 int i;
7994 unsigned off;
7996 if (!bmap)
7997 return NULL;
7999 off = isl_space_dim(bmap->dim, isl_dim_all);
8001 for (i = 0; i < bmap->n_div; ++i) {
8002 int pos;
8003 if (isl_int_is_zero(bmap->div[i][0]))
8004 continue;
8005 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8006 bmap->n_div-i);
8007 if (pos == -1)
8008 continue;
8009 isl_basic_map_swap_div(bmap, i, i + pos);
8010 --i;
8012 return bmap;
8015 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8017 return (struct isl_basic_set *)
8018 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8021 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8023 int i;
8025 if (!map)
8026 return 0;
8028 for (i = 0; i < map->n; ++i) {
8029 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8030 if (!map->p[i])
8031 goto error;
8034 return map;
8035 error:
8036 isl_map_free(map);
8037 return NULL;
8040 /* Apply the expansion computed by isl_merge_divs.
8041 * The expansion itself is given by "exp" while the resulting
8042 * list of divs is given by "div".
8044 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8045 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8047 int i, j;
8048 int n_div;
8050 bset = isl_basic_set_cow(bset);
8051 if (!bset || !div)
8052 goto error;
8054 if (div->n_row < bset->n_div)
8055 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8056 "not an expansion", goto error);
8058 n_div = bset->n_div;
8059 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8060 div->n_row - n_div, 0,
8061 2 * (div->n_row - n_div));
8063 for (i = n_div; i < div->n_row; ++i)
8064 if (isl_basic_set_alloc_div(bset) < 0)
8065 goto error;
8067 j = n_div - 1;
8068 for (i = div->n_row - 1; i >= 0; --i) {
8069 if (j >= 0 && exp[j] == i) {
8070 if (i != j)
8071 isl_basic_map_swap_div(bset, i, j);
8072 j--;
8073 } else {
8074 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8075 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8076 goto error;
8080 isl_mat_free(div);
8081 return bset;
8082 error:
8083 isl_basic_set_free(bset);
8084 isl_mat_free(div);
8085 return NULL;
8088 /* Look for a div in dst that corresponds to the div "div" in src.
8089 * The divs before "div" in src and dst are assumed to be the same.
8091 * Returns -1 if no corresponding div was found and the position
8092 * of the corresponding div in dst otherwise.
8094 static int find_div(struct isl_basic_map *dst,
8095 struct isl_basic_map *src, unsigned div)
8097 int i;
8099 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8101 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8102 for (i = div; i < dst->n_div; ++i)
8103 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8104 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8105 dst->n_div - div) == -1)
8106 return i;
8107 return -1;
8110 /* Align the divs of "dst" to those of "src", adding divs from "src"
8111 * if needed. That is, make sure that the first src->n_div divs
8112 * of the result are equal to those of src.
8114 * The result is not finalized as by design it will have redundant
8115 * divs if any divs from "src" were copied.
8117 __isl_give isl_basic_map *isl_basic_map_align_divs(
8118 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8120 int i;
8121 int known, extended;
8122 unsigned total;
8124 if (!dst || !src)
8125 return isl_basic_map_free(dst);
8127 if (src->n_div == 0)
8128 return dst;
8130 known = isl_basic_map_divs_known(src);
8131 if (known < 0)
8132 return isl_basic_map_free(dst);
8133 if (!known)
8134 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8135 "some src divs are unknown",
8136 return isl_basic_map_free(dst));
8138 src = isl_basic_map_order_divs(src);
8140 extended = 0;
8141 total = isl_space_dim(src->dim, isl_dim_all);
8142 for (i = 0; i < src->n_div; ++i) {
8143 int j = find_div(dst, src, i);
8144 if (j < 0) {
8145 if (!extended) {
8146 int extra = src->n_div - i;
8147 dst = isl_basic_map_cow(dst);
8148 if (!dst)
8149 return NULL;
8150 dst = isl_basic_map_extend_space(dst,
8151 isl_space_copy(dst->dim),
8152 extra, 0, 2 * extra);
8153 extended = 1;
8155 j = isl_basic_map_alloc_div(dst);
8156 if (j < 0)
8157 return isl_basic_map_free(dst);
8158 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8159 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8160 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8161 return isl_basic_map_free(dst);
8163 if (j != i)
8164 isl_basic_map_swap_div(dst, i, j);
8166 return dst;
8169 struct isl_basic_set *isl_basic_set_align_divs(
8170 struct isl_basic_set *dst, struct isl_basic_set *src)
8172 return (struct isl_basic_set *)isl_basic_map_align_divs(
8173 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8176 struct isl_map *isl_map_align_divs(struct isl_map *map)
8178 int i;
8180 if (!map)
8181 return NULL;
8182 if (map->n == 0)
8183 return map;
8184 map = isl_map_compute_divs(map);
8185 map = isl_map_cow(map);
8186 if (!map)
8187 return NULL;
8189 for (i = 1; i < map->n; ++i)
8190 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8191 for (i = 1; i < map->n; ++i) {
8192 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8193 if (!map->p[i])
8194 return isl_map_free(map);
8197 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8198 return map;
8201 struct isl_set *isl_set_align_divs(struct isl_set *set)
8203 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8206 /* Align the divs of the basic maps in "map" to those
8207 * of the basic maps in "list", as well as to the other basic maps in "map".
8208 * The elements in "list" are assumed to have known divs.
8210 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8211 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8213 int i, n;
8215 map = isl_map_compute_divs(map);
8216 map = isl_map_cow(map);
8217 if (!map || !list)
8218 return isl_map_free(map);
8219 if (map->n == 0)
8220 return map;
8222 n = isl_basic_map_list_n_basic_map(list);
8223 for (i = 0; i < n; ++i) {
8224 isl_basic_map *bmap;
8226 bmap = isl_basic_map_list_get_basic_map(list, i);
8227 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8228 isl_basic_map_free(bmap);
8230 if (!map->p[0])
8231 return isl_map_free(map);
8233 return isl_map_align_divs(map);
8236 /* Align the divs of each element of "list" to those of "bmap".
8237 * Both "bmap" and the elements of "list" are assumed to have known divs.
8239 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8240 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8242 int i, n;
8244 if (!list || !bmap)
8245 return isl_basic_map_list_free(list);
8247 n = isl_basic_map_list_n_basic_map(list);
8248 for (i = 0; i < n; ++i) {
8249 isl_basic_map *bmap_i;
8251 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8252 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8253 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8256 return list;
8259 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8260 __isl_take isl_map *map)
8262 if (!set || !map)
8263 goto error;
8264 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8265 map = isl_map_intersect_domain(map, set);
8266 set = isl_map_range(map);
8267 return set;
8268 error:
8269 isl_set_free(set);
8270 isl_map_free(map);
8271 return NULL;
8274 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8275 __isl_take isl_map *map)
8277 return isl_map_align_params_map_map_and(set, map, &set_apply);
8280 /* There is no need to cow as removing empty parts doesn't change
8281 * the meaning of the set.
8283 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8285 int i;
8287 if (!map)
8288 return NULL;
8290 for (i = map->n - 1; i >= 0; --i)
8291 remove_if_empty(map, i);
8293 return map;
8296 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8298 return (struct isl_set *)
8299 isl_map_remove_empty_parts((struct isl_map *)set);
8302 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8304 struct isl_basic_map *bmap;
8305 if (!map || map->n == 0)
8306 return NULL;
8307 bmap = map->p[map->n-1];
8308 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8309 return isl_basic_map_copy(bmap);
8312 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8314 return (struct isl_basic_set *)
8315 isl_map_copy_basic_map((struct isl_map *)set);
8318 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8319 __isl_keep isl_basic_map *bmap)
8321 int i;
8323 if (!map || !bmap)
8324 goto error;
8325 for (i = map->n-1; i >= 0; --i) {
8326 if (map->p[i] != bmap)
8327 continue;
8328 map = isl_map_cow(map);
8329 if (!map)
8330 goto error;
8331 isl_basic_map_free(map->p[i]);
8332 if (i != map->n-1) {
8333 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8334 map->p[i] = map->p[map->n-1];
8336 map->n--;
8337 return map;
8339 return map;
8340 error:
8341 isl_map_free(map);
8342 return NULL;
8345 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8346 struct isl_basic_set *bset)
8348 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8349 (struct isl_basic_map *)bset);
8352 /* Given two basic sets bset1 and bset2, compute the maximal difference
8353 * between the values of dimension pos in bset1 and those in bset2
8354 * for any common value of the parameters and dimensions preceding pos.
8356 static enum isl_lp_result basic_set_maximal_difference_at(
8357 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8358 int pos, isl_int *opt)
8360 isl_space *dims;
8361 struct isl_basic_map *bmap1 = NULL;
8362 struct isl_basic_map *bmap2 = NULL;
8363 struct isl_ctx *ctx;
8364 struct isl_vec *obj;
8365 unsigned total;
8366 unsigned nparam;
8367 unsigned dim1, dim2;
8368 enum isl_lp_result res;
8370 if (!bset1 || !bset2)
8371 return isl_lp_error;
8373 nparam = isl_basic_set_n_param(bset1);
8374 dim1 = isl_basic_set_n_dim(bset1);
8375 dim2 = isl_basic_set_n_dim(bset2);
8376 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8377 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8378 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8379 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8380 if (!bmap1 || !bmap2)
8381 goto error;
8382 bmap1 = isl_basic_map_cow(bmap1);
8383 bmap1 = isl_basic_map_extend(bmap1, nparam,
8384 pos, (dim1 - pos) + (dim2 - pos),
8385 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8386 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8387 if (!bmap1)
8388 goto error2;
8389 total = isl_basic_map_total_dim(bmap1);
8390 ctx = bmap1->ctx;
8391 obj = isl_vec_alloc(ctx, 1 + total);
8392 if (!obj)
8393 goto error2;
8394 isl_seq_clr(obj->block.data, 1 + total);
8395 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8396 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8397 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8398 opt, NULL, NULL);
8399 isl_basic_map_free(bmap1);
8400 isl_vec_free(obj);
8401 return res;
8402 error:
8403 isl_basic_map_free(bmap2);
8404 error2:
8405 isl_basic_map_free(bmap1);
8406 return isl_lp_error;
8409 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8410 * for any common value of the parameters and dimensions preceding pos
8411 * in both basic sets, the values of dimension pos in bset1 are
8412 * smaller or larger than those in bset2.
8414 * Returns
8415 * 1 if bset1 follows bset2
8416 * -1 if bset1 precedes bset2
8417 * 0 if bset1 and bset2 are incomparable
8418 * -2 if some error occurred.
8420 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8421 struct isl_basic_set *bset2, int pos)
8423 isl_int opt;
8424 enum isl_lp_result res;
8425 int cmp;
8427 isl_int_init(opt);
8429 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8431 if (res == isl_lp_empty)
8432 cmp = 0;
8433 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8434 res == isl_lp_unbounded)
8435 cmp = 1;
8436 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8437 cmp = -1;
8438 else
8439 cmp = -2;
8441 isl_int_clear(opt);
8442 return cmp;
8445 /* Given two basic sets bset1 and bset2, check whether
8446 * for any common value of the parameters and dimensions preceding pos
8447 * there is a value of dimension pos in bset1 that is larger
8448 * than a value of the same dimension in bset2.
8450 * Return
8451 * 1 if there exists such a pair
8452 * 0 if there is no such pair, but there is a pair of equal values
8453 * -1 otherwise
8454 * -2 if some error occurred.
8456 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8457 __isl_keep isl_basic_set *bset2, int pos)
8459 isl_int opt;
8460 enum isl_lp_result res;
8461 int cmp;
8463 isl_int_init(opt);
8465 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8467 if (res == isl_lp_empty)
8468 cmp = -1;
8469 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8470 res == isl_lp_unbounded)
8471 cmp = 1;
8472 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8473 cmp = -1;
8474 else if (res == isl_lp_ok)
8475 cmp = 0;
8476 else
8477 cmp = -2;
8479 isl_int_clear(opt);
8480 return cmp;
8483 /* Given two sets set1 and set2, check whether
8484 * for any common value of the parameters and dimensions preceding pos
8485 * there is a value of dimension pos in set1 that is larger
8486 * than a value of the same dimension in set2.
8488 * Return
8489 * 1 if there exists such a pair
8490 * 0 if there is no such pair, but there is a pair of equal values
8491 * -1 otherwise
8492 * -2 if some error occurred.
8494 int isl_set_follows_at(__isl_keep isl_set *set1,
8495 __isl_keep isl_set *set2, int pos)
8497 int i, j;
8498 int follows = -1;
8500 if (!set1 || !set2)
8501 return -2;
8503 for (i = 0; i < set1->n; ++i)
8504 for (j = 0; j < set2->n; ++j) {
8505 int f;
8506 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8507 if (f == 1 || f == -2)
8508 return f;
8509 if (f > follows)
8510 follows = f;
8513 return follows;
8516 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8517 unsigned pos, isl_int *val)
8519 int i;
8520 int d;
8521 unsigned total;
8523 if (!bmap)
8524 return -1;
8525 total = isl_basic_map_total_dim(bmap);
8526 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8527 for (; d+1 > pos; --d)
8528 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8529 break;
8530 if (d != pos)
8531 continue;
8532 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8533 return 0;
8534 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8535 return 0;
8536 if (!isl_int_is_one(bmap->eq[i][1+d]))
8537 return 0;
8538 if (val)
8539 isl_int_neg(*val, bmap->eq[i][0]);
8540 return 1;
8542 return 0;
8545 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8546 unsigned pos, isl_int *val)
8548 int i;
8549 isl_int v;
8550 isl_int tmp;
8551 int fixed;
8553 if (!map)
8554 return -1;
8555 if (map->n == 0)
8556 return 0;
8557 if (map->n == 1)
8558 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8559 isl_int_init(v);
8560 isl_int_init(tmp);
8561 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8562 for (i = 1; fixed == 1 && i < map->n; ++i) {
8563 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8564 if (fixed == 1 && isl_int_ne(tmp, v))
8565 fixed = 0;
8567 if (val)
8568 isl_int_set(*val, v);
8569 isl_int_clear(tmp);
8570 isl_int_clear(v);
8571 return fixed;
8574 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8575 unsigned pos, isl_int *val)
8577 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8578 pos, val);
8581 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8582 isl_int *val)
8584 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8587 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8588 enum isl_dim_type type, unsigned pos, isl_int *val)
8590 if (pos >= isl_basic_map_dim(bmap, type))
8591 return -1;
8592 return isl_basic_map_plain_has_fixed_var(bmap,
8593 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8596 /* If "bmap" obviously lies on a hyperplane where the given dimension
8597 * has a fixed value, then return that value.
8598 * Otherwise return NaN.
8600 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8601 __isl_keep isl_basic_map *bmap,
8602 enum isl_dim_type type, unsigned pos)
8604 isl_ctx *ctx;
8605 isl_val *v;
8606 int fixed;
8608 if (!bmap)
8609 return NULL;
8610 ctx = isl_basic_map_get_ctx(bmap);
8611 v = isl_val_alloc(ctx);
8612 if (!v)
8613 return NULL;
8614 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8615 if (fixed < 0)
8616 return isl_val_free(v);
8617 if (fixed) {
8618 isl_int_set_si(v->d, 1);
8619 return v;
8621 isl_val_free(v);
8622 return isl_val_nan(ctx);
8625 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8626 enum isl_dim_type type, unsigned pos, isl_int *val)
8628 if (pos >= isl_map_dim(map, type))
8629 return -1;
8630 return isl_map_plain_has_fixed_var(map,
8631 map_offset(map, type) - 1 + pos, val);
8634 /* If "map" obviously lies on a hyperplane where the given dimension
8635 * has a fixed value, then return that value.
8636 * Otherwise return NaN.
8638 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8639 enum isl_dim_type type, unsigned pos)
8641 isl_ctx *ctx;
8642 isl_val *v;
8643 int fixed;
8645 if (!map)
8646 return NULL;
8647 ctx = isl_map_get_ctx(map);
8648 v = isl_val_alloc(ctx);
8649 if (!v)
8650 return NULL;
8651 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8652 if (fixed < 0)
8653 return isl_val_free(v);
8654 if (fixed) {
8655 isl_int_set_si(v->d, 1);
8656 return v;
8658 isl_val_free(v);
8659 return isl_val_nan(ctx);
8662 /* If "set" obviously lies on a hyperplane where the given dimension
8663 * has a fixed value, then return that value.
8664 * Otherwise return NaN.
8666 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8667 enum isl_dim_type type, unsigned pos)
8669 return isl_map_plain_get_val_if_fixed(set, type, pos);
8672 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8673 enum isl_dim_type type, unsigned pos, isl_int *val)
8675 return isl_map_plain_is_fixed(set, type, pos, val);
8678 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8679 * then return this fixed value in *val.
8681 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8682 unsigned dim, isl_int *val)
8684 return isl_basic_set_plain_has_fixed_var(bset,
8685 isl_basic_set_n_param(bset) + dim, val);
8688 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8689 * then return this fixed value in *val.
8691 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8692 unsigned dim, isl_int *val)
8694 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8697 /* Check if input variable in has fixed value and if so and if val is not NULL,
8698 * then return this fixed value in *val.
8700 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8701 unsigned in, isl_int *val)
8703 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8706 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8707 * and if val is not NULL, then return this lower bound in *val.
8709 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8710 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8712 int i, i_eq = -1, i_ineq = -1;
8713 isl_int *c;
8714 unsigned total;
8715 unsigned nparam;
8717 if (!bset)
8718 return -1;
8719 total = isl_basic_set_total_dim(bset);
8720 nparam = isl_basic_set_n_param(bset);
8721 for (i = 0; i < bset->n_eq; ++i) {
8722 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8723 continue;
8724 if (i_eq != -1)
8725 return 0;
8726 i_eq = i;
8728 for (i = 0; i < bset->n_ineq; ++i) {
8729 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8730 continue;
8731 if (i_eq != -1 || i_ineq != -1)
8732 return 0;
8733 i_ineq = i;
8735 if (i_eq == -1 && i_ineq == -1)
8736 return 0;
8737 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8738 /* The coefficient should always be one due to normalization. */
8739 if (!isl_int_is_one(c[1+nparam+dim]))
8740 return 0;
8741 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8742 return 0;
8743 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8744 total - nparam - dim - 1) != -1)
8745 return 0;
8746 if (val)
8747 isl_int_neg(*val, c[0]);
8748 return 1;
8751 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8752 unsigned dim, isl_int *val)
8754 int i;
8755 isl_int v;
8756 isl_int tmp;
8757 int fixed;
8759 if (!set)
8760 return -1;
8761 if (set->n == 0)
8762 return 0;
8763 if (set->n == 1)
8764 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8765 dim, val);
8766 isl_int_init(v);
8767 isl_int_init(tmp);
8768 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8769 dim, &v);
8770 for (i = 1; fixed == 1 && i < set->n; ++i) {
8771 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8772 dim, &tmp);
8773 if (fixed == 1 && isl_int_ne(tmp, v))
8774 fixed = 0;
8776 if (val)
8777 isl_int_set(*val, v);
8778 isl_int_clear(tmp);
8779 isl_int_clear(v);
8780 return fixed;
8783 /* uset_gist depends on constraints without existentially quantified
8784 * variables sorting first.
8786 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8788 isl_int **c1 = (isl_int **) p1;
8789 isl_int **c2 = (isl_int **) p2;
8790 int l1, l2;
8791 unsigned size = *(unsigned *) arg;
8793 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8794 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8796 if (l1 != l2)
8797 return l1 - l2;
8799 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8802 static struct isl_basic_map *isl_basic_map_sort_constraints(
8803 struct isl_basic_map *bmap)
8805 unsigned total;
8807 if (!bmap)
8808 return NULL;
8809 if (bmap->n_ineq == 0)
8810 return bmap;
8811 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8812 return bmap;
8813 total = isl_basic_map_total_dim(bmap);
8814 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
8815 &sort_constraint_cmp, &total) < 0)
8816 return isl_basic_map_free(bmap);
8817 return bmap;
8820 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8821 __isl_take isl_basic_set *bset)
8823 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8824 (struct isl_basic_map *)bset);
8827 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8829 if (!bmap)
8830 return NULL;
8831 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8832 return bmap;
8833 bmap = isl_basic_map_remove_redundancies(bmap);
8834 bmap = isl_basic_map_sort_constraints(bmap);
8835 if (bmap)
8836 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8837 return bmap;
8840 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8842 return (struct isl_basic_set *)isl_basic_map_normalize(
8843 (struct isl_basic_map *)bset);
8846 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8847 const __isl_keep isl_basic_map *bmap2)
8849 int i, cmp;
8850 unsigned total;
8852 if (!bmap1 || !bmap2)
8853 return -1;
8855 if (bmap1 == bmap2)
8856 return 0;
8857 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8858 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8859 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8860 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8861 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8862 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8863 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8864 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8865 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8866 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8867 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8868 return 0;
8869 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8870 return 1;
8871 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8872 return -1;
8873 if (bmap1->n_eq != bmap2->n_eq)
8874 return bmap1->n_eq - bmap2->n_eq;
8875 if (bmap1->n_ineq != bmap2->n_ineq)
8876 return bmap1->n_ineq - bmap2->n_ineq;
8877 if (bmap1->n_div != bmap2->n_div)
8878 return bmap1->n_div - bmap2->n_div;
8879 total = isl_basic_map_total_dim(bmap1);
8880 for (i = 0; i < bmap1->n_eq; ++i) {
8881 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8882 if (cmp)
8883 return cmp;
8885 for (i = 0; i < bmap1->n_ineq; ++i) {
8886 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8887 if (cmp)
8888 return cmp;
8890 for (i = 0; i < bmap1->n_div; ++i) {
8891 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8892 if (cmp)
8893 return cmp;
8895 return 0;
8898 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8899 const __isl_keep isl_basic_set *bset2)
8901 return isl_basic_map_plain_cmp(bset1, bset2);
8904 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8906 int i, cmp;
8908 if (set1 == set2)
8909 return 0;
8910 if (set1->n != set2->n)
8911 return set1->n - set2->n;
8913 for (i = 0; i < set1->n; ++i) {
8914 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8915 if (cmp)
8916 return cmp;
8919 return 0;
8922 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8923 __isl_keep isl_basic_map *bmap2)
8925 if (!bmap1 || !bmap2)
8926 return isl_bool_error;
8927 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8930 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8931 __isl_keep isl_basic_set *bset2)
8933 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8934 (isl_basic_map *)bset2);
8937 static int qsort_bmap_cmp(const void *p1, const void *p2)
8939 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8940 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8942 return isl_basic_map_plain_cmp(bmap1, bmap2);
8945 /* Sort the basic maps of "map" and remove duplicate basic maps.
8947 * While removing basic maps, we make sure that the basic maps remain
8948 * sorted because isl_map_normalize expects the basic maps of the result
8949 * to be sorted.
8951 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
8953 int i, j;
8955 map = isl_map_remove_empty_parts(map);
8956 if (!map)
8957 return NULL;
8958 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8959 for (i = map->n - 1; i >= 1; --i) {
8960 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
8961 continue;
8962 isl_basic_map_free(map->p[i-1]);
8963 for (j = i; j < map->n; ++j)
8964 map->p[j - 1] = map->p[j];
8965 map->n--;
8968 return map;
8971 /* Remove obvious duplicates among the basic maps of "map".
8973 * Unlike isl_map_normalize, this function does not remove redundant
8974 * constraints and only removes duplicates that have exactly the same
8975 * constraints in the input. It does sort the constraints and
8976 * the basic maps to ease the detection of duplicates.
8978 * If "map" has already been normalized or if the basic maps are
8979 * disjoint, then there can be no duplicates.
8981 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
8983 int i;
8984 isl_basic_map *bmap;
8986 if (!map)
8987 return NULL;
8988 if (map->n <= 1)
8989 return map;
8990 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
8991 return map;
8992 for (i = 0; i < map->n; ++i) {
8993 bmap = isl_basic_map_copy(map->p[i]);
8994 bmap = isl_basic_map_sort_constraints(bmap);
8995 if (!bmap)
8996 return isl_map_free(map);
8997 isl_basic_map_free(map->p[i]);
8998 map->p[i] = bmap;
9001 map = sort_and_remove_duplicates(map);
9002 return map;
9005 /* We normalize in place, but if anything goes wrong we need
9006 * to return NULL, so we need to make sure we don't change the
9007 * meaning of any possible other copies of map.
9009 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9011 int i;
9012 struct isl_basic_map *bmap;
9014 if (!map)
9015 return NULL;
9016 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9017 return map;
9018 for (i = 0; i < map->n; ++i) {
9019 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9020 if (!bmap)
9021 goto error;
9022 isl_basic_map_free(map->p[i]);
9023 map->p[i] = bmap;
9026 map = sort_and_remove_duplicates(map);
9027 if (map)
9028 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9029 return map;
9030 error:
9031 isl_map_free(map);
9032 return NULL;
9035 struct isl_set *isl_set_normalize(struct isl_set *set)
9037 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9040 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9041 __isl_keep isl_map *map2)
9043 int i;
9044 isl_bool equal;
9046 if (!map1 || !map2)
9047 return isl_bool_error;
9049 if (map1 == map2)
9050 return isl_bool_true;
9051 if (!isl_space_is_equal(map1->dim, map2->dim))
9052 return isl_bool_false;
9054 map1 = isl_map_copy(map1);
9055 map2 = isl_map_copy(map2);
9056 map1 = isl_map_normalize(map1);
9057 map2 = isl_map_normalize(map2);
9058 if (!map1 || !map2)
9059 goto error;
9060 equal = map1->n == map2->n;
9061 for (i = 0; equal && i < map1->n; ++i) {
9062 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9063 if (equal < 0)
9064 goto error;
9066 isl_map_free(map1);
9067 isl_map_free(map2);
9068 return equal;
9069 error:
9070 isl_map_free(map1);
9071 isl_map_free(map2);
9072 return isl_bool_error;
9075 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9076 __isl_keep isl_set *set2)
9078 return isl_map_plain_is_equal((struct isl_map *)set1,
9079 (struct isl_map *)set2);
9082 /* Return an interval that ranges from min to max (inclusive)
9084 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9085 isl_int min, isl_int max)
9087 int k;
9088 struct isl_basic_set *bset = NULL;
9090 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9091 if (!bset)
9092 goto error;
9094 k = isl_basic_set_alloc_inequality(bset);
9095 if (k < 0)
9096 goto error;
9097 isl_int_set_si(bset->ineq[k][1], 1);
9098 isl_int_neg(bset->ineq[k][0], min);
9100 k = isl_basic_set_alloc_inequality(bset);
9101 if (k < 0)
9102 goto error;
9103 isl_int_set_si(bset->ineq[k][1], -1);
9104 isl_int_set(bset->ineq[k][0], max);
9106 return bset;
9107 error:
9108 isl_basic_set_free(bset);
9109 return NULL;
9112 /* Return the basic maps in "map" as a list.
9114 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9115 __isl_keep isl_map *map)
9117 int i;
9118 isl_ctx *ctx;
9119 isl_basic_map_list *list;
9121 if (!map)
9122 return NULL;
9123 ctx = isl_map_get_ctx(map);
9124 list = isl_basic_map_list_alloc(ctx, map->n);
9126 for (i = 0; i < map->n; ++i) {
9127 isl_basic_map *bmap;
9129 bmap = isl_basic_map_copy(map->p[i]);
9130 list = isl_basic_map_list_add(list, bmap);
9133 return list;
9136 /* Return the intersection of the elements in the non-empty list "list".
9137 * All elements are assumed to live in the same space.
9139 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9140 __isl_take isl_basic_map_list *list)
9142 int i, n;
9143 isl_basic_map *bmap;
9145 if (!list)
9146 return NULL;
9147 n = isl_basic_map_list_n_basic_map(list);
9148 if (n < 1)
9149 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9150 "expecting non-empty list", goto error);
9152 bmap = isl_basic_map_list_get_basic_map(list, 0);
9153 for (i = 1; i < n; ++i) {
9154 isl_basic_map *bmap_i;
9156 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9157 bmap = isl_basic_map_intersect(bmap, bmap_i);
9160 isl_basic_map_list_free(list);
9161 return bmap;
9162 error:
9163 isl_basic_map_list_free(list);
9164 return NULL;
9167 /* Return the intersection of the elements in the non-empty list "list".
9168 * All elements are assumed to live in the same space.
9170 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9171 __isl_take isl_basic_set_list *list)
9173 return isl_basic_map_list_intersect(list);
9176 /* Return the Cartesian product of the basic sets in list (in the given order).
9178 __isl_give isl_basic_set *isl_basic_set_list_product(
9179 __isl_take struct isl_basic_set_list *list)
9181 int i;
9182 unsigned dim;
9183 unsigned nparam;
9184 unsigned extra;
9185 unsigned n_eq;
9186 unsigned n_ineq;
9187 struct isl_basic_set *product = NULL;
9189 if (!list)
9190 goto error;
9191 isl_assert(list->ctx, list->n > 0, goto error);
9192 isl_assert(list->ctx, list->p[0], goto error);
9193 nparam = isl_basic_set_n_param(list->p[0]);
9194 dim = isl_basic_set_n_dim(list->p[0]);
9195 extra = list->p[0]->n_div;
9196 n_eq = list->p[0]->n_eq;
9197 n_ineq = list->p[0]->n_ineq;
9198 for (i = 1; i < list->n; ++i) {
9199 isl_assert(list->ctx, list->p[i], goto error);
9200 isl_assert(list->ctx,
9201 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9202 dim += isl_basic_set_n_dim(list->p[i]);
9203 extra += list->p[i]->n_div;
9204 n_eq += list->p[i]->n_eq;
9205 n_ineq += list->p[i]->n_ineq;
9207 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9208 n_eq, n_ineq);
9209 if (!product)
9210 goto error;
9211 dim = 0;
9212 for (i = 0; i < list->n; ++i) {
9213 isl_basic_set_add_constraints(product,
9214 isl_basic_set_copy(list->p[i]), dim);
9215 dim += isl_basic_set_n_dim(list->p[i]);
9217 isl_basic_set_list_free(list);
9218 return product;
9219 error:
9220 isl_basic_set_free(product);
9221 isl_basic_set_list_free(list);
9222 return NULL;
9225 struct isl_basic_map *isl_basic_map_product(
9226 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9228 isl_space *dim_result = NULL;
9229 struct isl_basic_map *bmap;
9230 unsigned in1, in2, out1, out2, nparam, total, pos;
9231 struct isl_dim_map *dim_map1, *dim_map2;
9233 if (!bmap1 || !bmap2)
9234 goto error;
9236 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9237 bmap2->dim, isl_dim_param), goto error);
9238 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9239 isl_space_copy(bmap2->dim));
9241 in1 = isl_basic_map_n_in(bmap1);
9242 in2 = isl_basic_map_n_in(bmap2);
9243 out1 = isl_basic_map_n_out(bmap1);
9244 out2 = isl_basic_map_n_out(bmap2);
9245 nparam = isl_basic_map_n_param(bmap1);
9247 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9248 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9249 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9250 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9251 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9252 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9253 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9254 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9255 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9256 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9257 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9259 bmap = isl_basic_map_alloc_space(dim_result,
9260 bmap1->n_div + bmap2->n_div,
9261 bmap1->n_eq + bmap2->n_eq,
9262 bmap1->n_ineq + bmap2->n_ineq);
9263 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9264 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9265 bmap = isl_basic_map_simplify(bmap);
9266 return isl_basic_map_finalize(bmap);
9267 error:
9268 isl_basic_map_free(bmap1);
9269 isl_basic_map_free(bmap2);
9270 return NULL;
9273 __isl_give isl_basic_map *isl_basic_map_flat_product(
9274 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9276 isl_basic_map *prod;
9278 prod = isl_basic_map_product(bmap1, bmap2);
9279 prod = isl_basic_map_flatten(prod);
9280 return prod;
9283 __isl_give isl_basic_set *isl_basic_set_flat_product(
9284 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9286 return isl_basic_map_flat_range_product(bset1, bset2);
9289 __isl_give isl_basic_map *isl_basic_map_domain_product(
9290 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9292 isl_space *space_result = NULL;
9293 isl_basic_map *bmap;
9294 unsigned in1, in2, out, nparam, total, pos;
9295 struct isl_dim_map *dim_map1, *dim_map2;
9297 if (!bmap1 || !bmap2)
9298 goto error;
9300 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9301 isl_space_copy(bmap2->dim));
9303 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9304 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9305 out = isl_basic_map_dim(bmap1, isl_dim_out);
9306 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9308 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9309 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9310 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9311 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9312 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9313 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9314 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9315 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9316 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9317 isl_dim_map_div(dim_map1, bmap1, pos += out);
9318 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9320 bmap = isl_basic_map_alloc_space(space_result,
9321 bmap1->n_div + bmap2->n_div,
9322 bmap1->n_eq + bmap2->n_eq,
9323 bmap1->n_ineq + bmap2->n_ineq);
9324 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9325 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9326 bmap = isl_basic_map_simplify(bmap);
9327 return isl_basic_map_finalize(bmap);
9328 error:
9329 isl_basic_map_free(bmap1);
9330 isl_basic_map_free(bmap2);
9331 return NULL;
9334 __isl_give isl_basic_map *isl_basic_map_range_product(
9335 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9337 isl_space *dim_result = NULL;
9338 isl_basic_map *bmap;
9339 unsigned in, out1, out2, nparam, total, pos;
9340 struct isl_dim_map *dim_map1, *dim_map2;
9342 if (!bmap1 || !bmap2)
9343 goto error;
9345 if (!isl_space_match(bmap1->dim, isl_dim_param,
9346 bmap2->dim, isl_dim_param))
9347 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9348 "parameters don't match", goto error);
9350 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9351 isl_space_copy(bmap2->dim));
9353 in = isl_basic_map_dim(bmap1, isl_dim_in);
9354 out1 = isl_basic_map_n_out(bmap1);
9355 out2 = isl_basic_map_n_out(bmap2);
9356 nparam = isl_basic_map_n_param(bmap1);
9358 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9359 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9360 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9361 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9362 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9363 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9364 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9365 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9366 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9367 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9368 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9370 bmap = isl_basic_map_alloc_space(dim_result,
9371 bmap1->n_div + bmap2->n_div,
9372 bmap1->n_eq + bmap2->n_eq,
9373 bmap1->n_ineq + bmap2->n_ineq);
9374 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9375 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9376 bmap = isl_basic_map_simplify(bmap);
9377 return isl_basic_map_finalize(bmap);
9378 error:
9379 isl_basic_map_free(bmap1);
9380 isl_basic_map_free(bmap2);
9381 return NULL;
9384 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9385 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9387 isl_basic_map *prod;
9389 prod = isl_basic_map_range_product(bmap1, bmap2);
9390 prod = isl_basic_map_flatten_range(prod);
9391 return prod;
9394 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9395 * and collect the results.
9396 * The result live in the space obtained by calling "space_product"
9397 * on the spaces of "map1" and "map2".
9398 * If "remove_duplicates" is set then the result may contain duplicates
9399 * (even if the inputs do not) and so we try and remove the obvious
9400 * duplicates.
9402 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9403 __isl_take isl_map *map2,
9404 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9405 __isl_take isl_space *right),
9406 __isl_give isl_basic_map *(*basic_map_product)(
9407 __isl_take isl_basic_map *left,
9408 __isl_take isl_basic_map *right),
9409 int remove_duplicates)
9411 unsigned flags = 0;
9412 struct isl_map *result;
9413 int i, j;
9415 if (!map1 || !map2)
9416 goto error;
9418 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9419 map2->dim, isl_dim_param), goto error);
9421 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9422 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9423 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9425 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9426 isl_space_copy(map2->dim)),
9427 map1->n * map2->n, flags);
9428 if (!result)
9429 goto error;
9430 for (i = 0; i < map1->n; ++i)
9431 for (j = 0; j < map2->n; ++j) {
9432 struct isl_basic_map *part;
9433 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9434 isl_basic_map_copy(map2->p[j]));
9435 if (isl_basic_map_is_empty(part))
9436 isl_basic_map_free(part);
9437 else
9438 result = isl_map_add_basic_map(result, part);
9439 if (!result)
9440 goto error;
9442 if (remove_duplicates)
9443 result = isl_map_remove_obvious_duplicates(result);
9444 isl_map_free(map1);
9445 isl_map_free(map2);
9446 return result;
9447 error:
9448 isl_map_free(map1);
9449 isl_map_free(map2);
9450 return NULL;
9453 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9455 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9456 __isl_take isl_map *map2)
9458 return map_product(map1, map2, &isl_space_product,
9459 &isl_basic_map_product, 0);
9462 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9463 __isl_take isl_map *map2)
9465 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9468 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9470 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9471 __isl_take isl_map *map2)
9473 isl_map *prod;
9475 prod = isl_map_product(map1, map2);
9476 prod = isl_map_flatten(prod);
9477 return prod;
9480 /* Given two set A and B, construct its Cartesian product A x B.
9482 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9484 return isl_map_range_product(set1, set2);
9487 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9488 __isl_take isl_set *set2)
9490 return isl_map_flat_range_product(set1, set2);
9493 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9495 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9496 __isl_take isl_map *map2)
9498 return map_product(map1, map2, &isl_space_domain_product,
9499 &isl_basic_map_domain_product, 1);
9502 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9504 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9505 __isl_take isl_map *map2)
9507 return map_product(map1, map2, &isl_space_range_product,
9508 &isl_basic_map_range_product, 1);
9511 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9512 __isl_take isl_map *map2)
9514 return isl_map_align_params_map_map_and(map1, map2,
9515 &map_domain_product_aligned);
9518 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9519 __isl_take isl_map *map2)
9521 return isl_map_align_params_map_map_and(map1, map2,
9522 &map_range_product_aligned);
9525 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9527 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9529 isl_space *space;
9530 int total1, keep1, total2, keep2;
9532 if (!map)
9533 return NULL;
9534 if (!isl_space_domain_is_wrapping(map->dim) ||
9535 !isl_space_range_is_wrapping(map->dim))
9536 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9537 "not a product", return isl_map_free(map));
9539 space = isl_map_get_space(map);
9540 total1 = isl_space_dim(space, isl_dim_in);
9541 total2 = isl_space_dim(space, isl_dim_out);
9542 space = isl_space_factor_domain(space);
9543 keep1 = isl_space_dim(space, isl_dim_in);
9544 keep2 = isl_space_dim(space, isl_dim_out);
9545 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9546 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9547 map = isl_map_reset_space(map, space);
9549 return map;
9552 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9554 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9556 isl_space *space;
9557 int total1, keep1, total2, keep2;
9559 if (!map)
9560 return NULL;
9561 if (!isl_space_domain_is_wrapping(map->dim) ||
9562 !isl_space_range_is_wrapping(map->dim))
9563 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9564 "not a product", return isl_map_free(map));
9566 space = isl_map_get_space(map);
9567 total1 = isl_space_dim(space, isl_dim_in);
9568 total2 = isl_space_dim(space, isl_dim_out);
9569 space = isl_space_factor_range(space);
9570 keep1 = isl_space_dim(space, isl_dim_in);
9571 keep2 = isl_space_dim(space, isl_dim_out);
9572 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9573 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9574 map = isl_map_reset_space(map, space);
9576 return map;
9579 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9581 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9583 isl_space *space;
9584 int total, keep;
9586 if (!map)
9587 return NULL;
9588 if (!isl_space_domain_is_wrapping(map->dim))
9589 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9590 "domain is not a product", return isl_map_free(map));
9592 space = isl_map_get_space(map);
9593 total = isl_space_dim(space, isl_dim_in);
9594 space = isl_space_domain_factor_domain(space);
9595 keep = isl_space_dim(space, isl_dim_in);
9596 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9597 map = isl_map_reset_space(map, space);
9599 return map;
9602 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9604 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9606 isl_space *space;
9607 int total, keep;
9609 if (!map)
9610 return NULL;
9611 if (!isl_space_domain_is_wrapping(map->dim))
9612 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9613 "domain is not a product", return isl_map_free(map));
9615 space = isl_map_get_space(map);
9616 total = isl_space_dim(space, isl_dim_in);
9617 space = isl_space_domain_factor_range(space);
9618 keep = isl_space_dim(space, isl_dim_in);
9619 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9620 map = isl_map_reset_space(map, space);
9622 return map;
9625 /* Given a map A -> [B -> C], extract the map A -> B.
9627 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9629 isl_space *space;
9630 int total, keep;
9632 if (!map)
9633 return NULL;
9634 if (!isl_space_range_is_wrapping(map->dim))
9635 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9636 "range is not a product", return isl_map_free(map));
9638 space = isl_map_get_space(map);
9639 total = isl_space_dim(space, isl_dim_out);
9640 space = isl_space_range_factor_domain(space);
9641 keep = isl_space_dim(space, isl_dim_out);
9642 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9643 map = isl_map_reset_space(map, space);
9645 return map;
9648 /* Given a map A -> [B -> C], extract the map A -> C.
9650 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9652 isl_space *space;
9653 int total, keep;
9655 if (!map)
9656 return NULL;
9657 if (!isl_space_range_is_wrapping(map->dim))
9658 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9659 "range is not a product", return isl_map_free(map));
9661 space = isl_map_get_space(map);
9662 total = isl_space_dim(space, isl_dim_out);
9663 space = isl_space_range_factor_range(space);
9664 keep = isl_space_dim(space, isl_dim_out);
9665 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9666 map = isl_map_reset_space(map, space);
9668 return map;
9671 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9673 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9674 __isl_take isl_map *map2)
9676 isl_map *prod;
9678 prod = isl_map_domain_product(map1, map2);
9679 prod = isl_map_flatten_domain(prod);
9680 return prod;
9683 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9685 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9686 __isl_take isl_map *map2)
9688 isl_map *prod;
9690 prod = isl_map_range_product(map1, map2);
9691 prod = isl_map_flatten_range(prod);
9692 return prod;
9695 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9697 int i;
9698 uint32_t hash = isl_hash_init();
9699 unsigned total;
9701 if (!bmap)
9702 return 0;
9703 bmap = isl_basic_map_copy(bmap);
9704 bmap = isl_basic_map_normalize(bmap);
9705 if (!bmap)
9706 return 0;
9707 total = isl_basic_map_total_dim(bmap);
9708 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9709 for (i = 0; i < bmap->n_eq; ++i) {
9710 uint32_t c_hash;
9711 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9712 isl_hash_hash(hash, c_hash);
9714 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9715 for (i = 0; i < bmap->n_ineq; ++i) {
9716 uint32_t c_hash;
9717 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9718 isl_hash_hash(hash, c_hash);
9720 isl_hash_byte(hash, bmap->n_div & 0xFF);
9721 for (i = 0; i < bmap->n_div; ++i) {
9722 uint32_t c_hash;
9723 if (isl_int_is_zero(bmap->div[i][0]))
9724 continue;
9725 isl_hash_byte(hash, i & 0xFF);
9726 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9727 isl_hash_hash(hash, c_hash);
9729 isl_basic_map_free(bmap);
9730 return hash;
9733 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9735 return isl_basic_map_get_hash((isl_basic_map *)bset);
9738 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9740 int i;
9741 uint32_t hash;
9743 if (!map)
9744 return 0;
9745 map = isl_map_copy(map);
9746 map = isl_map_normalize(map);
9747 if (!map)
9748 return 0;
9750 hash = isl_hash_init();
9751 for (i = 0; i < map->n; ++i) {
9752 uint32_t bmap_hash;
9753 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9754 isl_hash_hash(hash, bmap_hash);
9757 isl_map_free(map);
9759 return hash;
9762 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9764 return isl_map_get_hash((isl_map *)set);
9767 /* Check if the value for dimension dim is completely determined
9768 * by the values of the other parameters and variables.
9769 * That is, check if dimension dim is involved in an equality.
9771 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9773 int i;
9774 unsigned nparam;
9776 if (!bset)
9777 return -1;
9778 nparam = isl_basic_set_n_param(bset);
9779 for (i = 0; i < bset->n_eq; ++i)
9780 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9781 return 1;
9782 return 0;
9785 /* Check if the value for dimension dim is completely determined
9786 * by the values of the other parameters and variables.
9787 * That is, check if dimension dim is involved in an equality
9788 * for each of the subsets.
9790 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9792 int i;
9794 if (!set)
9795 return -1;
9796 for (i = 0; i < set->n; ++i) {
9797 int unique;
9798 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9799 if (unique != 1)
9800 return unique;
9802 return 1;
9805 /* Return the number of basic maps in the (current) representation of "map".
9807 int isl_map_n_basic_map(__isl_keep isl_map *map)
9809 return map ? map->n : 0;
9812 int isl_set_n_basic_set(__isl_keep isl_set *set)
9814 return set ? set->n : 0;
9817 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
9818 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9820 int i;
9822 if (!map)
9823 return isl_stat_error;
9825 for (i = 0; i < map->n; ++i)
9826 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9827 return isl_stat_error;
9829 return isl_stat_ok;
9832 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
9833 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9835 int i;
9837 if (!set)
9838 return isl_stat_error;
9840 for (i = 0; i < set->n; ++i)
9841 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9842 return isl_stat_error;
9844 return isl_stat_ok;
9847 /* Return a list of basic sets, the union of which is equal to "set".
9849 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9850 __isl_keep isl_set *set)
9852 int i;
9853 isl_basic_set_list *list;
9855 if (!set)
9856 return NULL;
9858 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
9859 for (i = 0; i < set->n; ++i) {
9860 isl_basic_set *bset;
9862 bset = isl_basic_set_copy(set->p[i]);
9863 list = isl_basic_set_list_add(list, bset);
9866 return list;
9869 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9871 isl_space *dim;
9873 if (!bset)
9874 return NULL;
9876 bset = isl_basic_set_cow(bset);
9877 if (!bset)
9878 return NULL;
9880 dim = isl_basic_set_get_space(bset);
9881 dim = isl_space_lift(dim, bset->n_div);
9882 if (!dim)
9883 goto error;
9884 isl_space_free(bset->dim);
9885 bset->dim = dim;
9886 bset->extra -= bset->n_div;
9887 bset->n_div = 0;
9889 bset = isl_basic_set_finalize(bset);
9891 return bset;
9892 error:
9893 isl_basic_set_free(bset);
9894 return NULL;
9897 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9899 int i;
9900 isl_space *dim;
9901 unsigned n_div;
9903 set = isl_set_align_divs(set);
9905 if (!set)
9906 return NULL;
9908 set = isl_set_cow(set);
9909 if (!set)
9910 return NULL;
9912 n_div = set->p[0]->n_div;
9913 dim = isl_set_get_space(set);
9914 dim = isl_space_lift(dim, n_div);
9915 if (!dim)
9916 goto error;
9917 isl_space_free(set->dim);
9918 set->dim = dim;
9920 for (i = 0; i < set->n; ++i) {
9921 set->p[i] = isl_basic_set_lift(set->p[i]);
9922 if (!set->p[i])
9923 goto error;
9926 return set;
9927 error:
9928 isl_set_free(set);
9929 return NULL;
9932 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9934 isl_space *dim;
9935 struct isl_basic_map *bmap;
9936 unsigned n_set;
9937 unsigned n_div;
9938 unsigned n_param;
9939 unsigned total;
9940 int i, k, l;
9942 set = isl_set_align_divs(set);
9944 if (!set)
9945 return NULL;
9947 dim = isl_set_get_space(set);
9948 if (set->n == 0 || set->p[0]->n_div == 0) {
9949 isl_set_free(set);
9950 return isl_map_identity(isl_space_map_from_set(dim));
9953 n_div = set->p[0]->n_div;
9954 dim = isl_space_map_from_set(dim);
9955 n_param = isl_space_dim(dim, isl_dim_param);
9956 n_set = isl_space_dim(dim, isl_dim_in);
9957 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9958 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9959 for (i = 0; i < n_set; ++i)
9960 bmap = var_equal(bmap, i);
9962 total = n_param + n_set + n_set + n_div;
9963 for (i = 0; i < n_div; ++i) {
9964 k = isl_basic_map_alloc_inequality(bmap);
9965 if (k < 0)
9966 goto error;
9967 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9968 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9969 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9970 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9971 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9972 set->p[0]->div[i][0]);
9974 l = isl_basic_map_alloc_inequality(bmap);
9975 if (l < 0)
9976 goto error;
9977 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9978 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9979 set->p[0]->div[i][0]);
9980 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9983 isl_set_free(set);
9984 bmap = isl_basic_map_simplify(bmap);
9985 bmap = isl_basic_map_finalize(bmap);
9986 return isl_map_from_basic_map(bmap);
9987 error:
9988 isl_set_free(set);
9989 isl_basic_map_free(bmap);
9990 return NULL;
9993 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9995 unsigned dim;
9996 int size = 0;
9998 if (!bset)
9999 return -1;
10001 dim = isl_basic_set_total_dim(bset);
10002 size += bset->n_eq * (1 + dim);
10003 size += bset->n_ineq * (1 + dim);
10004 size += bset->n_div * (2 + dim);
10006 return size;
10009 int isl_set_size(__isl_keep isl_set *set)
10011 int i;
10012 int size = 0;
10014 if (!set)
10015 return -1;
10017 for (i = 0; i < set->n; ++i)
10018 size += isl_basic_set_size(set->p[i]);
10020 return size;
10023 /* Check if there is any lower bound (if lower == 0) and/or upper
10024 * bound (if upper == 0) on the specified dim.
10026 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10027 enum isl_dim_type type, unsigned pos, int lower, int upper)
10029 int i;
10031 if (!bmap)
10032 return isl_bool_error;
10034 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10035 return isl_bool_error);
10037 pos += isl_basic_map_offset(bmap, type);
10039 for (i = 0; i < bmap->n_div; ++i) {
10040 if (isl_int_is_zero(bmap->div[i][0]))
10041 continue;
10042 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10043 return isl_bool_true;
10046 for (i = 0; i < bmap->n_eq; ++i)
10047 if (!isl_int_is_zero(bmap->eq[i][pos]))
10048 return isl_bool_true;
10050 for (i = 0; i < bmap->n_ineq; ++i) {
10051 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10052 if (sgn > 0)
10053 lower = 1;
10054 if (sgn < 0)
10055 upper = 1;
10058 return lower && upper;
10061 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10062 enum isl_dim_type type, unsigned pos)
10064 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10067 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10068 enum isl_dim_type type, unsigned pos)
10070 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10073 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10074 enum isl_dim_type type, unsigned pos)
10076 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10079 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10080 enum isl_dim_type type, unsigned pos)
10082 int i;
10084 if (!map)
10085 return -1;
10087 for (i = 0; i < map->n; ++i) {
10088 int bounded;
10089 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10090 if (bounded < 0 || !bounded)
10091 return bounded;
10094 return 1;
10097 /* Return 1 if the specified dim is involved in both an upper bound
10098 * and a lower bound.
10100 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10101 enum isl_dim_type type, unsigned pos)
10103 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10106 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10108 static isl_bool has_any_bound(__isl_keep isl_map *map,
10109 enum isl_dim_type type, unsigned pos,
10110 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10111 enum isl_dim_type type, unsigned pos))
10113 int i;
10115 if (!map)
10116 return isl_bool_error;
10118 for (i = 0; i < map->n; ++i) {
10119 isl_bool bounded;
10120 bounded = fn(map->p[i], type, pos);
10121 if (bounded < 0 || bounded)
10122 return bounded;
10125 return isl_bool_false;
10128 /* Return 1 if the specified dim is involved in any lower bound.
10130 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10131 enum isl_dim_type type, unsigned pos)
10133 return has_any_bound(set, type, pos,
10134 &isl_basic_map_dim_has_lower_bound);
10137 /* Return 1 if the specified dim is involved in any upper bound.
10139 isl_bool isl_set_dim_has_any_upper_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_upper_bound);
10146 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10148 static isl_bool has_bound(__isl_keep isl_map *map,
10149 enum isl_dim_type type, unsigned pos,
10150 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10151 enum isl_dim_type type, unsigned pos))
10153 int i;
10155 if (!map)
10156 return isl_bool_error;
10158 for (i = 0; i < map->n; ++i) {
10159 isl_bool bounded;
10160 bounded = fn(map->p[i], type, pos);
10161 if (bounded < 0 || !bounded)
10162 return bounded;
10165 return isl_bool_true;
10168 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10170 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10171 enum isl_dim_type type, unsigned pos)
10173 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10176 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10178 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10179 enum isl_dim_type type, unsigned pos)
10181 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10184 /* For each of the "n" variables starting at "first", determine
10185 * the sign of the variable and put the results in the first "n"
10186 * elements of the array "signs".
10187 * Sign
10188 * 1 means that the variable is non-negative
10189 * -1 means that the variable is non-positive
10190 * 0 means the variable attains both positive and negative values.
10192 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10193 unsigned first, unsigned n, int *signs)
10195 isl_vec *bound = NULL;
10196 struct isl_tab *tab = NULL;
10197 struct isl_tab_undo *snap;
10198 int i;
10200 if (!bset || !signs)
10201 return -1;
10203 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10204 tab = isl_tab_from_basic_set(bset, 0);
10205 if (!bound || !tab)
10206 goto error;
10208 isl_seq_clr(bound->el, bound->size);
10209 isl_int_set_si(bound->el[0], -1);
10211 snap = isl_tab_snap(tab);
10212 for (i = 0; i < n; ++i) {
10213 int empty;
10215 isl_int_set_si(bound->el[1 + first + i], -1);
10216 if (isl_tab_add_ineq(tab, bound->el) < 0)
10217 goto error;
10218 empty = tab->empty;
10219 isl_int_set_si(bound->el[1 + first + i], 0);
10220 if (isl_tab_rollback(tab, snap) < 0)
10221 goto error;
10223 if (empty) {
10224 signs[i] = 1;
10225 continue;
10228 isl_int_set_si(bound->el[1 + first + i], 1);
10229 if (isl_tab_add_ineq(tab, bound->el) < 0)
10230 goto error;
10231 empty = tab->empty;
10232 isl_int_set_si(bound->el[1 + first + i], 0);
10233 if (isl_tab_rollback(tab, snap) < 0)
10234 goto error;
10236 signs[i] = empty ? -1 : 0;
10239 isl_tab_free(tab);
10240 isl_vec_free(bound);
10241 return 0;
10242 error:
10243 isl_tab_free(tab);
10244 isl_vec_free(bound);
10245 return -1;
10248 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10249 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10251 if (!bset || !signs)
10252 return -1;
10253 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10254 return -1);
10256 first += pos(bset->dim, type) - 1;
10257 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10260 /* Is it possible for the integer division "div" to depend (possibly
10261 * indirectly) on any output dimensions?
10263 * If the div is undefined, then we conservatively assume that it
10264 * may depend on them.
10265 * Otherwise, we check if it actually depends on them or on any integer
10266 * divisions that may depend on them.
10268 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10270 int i;
10271 unsigned n_out, o_out;
10272 unsigned n_div, o_div;
10274 if (isl_int_is_zero(bmap->div[div][0]))
10275 return 1;
10277 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10278 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10280 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10281 return 1;
10283 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10284 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10286 for (i = 0; i < n_div; ++i) {
10287 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10288 continue;
10289 if (div_may_involve_output(bmap, i))
10290 return 1;
10293 return 0;
10296 /* Return the index of the equality of "bmap" that defines
10297 * the output dimension "pos" in terms of earlier dimensions.
10298 * The equality may also involve integer divisions, as long
10299 * as those integer divisions are defined in terms of
10300 * parameters or input dimensions.
10301 * Return bmap->n_eq if there is no such equality.
10302 * Return -1 on error.
10304 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10305 int pos)
10307 int j, k;
10308 unsigned n_out, o_out;
10309 unsigned n_div, o_div;
10311 if (!bmap)
10312 return -1;
10314 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10315 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10316 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10317 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10319 for (j = 0; j < bmap->n_eq; ++j) {
10320 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10321 continue;
10322 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10323 n_out - (pos + 1)) != -1)
10324 continue;
10325 for (k = 0; k < n_div; ++k) {
10326 if (isl_int_is_zero(bmap->eq[j][o_div + k]))
10327 continue;
10328 if (div_may_involve_output(bmap, k))
10329 break;
10331 if (k >= n_div)
10332 return j;
10335 return bmap->n_eq;
10338 /* Check if the given basic map is obviously single-valued.
10339 * In particular, for each output dimension, check that there is
10340 * an equality that defines the output dimension in terms of
10341 * earlier dimensions.
10343 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10345 int i;
10346 unsigned n_out;
10348 if (!bmap)
10349 return isl_bool_error;
10351 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10353 for (i = 0; i < n_out; ++i) {
10354 int eq;
10356 eq = isl_basic_map_output_defining_equality(bmap, i);
10357 if (eq < 0)
10358 return isl_bool_error;
10359 if (eq >= bmap->n_eq)
10360 return isl_bool_false;
10363 return isl_bool_true;
10366 /* Check if the given basic map is single-valued.
10367 * We simply compute
10369 * M \circ M^-1
10371 * and check if the result is a subset of the identity mapping.
10373 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10375 isl_space *space;
10376 isl_basic_map *test;
10377 isl_basic_map *id;
10378 isl_bool sv;
10380 sv = isl_basic_map_plain_is_single_valued(bmap);
10381 if (sv < 0 || sv)
10382 return sv;
10384 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10385 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10387 space = isl_basic_map_get_space(bmap);
10388 space = isl_space_map_from_set(isl_space_range(space));
10389 id = isl_basic_map_identity(space);
10391 sv = isl_basic_map_is_subset(test, id);
10393 isl_basic_map_free(test);
10394 isl_basic_map_free(id);
10396 return sv;
10399 /* Check if the given map is obviously single-valued.
10401 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10403 if (!map)
10404 return isl_bool_error;
10405 if (map->n == 0)
10406 return isl_bool_true;
10407 if (map->n >= 2)
10408 return isl_bool_false;
10410 return isl_basic_map_plain_is_single_valued(map->p[0]);
10413 /* Check if the given map is single-valued.
10414 * We simply compute
10416 * M \circ M^-1
10418 * and check if the result is a subset of the identity mapping.
10420 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10422 isl_space *dim;
10423 isl_map *test;
10424 isl_map *id;
10425 isl_bool sv;
10427 sv = isl_map_plain_is_single_valued(map);
10428 if (sv < 0 || sv)
10429 return sv;
10431 test = isl_map_reverse(isl_map_copy(map));
10432 test = isl_map_apply_range(test, isl_map_copy(map));
10434 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10435 id = isl_map_identity(dim);
10437 sv = isl_map_is_subset(test, id);
10439 isl_map_free(test);
10440 isl_map_free(id);
10442 return sv;
10445 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10447 isl_bool in;
10449 map = isl_map_copy(map);
10450 map = isl_map_reverse(map);
10451 in = isl_map_is_single_valued(map);
10452 isl_map_free(map);
10454 return in;
10457 /* Check if the given map is obviously injective.
10459 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10461 isl_bool in;
10463 map = isl_map_copy(map);
10464 map = isl_map_reverse(map);
10465 in = isl_map_plain_is_single_valued(map);
10466 isl_map_free(map);
10468 return in;
10471 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10473 isl_bool sv;
10475 sv = isl_map_is_single_valued(map);
10476 if (sv < 0 || !sv)
10477 return sv;
10479 return isl_map_is_injective(map);
10482 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10484 return isl_map_is_single_valued((isl_map *)set);
10487 int isl_map_is_translation(__isl_keep isl_map *map)
10489 int ok;
10490 isl_set *delta;
10492 delta = isl_map_deltas(isl_map_copy(map));
10493 ok = isl_set_is_singleton(delta);
10494 isl_set_free(delta);
10496 return ok;
10499 static int unique(isl_int *p, unsigned pos, unsigned len)
10501 if (isl_seq_first_non_zero(p, pos) != -1)
10502 return 0;
10503 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10504 return 0;
10505 return 1;
10508 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10510 int i, j;
10511 unsigned nvar;
10512 unsigned ovar;
10514 if (!bset)
10515 return -1;
10517 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10518 return 0;
10520 nvar = isl_basic_set_dim(bset, isl_dim_set);
10521 ovar = isl_space_offset(bset->dim, isl_dim_set);
10522 for (j = 0; j < nvar; ++j) {
10523 int lower = 0, upper = 0;
10524 for (i = 0; i < bset->n_eq; ++i) {
10525 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10526 continue;
10527 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10528 return 0;
10529 break;
10531 if (i < bset->n_eq)
10532 continue;
10533 for (i = 0; i < bset->n_ineq; ++i) {
10534 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10535 continue;
10536 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10537 return 0;
10538 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10539 lower = 1;
10540 else
10541 upper = 1;
10543 if (!lower || !upper)
10544 return 0;
10547 return 1;
10550 int isl_set_is_box(__isl_keep isl_set *set)
10552 if (!set)
10553 return -1;
10554 if (set->n != 1)
10555 return 0;
10557 return isl_basic_set_is_box(set->p[0]);
10560 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10562 if (!bset)
10563 return isl_bool_error;
10565 return isl_space_is_wrapping(bset->dim);
10568 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10570 if (!set)
10571 return isl_bool_error;
10573 return isl_space_is_wrapping(set->dim);
10576 /* Is the domain of "map" a wrapped relation?
10578 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10580 if (!map)
10581 return isl_bool_error;
10583 return isl_space_domain_is_wrapping(map->dim);
10586 /* Is the range of "map" a wrapped relation?
10588 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
10590 if (!map)
10591 return isl_bool_error;
10593 return isl_space_range_is_wrapping(map->dim);
10596 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10598 bmap = isl_basic_map_cow(bmap);
10599 if (!bmap)
10600 return NULL;
10602 bmap->dim = isl_space_wrap(bmap->dim);
10603 if (!bmap->dim)
10604 goto error;
10606 bmap = isl_basic_map_finalize(bmap);
10608 return (isl_basic_set *)bmap;
10609 error:
10610 isl_basic_map_free(bmap);
10611 return NULL;
10614 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10616 int i;
10618 map = isl_map_cow(map);
10619 if (!map)
10620 return NULL;
10622 for (i = 0; i < map->n; ++i) {
10623 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10624 if (!map->p[i])
10625 goto error;
10627 map->dim = isl_space_wrap(map->dim);
10628 if (!map->dim)
10629 goto error;
10631 return (isl_set *)map;
10632 error:
10633 isl_map_free(map);
10634 return NULL;
10637 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10639 bset = isl_basic_set_cow(bset);
10640 if (!bset)
10641 return NULL;
10643 bset->dim = isl_space_unwrap(bset->dim);
10644 if (!bset->dim)
10645 goto error;
10647 bset = isl_basic_set_finalize(bset);
10649 return (isl_basic_map *)bset;
10650 error:
10651 isl_basic_set_free(bset);
10652 return NULL;
10655 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10657 int i;
10659 if (!set)
10660 return NULL;
10662 if (!isl_set_is_wrapping(set))
10663 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10664 goto error);
10666 set = isl_set_cow(set);
10667 if (!set)
10668 return NULL;
10670 for (i = 0; i < set->n; ++i) {
10671 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10672 if (!set->p[i])
10673 goto error;
10676 set->dim = isl_space_unwrap(set->dim);
10677 if (!set->dim)
10678 goto error;
10680 return (isl_map *)set;
10681 error:
10682 isl_set_free(set);
10683 return NULL;
10686 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10687 enum isl_dim_type type)
10689 if (!bmap)
10690 return NULL;
10692 if (!isl_space_is_named_or_nested(bmap->dim, type))
10693 return bmap;
10695 bmap = isl_basic_map_cow(bmap);
10696 if (!bmap)
10697 return NULL;
10699 bmap->dim = isl_space_reset(bmap->dim, type);
10700 if (!bmap->dim)
10701 goto error;
10703 bmap = isl_basic_map_finalize(bmap);
10705 return bmap;
10706 error:
10707 isl_basic_map_free(bmap);
10708 return NULL;
10711 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10712 enum isl_dim_type type)
10714 int i;
10716 if (!map)
10717 return NULL;
10719 if (!isl_space_is_named_or_nested(map->dim, type))
10720 return map;
10722 map = isl_map_cow(map);
10723 if (!map)
10724 return NULL;
10726 for (i = 0; i < map->n; ++i) {
10727 map->p[i] = isl_basic_map_reset(map->p[i], type);
10728 if (!map->p[i])
10729 goto error;
10731 map->dim = isl_space_reset(map->dim, type);
10732 if (!map->dim)
10733 goto error;
10735 return map;
10736 error:
10737 isl_map_free(map);
10738 return NULL;
10741 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10743 if (!bmap)
10744 return NULL;
10746 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10747 return bmap;
10749 bmap = isl_basic_map_cow(bmap);
10750 if (!bmap)
10751 return NULL;
10753 bmap->dim = isl_space_flatten(bmap->dim);
10754 if (!bmap->dim)
10755 goto error;
10757 bmap = isl_basic_map_finalize(bmap);
10759 return bmap;
10760 error:
10761 isl_basic_map_free(bmap);
10762 return NULL;
10765 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10767 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10770 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10771 __isl_take isl_basic_map *bmap)
10773 if (!bmap)
10774 return NULL;
10776 if (!bmap->dim->nested[0])
10777 return bmap;
10779 bmap = isl_basic_map_cow(bmap);
10780 if (!bmap)
10781 return NULL;
10783 bmap->dim = isl_space_flatten_domain(bmap->dim);
10784 if (!bmap->dim)
10785 goto error;
10787 bmap = isl_basic_map_finalize(bmap);
10789 return bmap;
10790 error:
10791 isl_basic_map_free(bmap);
10792 return NULL;
10795 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10796 __isl_take isl_basic_map *bmap)
10798 if (!bmap)
10799 return NULL;
10801 if (!bmap->dim->nested[1])
10802 return bmap;
10804 bmap = isl_basic_map_cow(bmap);
10805 if (!bmap)
10806 return NULL;
10808 bmap->dim = isl_space_flatten_range(bmap->dim);
10809 if (!bmap->dim)
10810 goto error;
10812 bmap = isl_basic_map_finalize(bmap);
10814 return bmap;
10815 error:
10816 isl_basic_map_free(bmap);
10817 return NULL;
10820 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10822 int i;
10824 if (!map)
10825 return NULL;
10827 if (!map->dim->nested[0] && !map->dim->nested[1])
10828 return map;
10830 map = isl_map_cow(map);
10831 if (!map)
10832 return NULL;
10834 for (i = 0; i < map->n; ++i) {
10835 map->p[i] = isl_basic_map_flatten(map->p[i]);
10836 if (!map->p[i])
10837 goto error;
10839 map->dim = isl_space_flatten(map->dim);
10840 if (!map->dim)
10841 goto error;
10843 return map;
10844 error:
10845 isl_map_free(map);
10846 return NULL;
10849 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10851 return (isl_set *)isl_map_flatten((isl_map *)set);
10854 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10856 isl_space *dim, *flat_dim;
10857 isl_map *map;
10859 dim = isl_set_get_space(set);
10860 flat_dim = isl_space_flatten(isl_space_copy(dim));
10861 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10862 map = isl_map_intersect_domain(map, set);
10864 return map;
10867 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10869 int i;
10871 if (!map)
10872 return NULL;
10874 if (!map->dim->nested[0])
10875 return map;
10877 map = isl_map_cow(map);
10878 if (!map)
10879 return NULL;
10881 for (i = 0; i < map->n; ++i) {
10882 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10883 if (!map->p[i])
10884 goto error;
10886 map->dim = isl_space_flatten_domain(map->dim);
10887 if (!map->dim)
10888 goto error;
10890 return map;
10891 error:
10892 isl_map_free(map);
10893 return NULL;
10896 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10898 int i;
10900 if (!map)
10901 return NULL;
10903 if (!map->dim->nested[1])
10904 return map;
10906 map = isl_map_cow(map);
10907 if (!map)
10908 return NULL;
10910 for (i = 0; i < map->n; ++i) {
10911 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10912 if (!map->p[i])
10913 goto error;
10915 map->dim = isl_space_flatten_range(map->dim);
10916 if (!map->dim)
10917 goto error;
10919 return map;
10920 error:
10921 isl_map_free(map);
10922 return NULL;
10925 /* Reorder the dimensions of "bmap" according to the given dim_map
10926 * and set the dimension specification to "dim".
10928 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10929 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10931 isl_basic_map *res;
10932 unsigned flags;
10934 bmap = isl_basic_map_cow(bmap);
10935 if (!bmap || !dim || !dim_map)
10936 goto error;
10938 flags = bmap->flags;
10939 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10940 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10941 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10942 res = isl_basic_map_alloc_space(dim,
10943 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10944 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10945 if (res)
10946 res->flags = flags;
10947 res = isl_basic_map_finalize(res);
10948 return res;
10949 error:
10950 free(dim_map);
10951 isl_basic_map_free(bmap);
10952 isl_space_free(dim);
10953 return NULL;
10956 /* Reorder the dimensions of "map" according to given reordering.
10958 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10959 __isl_take isl_reordering *r)
10961 int i;
10962 struct isl_dim_map *dim_map;
10964 map = isl_map_cow(map);
10965 dim_map = isl_dim_map_from_reordering(r);
10966 if (!map || !r || !dim_map)
10967 goto error;
10969 for (i = 0; i < map->n; ++i) {
10970 struct isl_dim_map *dim_map_i;
10972 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10974 map->p[i] = isl_basic_map_realign(map->p[i],
10975 isl_space_copy(r->dim), dim_map_i);
10977 if (!map->p[i])
10978 goto error;
10981 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10983 isl_reordering_free(r);
10984 free(dim_map);
10985 return map;
10986 error:
10987 free(dim_map);
10988 isl_map_free(map);
10989 isl_reordering_free(r);
10990 return NULL;
10993 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10994 __isl_take isl_reordering *r)
10996 return (isl_set *)isl_map_realign((isl_map *)set, r);
10999 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11000 __isl_take isl_space *model)
11002 isl_ctx *ctx;
11004 if (!map || !model)
11005 goto error;
11007 ctx = isl_space_get_ctx(model);
11008 if (!isl_space_has_named_params(model))
11009 isl_die(ctx, isl_error_invalid,
11010 "model has unnamed parameters", goto error);
11011 if (!isl_space_has_named_params(map->dim))
11012 isl_die(ctx, isl_error_invalid,
11013 "relation has unnamed parameters", goto error);
11014 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11015 isl_reordering *exp;
11017 model = isl_space_drop_dims(model, isl_dim_in,
11018 0, isl_space_dim(model, isl_dim_in));
11019 model = isl_space_drop_dims(model, isl_dim_out,
11020 0, isl_space_dim(model, isl_dim_out));
11021 exp = isl_parameter_alignment_reordering(map->dim, model);
11022 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11023 map = isl_map_realign(map, exp);
11026 isl_space_free(model);
11027 return map;
11028 error:
11029 isl_space_free(model);
11030 isl_map_free(map);
11031 return NULL;
11034 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11035 __isl_take isl_space *model)
11037 return isl_map_align_params(set, model);
11040 /* Align the parameters of "bmap" to those of "model", introducing
11041 * additional parameters if needed.
11043 __isl_give isl_basic_map *isl_basic_map_align_params(
11044 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11046 isl_ctx *ctx;
11048 if (!bmap || !model)
11049 goto error;
11051 ctx = isl_space_get_ctx(model);
11052 if (!isl_space_has_named_params(model))
11053 isl_die(ctx, isl_error_invalid,
11054 "model has unnamed parameters", goto error);
11055 if (!isl_space_has_named_params(bmap->dim))
11056 isl_die(ctx, isl_error_invalid,
11057 "relation has unnamed parameters", goto error);
11058 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11059 isl_reordering *exp;
11060 struct isl_dim_map *dim_map;
11062 model = isl_space_drop_dims(model, isl_dim_in,
11063 0, isl_space_dim(model, isl_dim_in));
11064 model = isl_space_drop_dims(model, isl_dim_out,
11065 0, isl_space_dim(model, isl_dim_out));
11066 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11067 exp = isl_reordering_extend_space(exp,
11068 isl_basic_map_get_space(bmap));
11069 dim_map = isl_dim_map_from_reordering(exp);
11070 bmap = isl_basic_map_realign(bmap,
11071 exp ? isl_space_copy(exp->dim) : NULL,
11072 isl_dim_map_extend(dim_map, bmap));
11073 isl_reordering_free(exp);
11074 free(dim_map);
11077 isl_space_free(model);
11078 return bmap;
11079 error:
11080 isl_space_free(model);
11081 isl_basic_map_free(bmap);
11082 return NULL;
11085 /* Align the parameters of "bset" to those of "model", introducing
11086 * additional parameters if needed.
11088 __isl_give isl_basic_set *isl_basic_set_align_params(
11089 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11091 return isl_basic_map_align_params(bset, model);
11094 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11095 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11096 enum isl_dim_type c2, enum isl_dim_type c3,
11097 enum isl_dim_type c4, enum isl_dim_type c5)
11099 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11100 struct isl_mat *mat;
11101 int i, j, k;
11102 int pos;
11104 if (!bmap)
11105 return NULL;
11106 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11107 isl_basic_map_total_dim(bmap) + 1);
11108 if (!mat)
11109 return NULL;
11110 for (i = 0; i < bmap->n_eq; ++i)
11111 for (j = 0, pos = 0; j < 5; ++j) {
11112 int off = isl_basic_map_offset(bmap, c[j]);
11113 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11114 isl_int_set(mat->row[i][pos],
11115 bmap->eq[i][off + k]);
11116 ++pos;
11120 return mat;
11123 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11124 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11125 enum isl_dim_type c2, enum isl_dim_type c3,
11126 enum isl_dim_type c4, enum isl_dim_type c5)
11128 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11129 struct isl_mat *mat;
11130 int i, j, k;
11131 int pos;
11133 if (!bmap)
11134 return NULL;
11135 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11136 isl_basic_map_total_dim(bmap) + 1);
11137 if (!mat)
11138 return NULL;
11139 for (i = 0; i < bmap->n_ineq; ++i)
11140 for (j = 0, pos = 0; j < 5; ++j) {
11141 int off = isl_basic_map_offset(bmap, c[j]);
11142 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11143 isl_int_set(mat->row[i][pos],
11144 bmap->ineq[i][off + k]);
11145 ++pos;
11149 return mat;
11152 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11153 __isl_take isl_space *dim,
11154 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11155 enum isl_dim_type c2, enum isl_dim_type c3,
11156 enum isl_dim_type c4, enum isl_dim_type c5)
11158 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11159 isl_basic_map *bmap;
11160 unsigned total;
11161 unsigned extra;
11162 int i, j, k, l;
11163 int pos;
11165 if (!dim || !eq || !ineq)
11166 goto error;
11168 if (eq->n_col != ineq->n_col)
11169 isl_die(dim->ctx, isl_error_invalid,
11170 "equalities and inequalities matrices should have "
11171 "same number of columns", goto error);
11173 total = 1 + isl_space_dim(dim, isl_dim_all);
11175 if (eq->n_col < total)
11176 isl_die(dim->ctx, isl_error_invalid,
11177 "number of columns too small", goto error);
11179 extra = eq->n_col - total;
11181 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11182 eq->n_row, ineq->n_row);
11183 if (!bmap)
11184 goto error;
11185 for (i = 0; i < extra; ++i) {
11186 k = isl_basic_map_alloc_div(bmap);
11187 if (k < 0)
11188 goto error;
11189 isl_int_set_si(bmap->div[k][0], 0);
11191 for (i = 0; i < eq->n_row; ++i) {
11192 l = isl_basic_map_alloc_equality(bmap);
11193 if (l < 0)
11194 goto error;
11195 for (j = 0, pos = 0; j < 5; ++j) {
11196 int off = isl_basic_map_offset(bmap, c[j]);
11197 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11198 isl_int_set(bmap->eq[l][off + k],
11199 eq->row[i][pos]);
11200 ++pos;
11204 for (i = 0; i < ineq->n_row; ++i) {
11205 l = isl_basic_map_alloc_inequality(bmap);
11206 if (l < 0)
11207 goto error;
11208 for (j = 0, pos = 0; j < 5; ++j) {
11209 int off = isl_basic_map_offset(bmap, c[j]);
11210 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11211 isl_int_set(bmap->ineq[l][off + k],
11212 ineq->row[i][pos]);
11213 ++pos;
11218 isl_space_free(dim);
11219 isl_mat_free(eq);
11220 isl_mat_free(ineq);
11222 bmap = isl_basic_map_simplify(bmap);
11223 return isl_basic_map_finalize(bmap);
11224 error:
11225 isl_space_free(dim);
11226 isl_mat_free(eq);
11227 isl_mat_free(ineq);
11228 return NULL;
11231 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11232 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11233 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11235 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11236 c1, c2, c3, c4, isl_dim_in);
11239 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11240 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11241 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11243 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11244 c1, c2, c3, c4, isl_dim_in);
11247 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11248 __isl_take isl_space *dim,
11249 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, 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_set*)
11253 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11254 c1, c2, c3, c4, isl_dim_in);
11257 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11259 if (!bmap)
11260 return isl_bool_error;
11262 return isl_space_can_zip(bmap->dim);
11265 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11267 if (!map)
11268 return isl_bool_error;
11270 return isl_space_can_zip(map->dim);
11273 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11274 * (A -> C) -> (B -> D).
11276 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11278 unsigned pos;
11279 unsigned n1;
11280 unsigned n2;
11282 if (!bmap)
11283 return NULL;
11285 if (!isl_basic_map_can_zip(bmap))
11286 isl_die(bmap->ctx, isl_error_invalid,
11287 "basic map cannot be zipped", goto error);
11288 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11289 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11290 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11291 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11292 bmap = isl_basic_map_cow(bmap);
11293 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11294 if (!bmap)
11295 return NULL;
11296 bmap->dim = isl_space_zip(bmap->dim);
11297 if (!bmap->dim)
11298 goto error;
11299 return bmap;
11300 error:
11301 isl_basic_map_free(bmap);
11302 return NULL;
11305 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11306 * (A -> C) -> (B -> D).
11308 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11310 int i;
11312 if (!map)
11313 return NULL;
11315 if (!isl_map_can_zip(map))
11316 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11317 goto error);
11319 map = isl_map_cow(map);
11320 if (!map)
11321 return NULL;
11323 for (i = 0; i < map->n; ++i) {
11324 map->p[i] = isl_basic_map_zip(map->p[i]);
11325 if (!map->p[i])
11326 goto error;
11329 map->dim = isl_space_zip(map->dim);
11330 if (!map->dim)
11331 goto error;
11333 return map;
11334 error:
11335 isl_map_free(map);
11336 return NULL;
11339 /* Can we apply isl_basic_map_curry to "bmap"?
11340 * That is, does it have a nested relation in its domain?
11342 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11344 if (!bmap)
11345 return isl_bool_error;
11347 return isl_space_can_curry(bmap->dim);
11350 /* Can we apply isl_map_curry to "map"?
11351 * That is, does it have a nested relation in its domain?
11353 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11355 if (!map)
11356 return isl_bool_error;
11358 return isl_space_can_curry(map->dim);
11361 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11362 * A -> (B -> C).
11364 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11367 if (!bmap)
11368 return NULL;
11370 if (!isl_basic_map_can_curry(bmap))
11371 isl_die(bmap->ctx, isl_error_invalid,
11372 "basic map cannot be curried", goto error);
11373 bmap = isl_basic_map_cow(bmap);
11374 if (!bmap)
11375 return NULL;
11376 bmap->dim = isl_space_curry(bmap->dim);
11377 if (!bmap->dim)
11378 goto error;
11379 return bmap;
11380 error:
11381 isl_basic_map_free(bmap);
11382 return NULL;
11385 /* Given a map (A -> B) -> C, return the corresponding map
11386 * A -> (B -> C).
11388 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11390 int i;
11392 if (!map)
11393 return NULL;
11395 if (!isl_map_can_curry(map))
11396 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11397 goto error);
11399 map = isl_map_cow(map);
11400 if (!map)
11401 return NULL;
11403 for (i = 0; i < map->n; ++i) {
11404 map->p[i] = isl_basic_map_curry(map->p[i]);
11405 if (!map->p[i])
11406 goto error;
11409 map->dim = isl_space_curry(map->dim);
11410 if (!map->dim)
11411 goto error;
11413 return map;
11414 error:
11415 isl_map_free(map);
11416 return NULL;
11419 /* Can we apply isl_basic_map_uncurry to "bmap"?
11420 * That is, does it have a nested relation in its domain?
11422 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11424 if (!bmap)
11425 return isl_bool_error;
11427 return isl_space_can_uncurry(bmap->dim);
11430 /* Can we apply isl_map_uncurry to "map"?
11431 * That is, does it have a nested relation in its domain?
11433 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11435 if (!map)
11436 return isl_bool_error;
11438 return isl_space_can_uncurry(map->dim);
11441 /* Given a basic map A -> (B -> C), return the corresponding basic map
11442 * (A -> B) -> C.
11444 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11447 if (!bmap)
11448 return NULL;
11450 if (!isl_basic_map_can_uncurry(bmap))
11451 isl_die(bmap->ctx, isl_error_invalid,
11452 "basic map cannot be uncurried",
11453 return isl_basic_map_free(bmap));
11454 bmap = isl_basic_map_cow(bmap);
11455 if (!bmap)
11456 return NULL;
11457 bmap->dim = isl_space_uncurry(bmap->dim);
11458 if (!bmap->dim)
11459 return isl_basic_map_free(bmap);
11460 return bmap;
11463 /* Given a map A -> (B -> C), return the corresponding map
11464 * (A -> B) -> C.
11466 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11468 int i;
11470 if (!map)
11471 return NULL;
11473 if (!isl_map_can_uncurry(map))
11474 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11475 return isl_map_free(map));
11477 map = isl_map_cow(map);
11478 if (!map)
11479 return NULL;
11481 for (i = 0; i < map->n; ++i) {
11482 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11483 if (!map->p[i])
11484 return isl_map_free(map);
11487 map->dim = isl_space_uncurry(map->dim);
11488 if (!map->dim)
11489 return isl_map_free(map);
11491 return map;
11494 /* Construct a basic map mapping the domain of the affine expression
11495 * to a one-dimensional range prescribed by the affine expression.
11497 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11499 int k;
11500 int pos;
11501 isl_local_space *ls;
11502 isl_basic_map *bmap;
11504 if (!aff)
11505 return NULL;
11507 ls = isl_aff_get_local_space(aff);
11508 bmap = isl_basic_map_from_local_space(ls);
11509 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11510 k = isl_basic_map_alloc_equality(bmap);
11511 if (k < 0)
11512 goto error;
11514 pos = isl_basic_map_offset(bmap, isl_dim_out);
11515 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11516 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11517 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11518 aff->v->size - (pos + 1));
11520 isl_aff_free(aff);
11521 bmap = isl_basic_map_finalize(bmap);
11522 return bmap;
11523 error:
11524 isl_aff_free(aff);
11525 isl_basic_map_free(bmap);
11526 return NULL;
11529 /* Construct a map mapping the domain of the affine expression
11530 * to a one-dimensional range prescribed by the affine expression.
11532 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11534 isl_basic_map *bmap;
11536 bmap = isl_basic_map_from_aff(aff);
11537 return isl_map_from_basic_map(bmap);
11540 /* Construct a basic map mapping the domain the multi-affine expression
11541 * to its range, with each dimension in the range equated to the
11542 * corresponding affine expression.
11544 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11545 __isl_take isl_multi_aff *maff)
11547 int i;
11548 isl_space *space;
11549 isl_basic_map *bmap;
11551 if (!maff)
11552 return NULL;
11554 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11555 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11556 "invalid space", goto error);
11558 space = isl_space_domain(isl_multi_aff_get_space(maff));
11559 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11561 for (i = 0; i < maff->n; ++i) {
11562 isl_aff *aff;
11563 isl_basic_map *bmap_i;
11565 aff = isl_aff_copy(maff->p[i]);
11566 bmap_i = isl_basic_map_from_aff(aff);
11568 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11571 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11573 isl_multi_aff_free(maff);
11574 return bmap;
11575 error:
11576 isl_multi_aff_free(maff);
11577 return NULL;
11580 /* Construct a map mapping the domain the multi-affine expression
11581 * to its range, with each dimension in the range equated to the
11582 * corresponding affine expression.
11584 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11586 isl_basic_map *bmap;
11588 bmap = isl_basic_map_from_multi_aff(maff);
11589 return isl_map_from_basic_map(bmap);
11592 /* Construct a basic map mapping a domain in the given space to
11593 * to an n-dimensional range, with n the number of elements in the list,
11594 * where each coordinate in the range is prescribed by the
11595 * corresponding affine expression.
11596 * The domains of all affine expressions in the list are assumed to match
11597 * domain_dim.
11599 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11600 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11602 int i;
11603 isl_space *dim;
11604 isl_basic_map *bmap;
11606 if (!list)
11607 return NULL;
11609 dim = isl_space_from_domain(domain_dim);
11610 bmap = isl_basic_map_universe(dim);
11612 for (i = 0; i < list->n; ++i) {
11613 isl_aff *aff;
11614 isl_basic_map *bmap_i;
11616 aff = isl_aff_copy(list->p[i]);
11617 bmap_i = isl_basic_map_from_aff(aff);
11619 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11622 isl_aff_list_free(list);
11623 return bmap;
11626 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11627 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11629 return isl_map_equate(set, type1, pos1, type2, pos2);
11632 /* Construct a basic map where the given dimensions are equal to each other.
11634 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11635 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11637 isl_basic_map *bmap = NULL;
11638 int i;
11640 if (!space)
11641 return NULL;
11643 if (pos1 >= isl_space_dim(space, type1))
11644 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11645 "index out of bounds", goto error);
11646 if (pos2 >= isl_space_dim(space, type2))
11647 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11648 "index out of bounds", goto error);
11650 if (type1 == type2 && pos1 == pos2)
11651 return isl_basic_map_universe(space);
11653 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11654 i = isl_basic_map_alloc_equality(bmap);
11655 if (i < 0)
11656 goto error;
11657 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11658 pos1 += isl_basic_map_offset(bmap, type1);
11659 pos2 += isl_basic_map_offset(bmap, type2);
11660 isl_int_set_si(bmap->eq[i][pos1], -1);
11661 isl_int_set_si(bmap->eq[i][pos2], 1);
11662 bmap = isl_basic_map_finalize(bmap);
11663 isl_space_free(space);
11664 return bmap;
11665 error:
11666 isl_space_free(space);
11667 isl_basic_map_free(bmap);
11668 return NULL;
11671 /* Add a constraint imposing that the given two dimensions are equal.
11673 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11674 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11676 isl_basic_map *eq;
11678 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11680 bmap = isl_basic_map_intersect(bmap, eq);
11682 return bmap;
11685 /* Add a constraint imposing that the given two dimensions are equal.
11687 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11688 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11690 isl_basic_map *bmap;
11692 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11694 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11696 return map;
11699 /* Add a constraint imposing that the given two dimensions have opposite values.
11701 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11702 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11704 isl_basic_map *bmap = NULL;
11705 int i;
11707 if (!map)
11708 return NULL;
11710 if (pos1 >= isl_map_dim(map, type1))
11711 isl_die(map->ctx, isl_error_invalid,
11712 "index out of bounds", goto error);
11713 if (pos2 >= isl_map_dim(map, type2))
11714 isl_die(map->ctx, isl_error_invalid,
11715 "index out of bounds", goto error);
11717 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11718 i = isl_basic_map_alloc_equality(bmap);
11719 if (i < 0)
11720 goto error;
11721 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11722 pos1 += isl_basic_map_offset(bmap, type1);
11723 pos2 += isl_basic_map_offset(bmap, type2);
11724 isl_int_set_si(bmap->eq[i][pos1], 1);
11725 isl_int_set_si(bmap->eq[i][pos2], 1);
11726 bmap = isl_basic_map_finalize(bmap);
11728 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11730 return map;
11731 error:
11732 isl_basic_map_free(bmap);
11733 isl_map_free(map);
11734 return NULL;
11737 /* Construct a constraint imposing that the value of the first dimension is
11738 * greater than or equal to that of the second.
11740 static __isl_give isl_constraint *constraint_order_ge(
11741 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11742 enum isl_dim_type type2, int pos2)
11744 isl_constraint *c;
11746 if (!space)
11747 return NULL;
11749 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
11751 if (pos1 >= isl_constraint_dim(c, type1))
11752 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11753 "index out of bounds", return isl_constraint_free(c));
11754 if (pos2 >= isl_constraint_dim(c, type2))
11755 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11756 "index out of bounds", return isl_constraint_free(c));
11758 if (type1 == type2 && pos1 == pos2)
11759 return c;
11761 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11762 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11764 return c;
11767 /* Add a constraint imposing that the value of the first dimension is
11768 * greater than or equal to that of the second.
11770 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11771 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11773 isl_constraint *c;
11774 isl_space *space;
11776 if (type1 == type2 && pos1 == pos2)
11777 return bmap;
11778 space = isl_basic_map_get_space(bmap);
11779 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11780 bmap = isl_basic_map_add_constraint(bmap, c);
11782 return bmap;
11785 /* Add a constraint imposing that the value of the first dimension is
11786 * greater than or equal to that of the second.
11788 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11789 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11791 isl_constraint *c;
11792 isl_space *space;
11794 if (type1 == type2 && pos1 == pos2)
11795 return map;
11796 space = isl_map_get_space(map);
11797 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11798 map = isl_map_add_constraint(map, c);
11800 return map;
11803 /* Add a constraint imposing that the value of the first dimension is
11804 * less than or equal to that of the second.
11806 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11807 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11809 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11812 /* Construct a basic map where the value of the first dimension is
11813 * greater than that of the second.
11815 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11816 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11818 isl_basic_map *bmap = NULL;
11819 int i;
11821 if (!space)
11822 return NULL;
11824 if (pos1 >= isl_space_dim(space, type1))
11825 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11826 "index out of bounds", goto error);
11827 if (pos2 >= isl_space_dim(space, type2))
11828 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11829 "index out of bounds", goto error);
11831 if (type1 == type2 && pos1 == pos2)
11832 return isl_basic_map_empty(space);
11834 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11835 i = isl_basic_map_alloc_inequality(bmap);
11836 if (i < 0)
11837 return isl_basic_map_free(bmap);
11838 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11839 pos1 += isl_basic_map_offset(bmap, type1);
11840 pos2 += isl_basic_map_offset(bmap, type2);
11841 isl_int_set_si(bmap->ineq[i][pos1], 1);
11842 isl_int_set_si(bmap->ineq[i][pos2], -1);
11843 isl_int_set_si(bmap->ineq[i][0], -1);
11844 bmap = isl_basic_map_finalize(bmap);
11846 return bmap;
11847 error:
11848 isl_space_free(space);
11849 isl_basic_map_free(bmap);
11850 return NULL;
11853 /* Add a constraint imposing that the value of the first dimension is
11854 * greater than that of the second.
11856 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11857 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11859 isl_basic_map *gt;
11861 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11863 bmap = isl_basic_map_intersect(bmap, gt);
11865 return bmap;
11868 /* Add a constraint imposing that the value of the first dimension is
11869 * greater than that of the second.
11871 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11872 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11874 isl_basic_map *bmap;
11876 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11878 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11880 return map;
11883 /* Add a constraint imposing that the value of the first dimension is
11884 * smaller than that of the second.
11886 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11887 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11889 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11892 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11893 int pos)
11895 isl_aff *div;
11896 isl_local_space *ls;
11898 if (!bmap)
11899 return NULL;
11901 if (!isl_basic_map_divs_known(bmap))
11902 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11903 "some divs are unknown", return NULL);
11905 ls = isl_basic_map_get_local_space(bmap);
11906 div = isl_local_space_get_div(ls, pos);
11907 isl_local_space_free(ls);
11909 return div;
11912 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11913 int pos)
11915 return isl_basic_map_get_div(bset, pos);
11918 /* Plug in "subs" for dimension "type", "pos" of "bset".
11920 * Let i be the dimension to replace and let "subs" be of the form
11922 * f/d
11924 * Any integer division with a non-zero coefficient for i,
11926 * floor((a i + g)/m)
11928 * is replaced by
11930 * floor((a f + d g)/(m d))
11932 * Constraints of the form
11934 * a i + g
11936 * are replaced by
11938 * a f + d g
11940 * We currently require that "subs" is an integral expression.
11941 * Handling rational expressions may require us to add stride constraints
11942 * as we do in isl_basic_set_preimage_multi_aff.
11944 __isl_give isl_basic_set *isl_basic_set_substitute(
11945 __isl_take isl_basic_set *bset,
11946 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11948 int i;
11949 isl_int v;
11950 isl_ctx *ctx;
11952 if (bset && isl_basic_set_plain_is_empty(bset))
11953 return bset;
11955 bset = isl_basic_set_cow(bset);
11956 if (!bset || !subs)
11957 goto error;
11959 ctx = isl_basic_set_get_ctx(bset);
11960 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11961 isl_die(ctx, isl_error_invalid,
11962 "spaces don't match", goto error);
11963 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11964 isl_die(ctx, isl_error_unsupported,
11965 "cannot handle divs yet", goto error);
11966 if (!isl_int_is_one(subs->v->el[0]))
11967 isl_die(ctx, isl_error_invalid,
11968 "can only substitute integer expressions", goto error);
11970 pos += isl_basic_set_offset(bset, type);
11972 isl_int_init(v);
11974 for (i = 0; i < bset->n_eq; ++i) {
11975 if (isl_int_is_zero(bset->eq[i][pos]))
11976 continue;
11977 isl_int_set(v, bset->eq[i][pos]);
11978 isl_int_set_si(bset->eq[i][pos], 0);
11979 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11980 v, subs->v->el + 1, subs->v->size - 1);
11983 for (i = 0; i < bset->n_ineq; ++i) {
11984 if (isl_int_is_zero(bset->ineq[i][pos]))
11985 continue;
11986 isl_int_set(v, bset->ineq[i][pos]);
11987 isl_int_set_si(bset->ineq[i][pos], 0);
11988 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11989 v, subs->v->el + 1, subs->v->size - 1);
11992 for (i = 0; i < bset->n_div; ++i) {
11993 if (isl_int_is_zero(bset->div[i][1 + pos]))
11994 continue;
11995 isl_int_set(v, bset->div[i][1 + pos]);
11996 isl_int_set_si(bset->div[i][1 + pos], 0);
11997 isl_seq_combine(bset->div[i] + 1,
11998 subs->v->el[0], bset->div[i] + 1,
11999 v, subs->v->el + 1, subs->v->size - 1);
12000 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12003 isl_int_clear(v);
12005 bset = isl_basic_set_simplify(bset);
12006 return isl_basic_set_finalize(bset);
12007 error:
12008 isl_basic_set_free(bset);
12009 return NULL;
12012 /* Plug in "subs" for dimension "type", "pos" of "set".
12014 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12015 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12017 int i;
12019 if (set && isl_set_plain_is_empty(set))
12020 return set;
12022 set = isl_set_cow(set);
12023 if (!set || !subs)
12024 goto error;
12026 for (i = set->n - 1; i >= 0; --i) {
12027 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12028 if (remove_if_empty(set, i) < 0)
12029 goto error;
12032 return set;
12033 error:
12034 isl_set_free(set);
12035 return NULL;
12038 /* Check if the range of "ma" is compatible with the domain or range
12039 * (depending on "type") of "bmap".
12040 * Return -1 if anything is wrong.
12042 static int check_basic_map_compatible_range_multi_aff(
12043 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12044 __isl_keep isl_multi_aff *ma)
12046 int m;
12047 isl_space *ma_space;
12049 ma_space = isl_multi_aff_get_space(ma);
12051 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12052 if (m < 0)
12053 goto error;
12054 if (!m)
12055 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12056 "parameters don't match", goto error);
12057 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12058 if (m < 0)
12059 goto error;
12060 if (!m)
12061 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12062 "spaces don't match", goto error);
12064 isl_space_free(ma_space);
12065 return m;
12066 error:
12067 isl_space_free(ma_space);
12068 return -1;
12071 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12072 * coefficients before the transformed range of dimensions,
12073 * the "n_after" coefficients after the transformed range of dimensions
12074 * and the coefficients of the other divs in "bmap".
12076 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12077 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12079 int i;
12080 int n_param;
12081 int n_set;
12082 isl_local_space *ls;
12084 if (n_div == 0)
12085 return 0;
12087 ls = isl_aff_get_domain_local_space(ma->p[0]);
12088 if (!ls)
12089 return -1;
12091 n_param = isl_local_space_dim(ls, isl_dim_param);
12092 n_set = isl_local_space_dim(ls, isl_dim_set);
12093 for (i = 0; i < n_div; ++i) {
12094 int o_bmap = 0, o_ls = 0;
12096 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12097 o_bmap += 1 + 1 + n_param;
12098 o_ls += 1 + 1 + n_param;
12099 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12100 o_bmap += n_before;
12101 isl_seq_cpy(bmap->div[i] + o_bmap,
12102 ls->div->row[i] + o_ls, n_set);
12103 o_bmap += n_set;
12104 o_ls += n_set;
12105 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12106 o_bmap += n_after;
12107 isl_seq_cpy(bmap->div[i] + o_bmap,
12108 ls->div->row[i] + o_ls, n_div);
12109 o_bmap += n_div;
12110 o_ls += n_div;
12111 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12112 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12113 goto error;
12116 isl_local_space_free(ls);
12117 return 0;
12118 error:
12119 isl_local_space_free(ls);
12120 return -1;
12123 /* How many stride constraints does "ma" enforce?
12124 * That is, how many of the affine expressions have a denominator
12125 * different from one?
12127 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12129 int i;
12130 int strides = 0;
12132 for (i = 0; i < ma->n; ++i)
12133 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12134 strides++;
12136 return strides;
12139 /* For each affine expression in ma of the form
12141 * x_i = (f_i y + h_i)/m_i
12143 * with m_i different from one, add a constraint to "bmap"
12144 * of the form
12146 * f_i y + h_i = m_i alpha_i
12148 * with alpha_i an additional existentially quantified variable.
12150 static __isl_give isl_basic_map *add_ma_strides(
12151 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12152 int n_before, int n_after)
12154 int i, k;
12155 int div;
12156 int total;
12157 int n_param;
12158 int n_in;
12159 int n_div;
12161 total = isl_basic_map_total_dim(bmap);
12162 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12163 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12164 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12165 for (i = 0; i < ma->n; ++i) {
12166 int o_bmap = 0, o_ma = 1;
12168 if (isl_int_is_one(ma->p[i]->v->el[0]))
12169 continue;
12170 div = isl_basic_map_alloc_div(bmap);
12171 k = isl_basic_map_alloc_equality(bmap);
12172 if (div < 0 || k < 0)
12173 goto error;
12174 isl_int_set_si(bmap->div[div][0], 0);
12175 isl_seq_cpy(bmap->eq[k] + o_bmap,
12176 ma->p[i]->v->el + o_ma, 1 + n_param);
12177 o_bmap += 1 + n_param;
12178 o_ma += 1 + n_param;
12179 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12180 o_bmap += n_before;
12181 isl_seq_cpy(bmap->eq[k] + o_bmap,
12182 ma->p[i]->v->el + o_ma, n_in);
12183 o_bmap += n_in;
12184 o_ma += n_in;
12185 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12186 o_bmap += n_after;
12187 isl_seq_cpy(bmap->eq[k] + o_bmap,
12188 ma->p[i]->v->el + o_ma, n_div);
12189 o_bmap += n_div;
12190 o_ma += n_div;
12191 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12192 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12193 total++;
12196 return bmap;
12197 error:
12198 isl_basic_map_free(bmap);
12199 return NULL;
12202 /* Replace the domain or range space (depending on "type) of "space" by "set".
12204 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12205 enum isl_dim_type type, __isl_take isl_space *set)
12207 if (type == isl_dim_in) {
12208 space = isl_space_range(space);
12209 space = isl_space_map_from_domain_and_range(set, space);
12210 } else {
12211 space = isl_space_domain(space);
12212 space = isl_space_map_from_domain_and_range(space, set);
12215 return space;
12218 /* Compute the preimage of the domain or range (depending on "type")
12219 * of "bmap" under the function represented by "ma".
12220 * In other words, plug in "ma" in the domain or range of "bmap".
12221 * The result is a basic map that lives in the same space as "bmap"
12222 * except that the domain or range has been replaced by
12223 * the domain space of "ma".
12225 * If bmap is represented by
12227 * A(p) + S u + B x + T v + C(divs) >= 0,
12229 * where u and x are input and output dimensions if type == isl_dim_out
12230 * while x and v are input and output dimensions if type == isl_dim_in,
12231 * and ma is represented by
12233 * x = D(p) + F(y) + G(divs')
12235 * then the result is
12237 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12239 * The divs in the input set are similarly adjusted.
12240 * In particular
12242 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12244 * becomes
12246 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12247 * B_i G(divs') + c_i(divs))/n_i)
12249 * If bmap is not a rational map and if F(y) involves any denominators
12251 * x_i = (f_i y + h_i)/m_i
12253 * then additional constraints are added to ensure that we only
12254 * map back integer points. That is we enforce
12256 * f_i y + h_i = m_i alpha_i
12258 * with alpha_i an additional existentially quantified variable.
12260 * We first copy over the divs from "ma".
12261 * Then we add the modified constraints and divs from "bmap".
12262 * Finally, we add the stride constraints, if needed.
12264 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12265 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12266 __isl_take isl_multi_aff *ma)
12268 int i, k;
12269 isl_space *space;
12270 isl_basic_map *res = NULL;
12271 int n_before, n_after, n_div_bmap, n_div_ma;
12272 isl_int f, c1, c2, g;
12273 int rational, strides;
12275 isl_int_init(f);
12276 isl_int_init(c1);
12277 isl_int_init(c2);
12278 isl_int_init(g);
12280 ma = isl_multi_aff_align_divs(ma);
12281 if (!bmap || !ma)
12282 goto error;
12283 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12284 goto error;
12286 if (type == isl_dim_in) {
12287 n_before = 0;
12288 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12289 } else {
12290 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12291 n_after = 0;
12293 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12294 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12296 space = isl_multi_aff_get_domain_space(ma);
12297 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12298 rational = isl_basic_map_is_rational(bmap);
12299 strides = rational ? 0 : multi_aff_strides(ma);
12300 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12301 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12302 if (rational)
12303 res = isl_basic_map_set_rational(res);
12305 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12306 if (isl_basic_map_alloc_div(res) < 0)
12307 goto error;
12309 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12310 goto error;
12312 for (i = 0; i < bmap->n_eq; ++i) {
12313 k = isl_basic_map_alloc_equality(res);
12314 if (k < 0)
12315 goto error;
12316 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12317 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12320 for (i = 0; i < bmap->n_ineq; ++i) {
12321 k = isl_basic_map_alloc_inequality(res);
12322 if (k < 0)
12323 goto error;
12324 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12325 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12328 for (i = 0; i < bmap->n_div; ++i) {
12329 if (isl_int_is_zero(bmap->div[i][0])) {
12330 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12331 continue;
12333 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12334 n_before, n_after, n_div_ma, n_div_bmap,
12335 f, c1, c2, g, 1);
12338 if (strides)
12339 res = add_ma_strides(res, ma, n_before, n_after);
12341 isl_int_clear(f);
12342 isl_int_clear(c1);
12343 isl_int_clear(c2);
12344 isl_int_clear(g);
12345 isl_basic_map_free(bmap);
12346 isl_multi_aff_free(ma);
12347 res = isl_basic_set_simplify(res);
12348 return isl_basic_map_finalize(res);
12349 error:
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 isl_basic_map_free(res);
12357 return NULL;
12360 /* Compute the preimage of "bset" under the function represented by "ma".
12361 * In other words, plug in "ma" in "bset". The result is a basic set
12362 * that lives in the domain space of "ma".
12364 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12365 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12367 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12370 /* Compute the preimage of the domain of "bmap" under the function
12371 * represented by "ma".
12372 * In other words, plug in "ma" in the domain of "bmap".
12373 * The result is a basic map that lives in the same space as "bmap"
12374 * except that the domain has been replaced by the domain space of "ma".
12376 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12377 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12379 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12382 /* Compute the preimage of the range of "bmap" under the function
12383 * represented by "ma".
12384 * In other words, plug in "ma" in the range of "bmap".
12385 * The result is a basic map that lives in the same space as "bmap"
12386 * except that the range has been replaced by the domain space of "ma".
12388 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12389 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12391 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12394 /* Check if the range of "ma" is compatible with the domain or range
12395 * (depending on "type") of "map".
12396 * Return -1 if anything is wrong.
12398 static int check_map_compatible_range_multi_aff(
12399 __isl_keep isl_map *map, enum isl_dim_type type,
12400 __isl_keep isl_multi_aff *ma)
12402 int m;
12403 isl_space *ma_space;
12405 ma_space = isl_multi_aff_get_space(ma);
12406 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12407 isl_space_free(ma_space);
12408 if (m >= 0 && !m)
12409 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12410 "spaces don't match", return -1);
12411 return m;
12414 /* Compute the preimage of the domain or range (depending on "type")
12415 * of "map" under the function represented by "ma".
12416 * In other words, plug in "ma" in the domain or range of "map".
12417 * The result is a map that lives in the same space as "map"
12418 * except that the domain or range has been replaced by
12419 * the domain space of "ma".
12421 * The parameters are assumed to have been aligned.
12423 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12424 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12426 int i;
12427 isl_space *space;
12429 map = isl_map_cow(map);
12430 ma = isl_multi_aff_align_divs(ma);
12431 if (!map || !ma)
12432 goto error;
12433 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12434 goto error;
12436 for (i = 0; i < map->n; ++i) {
12437 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12438 isl_multi_aff_copy(ma));
12439 if (!map->p[i])
12440 goto error;
12443 space = isl_multi_aff_get_domain_space(ma);
12444 space = isl_space_set(isl_map_get_space(map), type, space);
12446 isl_space_free(map->dim);
12447 map->dim = space;
12448 if (!map->dim)
12449 goto error;
12451 isl_multi_aff_free(ma);
12452 if (map->n > 1)
12453 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12454 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12455 return map;
12456 error:
12457 isl_multi_aff_free(ma);
12458 isl_map_free(map);
12459 return NULL;
12462 /* Compute the preimage of the domain or range (depending on "type")
12463 * of "map" under the function represented by "ma".
12464 * In other words, plug in "ma" in the domain or range of "map".
12465 * The result is a map that lives in the same space as "map"
12466 * except that the domain or range has been replaced by
12467 * the domain space of "ma".
12469 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12470 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12472 if (!map || !ma)
12473 goto error;
12475 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12476 return map_preimage_multi_aff(map, type, ma);
12478 if (!isl_space_has_named_params(map->dim) ||
12479 !isl_space_has_named_params(ma->space))
12480 isl_die(map->ctx, isl_error_invalid,
12481 "unaligned unnamed parameters", goto error);
12482 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12483 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12485 return map_preimage_multi_aff(map, type, ma);
12486 error:
12487 isl_multi_aff_free(ma);
12488 return isl_map_free(map);
12491 /* Compute the preimage of "set" under the function represented by "ma".
12492 * In other words, plug in "ma" in "set". The result is a set
12493 * that lives in the domain space of "ma".
12495 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12496 __isl_take isl_multi_aff *ma)
12498 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12501 /* Compute the preimage of the domain of "map" under the function
12502 * represented by "ma".
12503 * In other words, plug in "ma" in the domain of "map".
12504 * The result is a map that lives in the same space as "map"
12505 * except that the domain has been replaced by the domain space of "ma".
12507 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12508 __isl_take isl_multi_aff *ma)
12510 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12513 /* Compute the preimage of the range of "map" under the function
12514 * represented by "ma".
12515 * In other words, plug in "ma" in the range of "map".
12516 * The result is a map that lives in the same space as "map"
12517 * except that the range has been replaced by the domain space of "ma".
12519 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12520 __isl_take isl_multi_aff *ma)
12522 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12525 /* Compute the preimage of "map" under the function represented by "pma".
12526 * In other words, plug in "pma" in the domain or range of "map".
12527 * The result is a map that lives in the same space as "map",
12528 * except that the space of type "type" has been replaced by
12529 * the domain space of "pma".
12531 * The parameters of "map" and "pma" are assumed to have been aligned.
12533 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12534 __isl_take isl_map *map, enum isl_dim_type type,
12535 __isl_take isl_pw_multi_aff *pma)
12537 int i;
12538 isl_map *res;
12540 if (!pma)
12541 goto error;
12543 if (pma->n == 0) {
12544 isl_pw_multi_aff_free(pma);
12545 res = isl_map_empty(isl_map_get_space(map));
12546 isl_map_free(map);
12547 return res;
12550 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12551 isl_multi_aff_copy(pma->p[0].maff));
12552 if (type == isl_dim_in)
12553 res = isl_map_intersect_domain(res,
12554 isl_map_copy(pma->p[0].set));
12555 else
12556 res = isl_map_intersect_range(res,
12557 isl_map_copy(pma->p[0].set));
12559 for (i = 1; i < pma->n; ++i) {
12560 isl_map *res_i;
12562 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12563 isl_multi_aff_copy(pma->p[i].maff));
12564 if (type == isl_dim_in)
12565 res_i = isl_map_intersect_domain(res_i,
12566 isl_map_copy(pma->p[i].set));
12567 else
12568 res_i = isl_map_intersect_range(res_i,
12569 isl_map_copy(pma->p[i].set));
12570 res = isl_map_union(res, res_i);
12573 isl_pw_multi_aff_free(pma);
12574 isl_map_free(map);
12575 return res;
12576 error:
12577 isl_pw_multi_aff_free(pma);
12578 isl_map_free(map);
12579 return NULL;
12582 /* Compute the preimage of "map" under the function represented by "pma".
12583 * In other words, plug in "pma" in the domain or range of "map".
12584 * The result is a map that lives in the same space as "map",
12585 * except that the space of type "type" has been replaced by
12586 * the domain space of "pma".
12588 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12589 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12591 if (!map || !pma)
12592 goto error;
12594 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12595 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12597 if (!isl_space_has_named_params(map->dim) ||
12598 !isl_space_has_named_params(pma->dim))
12599 isl_die(map->ctx, isl_error_invalid,
12600 "unaligned unnamed parameters", goto error);
12601 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12602 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12604 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12605 error:
12606 isl_pw_multi_aff_free(pma);
12607 return isl_map_free(map);
12610 /* Compute the preimage of "set" under the function represented by "pma".
12611 * In other words, plug in "pma" in "set". The result is a set
12612 * that lives in the domain space of "pma".
12614 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12615 __isl_take isl_pw_multi_aff *pma)
12617 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12620 /* Compute the preimage of the domain of "map" under the function
12621 * represented by "pma".
12622 * In other words, plug in "pma" in the domain of "map".
12623 * The result is a map that lives in the same space as "map",
12624 * except that domain space has been replaced by the domain space of "pma".
12626 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12627 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12629 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12632 /* Compute the preimage of the range of "map" under the function
12633 * represented by "pma".
12634 * In other words, plug in "pma" in the range of "map".
12635 * The result is a map that lives in the same space as "map",
12636 * except that range space has been replaced by the domain space of "pma".
12638 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12639 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12641 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12644 /* Compute the preimage of "map" under the function represented by "mpa".
12645 * In other words, plug in "mpa" in the domain or range of "map".
12646 * The result is a map that lives in the same space as "map",
12647 * except that the space of type "type" has been replaced by
12648 * the domain space of "mpa".
12650 * If the map does not involve any constraints that refer to the
12651 * dimensions of the substituted space, then the only possible
12652 * effect of "mpa" on the map is to map the space to a different space.
12653 * We create a separate isl_multi_aff to effectuate this change
12654 * in order to avoid spurious splitting of the map along the pieces
12655 * of "mpa".
12657 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12658 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12660 int n;
12661 isl_pw_multi_aff *pma;
12663 if (!map || !mpa)
12664 goto error;
12666 n = isl_map_dim(map, type);
12667 if (!isl_map_involves_dims(map, type, 0, n)) {
12668 isl_space *space;
12669 isl_multi_aff *ma;
12671 space = isl_multi_pw_aff_get_space(mpa);
12672 isl_multi_pw_aff_free(mpa);
12673 ma = isl_multi_aff_zero(space);
12674 return isl_map_preimage_multi_aff(map, type, ma);
12677 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12678 return isl_map_preimage_pw_multi_aff(map, type, pma);
12679 error:
12680 isl_map_free(map);
12681 isl_multi_pw_aff_free(mpa);
12682 return NULL;
12685 /* Compute the preimage of "map" under the function represented by "mpa".
12686 * In other words, plug in "mpa" in the domain "map".
12687 * The result is a map that lives in the same space as "map",
12688 * except that domain space has been replaced by the domain space of "mpa".
12690 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12691 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12693 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12696 /* Compute the preimage of "set" by the function represented by "mpa".
12697 * In other words, plug in "mpa" in "set".
12699 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12700 __isl_take isl_multi_pw_aff *mpa)
12702 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);