hide internal isl_basic_map functions
[isl.git] / isl_map.c
blob731e7abc735f8f76309ee8195a8bdff39b284464
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <string.h>
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
21 #include <isl_blk.h>
22 #include <isl/constraint.h>
23 #include "isl_space_private.h"
24 #include "isl_equalities.h"
25 #include <isl_lp_private.h>
26 #include <isl_seq.h>
27 #include <isl/set.h>
28 #include <isl/map.h>
29 #include <isl_reordering.h>
30 #include "isl_sample.h"
31 #include <isl_sort.h>
32 #include "isl_tab.h"
33 #include <isl/vec.h>
34 #include <isl_mat_private.h>
35 #include <isl_vec_private.h>
36 #include <isl_dim_map.h>
37 #include <isl_local_space_private.h>
38 #include <isl_aff_private.h>
39 #include <isl_options_private.h>
40 #include <isl_morph.h>
41 #include <isl_val_private.h>
42 #include <isl/deprecated/map_int.h>
43 #include <isl/deprecated/set_int.h>
45 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
47 switch (type) {
48 case isl_dim_param: return dim->nparam;
49 case isl_dim_in: return dim->n_in;
50 case isl_dim_out: return dim->n_out;
51 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
52 default: return 0;
56 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
58 switch (type) {
59 case isl_dim_param: return 1;
60 case isl_dim_in: return 1 + dim->nparam;
61 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
62 default: return 0;
66 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
67 enum isl_dim_type type)
69 if (!bmap)
70 return 0;
71 switch (type) {
72 case isl_dim_cst: return 1;
73 case isl_dim_param:
74 case isl_dim_in:
75 case isl_dim_out: return isl_space_dim(bmap->dim, type);
76 case isl_dim_div: return bmap->n_div;
77 case isl_dim_all: return isl_basic_map_total_dim(bmap);
78 default: return 0;
82 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
84 return map ? n(map->dim, type) : 0;
87 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
89 return set ? n(set->dim, type) : 0;
92 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
93 enum isl_dim_type type)
95 isl_space *dim = bmap->dim;
96 switch (type) {
97 case isl_dim_cst: return 0;
98 case isl_dim_param: return 1;
99 case isl_dim_in: return 1 + dim->nparam;
100 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
101 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
102 default: return 0;
106 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
107 enum isl_dim_type type)
109 return isl_basic_map_offset(bset, type);
112 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
114 return pos(map->dim, type);
117 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
118 enum isl_dim_type type)
120 return isl_basic_map_dim(bset, type);
123 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
125 return isl_basic_set_dim(bset, isl_dim_set);
128 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
130 return isl_basic_set_dim(bset, isl_dim_param);
133 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
135 if (!bset)
136 return 0;
137 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
140 unsigned isl_set_n_dim(__isl_keep isl_set *set)
142 return isl_set_dim(set, isl_dim_set);
145 unsigned isl_set_n_param(__isl_keep isl_set *set)
147 return isl_set_dim(set, isl_dim_param);
150 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
152 return bmap ? bmap->dim->n_in : 0;
155 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
157 return bmap ? bmap->dim->n_out : 0;
160 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
162 return bmap ? bmap->dim->nparam : 0;
165 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
167 return bmap ? bmap->n_div : 0;
170 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
172 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
175 unsigned isl_map_n_in(const struct isl_map *map)
177 return map ? map->dim->n_in : 0;
180 unsigned isl_map_n_out(const struct isl_map *map)
182 return map ? map->dim->n_out : 0;
185 unsigned isl_map_n_param(const struct isl_map *map)
187 return map ? map->dim->nparam : 0;
190 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
192 int m;
193 if (!map || !set)
194 return -1;
195 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
196 if (m < 0 || !m)
197 return m;
198 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
199 set->dim, isl_dim_set);
202 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
203 struct isl_basic_set *bset)
205 int m;
206 if (!bmap || !bset)
207 return -1;
208 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
209 if (m < 0 || !m)
210 return m;
211 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
212 bset->dim, isl_dim_set);
215 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
217 int m;
218 if (!map || !set)
219 return -1;
220 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
221 if (m < 0 || !m)
222 return m;
223 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
224 set->dim, isl_dim_set);
227 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
228 struct isl_basic_set *bset)
230 int m;
231 if (!bmap || !bset)
232 return -1;
233 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
234 if (m < 0 || !m)
235 return m;
236 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
237 bset->dim, isl_dim_set);
240 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
242 return bmap ? bmap->ctx : NULL;
245 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
247 return bset ? bset->ctx : NULL;
250 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
252 return map ? map->ctx : NULL;
255 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
257 return set ? set->ctx : NULL;
260 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
262 if (!bmap)
263 return NULL;
264 return isl_space_copy(bmap->dim);
267 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
269 if (!bset)
270 return NULL;
271 return isl_space_copy(bset->dim);
274 /* Extract the divs in "bmap" as a matrix.
276 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
278 int i;
279 isl_ctx *ctx;
280 isl_mat *div;
281 unsigned total;
282 unsigned cols;
284 if (!bmap)
285 return NULL;
287 ctx = isl_basic_map_get_ctx(bmap);
288 total = isl_space_dim(bmap->dim, isl_dim_all);
289 cols = 1 + 1 + total + bmap->n_div;
290 div = isl_mat_alloc(ctx, bmap->n_div, cols);
291 if (!div)
292 return NULL;
294 for (i = 0; i < bmap->n_div; ++i)
295 isl_seq_cpy(div->row[i], bmap->div[i], cols);
297 return div;
300 /* Extract the divs in "bset" as a matrix.
302 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
304 return isl_basic_map_get_divs(bset);
307 __isl_give isl_local_space *isl_basic_map_get_local_space(
308 __isl_keep isl_basic_map *bmap)
310 isl_mat *div;
312 if (!bmap)
313 return NULL;
315 div = isl_basic_map_get_divs(bmap);
316 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
319 __isl_give isl_local_space *isl_basic_set_get_local_space(
320 __isl_keep isl_basic_set *bset)
322 return isl_basic_map_get_local_space(bset);
325 __isl_give isl_basic_map *isl_basic_map_from_local_space(
326 __isl_take isl_local_space *ls)
328 int i;
329 int n_div;
330 isl_basic_map *bmap;
332 if (!ls)
333 return NULL;
335 n_div = isl_local_space_dim(ls, isl_dim_div);
336 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
337 n_div, 0, 2 * n_div);
339 for (i = 0; i < n_div; ++i)
340 if (isl_basic_map_alloc_div(bmap) < 0)
341 goto error;
343 for (i = 0; i < n_div; ++i) {
344 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
345 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
346 goto error;
349 isl_local_space_free(ls);
350 return bmap;
351 error:
352 isl_local_space_free(ls);
353 isl_basic_map_free(bmap);
354 return NULL;
357 __isl_give isl_basic_set *isl_basic_set_from_local_space(
358 __isl_take isl_local_space *ls)
360 return isl_basic_map_from_local_space(ls);
363 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
365 if (!map)
366 return NULL;
367 return isl_space_copy(map->dim);
370 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
372 if (!set)
373 return NULL;
374 return isl_space_copy(set->dim);
377 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
378 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
380 bmap = isl_basic_map_cow(bmap);
381 if (!bmap)
382 return NULL;
383 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
384 if (!bmap->dim)
385 goto error;
386 bmap = isl_basic_map_finalize(bmap);
387 return bmap;
388 error:
389 isl_basic_map_free(bmap);
390 return NULL;
393 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
394 __isl_take isl_basic_set *bset, const char *s)
396 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
399 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
400 enum isl_dim_type type)
402 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
405 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
406 enum isl_dim_type type, const char *s)
408 int i;
410 map = isl_map_cow(map);
411 if (!map)
412 return NULL;
414 map->dim = isl_space_set_tuple_name(map->dim, type, s);
415 if (!map->dim)
416 goto error;
418 for (i = 0; i < map->n; ++i) {
419 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
420 if (!map->p[i])
421 goto error;
424 return map;
425 error:
426 isl_map_free(map);
427 return NULL;
430 /* Replace the identifier of the tuple of type "type" by "id".
432 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
433 __isl_take isl_basic_map *bmap,
434 enum isl_dim_type type, __isl_take isl_id *id)
436 bmap = isl_basic_map_cow(bmap);
437 if (!bmap)
438 goto error;
439 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
440 if (!bmap->dim)
441 return isl_basic_map_free(bmap);
442 bmap = isl_basic_map_finalize(bmap);
443 return bmap;
444 error:
445 isl_id_free(id);
446 return NULL;
449 /* Replace the identifier of the tuple by "id".
451 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
452 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
454 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
457 /* Does the input or output tuple have a name?
459 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
461 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
464 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
465 enum isl_dim_type type)
467 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
470 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
471 const char *s)
473 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
476 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
477 enum isl_dim_type type, __isl_take isl_id *id)
479 map = isl_map_cow(map);
480 if (!map)
481 goto error;
483 map->dim = isl_space_set_tuple_id(map->dim, type, id);
485 return isl_map_reset_space(map, isl_space_copy(map->dim));
486 error:
487 isl_id_free(id);
488 return NULL;
491 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
492 __isl_take isl_id *id)
494 return isl_map_set_tuple_id(set, isl_dim_set, id);
497 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
498 enum isl_dim_type type)
500 map = isl_map_cow(map);
501 if (!map)
502 return NULL;
504 map->dim = isl_space_reset_tuple_id(map->dim, type);
506 return isl_map_reset_space(map, isl_space_copy(map->dim));
509 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
511 return isl_map_reset_tuple_id(set, isl_dim_set);
514 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
516 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
519 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
520 enum isl_dim_type type)
522 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
525 int isl_set_has_tuple_id(__isl_keep isl_set *set)
527 return isl_map_has_tuple_id(set, isl_dim_set);
530 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
532 return isl_map_get_tuple_id(set, isl_dim_set);
535 /* Does the set tuple have a name?
537 int isl_set_has_tuple_name(__isl_keep isl_set *set)
539 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
543 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
545 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
548 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
550 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
553 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
554 enum isl_dim_type type, unsigned pos)
556 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
559 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
560 enum isl_dim_type type, unsigned pos)
562 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
565 /* Does the given dimension have a name?
567 int isl_map_has_dim_name(__isl_keep isl_map *map,
568 enum isl_dim_type type, unsigned pos)
570 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
573 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
574 enum isl_dim_type type, unsigned pos)
576 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
579 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
580 enum isl_dim_type type, unsigned pos)
582 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
585 /* Does the given dimension have a name?
587 int isl_set_has_dim_name(__isl_keep isl_set *set,
588 enum isl_dim_type type, unsigned pos)
590 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
593 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
594 __isl_take isl_basic_map *bmap,
595 enum isl_dim_type type, unsigned pos, const char *s)
597 bmap = isl_basic_map_cow(bmap);
598 if (!bmap)
599 return NULL;
600 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
601 if (!bmap->dim)
602 goto error;
603 return isl_basic_map_finalize(bmap);
604 error:
605 isl_basic_map_free(bmap);
606 return NULL;
609 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
610 enum isl_dim_type type, unsigned pos, const char *s)
612 int i;
614 map = isl_map_cow(map);
615 if (!map)
616 return NULL;
618 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
619 if (!map->dim)
620 goto error;
622 for (i = 0; i < map->n; ++i) {
623 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
624 if (!map->p[i])
625 goto error;
628 return map;
629 error:
630 isl_map_free(map);
631 return NULL;
634 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
635 __isl_take isl_basic_set *bset,
636 enum isl_dim_type type, unsigned pos, const char *s)
638 return (isl_basic_set *)isl_basic_map_set_dim_name(
639 (isl_basic_map *)bset, type, pos, s);
642 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
643 enum isl_dim_type type, unsigned pos, const char *s)
645 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
648 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
649 enum isl_dim_type type, unsigned pos)
651 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
654 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
655 enum isl_dim_type type, unsigned pos)
657 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
660 int isl_map_has_dim_id(__isl_keep isl_map *map,
661 enum isl_dim_type type, unsigned pos)
663 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
666 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
667 enum isl_dim_type type, unsigned pos)
669 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
672 int isl_set_has_dim_id(__isl_keep isl_set *set,
673 enum isl_dim_type type, unsigned pos)
675 return isl_map_has_dim_id(set, type, pos);
678 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
679 enum isl_dim_type type, unsigned pos)
681 return isl_map_get_dim_id(set, type, pos);
684 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
685 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
687 map = isl_map_cow(map);
688 if (!map)
689 goto error;
691 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
693 return isl_map_reset_space(map, isl_space_copy(map->dim));
694 error:
695 isl_id_free(id);
696 return NULL;
699 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
700 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
702 return isl_map_set_dim_id(set, type, pos, id);
705 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
706 __isl_keep isl_id *id)
708 if (!map)
709 return -1;
710 return isl_space_find_dim_by_id(map->dim, type, id);
713 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
714 __isl_keep isl_id *id)
716 return isl_map_find_dim_by_id(set, type, id);
719 /* Return the position of the dimension of the given type and name
720 * in "bmap".
721 * Return -1 if no such dimension can be found.
723 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
724 enum isl_dim_type type, const char *name)
726 if (!bmap)
727 return -1;
728 return isl_space_find_dim_by_name(bmap->dim, type, name);
731 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
732 const char *name)
734 if (!map)
735 return -1;
736 return isl_space_find_dim_by_name(map->dim, type, name);
739 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
740 const char *name)
742 return isl_map_find_dim_by_name(set, type, name);
745 /* Reset the user pointer on all identifiers of parameters and tuples
746 * of the space of "map".
748 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
750 isl_space *space;
752 space = isl_map_get_space(map);
753 space = isl_space_reset_user(space);
754 map = isl_map_reset_space(map, space);
756 return map;
759 /* Reset the user pointer on all identifiers of parameters and tuples
760 * of the space of "set".
762 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
764 return isl_map_reset_user(set);
767 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
769 if (!bmap)
770 return -1;
771 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
774 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
776 return isl_basic_map_is_rational(bset);
779 /* Does "bmap" contain any rational points?
781 * If "bmap" has an equality for each dimension, equating the dimension
782 * to an integer constant, then it has no rational points, even if it
783 * is marked as rational.
785 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
787 int has_rational = 1;
788 unsigned total;
790 if (!bmap)
791 return -1;
792 if (isl_basic_map_plain_is_empty(bmap))
793 return 0;
794 if (!isl_basic_map_is_rational(bmap))
795 return 0;
796 bmap = isl_basic_map_copy(bmap);
797 bmap = isl_basic_map_implicit_equalities(bmap);
798 if (!bmap)
799 return -1;
800 total = isl_basic_map_total_dim(bmap);
801 if (bmap->n_eq == total) {
802 int i, j;
803 for (i = 0; i < bmap->n_eq; ++i) {
804 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
805 if (j < 0)
806 break;
807 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
808 !isl_int_is_negone(bmap->eq[i][1 + j]))
809 break;
810 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
811 total - j - 1);
812 if (j >= 0)
813 break;
815 if (i == bmap->n_eq)
816 has_rational = 0;
818 isl_basic_map_free(bmap);
820 return has_rational;
823 /* Does "map" contain any rational points?
825 int isl_map_has_rational(__isl_keep isl_map *map)
827 int i;
828 int has_rational;
830 if (!map)
831 return -1;
832 for (i = 0; i < map->n; ++i) {
833 has_rational = isl_basic_map_has_rational(map->p[i]);
834 if (has_rational < 0)
835 return -1;
836 if (has_rational)
837 return 1;
839 return 0;
842 /* Does "set" contain any rational points?
844 int isl_set_has_rational(__isl_keep isl_set *set)
846 return isl_map_has_rational(set);
849 /* Is this basic set a parameter domain?
851 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
853 if (!bset)
854 return -1;
855 return isl_space_is_params(bset->dim);
858 /* Is this set a parameter domain?
860 int isl_set_is_params(__isl_keep isl_set *set)
862 if (!set)
863 return -1;
864 return isl_space_is_params(set->dim);
867 /* Is this map actually a parameter domain?
868 * Users should never call this function. Outside of isl,
869 * a map can never be a parameter domain.
871 int isl_map_is_params(__isl_keep isl_map *map)
873 if (!map)
874 return -1;
875 return isl_space_is_params(map->dim);
878 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
879 struct isl_basic_map *bmap, unsigned extra,
880 unsigned n_eq, unsigned n_ineq)
882 int i;
883 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
885 bmap->ctx = ctx;
886 isl_ctx_ref(ctx);
888 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
889 if (isl_blk_is_error(bmap->block))
890 goto error;
892 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
893 if ((n_ineq + n_eq) && !bmap->ineq)
894 goto error;
896 if (extra == 0) {
897 bmap->block2 = isl_blk_empty();
898 bmap->div = NULL;
899 } else {
900 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
901 if (isl_blk_is_error(bmap->block2))
902 goto error;
904 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
905 if (!bmap->div)
906 goto error;
909 for (i = 0; i < n_ineq + n_eq; ++i)
910 bmap->ineq[i] = bmap->block.data + i * row_size;
912 for (i = 0; i < extra; ++i)
913 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
915 bmap->ref = 1;
916 bmap->flags = 0;
917 bmap->c_size = n_eq + n_ineq;
918 bmap->eq = bmap->ineq + n_ineq;
919 bmap->extra = extra;
920 bmap->n_eq = 0;
921 bmap->n_ineq = 0;
922 bmap->n_div = 0;
923 bmap->sample = NULL;
925 return bmap;
926 error:
927 isl_basic_map_free(bmap);
928 return NULL;
931 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
932 unsigned nparam, unsigned dim, unsigned extra,
933 unsigned n_eq, unsigned n_ineq)
935 struct isl_basic_map *bmap;
936 isl_space *space;
938 space = isl_space_set_alloc(ctx, nparam, dim);
939 if (!space)
940 return NULL;
942 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
943 return (struct isl_basic_set *)bmap;
946 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
947 unsigned extra, unsigned n_eq, unsigned n_ineq)
949 struct isl_basic_map *bmap;
950 if (!dim)
951 return NULL;
952 isl_assert(dim->ctx, dim->n_in == 0, goto error);
953 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
954 return (struct isl_basic_set *)bmap;
955 error:
956 isl_space_free(dim);
957 return NULL;
960 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
961 unsigned extra, unsigned n_eq, unsigned n_ineq)
963 struct isl_basic_map *bmap;
965 if (!dim)
966 return NULL;
967 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
968 if (!bmap)
969 goto error;
970 bmap->dim = dim;
972 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
973 error:
974 isl_space_free(dim);
975 return NULL;
978 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
979 unsigned nparam, unsigned in, unsigned out, unsigned extra,
980 unsigned n_eq, unsigned n_ineq)
982 struct isl_basic_map *bmap;
983 isl_space *dim;
985 dim = isl_space_alloc(ctx, nparam, in, out);
986 if (!dim)
987 return NULL;
989 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
990 return bmap;
993 static void dup_constraints(
994 struct isl_basic_map *dst, struct isl_basic_map *src)
996 int i;
997 unsigned total = isl_basic_map_total_dim(src);
999 for (i = 0; i < src->n_eq; ++i) {
1000 int j = isl_basic_map_alloc_equality(dst);
1001 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1004 for (i = 0; i < src->n_ineq; ++i) {
1005 int j = isl_basic_map_alloc_inequality(dst);
1006 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1009 for (i = 0; i < src->n_div; ++i) {
1010 int j = isl_basic_map_alloc_div(dst);
1011 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1013 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1016 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1018 struct isl_basic_map *dup;
1020 if (!bmap)
1021 return NULL;
1022 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1023 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1024 if (!dup)
1025 return NULL;
1026 dup_constraints(dup, bmap);
1027 dup->flags = bmap->flags;
1028 dup->sample = isl_vec_copy(bmap->sample);
1029 return dup;
1032 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1034 struct isl_basic_map *dup;
1036 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1037 return (struct isl_basic_set *)dup;
1040 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1042 if (!bset)
1043 return NULL;
1045 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1046 bset->ref++;
1047 return bset;
1049 return isl_basic_set_dup(bset);
1052 struct isl_set *isl_set_copy(struct isl_set *set)
1054 if (!set)
1055 return NULL;
1057 set->ref++;
1058 return set;
1061 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1063 if (!bmap)
1064 return NULL;
1066 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1067 bmap->ref++;
1068 return bmap;
1070 bmap = isl_basic_map_dup(bmap);
1071 if (bmap)
1072 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1073 return bmap;
1076 struct isl_map *isl_map_copy(struct isl_map *map)
1078 if (!map)
1079 return NULL;
1081 map->ref++;
1082 return map;
1085 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1087 if (!bmap)
1088 return NULL;
1090 if (--bmap->ref > 0)
1091 return NULL;
1093 isl_ctx_deref(bmap->ctx);
1094 free(bmap->div);
1095 isl_blk_free(bmap->ctx, bmap->block2);
1096 free(bmap->ineq);
1097 isl_blk_free(bmap->ctx, bmap->block);
1098 isl_vec_free(bmap->sample);
1099 isl_space_free(bmap->dim);
1100 free(bmap);
1102 return NULL;
1105 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1107 return isl_basic_map_free((struct isl_basic_map *)bset);
1110 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1112 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1115 __isl_give isl_map *isl_map_align_params_map_map_and(
1116 __isl_take isl_map *map1, __isl_take isl_map *map2,
1117 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1118 __isl_take isl_map *map2))
1120 if (!map1 || !map2)
1121 goto error;
1122 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1123 return fn(map1, map2);
1124 if (!isl_space_has_named_params(map1->dim) ||
1125 !isl_space_has_named_params(map2->dim))
1126 isl_die(map1->ctx, isl_error_invalid,
1127 "unaligned unnamed parameters", goto error);
1128 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1129 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1130 return fn(map1, map2);
1131 error:
1132 isl_map_free(map1);
1133 isl_map_free(map2);
1134 return NULL;
1137 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1138 __isl_keep isl_map *map2,
1139 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1141 int r;
1143 if (!map1 || !map2)
1144 return -1;
1145 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1146 return fn(map1, map2);
1147 if (!isl_space_has_named_params(map1->dim) ||
1148 !isl_space_has_named_params(map2->dim))
1149 isl_die(map1->ctx, isl_error_invalid,
1150 "unaligned unnamed parameters", return -1);
1151 map1 = isl_map_copy(map1);
1152 map2 = isl_map_copy(map2);
1153 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1154 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1155 r = fn(map1, map2);
1156 isl_map_free(map1);
1157 isl_map_free(map2);
1158 return r;
1161 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1163 struct isl_ctx *ctx;
1164 if (!bmap)
1165 return -1;
1166 ctx = bmap->ctx;
1167 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1168 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1169 return -1);
1170 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1171 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1172 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1173 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1174 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1175 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1176 isl_int *t;
1177 int j = isl_basic_map_alloc_inequality(bmap);
1178 if (j < 0)
1179 return -1;
1180 t = bmap->ineq[j];
1181 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1182 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1183 bmap->eq[-1] = t;
1184 bmap->n_eq++;
1185 bmap->n_ineq--;
1186 bmap->eq--;
1187 return 0;
1189 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1190 bmap->extra - bmap->n_div);
1191 return bmap->n_eq++;
1194 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1196 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1199 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1201 if (!bmap)
1202 return -1;
1203 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1204 bmap->n_eq -= n;
1205 return 0;
1208 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1210 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1213 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1215 isl_int *t;
1216 if (!bmap)
1217 return -1;
1218 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1220 if (pos != bmap->n_eq - 1) {
1221 t = bmap->eq[pos];
1222 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1223 bmap->eq[bmap->n_eq - 1] = t;
1225 bmap->n_eq--;
1226 return 0;
1229 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1231 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1234 /* Turn inequality "pos" of "bmap" into an equality.
1236 * In particular, we move the inequality in front of the equalities
1237 * and move the last inequality in the position of the moved inequality.
1238 * Note that isl_tab_make_equalities_explicit depends on this particular
1239 * change in the ordering of the constraints.
1241 void isl_basic_map_inequality_to_equality(
1242 struct isl_basic_map *bmap, unsigned pos)
1244 isl_int *t;
1246 t = bmap->ineq[pos];
1247 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1248 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1249 bmap->eq[-1] = t;
1250 bmap->n_eq++;
1251 bmap->n_ineq--;
1252 bmap->eq--;
1253 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1254 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1255 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1256 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1259 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1261 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1264 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1266 struct isl_ctx *ctx;
1267 if (!bmap)
1268 return -1;
1269 ctx = bmap->ctx;
1270 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1274 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1275 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1276 1 + isl_basic_map_total_dim(bmap),
1277 bmap->extra - bmap->n_div);
1278 return bmap->n_ineq++;
1281 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1283 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1286 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1288 if (!bmap)
1289 return -1;
1290 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1291 bmap->n_ineq -= n;
1292 return 0;
1295 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1297 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1300 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1302 isl_int *t;
1303 if (!bmap)
1304 return -1;
1305 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1307 if (pos != bmap->n_ineq - 1) {
1308 t = bmap->ineq[pos];
1309 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1310 bmap->ineq[bmap->n_ineq - 1] = t;
1311 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1313 bmap->n_ineq--;
1314 return 0;
1317 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1319 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1322 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1323 isl_int *eq)
1325 int k;
1327 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1328 if (!bmap)
1329 return NULL;
1330 k = isl_basic_map_alloc_equality(bmap);
1331 if (k < 0)
1332 goto error;
1333 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1334 return bmap;
1335 error:
1336 isl_basic_map_free(bmap);
1337 return NULL;
1340 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1341 isl_int *eq)
1343 return (isl_basic_set *)
1344 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1347 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1348 isl_int *ineq)
1350 int k;
1352 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1353 if (!bmap)
1354 return NULL;
1355 k = isl_basic_map_alloc_inequality(bmap);
1356 if (k < 0)
1357 goto error;
1358 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1359 return bmap;
1360 error:
1361 isl_basic_map_free(bmap);
1362 return NULL;
1365 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1366 isl_int *ineq)
1368 return (isl_basic_set *)
1369 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1372 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1374 if (!bmap)
1375 return -1;
1376 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1377 isl_seq_clr(bmap->div[bmap->n_div] +
1378 1 + 1 + isl_basic_map_total_dim(bmap),
1379 bmap->extra - bmap->n_div);
1380 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1381 return bmap->n_div++;
1384 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1386 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1389 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1391 if (!bmap)
1392 return -1;
1393 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1394 bmap->n_div -= n;
1395 return 0;
1398 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1400 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1403 /* Copy constraint from src to dst, putting the vars of src at offset
1404 * dim_off in dst and the divs of src at offset div_off in dst.
1405 * If both sets are actually map, then dim_off applies to the input
1406 * variables.
1408 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1409 struct isl_basic_map *src_map, isl_int *src,
1410 unsigned in_off, unsigned out_off, unsigned div_off)
1412 unsigned src_nparam = isl_basic_map_n_param(src_map);
1413 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1414 unsigned src_in = isl_basic_map_n_in(src_map);
1415 unsigned dst_in = isl_basic_map_n_in(dst_map);
1416 unsigned src_out = isl_basic_map_n_out(src_map);
1417 unsigned dst_out = isl_basic_map_n_out(dst_map);
1418 isl_int_set(dst[0], src[0]);
1419 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1420 if (dst_nparam > src_nparam)
1421 isl_seq_clr(dst+1+src_nparam,
1422 dst_nparam - src_nparam);
1423 isl_seq_clr(dst+1+dst_nparam, in_off);
1424 isl_seq_cpy(dst+1+dst_nparam+in_off,
1425 src+1+src_nparam,
1426 isl_min(dst_in-in_off, src_in));
1427 if (dst_in-in_off > src_in)
1428 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1429 dst_in - in_off - src_in);
1430 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1431 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1432 src+1+src_nparam+src_in,
1433 isl_min(dst_out-out_off, src_out));
1434 if (dst_out-out_off > src_out)
1435 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1436 dst_out - out_off - src_out);
1437 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1438 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1439 src+1+src_nparam+src_in+src_out,
1440 isl_min(dst_map->extra-div_off, src_map->n_div));
1441 if (dst_map->n_div-div_off > src_map->n_div)
1442 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1443 div_off+src_map->n_div,
1444 dst_map->n_div - div_off - src_map->n_div);
1447 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1448 struct isl_basic_map *src_map, isl_int *src,
1449 unsigned in_off, unsigned out_off, unsigned div_off)
1451 isl_int_set(dst[0], src[0]);
1452 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1455 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1456 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1458 int i;
1459 unsigned div_off;
1461 if (!bmap1 || !bmap2)
1462 goto error;
1464 div_off = bmap1->n_div;
1466 for (i = 0; i < bmap2->n_eq; ++i) {
1467 int i1 = isl_basic_map_alloc_equality(bmap1);
1468 if (i1 < 0)
1469 goto error;
1470 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1471 i_pos, o_pos, div_off);
1474 for (i = 0; i < bmap2->n_ineq; ++i) {
1475 int i1 = isl_basic_map_alloc_inequality(bmap1);
1476 if (i1 < 0)
1477 goto error;
1478 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1479 i_pos, o_pos, div_off);
1482 for (i = 0; i < bmap2->n_div; ++i) {
1483 int i1 = isl_basic_map_alloc_div(bmap1);
1484 if (i1 < 0)
1485 goto error;
1486 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1487 i_pos, o_pos, div_off);
1490 isl_basic_map_free(bmap2);
1492 return bmap1;
1494 error:
1495 isl_basic_map_free(bmap1);
1496 isl_basic_map_free(bmap2);
1497 return NULL;
1500 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1501 struct isl_basic_set *bset2, unsigned pos)
1503 return (struct isl_basic_set *)
1504 add_constraints((struct isl_basic_map *)bset1,
1505 (struct isl_basic_map *)bset2, 0, pos);
1508 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1509 __isl_take isl_space *dim, unsigned extra,
1510 unsigned n_eq, unsigned n_ineq)
1512 struct isl_basic_map *ext;
1513 unsigned flags;
1514 int dims_ok;
1516 if (!dim)
1517 goto error;
1519 if (!base)
1520 goto error;
1522 dims_ok = isl_space_is_equal(base->dim, dim) &&
1523 base->extra >= base->n_div + extra;
1525 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1526 room_for_ineq(base, n_ineq)) {
1527 isl_space_free(dim);
1528 return base;
1531 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1532 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1533 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1534 extra += base->extra;
1535 n_eq += base->n_eq;
1536 n_ineq += base->n_ineq;
1538 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1539 dim = NULL;
1540 if (!ext)
1541 goto error;
1543 if (dims_ok)
1544 ext->sample = isl_vec_copy(base->sample);
1545 flags = base->flags;
1546 ext = add_constraints(ext, base, 0, 0);
1547 if (ext) {
1548 ext->flags = flags;
1549 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1552 return ext;
1554 error:
1555 isl_space_free(dim);
1556 isl_basic_map_free(base);
1557 return NULL;
1560 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1561 __isl_take isl_space *dim, unsigned extra,
1562 unsigned n_eq, unsigned n_ineq)
1564 return (struct isl_basic_set *)
1565 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1566 extra, n_eq, n_ineq);
1569 struct isl_basic_map *isl_basic_map_extend_constraints(
1570 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1572 if (!base)
1573 return NULL;
1574 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1575 0, n_eq, n_ineq);
1578 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1579 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1580 unsigned n_eq, unsigned n_ineq)
1582 struct isl_basic_map *bmap;
1583 isl_space *dim;
1585 if (!base)
1586 return NULL;
1587 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1588 if (!dim)
1589 goto error;
1591 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1592 return bmap;
1593 error:
1594 isl_basic_map_free(base);
1595 return NULL;
1598 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1599 unsigned nparam, unsigned dim, unsigned extra,
1600 unsigned n_eq, unsigned n_ineq)
1602 return (struct isl_basic_set *)
1603 isl_basic_map_extend((struct isl_basic_map *)base,
1604 nparam, 0, dim, extra, n_eq, n_ineq);
1607 struct isl_basic_set *isl_basic_set_extend_constraints(
1608 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1610 return (struct isl_basic_set *)
1611 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1612 n_eq, n_ineq);
1615 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1617 return (struct isl_basic_set *)
1618 isl_basic_map_cow((struct isl_basic_map *)bset);
1621 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1623 if (!bmap)
1624 return NULL;
1626 if (bmap->ref > 1) {
1627 bmap->ref--;
1628 bmap = isl_basic_map_dup(bmap);
1630 if (bmap) {
1631 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1632 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1634 return bmap;
1637 struct isl_set *isl_set_cow(struct isl_set *set)
1639 if (!set)
1640 return NULL;
1642 if (set->ref == 1)
1643 return set;
1644 set->ref--;
1645 return isl_set_dup(set);
1648 struct isl_map *isl_map_cow(struct isl_map *map)
1650 if (!map)
1651 return NULL;
1653 if (map->ref == 1)
1654 return map;
1655 map->ref--;
1656 return isl_map_dup(map);
1659 static void swap_vars(struct isl_blk blk, isl_int *a,
1660 unsigned a_len, unsigned b_len)
1662 isl_seq_cpy(blk.data, a+a_len, b_len);
1663 isl_seq_cpy(blk.data+b_len, a, a_len);
1664 isl_seq_cpy(a, blk.data, b_len+a_len);
1667 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1668 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1670 int i;
1671 struct isl_blk blk;
1673 if (!bmap)
1674 goto error;
1676 isl_assert(bmap->ctx,
1677 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1679 if (n1 == 0 || n2 == 0)
1680 return bmap;
1682 bmap = isl_basic_map_cow(bmap);
1683 if (!bmap)
1684 return NULL;
1686 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1687 if (isl_blk_is_error(blk))
1688 goto error;
1690 for (i = 0; i < bmap->n_eq; ++i)
1691 swap_vars(blk,
1692 bmap->eq[i] + pos, n1, n2);
1694 for (i = 0; i < bmap->n_ineq; ++i)
1695 swap_vars(blk,
1696 bmap->ineq[i] + pos, n1, n2);
1698 for (i = 0; i < bmap->n_div; ++i)
1699 swap_vars(blk,
1700 bmap->div[i]+1 + pos, n1, n2);
1702 isl_blk_free(bmap->ctx, blk);
1704 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1705 bmap = isl_basic_map_gauss(bmap, NULL);
1706 return isl_basic_map_finalize(bmap);
1707 error:
1708 isl_basic_map_free(bmap);
1709 return NULL;
1712 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1714 int i = 0;
1715 unsigned total;
1716 if (!bmap)
1717 goto error;
1718 total = isl_basic_map_total_dim(bmap);
1719 isl_basic_map_free_div(bmap, bmap->n_div);
1720 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1721 if (bmap->n_eq > 0)
1722 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1723 else {
1724 i = isl_basic_map_alloc_equality(bmap);
1725 if (i < 0)
1726 goto error;
1728 isl_int_set_si(bmap->eq[i][0], 1);
1729 isl_seq_clr(bmap->eq[i]+1, total);
1730 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1731 isl_vec_free(bmap->sample);
1732 bmap->sample = NULL;
1733 return isl_basic_map_finalize(bmap);
1734 error:
1735 isl_basic_map_free(bmap);
1736 return NULL;
1739 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1741 return (struct isl_basic_set *)
1742 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1745 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1746 * of "bmap").
1748 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1750 isl_int *t = bmap->div[a];
1751 bmap->div[a] = bmap->div[b];
1752 bmap->div[b] = t;
1755 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1756 * div definitions accordingly.
1758 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1760 int i;
1761 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1763 swap_div(bmap, a, b);
1765 for (i = 0; i < bmap->n_eq; ++i)
1766 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1768 for (i = 0; i < bmap->n_ineq; ++i)
1769 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1771 for (i = 0; i < bmap->n_div; ++i)
1772 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1773 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1776 /* Eliminate the specified n dimensions starting at first from the
1777 * constraints, without removing the dimensions from the space.
1778 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1780 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1781 enum isl_dim_type type, unsigned first, unsigned n)
1783 int i;
1785 if (!map)
1786 return NULL;
1787 if (n == 0)
1788 return map;
1790 if (first + n > isl_map_dim(map, type) || first + n < first)
1791 isl_die(map->ctx, isl_error_invalid,
1792 "index out of bounds", goto error);
1794 map = isl_map_cow(map);
1795 if (!map)
1796 return NULL;
1798 for (i = 0; i < map->n; ++i) {
1799 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1800 if (!map->p[i])
1801 goto error;
1803 return map;
1804 error:
1805 isl_map_free(map);
1806 return NULL;
1809 /* Eliminate the specified n dimensions starting at first from the
1810 * constraints, without removing the dimensions from the space.
1811 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1813 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1814 enum isl_dim_type type, unsigned first, unsigned n)
1816 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1819 /* Eliminate the specified n dimensions starting at first from the
1820 * constraints, without removing the dimensions from the space.
1821 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1823 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1824 unsigned first, unsigned n)
1826 return isl_set_eliminate(set, isl_dim_set, first, n);
1829 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1830 __isl_take isl_basic_map *bmap)
1832 if (!bmap)
1833 return NULL;
1834 bmap = isl_basic_map_eliminate_vars(bmap,
1835 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1836 if (!bmap)
1837 return NULL;
1838 bmap->n_div = 0;
1839 return isl_basic_map_finalize(bmap);
1842 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1843 __isl_take isl_basic_set *bset)
1845 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1846 (struct isl_basic_map *)bset);
1849 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1851 int i;
1853 if (!map)
1854 return NULL;
1855 if (map->n == 0)
1856 return map;
1858 map = isl_map_cow(map);
1859 if (!map)
1860 return NULL;
1862 for (i = 0; i < map->n; ++i) {
1863 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1864 if (!map->p[i])
1865 goto error;
1867 return map;
1868 error:
1869 isl_map_free(map);
1870 return NULL;
1873 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1875 return isl_map_remove_divs(set);
1878 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1879 enum isl_dim_type type, unsigned first, unsigned n)
1881 if (!bmap)
1882 return NULL;
1883 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1884 goto error);
1885 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1886 return bmap;
1887 bmap = isl_basic_map_eliminate_vars(bmap,
1888 isl_basic_map_offset(bmap, type) - 1 + first, n);
1889 if (!bmap)
1890 return bmap;
1891 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1892 return bmap;
1893 bmap = isl_basic_map_drop(bmap, type, first, n);
1894 return bmap;
1895 error:
1896 isl_basic_map_free(bmap);
1897 return NULL;
1900 /* Return true if the definition of the given div (recursively) involves
1901 * any of the given variables.
1903 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1904 unsigned first, unsigned n)
1906 int i;
1907 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1909 if (isl_int_is_zero(bmap->div[div][0]))
1910 return 0;
1911 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1912 return 1;
1914 for (i = bmap->n_div - 1; i >= 0; --i) {
1915 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1916 continue;
1917 if (div_involves_vars(bmap, i, first, n))
1918 return 1;
1921 return 0;
1924 /* Try and add a lower and/or upper bound on "div" to "bmap"
1925 * based on inequality "i".
1926 * "total" is the total number of variables (excluding the divs).
1927 * "v" is a temporary object that can be used during the calculations.
1928 * If "lb" is set, then a lower bound should be constructed.
1929 * If "ub" is set, then an upper bound should be constructed.
1931 * The calling function has already checked that the inequality does not
1932 * reference "div", but we still need to check that the inequality is
1933 * of the right form. We'll consider the case where we want to construct
1934 * a lower bound. The construction of upper bounds is similar.
1936 * Let "div" be of the form
1938 * q = floor((a + f(x))/d)
1940 * We essentially check if constraint "i" is of the form
1942 * b + f(x) >= 0
1944 * so that we can use it to derive a lower bound on "div".
1945 * However, we allow a slightly more general form
1947 * b + g(x) >= 0
1949 * with the condition that the coefficients of g(x) - f(x) are all
1950 * divisible by d.
1951 * Rewriting this constraint as
1953 * 0 >= -b - g(x)
1955 * adding a + f(x) to both sides and dividing by d, we obtain
1957 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1959 * Taking the floor on both sides, we obtain
1961 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1963 * or
1965 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1967 * In the case of an upper bound, we construct the constraint
1969 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1972 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1973 __isl_take isl_basic_map *bmap, int div, int i,
1974 unsigned total, isl_int v, int lb, int ub)
1976 int j;
1978 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1979 if (lb) {
1980 isl_int_sub(v, bmap->ineq[i][1 + j],
1981 bmap->div[div][1 + 1 + j]);
1982 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1984 if (ub) {
1985 isl_int_add(v, bmap->ineq[i][1 + j],
1986 bmap->div[div][1 + 1 + j]);
1987 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1990 if (!lb && !ub)
1991 return bmap;
1993 bmap = isl_basic_map_cow(bmap);
1994 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1995 if (lb) {
1996 int k = isl_basic_map_alloc_inequality(bmap);
1997 if (k < 0)
1998 goto error;
1999 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2000 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2001 bmap->div[div][1 + j]);
2002 isl_int_cdiv_q(bmap->ineq[k][j],
2003 bmap->ineq[k][j], bmap->div[div][0]);
2005 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2007 if (ub) {
2008 int k = isl_basic_map_alloc_inequality(bmap);
2009 if (k < 0)
2010 goto error;
2011 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2012 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2013 bmap->div[div][1 + j]);
2014 isl_int_fdiv_q(bmap->ineq[k][j],
2015 bmap->ineq[k][j], bmap->div[div][0]);
2017 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2020 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2021 return bmap;
2022 error:
2023 isl_basic_map_free(bmap);
2024 return NULL;
2027 /* This function is called right before "div" is eliminated from "bmap"
2028 * using Fourier-Motzkin.
2029 * Look through the constraints of "bmap" for constraints on the argument
2030 * of the integer division and use them to construct constraints on the
2031 * integer division itself. These constraints can then be combined
2032 * during the Fourier-Motzkin elimination.
2033 * Note that it is only useful to introduce lower bounds on "div"
2034 * if "bmap" already contains upper bounds on "div" as the newly
2035 * introduce lower bounds can then be combined with the pre-existing
2036 * upper bounds. Similarly for upper bounds.
2037 * We therefore first check if "bmap" contains any lower and/or upper bounds
2038 * on "div".
2040 * It is interesting to note that the introduction of these constraints
2041 * can indeed lead to more accurate results, even when compared to
2042 * deriving constraints on the argument of "div" from constraints on "div".
2043 * Consider, for example, the set
2045 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2047 * The second constraint can be rewritten as
2049 * 2 * [(-i-2j+3)/4] + k >= 0
2051 * from which we can derive
2053 * -i - 2j + 3 >= -2k
2055 * or
2057 * i + 2j <= 3 + 2k
2059 * Combined with the first constraint, we obtain
2061 * -3 <= 3 + 2k or k >= -3
2063 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2064 * the first constraint, we obtain
2066 * [(i + 2j)/4] >= [-3/4] = -1
2068 * Combining this constraint with the second constraint, we obtain
2070 * k >= -2
2072 static __isl_give isl_basic_map *insert_bounds_on_div(
2073 __isl_take isl_basic_map *bmap, int div)
2075 int i;
2076 int check_lb, check_ub;
2077 isl_int v;
2078 unsigned total;
2080 if (!bmap)
2081 return NULL;
2083 if (isl_int_is_zero(bmap->div[div][0]))
2084 return bmap;
2086 total = isl_space_dim(bmap->dim, isl_dim_all);
2088 check_lb = 0;
2089 check_ub = 0;
2090 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2091 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2092 if (s > 0)
2093 check_ub = 1;
2094 if (s < 0)
2095 check_lb = 1;
2098 if (!check_lb && !check_ub)
2099 return bmap;
2101 isl_int_init(v);
2103 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2104 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2105 continue;
2107 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2108 check_lb, check_ub);
2111 isl_int_clear(v);
2113 return bmap;
2116 /* Remove all divs (recursively) involving any of the given dimensions
2117 * in their definitions.
2119 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2120 __isl_take isl_basic_map *bmap,
2121 enum isl_dim_type type, unsigned first, unsigned n)
2123 int i;
2125 if (!bmap)
2126 return NULL;
2127 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2128 goto error);
2129 first += isl_basic_map_offset(bmap, type);
2131 for (i = bmap->n_div - 1; i >= 0; --i) {
2132 if (!div_involves_vars(bmap, i, first, n))
2133 continue;
2134 bmap = insert_bounds_on_div(bmap, i);
2135 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2136 if (!bmap)
2137 return NULL;
2138 i = bmap->n_div;
2141 return bmap;
2142 error:
2143 isl_basic_map_free(bmap);
2144 return NULL;
2147 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2148 __isl_take isl_basic_set *bset,
2149 enum isl_dim_type type, unsigned first, unsigned n)
2151 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2154 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2155 enum isl_dim_type type, unsigned first, unsigned n)
2157 int i;
2159 if (!map)
2160 return NULL;
2161 if (map->n == 0)
2162 return map;
2164 map = isl_map_cow(map);
2165 if (!map)
2166 return NULL;
2168 for (i = 0; i < map->n; ++i) {
2169 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2170 type, first, n);
2171 if (!map->p[i])
2172 goto error;
2174 return map;
2175 error:
2176 isl_map_free(map);
2177 return NULL;
2180 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2181 enum isl_dim_type type, unsigned first, unsigned n)
2183 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2184 type, first, n);
2187 /* Does the desciption of "bmap" depend on the specified dimensions?
2188 * We also check whether the dimensions appear in any of the div definitions.
2189 * In principle there is no need for this check. If the dimensions appear
2190 * in a div definition, they also appear in the defining constraints of that
2191 * div.
2193 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2194 enum isl_dim_type type, unsigned first, unsigned n)
2196 int i;
2198 if (!bmap)
2199 return -1;
2201 if (first + n > isl_basic_map_dim(bmap, type))
2202 isl_die(bmap->ctx, isl_error_invalid,
2203 "index out of bounds", return -1);
2205 first += isl_basic_map_offset(bmap, type);
2206 for (i = 0; i < bmap->n_eq; ++i)
2207 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2208 return 1;
2209 for (i = 0; i < bmap->n_ineq; ++i)
2210 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2211 return 1;
2212 for (i = 0; i < bmap->n_div; ++i) {
2213 if (isl_int_is_zero(bmap->div[i][0]))
2214 continue;
2215 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2216 return 1;
2219 return 0;
2222 int isl_map_involves_dims(__isl_keep isl_map *map,
2223 enum isl_dim_type type, unsigned first, unsigned n)
2225 int i;
2227 if (!map)
2228 return -1;
2230 if (first + n > isl_map_dim(map, type))
2231 isl_die(map->ctx, isl_error_invalid,
2232 "index out of bounds", return -1);
2234 for (i = 0; i < map->n; ++i) {
2235 int involves = isl_basic_map_involves_dims(map->p[i],
2236 type, first, n);
2237 if (involves < 0 || involves)
2238 return involves;
2241 return 0;
2244 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2245 enum isl_dim_type type, unsigned first, unsigned n)
2247 return isl_basic_map_involves_dims(bset, type, first, n);
2250 int isl_set_involves_dims(__isl_keep isl_set *set,
2251 enum isl_dim_type type, unsigned first, unsigned n)
2253 return isl_map_involves_dims(set, type, first, n);
2256 /* Return true if the definition of the given div is unknown or depends
2257 * on unknown divs.
2259 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2261 int i;
2262 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2264 if (isl_int_is_zero(bmap->div[div][0]))
2265 return 1;
2267 for (i = bmap->n_div - 1; i >= 0; --i) {
2268 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2269 continue;
2270 if (div_is_unknown(bmap, i))
2271 return 1;
2274 return 0;
2277 /* Remove all divs that are unknown or defined in terms of unknown divs.
2279 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2280 __isl_take isl_basic_map *bmap)
2282 int i;
2284 if (!bmap)
2285 return NULL;
2287 for (i = bmap->n_div - 1; i >= 0; --i) {
2288 if (!div_is_unknown(bmap, i))
2289 continue;
2290 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2291 if (!bmap)
2292 return NULL;
2293 i = bmap->n_div;
2296 return bmap;
2299 /* Remove all divs that are unknown or defined in terms of unknown divs.
2301 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2302 __isl_take isl_basic_set *bset)
2304 return isl_basic_map_remove_unknown_divs(bset);
2307 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2309 int i;
2311 if (!map)
2312 return NULL;
2313 if (map->n == 0)
2314 return map;
2316 map = isl_map_cow(map);
2317 if (!map)
2318 return NULL;
2320 for (i = 0; i < map->n; ++i) {
2321 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2322 if (!map->p[i])
2323 goto error;
2325 return map;
2326 error:
2327 isl_map_free(map);
2328 return NULL;
2331 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2333 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2336 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2337 __isl_take isl_basic_set *bset,
2338 enum isl_dim_type type, unsigned first, unsigned n)
2340 return (isl_basic_set *)
2341 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2344 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2345 enum isl_dim_type type, unsigned first, unsigned n)
2347 int i;
2349 if (n == 0)
2350 return map;
2352 map = isl_map_cow(map);
2353 if (!map)
2354 return NULL;
2355 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2357 for (i = 0; i < map->n; ++i) {
2358 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2359 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2360 if (!map->p[i])
2361 goto error;
2363 map = isl_map_drop(map, type, first, n);
2364 return map;
2365 error:
2366 isl_map_free(map);
2367 return NULL;
2370 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2371 enum isl_dim_type type, unsigned first, unsigned n)
2373 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2376 /* Project out n inputs starting at first using Fourier-Motzkin */
2377 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2378 unsigned first, unsigned n)
2380 return isl_map_remove_dims(map, isl_dim_in, first, n);
2383 static void dump_term(struct isl_basic_map *bmap,
2384 isl_int c, int pos, FILE *out)
2386 const char *name;
2387 unsigned in = isl_basic_map_n_in(bmap);
2388 unsigned dim = in + isl_basic_map_n_out(bmap);
2389 unsigned nparam = isl_basic_map_n_param(bmap);
2390 if (!pos)
2391 isl_int_print(out, c, 0);
2392 else {
2393 if (!isl_int_is_one(c))
2394 isl_int_print(out, c, 0);
2395 if (pos < 1 + nparam) {
2396 name = isl_space_get_dim_name(bmap->dim,
2397 isl_dim_param, pos - 1);
2398 if (name)
2399 fprintf(out, "%s", name);
2400 else
2401 fprintf(out, "p%d", pos - 1);
2402 } else if (pos < 1 + nparam + in)
2403 fprintf(out, "i%d", pos - 1 - nparam);
2404 else if (pos < 1 + nparam + dim)
2405 fprintf(out, "o%d", pos - 1 - nparam - in);
2406 else
2407 fprintf(out, "e%d", pos - 1 - nparam - dim);
2411 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2412 int sign, FILE *out)
2414 int i;
2415 int first;
2416 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2417 isl_int v;
2419 isl_int_init(v);
2420 for (i = 0, first = 1; i < len; ++i) {
2421 if (isl_int_sgn(c[i]) * sign <= 0)
2422 continue;
2423 if (!first)
2424 fprintf(out, " + ");
2425 first = 0;
2426 isl_int_abs(v, c[i]);
2427 dump_term(bmap, v, i, out);
2429 isl_int_clear(v);
2430 if (first)
2431 fprintf(out, "0");
2434 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2435 const char *op, FILE *out, int indent)
2437 int i;
2439 fprintf(out, "%*s", indent, "");
2441 dump_constraint_sign(bmap, c, 1, out);
2442 fprintf(out, " %s ", op);
2443 dump_constraint_sign(bmap, c, -1, out);
2445 fprintf(out, "\n");
2447 for (i = bmap->n_div; i < bmap->extra; ++i) {
2448 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2449 continue;
2450 fprintf(out, "%*s", indent, "");
2451 fprintf(out, "ERROR: unused div coefficient not zero\n");
2452 abort();
2456 static void dump_constraints(struct isl_basic_map *bmap,
2457 isl_int **c, unsigned n,
2458 const char *op, FILE *out, int indent)
2460 int i;
2462 for (i = 0; i < n; ++i)
2463 dump_constraint(bmap, c[i], op, out, indent);
2466 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2468 int j;
2469 int first = 1;
2470 unsigned total = isl_basic_map_total_dim(bmap);
2472 for (j = 0; j < 1 + total; ++j) {
2473 if (isl_int_is_zero(exp[j]))
2474 continue;
2475 if (!first && isl_int_is_pos(exp[j]))
2476 fprintf(out, "+");
2477 dump_term(bmap, exp[j], j, out);
2478 first = 0;
2482 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2484 int i;
2486 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2487 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2489 for (i = 0; i < bmap->n_div; ++i) {
2490 fprintf(out, "%*s", indent, "");
2491 fprintf(out, "e%d = [(", i);
2492 dump_affine(bmap, bmap->div[i]+1, out);
2493 fprintf(out, ")/");
2494 isl_int_print(out, bmap->div[i][0], 0);
2495 fprintf(out, "]\n");
2499 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2500 FILE *out, int indent)
2502 if (!bset) {
2503 fprintf(out, "null basic set\n");
2504 return;
2507 fprintf(out, "%*s", indent, "");
2508 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2509 bset->ref, bset->dim->nparam, bset->dim->n_out,
2510 bset->extra, bset->flags);
2511 dump((struct isl_basic_map *)bset, out, indent);
2514 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2515 FILE *out, int indent)
2517 if (!bmap) {
2518 fprintf(out, "null basic map\n");
2519 return;
2522 fprintf(out, "%*s", indent, "");
2523 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2524 "flags: %x, n_name: %d\n",
2525 bmap->ref,
2526 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2527 bmap->extra, bmap->flags, bmap->dim->n_id);
2528 dump(bmap, out, indent);
2531 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2533 unsigned total;
2534 if (!bmap)
2535 return -1;
2536 total = isl_basic_map_total_dim(bmap);
2537 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2538 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2539 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2540 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2541 return 0;
2544 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2545 unsigned flags)
2547 struct isl_set *set;
2549 if (!dim)
2550 return NULL;
2551 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2552 isl_assert(dim->ctx, n >= 0, goto error);
2553 set = isl_alloc(dim->ctx, struct isl_set,
2554 sizeof(struct isl_set) +
2555 (n - 1) * sizeof(struct isl_basic_set *));
2556 if (!set)
2557 goto error;
2559 set->ctx = dim->ctx;
2560 isl_ctx_ref(set->ctx);
2561 set->ref = 1;
2562 set->size = n;
2563 set->n = 0;
2564 set->dim = dim;
2565 set->flags = flags;
2566 return set;
2567 error:
2568 isl_space_free(dim);
2569 return NULL;
2572 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2573 unsigned nparam, unsigned dim, int n, unsigned flags)
2575 struct isl_set *set;
2576 isl_space *dims;
2578 dims = isl_space_alloc(ctx, nparam, 0, dim);
2579 if (!dims)
2580 return NULL;
2582 set = isl_set_alloc_space(dims, n, flags);
2583 return set;
2586 /* Make sure "map" has room for at least "n" more basic maps.
2588 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2590 int i;
2591 struct isl_map *grown = NULL;
2593 if (!map)
2594 return NULL;
2595 isl_assert(map->ctx, n >= 0, goto error);
2596 if (map->n + n <= map->size)
2597 return map;
2598 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2599 if (!grown)
2600 goto error;
2601 for (i = 0; i < map->n; ++i) {
2602 grown->p[i] = isl_basic_map_copy(map->p[i]);
2603 if (!grown->p[i])
2604 goto error;
2605 grown->n++;
2607 isl_map_free(map);
2608 return grown;
2609 error:
2610 isl_map_free(grown);
2611 isl_map_free(map);
2612 return NULL;
2615 /* Make sure "set" has room for at least "n" more basic sets.
2617 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2619 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2622 struct isl_set *isl_set_dup(struct isl_set *set)
2624 int i;
2625 struct isl_set *dup;
2627 if (!set)
2628 return NULL;
2630 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2631 if (!dup)
2632 return NULL;
2633 for (i = 0; i < set->n; ++i)
2634 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2635 return dup;
2638 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2640 return isl_map_from_basic_map(bset);
2643 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2645 struct isl_map *map;
2647 if (!bmap)
2648 return NULL;
2650 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2651 return isl_map_add_basic_map(map, bmap);
2654 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2655 __isl_take isl_basic_set *bset)
2657 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2658 (struct isl_basic_map *)bset);
2661 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2663 int i;
2665 if (!set)
2666 return NULL;
2668 if (--set->ref > 0)
2669 return NULL;
2671 isl_ctx_deref(set->ctx);
2672 for (i = 0; i < set->n; ++i)
2673 isl_basic_set_free(set->p[i]);
2674 isl_space_free(set->dim);
2675 free(set);
2677 return NULL;
2680 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2682 int i;
2684 if (!set) {
2685 fprintf(out, "null set\n");
2686 return;
2689 fprintf(out, "%*s", indent, "");
2690 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2691 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2692 set->flags);
2693 for (i = 0; i < set->n; ++i) {
2694 fprintf(out, "%*s", indent, "");
2695 fprintf(out, "basic set %d:\n", i);
2696 isl_basic_set_print_internal(set->p[i], out, indent+4);
2700 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2702 int i;
2704 if (!map) {
2705 fprintf(out, "null map\n");
2706 return;
2709 fprintf(out, "%*s", indent, "");
2710 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2711 "flags: %x, n_name: %d\n",
2712 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2713 map->dim->n_out, map->flags, map->dim->n_id);
2714 for (i = 0; i < map->n; ++i) {
2715 fprintf(out, "%*s", indent, "");
2716 fprintf(out, "basic map %d:\n", i);
2717 isl_basic_map_print_internal(map->p[i], out, indent+4);
2721 struct isl_basic_map *isl_basic_map_intersect_domain(
2722 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2724 struct isl_basic_map *bmap_domain;
2726 if (!bmap || !bset)
2727 goto error;
2729 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2730 bset->dim, isl_dim_param), goto error);
2732 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2733 isl_assert(bset->ctx,
2734 isl_basic_map_compatible_domain(bmap, bset), goto error);
2736 bmap = isl_basic_map_cow(bmap);
2737 if (!bmap)
2738 goto error;
2739 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2740 bset->n_div, bset->n_eq, bset->n_ineq);
2741 bmap_domain = isl_basic_map_from_domain(bset);
2742 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2744 bmap = isl_basic_map_simplify(bmap);
2745 return isl_basic_map_finalize(bmap);
2746 error:
2747 isl_basic_map_free(bmap);
2748 isl_basic_set_free(bset);
2749 return NULL;
2752 struct isl_basic_map *isl_basic_map_intersect_range(
2753 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2755 struct isl_basic_map *bmap_range;
2757 if (!bmap || !bset)
2758 goto error;
2760 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2761 bset->dim, isl_dim_param), goto error);
2763 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2764 isl_assert(bset->ctx,
2765 isl_basic_map_compatible_range(bmap, bset), goto error);
2767 if (isl_basic_set_is_universe(bset)) {
2768 isl_basic_set_free(bset);
2769 return bmap;
2772 bmap = isl_basic_map_cow(bmap);
2773 if (!bmap)
2774 goto error;
2775 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2776 bset->n_div, bset->n_eq, bset->n_ineq);
2777 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2778 bmap = add_constraints(bmap, bmap_range, 0, 0);
2780 bmap = isl_basic_map_simplify(bmap);
2781 return isl_basic_map_finalize(bmap);
2782 error:
2783 isl_basic_map_free(bmap);
2784 isl_basic_set_free(bset);
2785 return NULL;
2788 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2790 int i;
2791 unsigned total;
2792 isl_int s;
2794 if (!bmap || !vec)
2795 return -1;
2797 total = 1 + isl_basic_map_total_dim(bmap);
2798 if (total != vec->size)
2799 return -1;
2801 isl_int_init(s);
2803 for (i = 0; i < bmap->n_eq; ++i) {
2804 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2805 if (!isl_int_is_zero(s)) {
2806 isl_int_clear(s);
2807 return 0;
2811 for (i = 0; i < bmap->n_ineq; ++i) {
2812 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2813 if (isl_int_is_neg(s)) {
2814 isl_int_clear(s);
2815 return 0;
2819 isl_int_clear(s);
2821 return 1;
2824 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2826 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2829 struct isl_basic_map *isl_basic_map_intersect(
2830 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2832 struct isl_vec *sample = NULL;
2834 if (!bmap1 || !bmap2)
2835 goto error;
2837 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2838 bmap2->dim, isl_dim_param), goto error);
2839 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2840 isl_space_dim(bmap1->dim, isl_dim_param) &&
2841 isl_space_dim(bmap2->dim, isl_dim_all) !=
2842 isl_space_dim(bmap2->dim, isl_dim_param))
2843 return isl_basic_map_intersect(bmap2, bmap1);
2845 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2846 isl_space_dim(bmap2->dim, isl_dim_param))
2847 isl_assert(bmap1->ctx,
2848 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2850 if (bmap1->sample &&
2851 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2852 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2853 sample = isl_vec_copy(bmap1->sample);
2854 else if (bmap2->sample &&
2855 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2856 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2857 sample = isl_vec_copy(bmap2->sample);
2859 bmap1 = isl_basic_map_cow(bmap1);
2860 if (!bmap1)
2861 goto error;
2862 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2863 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2864 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2866 if (!bmap1)
2867 isl_vec_free(sample);
2868 else if (sample) {
2869 isl_vec_free(bmap1->sample);
2870 bmap1->sample = sample;
2873 bmap1 = isl_basic_map_simplify(bmap1);
2874 return isl_basic_map_finalize(bmap1);
2875 error:
2876 if (sample)
2877 isl_vec_free(sample);
2878 isl_basic_map_free(bmap1);
2879 isl_basic_map_free(bmap2);
2880 return NULL;
2883 struct isl_basic_set *isl_basic_set_intersect(
2884 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2886 return (struct isl_basic_set *)
2887 isl_basic_map_intersect(
2888 (struct isl_basic_map *)bset1,
2889 (struct isl_basic_map *)bset2);
2892 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2893 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2895 return isl_basic_set_intersect(bset1, bset2);
2898 /* Special case of isl_map_intersect, where both map1 and map2
2899 * are convex, without any divs and such that either map1 or map2
2900 * contains a single constraint. This constraint is then simply
2901 * added to the other map.
2903 static __isl_give isl_map *map_intersect_add_constraint(
2904 __isl_take isl_map *map1, __isl_take isl_map *map2)
2906 isl_assert(map1->ctx, map1->n == 1, goto error);
2907 isl_assert(map2->ctx, map1->n == 1, goto error);
2908 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2909 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2911 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2912 return isl_map_intersect(map2, map1);
2914 isl_assert(map2->ctx,
2915 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2917 map1 = isl_map_cow(map1);
2918 if (!map1)
2919 goto error;
2920 if (isl_map_plain_is_empty(map1)) {
2921 isl_map_free(map2);
2922 return map1;
2924 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2925 if (map2->p[0]->n_eq == 1)
2926 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2927 else
2928 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2929 map2->p[0]->ineq[0]);
2931 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2932 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2933 if (!map1->p[0])
2934 goto error;
2936 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2937 isl_basic_map_free(map1->p[0]);
2938 map1->n = 0;
2941 isl_map_free(map2);
2943 return map1;
2944 error:
2945 isl_map_free(map1);
2946 isl_map_free(map2);
2947 return NULL;
2950 /* map2 may be either a parameter domain or a map living in the same
2951 * space as map1.
2953 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2954 __isl_take isl_map *map2)
2956 unsigned flags = 0;
2957 isl_map *result;
2958 int i, j;
2960 if (!map1 || !map2)
2961 goto error;
2963 if ((isl_map_plain_is_empty(map1) ||
2964 isl_map_plain_is_universe(map2)) &&
2965 isl_space_is_equal(map1->dim, map2->dim)) {
2966 isl_map_free(map2);
2967 return map1;
2969 if ((isl_map_plain_is_empty(map2) ||
2970 isl_map_plain_is_universe(map1)) &&
2971 isl_space_is_equal(map1->dim, map2->dim)) {
2972 isl_map_free(map1);
2973 return map2;
2976 if (map1->n == 1 && map2->n == 1 &&
2977 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2978 isl_space_is_equal(map1->dim, map2->dim) &&
2979 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2980 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2981 return map_intersect_add_constraint(map1, map2);
2983 if (isl_space_dim(map2->dim, isl_dim_all) !=
2984 isl_space_dim(map2->dim, isl_dim_param))
2985 isl_assert(map1->ctx,
2986 isl_space_is_equal(map1->dim, map2->dim), goto error);
2988 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2989 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2990 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2992 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2993 map1->n * map2->n, flags);
2994 if (!result)
2995 goto error;
2996 for (i = 0; i < map1->n; ++i)
2997 for (j = 0; j < map2->n; ++j) {
2998 struct isl_basic_map *part;
2999 part = isl_basic_map_intersect(
3000 isl_basic_map_copy(map1->p[i]),
3001 isl_basic_map_copy(map2->p[j]));
3002 if (isl_basic_map_is_empty(part) < 0)
3003 part = isl_basic_map_free(part);
3004 result = isl_map_add_basic_map(result, part);
3005 if (!result)
3006 goto error;
3008 isl_map_free(map1);
3009 isl_map_free(map2);
3010 return result;
3011 error:
3012 isl_map_free(map1);
3013 isl_map_free(map2);
3014 return NULL;
3017 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3018 __isl_take isl_map *map2)
3020 if (!map1 || !map2)
3021 goto error;
3022 if (!isl_space_is_equal(map1->dim, map2->dim))
3023 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3024 "spaces don't match", goto error);
3025 return map_intersect_internal(map1, map2);
3026 error:
3027 isl_map_free(map1);
3028 isl_map_free(map2);
3029 return NULL;
3032 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3033 __isl_take isl_map *map2)
3035 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3038 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3040 return (struct isl_set *)
3041 isl_map_intersect((struct isl_map *)set1,
3042 (struct isl_map *)set2);
3045 /* map_intersect_internal accepts intersections
3046 * with parameter domains, so we can just call that function.
3048 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3049 __isl_take isl_set *params)
3051 return map_intersect_internal(map, params);
3054 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3055 __isl_take isl_map *map2)
3057 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3060 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3061 __isl_take isl_set *params)
3063 return isl_map_intersect_params(set, params);
3066 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3068 isl_space *space;
3069 unsigned pos, n1, n2;
3071 if (!bmap)
3072 return NULL;
3073 bmap = isl_basic_map_cow(bmap);
3074 if (!bmap)
3075 return NULL;
3076 space = isl_space_reverse(isl_space_copy(bmap->dim));
3077 pos = isl_basic_map_offset(bmap, isl_dim_in);
3078 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3079 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3080 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3081 return isl_basic_map_reset_space(bmap, space);
3084 static __isl_give isl_basic_map *basic_map_space_reset(
3085 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3087 isl_space *space;
3089 if (!bmap)
3090 return NULL;
3091 if (!isl_space_is_named_or_nested(bmap->dim, type))
3092 return bmap;
3094 space = isl_basic_map_get_space(bmap);
3095 space = isl_space_reset(space, type);
3096 bmap = isl_basic_map_reset_space(bmap, space);
3097 return bmap;
3100 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3101 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3102 unsigned pos, unsigned n)
3104 isl_space *res_dim;
3105 struct isl_basic_map *res;
3106 struct isl_dim_map *dim_map;
3107 unsigned total, off;
3108 enum isl_dim_type t;
3110 if (n == 0)
3111 return basic_map_space_reset(bmap, type);
3113 if (!bmap)
3114 return NULL;
3116 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3118 total = isl_basic_map_total_dim(bmap) + n;
3119 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3120 off = 0;
3121 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3122 if (t != type) {
3123 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3124 } else {
3125 unsigned size = isl_basic_map_dim(bmap, t);
3126 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3127 0, pos, off);
3128 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3129 pos, size - pos, off + pos + n);
3131 off += isl_space_dim(res_dim, t);
3133 isl_dim_map_div(dim_map, bmap, off);
3135 res = isl_basic_map_alloc_space(res_dim,
3136 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3137 if (isl_basic_map_is_rational(bmap))
3138 res = isl_basic_map_set_rational(res);
3139 if (isl_basic_map_plain_is_empty(bmap)) {
3140 isl_basic_map_free(bmap);
3141 free(dim_map);
3142 return isl_basic_map_set_to_empty(res);
3144 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3145 return isl_basic_map_finalize(res);
3148 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3149 __isl_take isl_basic_set *bset,
3150 enum isl_dim_type type, unsigned pos, unsigned n)
3152 return isl_basic_map_insert_dims(bset, type, pos, n);
3155 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3156 enum isl_dim_type type, unsigned n)
3158 if (!bmap)
3159 return NULL;
3160 return isl_basic_map_insert_dims(bmap, type,
3161 isl_basic_map_dim(bmap, type), n);
3164 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3165 enum isl_dim_type type, unsigned n)
3167 if (!bset)
3168 return NULL;
3169 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3170 return isl_basic_map_add_dims(bset, type, n);
3171 error:
3172 isl_basic_set_free(bset);
3173 return NULL;
3176 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3177 enum isl_dim_type type)
3179 isl_space *space;
3181 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3182 return map;
3184 space = isl_map_get_space(map);
3185 space = isl_space_reset(space, type);
3186 map = isl_map_reset_space(map, space);
3187 return map;
3190 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3191 enum isl_dim_type type, unsigned pos, unsigned n)
3193 int i;
3195 if (n == 0)
3196 return map_space_reset(map, type);
3198 map = isl_map_cow(map);
3199 if (!map)
3200 return NULL;
3202 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3203 if (!map->dim)
3204 goto error;
3206 for (i = 0; i < map->n; ++i) {
3207 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3208 if (!map->p[i])
3209 goto error;
3212 return map;
3213 error:
3214 isl_map_free(map);
3215 return NULL;
3218 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3219 enum isl_dim_type type, unsigned pos, unsigned n)
3221 return isl_map_insert_dims(set, type, pos, n);
3224 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3225 enum isl_dim_type type, unsigned n)
3227 if (!map)
3228 return NULL;
3229 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3232 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3233 enum isl_dim_type type, unsigned n)
3235 if (!set)
3236 return NULL;
3237 isl_assert(set->ctx, type != isl_dim_in, goto error);
3238 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3239 error:
3240 isl_set_free(set);
3241 return NULL;
3244 __isl_give isl_basic_map *isl_basic_map_move_dims(
3245 __isl_take isl_basic_map *bmap,
3246 enum isl_dim_type dst_type, unsigned dst_pos,
3247 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3249 struct isl_dim_map *dim_map;
3250 struct isl_basic_map *res;
3251 enum isl_dim_type t;
3252 unsigned total, off;
3254 if (!bmap)
3255 return NULL;
3256 if (n == 0)
3257 return bmap;
3259 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3260 goto error);
3262 if (dst_type == src_type && dst_pos == src_pos)
3263 return bmap;
3265 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3267 if (pos(bmap->dim, dst_type) + dst_pos ==
3268 pos(bmap->dim, src_type) + src_pos +
3269 ((src_type < dst_type) ? n : 0)) {
3270 bmap = isl_basic_map_cow(bmap);
3271 if (!bmap)
3272 return NULL;
3274 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3275 src_type, src_pos, n);
3276 if (!bmap->dim)
3277 goto error;
3279 bmap = isl_basic_map_finalize(bmap);
3281 return bmap;
3284 total = isl_basic_map_total_dim(bmap);
3285 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3287 off = 0;
3288 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3289 unsigned size = isl_space_dim(bmap->dim, t);
3290 if (t == dst_type) {
3291 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3292 0, dst_pos, off);
3293 off += dst_pos;
3294 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3295 src_pos, n, off);
3296 off += n;
3297 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3298 dst_pos, size - dst_pos, off);
3299 off += size - dst_pos;
3300 } else if (t == src_type) {
3301 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3302 0, src_pos, off);
3303 off += src_pos;
3304 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3305 src_pos + n, size - src_pos - n, off);
3306 off += size - src_pos - n;
3307 } else {
3308 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3309 off += size;
3312 isl_dim_map_div(dim_map, bmap, off);
3314 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3315 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3316 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3317 if (!bmap)
3318 goto error;
3320 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3321 src_type, src_pos, n);
3322 if (!bmap->dim)
3323 goto error;
3325 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3326 bmap = isl_basic_map_gauss(bmap, NULL);
3327 bmap = isl_basic_map_finalize(bmap);
3329 return bmap;
3330 error:
3331 isl_basic_map_free(bmap);
3332 return NULL;
3335 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3336 enum isl_dim_type dst_type, unsigned dst_pos,
3337 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3339 return (isl_basic_set *)isl_basic_map_move_dims(
3340 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3343 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3344 enum isl_dim_type dst_type, unsigned dst_pos,
3345 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3347 if (!set)
3348 return NULL;
3349 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3350 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3351 src_type, src_pos, n);
3352 error:
3353 isl_set_free(set);
3354 return NULL;
3357 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3358 enum isl_dim_type dst_type, unsigned dst_pos,
3359 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3361 int i;
3363 if (!map)
3364 return NULL;
3365 if (n == 0)
3366 return map;
3368 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3369 goto error);
3371 if (dst_type == src_type && dst_pos == src_pos)
3372 return map;
3374 isl_assert(map->ctx, dst_type != src_type, goto error);
3376 map = isl_map_cow(map);
3377 if (!map)
3378 return NULL;
3380 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3381 if (!map->dim)
3382 goto error;
3384 for (i = 0; i < map->n; ++i) {
3385 map->p[i] = isl_basic_map_move_dims(map->p[i],
3386 dst_type, dst_pos,
3387 src_type, src_pos, n);
3388 if (!map->p[i])
3389 goto error;
3392 return map;
3393 error:
3394 isl_map_free(map);
3395 return NULL;
3398 /* Move the specified dimensions to the last columns right before
3399 * the divs. Don't change the dimension specification of bmap.
3400 * That's the responsibility of the caller.
3402 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3403 enum isl_dim_type type, unsigned first, unsigned n)
3405 struct isl_dim_map *dim_map;
3406 struct isl_basic_map *res;
3407 enum isl_dim_type t;
3408 unsigned total, off;
3410 if (!bmap)
3411 return NULL;
3412 if (pos(bmap->dim, type) + first + n ==
3413 1 + isl_space_dim(bmap->dim, isl_dim_all))
3414 return bmap;
3416 total = isl_basic_map_total_dim(bmap);
3417 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3419 off = 0;
3420 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3421 unsigned size = isl_space_dim(bmap->dim, t);
3422 if (t == type) {
3423 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3424 0, first, off);
3425 off += first;
3426 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3427 first, n, total - bmap->n_div - n);
3428 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3429 first + n, size - (first + n), off);
3430 off += size - (first + n);
3431 } else {
3432 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3433 off += size;
3436 isl_dim_map_div(dim_map, bmap, off + n);
3438 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3439 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3440 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3441 return res;
3444 /* Insert "n" rows in the divs of "bmap".
3446 * The number of columns is not changed, which means that the last
3447 * dimensions of "bmap" are being reintepreted as the new divs.
3448 * The space of "bmap" is not adjusted, however, which means
3449 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3450 * from the space of "bmap" is the responsibility of the caller.
3452 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3453 int n)
3455 int i;
3456 size_t row_size;
3457 isl_int **new_div;
3458 isl_int *old;
3460 bmap = isl_basic_map_cow(bmap);
3461 if (!bmap)
3462 return NULL;
3464 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3465 old = bmap->block2.data;
3466 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3467 (bmap->extra + n) * (1 + row_size));
3468 if (!bmap->block2.data)
3469 return isl_basic_map_free(bmap);
3470 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3471 if (!new_div)
3472 return isl_basic_map_free(bmap);
3473 for (i = 0; i < n; ++i) {
3474 new_div[i] = bmap->block2.data +
3475 (bmap->extra + i) * (1 + row_size);
3476 isl_seq_clr(new_div[i], 1 + row_size);
3478 for (i = 0; i < bmap->extra; ++i)
3479 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3480 free(bmap->div);
3481 bmap->div = new_div;
3482 bmap->n_div += n;
3483 bmap->extra += n;
3485 return bmap;
3488 /* Turn the n dimensions of type type, starting at first
3489 * into existentially quantified variables.
3491 __isl_give isl_basic_map *isl_basic_map_project_out(
3492 __isl_take isl_basic_map *bmap,
3493 enum isl_dim_type type, unsigned first, unsigned n)
3495 if (n == 0)
3496 return basic_map_space_reset(bmap, type);
3498 if (!bmap)
3499 return NULL;
3501 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3502 return isl_basic_map_remove_dims(bmap, type, first, n);
3504 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3505 goto error);
3507 bmap = move_last(bmap, type, first, n);
3508 bmap = isl_basic_map_cow(bmap);
3509 bmap = insert_div_rows(bmap, n);
3510 if (!bmap)
3511 return NULL;
3513 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3514 if (!bmap->dim)
3515 goto error;
3516 bmap = isl_basic_map_simplify(bmap);
3517 bmap = isl_basic_map_drop_redundant_divs(bmap);
3518 return isl_basic_map_finalize(bmap);
3519 error:
3520 isl_basic_map_free(bmap);
3521 return NULL;
3524 /* Turn the n dimensions of type type, starting at first
3525 * into existentially quantified variables.
3527 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3528 enum isl_dim_type type, unsigned first, unsigned n)
3530 return (isl_basic_set *)isl_basic_map_project_out(
3531 (isl_basic_map *)bset, type, first, n);
3534 /* Turn the n dimensions of type type, starting at first
3535 * into existentially quantified variables.
3537 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3538 enum isl_dim_type type, unsigned first, unsigned n)
3540 int i;
3542 if (!map)
3543 return NULL;
3545 if (n == 0)
3546 return map_space_reset(map, type);
3548 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3550 map = isl_map_cow(map);
3551 if (!map)
3552 return NULL;
3554 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3555 if (!map->dim)
3556 goto error;
3558 for (i = 0; i < map->n; ++i) {
3559 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3560 if (!map->p[i])
3561 goto error;
3564 return map;
3565 error:
3566 isl_map_free(map);
3567 return NULL;
3570 /* Turn the n dimensions of type type, starting at first
3571 * into existentially quantified variables.
3573 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3574 enum isl_dim_type type, unsigned first, unsigned n)
3576 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3579 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3581 int i, j;
3583 for (i = 0; i < n; ++i) {
3584 j = isl_basic_map_alloc_div(bmap);
3585 if (j < 0)
3586 goto error;
3587 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3589 return bmap;
3590 error:
3591 isl_basic_map_free(bmap);
3592 return NULL;
3595 struct isl_basic_map *isl_basic_map_apply_range(
3596 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3598 isl_space *dim_result = NULL;
3599 struct isl_basic_map *bmap;
3600 unsigned n_in, n_out, n, nparam, total, pos;
3601 struct isl_dim_map *dim_map1, *dim_map2;
3603 if (!bmap1 || !bmap2)
3604 goto error;
3605 if (!isl_space_match(bmap1->dim, isl_dim_param,
3606 bmap2->dim, isl_dim_param))
3607 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3608 "parameters don't match", goto error);
3609 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3610 bmap2->dim, isl_dim_in))
3611 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3612 "spaces don't match", goto error);
3614 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3615 isl_space_copy(bmap2->dim));
3617 n_in = isl_basic_map_n_in(bmap1);
3618 n_out = isl_basic_map_n_out(bmap2);
3619 n = isl_basic_map_n_out(bmap1);
3620 nparam = isl_basic_map_n_param(bmap1);
3622 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3623 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3624 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3625 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3626 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3627 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3628 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3629 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3630 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3631 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3632 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3634 bmap = isl_basic_map_alloc_space(dim_result,
3635 bmap1->n_div + bmap2->n_div + n,
3636 bmap1->n_eq + bmap2->n_eq,
3637 bmap1->n_ineq + bmap2->n_ineq);
3638 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3639 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3640 bmap = add_divs(bmap, n);
3641 bmap = isl_basic_map_simplify(bmap);
3642 bmap = isl_basic_map_drop_redundant_divs(bmap);
3643 return isl_basic_map_finalize(bmap);
3644 error:
3645 isl_basic_map_free(bmap1);
3646 isl_basic_map_free(bmap2);
3647 return NULL;
3650 struct isl_basic_set *isl_basic_set_apply(
3651 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3653 if (!bset || !bmap)
3654 goto error;
3656 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3657 goto error);
3659 return (struct isl_basic_set *)
3660 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3661 error:
3662 isl_basic_set_free(bset);
3663 isl_basic_map_free(bmap);
3664 return NULL;
3667 struct isl_basic_map *isl_basic_map_apply_domain(
3668 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3670 if (!bmap1 || !bmap2)
3671 goto error;
3673 isl_assert(bmap1->ctx,
3674 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3675 isl_assert(bmap1->ctx,
3676 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3677 goto error);
3679 bmap1 = isl_basic_map_reverse(bmap1);
3680 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3681 return isl_basic_map_reverse(bmap1);
3682 error:
3683 isl_basic_map_free(bmap1);
3684 isl_basic_map_free(bmap2);
3685 return NULL;
3688 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3689 * A \cap B -> f(A) + f(B)
3691 struct isl_basic_map *isl_basic_map_sum(
3692 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3694 unsigned n_in, n_out, nparam, total, pos;
3695 struct isl_basic_map *bmap = NULL;
3696 struct isl_dim_map *dim_map1, *dim_map2;
3697 int i;
3699 if (!bmap1 || !bmap2)
3700 goto error;
3702 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3703 goto error);
3705 nparam = isl_basic_map_n_param(bmap1);
3706 n_in = isl_basic_map_n_in(bmap1);
3707 n_out = isl_basic_map_n_out(bmap1);
3709 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3710 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3711 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3712 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3713 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3714 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3715 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3716 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3717 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3718 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3719 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3721 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3722 bmap1->n_div + bmap2->n_div + 2 * n_out,
3723 bmap1->n_eq + bmap2->n_eq + n_out,
3724 bmap1->n_ineq + bmap2->n_ineq);
3725 for (i = 0; i < n_out; ++i) {
3726 int j = isl_basic_map_alloc_equality(bmap);
3727 if (j < 0)
3728 goto error;
3729 isl_seq_clr(bmap->eq[j], 1+total);
3730 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3731 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3732 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3734 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3735 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3736 bmap = add_divs(bmap, 2 * n_out);
3738 bmap = isl_basic_map_simplify(bmap);
3739 return isl_basic_map_finalize(bmap);
3740 error:
3741 isl_basic_map_free(bmap);
3742 isl_basic_map_free(bmap1);
3743 isl_basic_map_free(bmap2);
3744 return NULL;
3747 /* Given two maps A -> f(A) and B -> g(B), construct a map
3748 * A \cap B -> f(A) + f(B)
3750 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3752 struct isl_map *result;
3753 int i, j;
3755 if (!map1 || !map2)
3756 goto error;
3758 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3760 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3761 map1->n * map2->n, 0);
3762 if (!result)
3763 goto error;
3764 for (i = 0; i < map1->n; ++i)
3765 for (j = 0; j < map2->n; ++j) {
3766 struct isl_basic_map *part;
3767 part = isl_basic_map_sum(
3768 isl_basic_map_copy(map1->p[i]),
3769 isl_basic_map_copy(map2->p[j]));
3770 if (isl_basic_map_is_empty(part))
3771 isl_basic_map_free(part);
3772 else
3773 result = isl_map_add_basic_map(result, part);
3774 if (!result)
3775 goto error;
3777 isl_map_free(map1);
3778 isl_map_free(map2);
3779 return result;
3780 error:
3781 isl_map_free(map1);
3782 isl_map_free(map2);
3783 return NULL;
3786 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3787 __isl_take isl_set *set2)
3789 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3792 /* Given a basic map A -> f(A), construct A -> -f(A).
3794 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3796 int i, j;
3797 unsigned off, n;
3799 bmap = isl_basic_map_cow(bmap);
3800 if (!bmap)
3801 return NULL;
3803 n = isl_basic_map_dim(bmap, isl_dim_out);
3804 off = isl_basic_map_offset(bmap, isl_dim_out);
3805 for (i = 0; i < bmap->n_eq; ++i)
3806 for (j = 0; j < n; ++j)
3807 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3808 for (i = 0; i < bmap->n_ineq; ++i)
3809 for (j = 0; j < n; ++j)
3810 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3811 for (i = 0; i < bmap->n_div; ++i)
3812 for (j = 0; j < n; ++j)
3813 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3814 bmap = isl_basic_map_gauss(bmap, NULL);
3815 return isl_basic_map_finalize(bmap);
3818 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3820 return isl_basic_map_neg(bset);
3823 /* Given a map A -> f(A), construct A -> -f(A).
3825 struct isl_map *isl_map_neg(struct isl_map *map)
3827 int i;
3829 map = isl_map_cow(map);
3830 if (!map)
3831 return NULL;
3833 for (i = 0; i < map->n; ++i) {
3834 map->p[i] = isl_basic_map_neg(map->p[i]);
3835 if (!map->p[i])
3836 goto error;
3839 return map;
3840 error:
3841 isl_map_free(map);
3842 return NULL;
3845 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3847 return (isl_set *)isl_map_neg((isl_map *)set);
3850 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3851 * A -> floor(f(A)/d).
3853 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3854 isl_int d)
3856 unsigned n_in, n_out, nparam, total, pos;
3857 struct isl_basic_map *result = NULL;
3858 struct isl_dim_map *dim_map;
3859 int i;
3861 if (!bmap)
3862 return NULL;
3864 nparam = isl_basic_map_n_param(bmap);
3865 n_in = isl_basic_map_n_in(bmap);
3866 n_out = isl_basic_map_n_out(bmap);
3868 total = nparam + n_in + n_out + bmap->n_div + n_out;
3869 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3870 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3871 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3872 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3873 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3875 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3876 bmap->n_div + n_out,
3877 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3878 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3879 result = add_divs(result, n_out);
3880 for (i = 0; i < n_out; ++i) {
3881 int j;
3882 j = isl_basic_map_alloc_inequality(result);
3883 if (j < 0)
3884 goto error;
3885 isl_seq_clr(result->ineq[j], 1+total);
3886 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3887 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3888 j = isl_basic_map_alloc_inequality(result);
3889 if (j < 0)
3890 goto error;
3891 isl_seq_clr(result->ineq[j], 1+total);
3892 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3893 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3894 isl_int_sub_ui(result->ineq[j][0], d, 1);
3897 result = isl_basic_map_simplify(result);
3898 return isl_basic_map_finalize(result);
3899 error:
3900 isl_basic_map_free(result);
3901 return NULL;
3904 /* Given a map A -> f(A) and an integer d, construct a map
3905 * A -> floor(f(A)/d).
3907 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3909 int i;
3911 map = isl_map_cow(map);
3912 if (!map)
3913 return NULL;
3915 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3916 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3917 for (i = 0; i < map->n; ++i) {
3918 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3919 if (!map->p[i])
3920 goto error;
3923 return map;
3924 error:
3925 isl_map_free(map);
3926 return NULL;
3929 /* Given a map A -> f(A) and an integer d, construct a map
3930 * A -> floor(f(A)/d).
3932 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3933 __isl_take isl_val *d)
3935 if (!map || !d)
3936 goto error;
3937 if (!isl_val_is_int(d))
3938 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3939 "expecting integer denominator", goto error);
3940 map = isl_map_floordiv(map, d->n);
3941 isl_val_free(d);
3942 return map;
3943 error:
3944 isl_map_free(map);
3945 isl_val_free(d);
3946 return NULL;
3949 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3951 int i;
3952 unsigned nparam;
3953 unsigned n_in;
3955 i = isl_basic_map_alloc_equality(bmap);
3956 if (i < 0)
3957 goto error;
3958 nparam = isl_basic_map_n_param(bmap);
3959 n_in = isl_basic_map_n_in(bmap);
3960 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3961 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3962 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3963 return isl_basic_map_finalize(bmap);
3964 error:
3965 isl_basic_map_free(bmap);
3966 return NULL;
3969 /* Add a constraints to "bmap" expressing i_pos < o_pos
3971 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3973 int i;
3974 unsigned nparam;
3975 unsigned n_in;
3977 i = isl_basic_map_alloc_inequality(bmap);
3978 if (i < 0)
3979 goto error;
3980 nparam = isl_basic_map_n_param(bmap);
3981 n_in = isl_basic_map_n_in(bmap);
3982 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3983 isl_int_set_si(bmap->ineq[i][0], -1);
3984 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3985 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3986 return isl_basic_map_finalize(bmap);
3987 error:
3988 isl_basic_map_free(bmap);
3989 return NULL;
3992 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3994 static __isl_give isl_basic_map *var_less_or_equal(
3995 __isl_take isl_basic_map *bmap, unsigned pos)
3997 int i;
3998 unsigned nparam;
3999 unsigned n_in;
4001 i = isl_basic_map_alloc_inequality(bmap);
4002 if (i < 0)
4003 goto error;
4004 nparam = isl_basic_map_n_param(bmap);
4005 n_in = isl_basic_map_n_in(bmap);
4006 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4007 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4008 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4009 return isl_basic_map_finalize(bmap);
4010 error:
4011 isl_basic_map_free(bmap);
4012 return NULL;
4015 /* Add a constraints to "bmap" expressing i_pos > o_pos
4017 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4019 int i;
4020 unsigned nparam;
4021 unsigned n_in;
4023 i = isl_basic_map_alloc_inequality(bmap);
4024 if (i < 0)
4025 goto error;
4026 nparam = isl_basic_map_n_param(bmap);
4027 n_in = isl_basic_map_n_in(bmap);
4028 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4029 isl_int_set_si(bmap->ineq[i][0], -1);
4030 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4031 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4032 return isl_basic_map_finalize(bmap);
4033 error:
4034 isl_basic_map_free(bmap);
4035 return NULL;
4038 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4040 static __isl_give isl_basic_map *var_more_or_equal(
4041 __isl_take isl_basic_map *bmap, unsigned pos)
4043 int i;
4044 unsigned nparam;
4045 unsigned n_in;
4047 i = isl_basic_map_alloc_inequality(bmap);
4048 if (i < 0)
4049 goto error;
4050 nparam = isl_basic_map_n_param(bmap);
4051 n_in = isl_basic_map_n_in(bmap);
4052 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4053 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4054 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4055 return isl_basic_map_finalize(bmap);
4056 error:
4057 isl_basic_map_free(bmap);
4058 return NULL;
4061 __isl_give isl_basic_map *isl_basic_map_equal(
4062 __isl_take isl_space *dim, unsigned n_equal)
4064 int i;
4065 struct isl_basic_map *bmap;
4066 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4067 if (!bmap)
4068 return NULL;
4069 for (i = 0; i < n_equal && bmap; ++i)
4070 bmap = var_equal(bmap, i);
4071 return isl_basic_map_finalize(bmap);
4074 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4076 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4077 unsigned pos)
4079 int i;
4080 struct isl_basic_map *bmap;
4081 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4082 if (!bmap)
4083 return NULL;
4084 for (i = 0; i < pos && bmap; ++i)
4085 bmap = var_equal(bmap, i);
4086 if (bmap)
4087 bmap = var_less(bmap, pos);
4088 return isl_basic_map_finalize(bmap);
4091 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4093 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4094 __isl_take isl_space *dim, unsigned pos)
4096 int i;
4097 isl_basic_map *bmap;
4099 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4100 for (i = 0; i < pos; ++i)
4101 bmap = var_equal(bmap, i);
4102 bmap = var_less_or_equal(bmap, pos);
4103 return isl_basic_map_finalize(bmap);
4106 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4108 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4109 unsigned pos)
4111 int i;
4112 struct isl_basic_map *bmap;
4113 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4114 if (!bmap)
4115 return NULL;
4116 for (i = 0; i < pos && bmap; ++i)
4117 bmap = var_equal(bmap, i);
4118 if (bmap)
4119 bmap = var_more(bmap, pos);
4120 return isl_basic_map_finalize(bmap);
4123 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4125 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4126 __isl_take isl_space *dim, unsigned pos)
4128 int i;
4129 isl_basic_map *bmap;
4131 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4132 for (i = 0; i < pos; ++i)
4133 bmap = var_equal(bmap, i);
4134 bmap = var_more_or_equal(bmap, pos);
4135 return isl_basic_map_finalize(bmap);
4138 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4139 unsigned n, int equal)
4141 struct isl_map *map;
4142 int i;
4144 if (n == 0 && equal)
4145 return isl_map_universe(dims);
4147 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4149 for (i = 0; i + 1 < n; ++i)
4150 map = isl_map_add_basic_map(map,
4151 isl_basic_map_less_at(isl_space_copy(dims), i));
4152 if (n > 0) {
4153 if (equal)
4154 map = isl_map_add_basic_map(map,
4155 isl_basic_map_less_or_equal_at(dims, n - 1));
4156 else
4157 map = isl_map_add_basic_map(map,
4158 isl_basic_map_less_at(dims, n - 1));
4159 } else
4160 isl_space_free(dims);
4162 return map;
4165 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4167 if (!dims)
4168 return NULL;
4169 return map_lex_lte_first(dims, dims->n_out, equal);
4172 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4174 return map_lex_lte_first(dim, n, 0);
4177 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4179 return map_lex_lte_first(dim, n, 1);
4182 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4184 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4187 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4189 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4192 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4193 unsigned n, int equal)
4195 struct isl_map *map;
4196 int i;
4198 if (n == 0 && equal)
4199 return isl_map_universe(dims);
4201 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4203 for (i = 0; i + 1 < n; ++i)
4204 map = isl_map_add_basic_map(map,
4205 isl_basic_map_more_at(isl_space_copy(dims), i));
4206 if (n > 0) {
4207 if (equal)
4208 map = isl_map_add_basic_map(map,
4209 isl_basic_map_more_or_equal_at(dims, n - 1));
4210 else
4211 map = isl_map_add_basic_map(map,
4212 isl_basic_map_more_at(dims, n - 1));
4213 } else
4214 isl_space_free(dims);
4216 return map;
4219 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4221 if (!dims)
4222 return NULL;
4223 return map_lex_gte_first(dims, dims->n_out, equal);
4226 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4228 return map_lex_gte_first(dim, n, 0);
4231 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4233 return map_lex_gte_first(dim, n, 1);
4236 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4238 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4241 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4243 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4246 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4247 __isl_take isl_set *set2)
4249 isl_map *map;
4250 map = isl_map_lex_le(isl_set_get_space(set1));
4251 map = isl_map_intersect_domain(map, set1);
4252 map = isl_map_intersect_range(map, set2);
4253 return map;
4256 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4257 __isl_take isl_set *set2)
4259 isl_map *map;
4260 map = isl_map_lex_lt(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_ge_set(__isl_take isl_set *set1,
4267 __isl_take isl_set *set2)
4269 isl_map *map;
4270 map = isl_map_lex_ge(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_gt_set(__isl_take isl_set *set1,
4277 __isl_take isl_set *set2)
4279 isl_map *map;
4280 map = isl_map_lex_gt(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_map_lex_le_map(__isl_take isl_map *map1,
4287 __isl_take isl_map *map2)
4289 isl_map *map;
4290 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4291 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4292 map = isl_map_apply_range(map, isl_map_reverse(map2));
4293 return map;
4296 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4297 __isl_take isl_map *map2)
4299 isl_map *map;
4300 map = isl_map_lex_lt(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_ge_map(__isl_take isl_map *map1,
4307 __isl_take isl_map *map2)
4309 isl_map *map;
4310 map = isl_map_lex_ge(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_gt_map(__isl_take isl_map *map1,
4317 __isl_take isl_map *map2)
4319 isl_map *map;
4320 map = isl_map_lex_gt(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_basic_map *isl_basic_map_from_basic_set(
4327 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4329 struct isl_basic_map *bmap;
4331 bset = isl_basic_set_cow(bset);
4332 if (!bset || !dim)
4333 goto error;
4335 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4336 isl_space_free(bset->dim);
4337 bmap = (struct isl_basic_map *) bset;
4338 bmap->dim = dim;
4339 return isl_basic_map_finalize(bmap);
4340 error:
4341 isl_basic_set_free(bset);
4342 isl_space_free(dim);
4343 return NULL;
4346 /* For a div d = floor(f/m), add the constraint
4348 * f - m d >= 0
4350 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4351 unsigned pos, isl_int *div)
4353 int i;
4354 unsigned total = isl_basic_map_total_dim(bmap);
4356 i = isl_basic_map_alloc_inequality(bmap);
4357 if (i < 0)
4358 return -1;
4359 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4360 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4362 return 0;
4365 /* For a div d = floor(f/m), add the constraint
4367 * -(f-(n-1)) + m d >= 0
4369 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4370 unsigned pos, isl_int *div)
4372 int i;
4373 unsigned total = isl_basic_map_total_dim(bmap);
4375 i = isl_basic_map_alloc_inequality(bmap);
4376 if (i < 0)
4377 return -1;
4378 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4379 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4380 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4381 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4383 return 0;
4386 /* For a div d = floor(f/m), add the constraints
4388 * f - m d >= 0
4389 * -(f-(n-1)) + m d >= 0
4391 * Note that the second constraint is the negation of
4393 * f - m d >= n
4395 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4396 unsigned pos, isl_int *div)
4398 if (add_upper_div_constraint(bmap, pos, div) < 0)
4399 return -1;
4400 if (add_lower_div_constraint(bmap, pos, div) < 0)
4401 return -1;
4402 return 0;
4405 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4406 unsigned pos, isl_int *div)
4408 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4409 pos, div);
4412 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4414 unsigned total = isl_basic_map_total_dim(bmap);
4415 unsigned div_pos = total - bmap->n_div + div;
4417 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4418 bmap->div[div]);
4421 /* For each known div d = floor(f/m), add the constraints
4423 * f - m d >= 0
4424 * -(f-(n-1)) + m d >= 0
4426 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4427 __isl_take isl_basic_map *bmap)
4429 int i;
4430 unsigned n_div;
4432 if (!bmap)
4433 return NULL;
4434 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4435 if (n_div == 0)
4436 return bmap;
4437 bmap = isl_basic_map_cow(bmap);
4438 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4439 if (!bmap)
4440 return NULL;
4441 for (i = 0; i < n_div; ++i) {
4442 if (isl_int_is_zero(bmap->div[i][0]))
4443 continue;
4444 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4445 return isl_basic_map_free(bmap);
4448 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4449 bmap = isl_basic_map_finalize(bmap);
4450 return bmap;
4453 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4455 * In particular, if this div is of the form d = floor(f/m),
4456 * then add the constraint
4458 * f - m d >= 0
4460 * if sign < 0 or the constraint
4462 * -(f-(n-1)) + m d >= 0
4464 * if sign > 0.
4466 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4467 unsigned div, int sign)
4469 unsigned total;
4470 unsigned div_pos;
4472 if (!bmap)
4473 return -1;
4475 total = isl_basic_map_total_dim(bmap);
4476 div_pos = total - bmap->n_div + div;
4478 if (sign < 0)
4479 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4480 else
4481 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4484 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4486 return isl_basic_map_add_div_constraints(bset, div);
4489 struct isl_basic_set *isl_basic_map_underlying_set(
4490 struct isl_basic_map *bmap)
4492 if (!bmap)
4493 goto error;
4494 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4495 bmap->n_div == 0 &&
4496 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4497 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4498 return (struct isl_basic_set *)bmap;
4499 bmap = isl_basic_map_cow(bmap);
4500 if (!bmap)
4501 goto error;
4502 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4503 if (!bmap->dim)
4504 goto error;
4505 bmap->extra -= bmap->n_div;
4506 bmap->n_div = 0;
4507 bmap = isl_basic_map_finalize(bmap);
4508 return (struct isl_basic_set *)bmap;
4509 error:
4510 isl_basic_map_free(bmap);
4511 return NULL;
4514 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4515 __isl_take isl_basic_set *bset)
4517 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4520 /* Replace each element in "list" by the result of applying
4521 * isl_basic_map_underlying_set to the element.
4523 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4524 __isl_take isl_basic_map_list *list)
4526 int i, n;
4528 if (!list)
4529 return NULL;
4531 n = isl_basic_map_list_n_basic_map(list);
4532 for (i = 0; i < n; ++i) {
4533 isl_basic_map *bmap;
4534 isl_basic_set *bset;
4536 bmap = isl_basic_map_list_get_basic_map(list, i);
4537 bset = isl_basic_set_underlying_set(bmap);
4538 list = isl_basic_set_list_set_basic_set(list, i, bset);
4541 return list;
4544 struct isl_basic_map *isl_basic_map_overlying_set(
4545 struct isl_basic_set *bset, struct isl_basic_map *like)
4547 struct isl_basic_map *bmap;
4548 struct isl_ctx *ctx;
4549 unsigned total;
4550 int i;
4552 if (!bset || !like)
4553 goto error;
4554 ctx = bset->ctx;
4555 isl_assert(ctx, bset->n_div == 0, goto error);
4556 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4557 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4558 goto error);
4559 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4560 isl_basic_map_free(like);
4561 return (struct isl_basic_map *)bset;
4563 bset = isl_basic_set_cow(bset);
4564 if (!bset)
4565 goto error;
4566 total = bset->dim->n_out + bset->extra;
4567 bmap = (struct isl_basic_map *)bset;
4568 isl_space_free(bmap->dim);
4569 bmap->dim = isl_space_copy(like->dim);
4570 if (!bmap->dim)
4571 goto error;
4572 bmap->n_div = like->n_div;
4573 bmap->extra += like->n_div;
4574 if (bmap->extra) {
4575 unsigned ltotal;
4576 isl_int **div;
4577 ltotal = total - bmap->extra + like->extra;
4578 if (ltotal > total)
4579 ltotal = total;
4580 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4581 bmap->extra * (1 + 1 + total));
4582 if (isl_blk_is_error(bmap->block2))
4583 goto error;
4584 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4585 if (!div)
4586 goto error;
4587 bmap->div = div;
4588 for (i = 0; i < bmap->extra; ++i)
4589 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4590 for (i = 0; i < like->n_div; ++i) {
4591 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4592 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4594 bmap = isl_basic_map_add_known_div_constraints(bmap);
4596 isl_basic_map_free(like);
4597 bmap = isl_basic_map_simplify(bmap);
4598 bmap = isl_basic_map_finalize(bmap);
4599 return bmap;
4600 error:
4601 isl_basic_map_free(like);
4602 isl_basic_set_free(bset);
4603 return NULL;
4606 struct isl_basic_set *isl_basic_set_from_underlying_set(
4607 struct isl_basic_set *bset, struct isl_basic_set *like)
4609 return (struct isl_basic_set *)
4610 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4613 struct isl_set *isl_set_from_underlying_set(
4614 struct isl_set *set, struct isl_basic_set *like)
4616 int i;
4618 if (!set || !like)
4619 goto error;
4620 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4621 goto error);
4622 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4623 isl_basic_set_free(like);
4624 return set;
4626 set = isl_set_cow(set);
4627 if (!set)
4628 goto error;
4629 for (i = 0; i < set->n; ++i) {
4630 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4631 isl_basic_set_copy(like));
4632 if (!set->p[i])
4633 goto error;
4635 isl_space_free(set->dim);
4636 set->dim = isl_space_copy(like->dim);
4637 if (!set->dim)
4638 goto error;
4639 isl_basic_set_free(like);
4640 return set;
4641 error:
4642 isl_basic_set_free(like);
4643 isl_set_free(set);
4644 return NULL;
4647 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4649 int i;
4651 map = isl_map_cow(map);
4652 if (!map)
4653 return NULL;
4654 map->dim = isl_space_cow(map->dim);
4655 if (!map->dim)
4656 goto error;
4658 for (i = 1; i < map->n; ++i)
4659 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4660 goto error);
4661 for (i = 0; i < map->n; ++i) {
4662 map->p[i] = (struct isl_basic_map *)
4663 isl_basic_map_underlying_set(map->p[i]);
4664 if (!map->p[i])
4665 goto error;
4667 if (map->n == 0)
4668 map->dim = isl_space_underlying(map->dim, 0);
4669 else {
4670 isl_space_free(map->dim);
4671 map->dim = isl_space_copy(map->p[0]->dim);
4673 if (!map->dim)
4674 goto error;
4675 return (struct isl_set *)map;
4676 error:
4677 isl_map_free(map);
4678 return NULL;
4681 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4683 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4686 __isl_give isl_basic_map *isl_basic_map_reset_space(
4687 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4689 bmap = isl_basic_map_cow(bmap);
4690 if (!bmap || !dim)
4691 goto error;
4693 isl_space_free(bmap->dim);
4694 bmap->dim = dim;
4696 bmap = isl_basic_map_finalize(bmap);
4698 return bmap;
4699 error:
4700 isl_basic_map_free(bmap);
4701 isl_space_free(dim);
4702 return NULL;
4705 __isl_give isl_basic_set *isl_basic_set_reset_space(
4706 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4708 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4709 dim);
4712 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4713 __isl_take isl_space *dim)
4715 int i;
4717 map = isl_map_cow(map);
4718 if (!map || !dim)
4719 goto error;
4721 for (i = 0; i < map->n; ++i) {
4722 map->p[i] = isl_basic_map_reset_space(map->p[i],
4723 isl_space_copy(dim));
4724 if (!map->p[i])
4725 goto error;
4727 isl_space_free(map->dim);
4728 map->dim = dim;
4730 return map;
4731 error:
4732 isl_map_free(map);
4733 isl_space_free(dim);
4734 return NULL;
4737 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4738 __isl_take isl_space *dim)
4740 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4743 /* Compute the parameter domain of the given basic set.
4745 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4747 isl_space *space;
4748 unsigned n;
4750 if (isl_basic_set_is_params(bset))
4751 return bset;
4753 n = isl_basic_set_dim(bset, isl_dim_set);
4754 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4755 space = isl_basic_set_get_space(bset);
4756 space = isl_space_params(space);
4757 bset = isl_basic_set_reset_space(bset, space);
4758 return bset;
4761 /* Construct a zero-dimensional basic set with the given parameter domain.
4763 __isl_give isl_basic_set *isl_basic_set_from_params(
4764 __isl_take isl_basic_set *bset)
4766 isl_space *space;
4767 space = isl_basic_set_get_space(bset);
4768 space = isl_space_set_from_params(space);
4769 bset = isl_basic_set_reset_space(bset, space);
4770 return bset;
4773 /* Compute the parameter domain of the given set.
4775 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4777 isl_space *space;
4778 unsigned n;
4780 if (isl_set_is_params(set))
4781 return set;
4783 n = isl_set_dim(set, isl_dim_set);
4784 set = isl_set_project_out(set, isl_dim_set, 0, n);
4785 space = isl_set_get_space(set);
4786 space = isl_space_params(space);
4787 set = isl_set_reset_space(set, space);
4788 return set;
4791 /* Construct a zero-dimensional set with the given parameter domain.
4793 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4795 isl_space *space;
4796 space = isl_set_get_space(set);
4797 space = isl_space_set_from_params(space);
4798 set = isl_set_reset_space(set, space);
4799 return set;
4802 /* Compute the parameter domain of the given map.
4804 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4806 isl_space *space;
4807 unsigned n;
4809 n = isl_map_dim(map, isl_dim_in);
4810 map = isl_map_project_out(map, isl_dim_in, 0, n);
4811 n = isl_map_dim(map, isl_dim_out);
4812 map = isl_map_project_out(map, isl_dim_out, 0, n);
4813 space = isl_map_get_space(map);
4814 space = isl_space_params(space);
4815 map = isl_map_reset_space(map, space);
4816 return map;
4819 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4821 isl_space *space;
4822 unsigned n_out;
4824 if (!bmap)
4825 return NULL;
4826 space = isl_space_domain(isl_basic_map_get_space(bmap));
4828 n_out = isl_basic_map_n_out(bmap);
4829 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
4831 return isl_basic_map_reset_space(bmap, space);
4834 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4836 if (!bmap)
4837 return -1;
4838 return isl_space_may_be_set(bmap->dim);
4841 /* Is this basic map actually a set?
4842 * Users should never call this function. Outside of isl,
4843 * the type should indicate whether something is a set or a map.
4845 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4847 if (!bmap)
4848 return -1;
4849 return isl_space_is_set(bmap->dim);
4852 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4854 if (!bmap)
4855 return NULL;
4856 if (isl_basic_map_is_set(bmap))
4857 return bmap;
4858 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4861 __isl_give isl_basic_map *isl_basic_map_domain_map(
4862 __isl_take isl_basic_map *bmap)
4864 int i, k;
4865 isl_space *dim;
4866 isl_basic_map *domain;
4867 int nparam, n_in, n_out;
4868 unsigned total;
4870 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4871 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4872 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4874 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4875 domain = isl_basic_map_universe(dim);
4877 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4878 bmap = isl_basic_map_apply_range(bmap, domain);
4879 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4881 total = isl_basic_map_total_dim(bmap);
4883 for (i = 0; i < n_in; ++i) {
4884 k = isl_basic_map_alloc_equality(bmap);
4885 if (k < 0)
4886 goto error;
4887 isl_seq_clr(bmap->eq[k], 1 + total);
4888 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4889 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4892 bmap = isl_basic_map_gauss(bmap, NULL);
4893 return isl_basic_map_finalize(bmap);
4894 error:
4895 isl_basic_map_free(bmap);
4896 return NULL;
4899 __isl_give isl_basic_map *isl_basic_map_range_map(
4900 __isl_take isl_basic_map *bmap)
4902 int i, k;
4903 isl_space *dim;
4904 isl_basic_map *range;
4905 int nparam, n_in, n_out;
4906 unsigned total;
4908 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4909 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4910 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4912 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4913 range = isl_basic_map_universe(dim);
4915 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4916 bmap = isl_basic_map_apply_range(bmap, range);
4917 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4919 total = isl_basic_map_total_dim(bmap);
4921 for (i = 0; i < n_out; ++i) {
4922 k = isl_basic_map_alloc_equality(bmap);
4923 if (k < 0)
4924 goto error;
4925 isl_seq_clr(bmap->eq[k], 1 + total);
4926 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4927 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4930 bmap = isl_basic_map_gauss(bmap, NULL);
4931 return isl_basic_map_finalize(bmap);
4932 error:
4933 isl_basic_map_free(bmap);
4934 return NULL;
4937 int isl_map_may_be_set(__isl_keep isl_map *map)
4939 if (!map)
4940 return -1;
4941 return isl_space_may_be_set(map->dim);
4944 /* Is this map actually a set?
4945 * Users should never call this function. Outside of isl,
4946 * the type should indicate whether something is a set or a map.
4948 int isl_map_is_set(__isl_keep isl_map *map)
4950 if (!map)
4951 return -1;
4952 return isl_space_is_set(map->dim);
4955 struct isl_set *isl_map_range(struct isl_map *map)
4957 int i;
4958 struct isl_set *set;
4960 if (!map)
4961 goto error;
4962 if (isl_map_is_set(map))
4963 return (isl_set *)map;
4965 map = isl_map_cow(map);
4966 if (!map)
4967 goto error;
4969 set = (struct isl_set *) map;
4970 set->dim = isl_space_range(set->dim);
4971 if (!set->dim)
4972 goto error;
4973 for (i = 0; i < map->n; ++i) {
4974 set->p[i] = isl_basic_map_range(map->p[i]);
4975 if (!set->p[i])
4976 goto error;
4978 ISL_F_CLR(set, ISL_MAP_DISJOINT);
4979 ISL_F_CLR(set, ISL_SET_NORMALIZED);
4980 return set;
4981 error:
4982 isl_map_free(map);
4983 return NULL;
4986 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4988 int i;
4990 map = isl_map_cow(map);
4991 if (!map)
4992 return NULL;
4994 map->dim = isl_space_domain_map(map->dim);
4995 if (!map->dim)
4996 goto error;
4997 for (i = 0; i < map->n; ++i) {
4998 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4999 if (!map->p[i])
5000 goto error;
5002 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5003 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5004 return map;
5005 error:
5006 isl_map_free(map);
5007 return NULL;
5010 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5012 int i;
5013 isl_space *range_dim;
5015 map = isl_map_cow(map);
5016 if (!map)
5017 return NULL;
5019 range_dim = isl_space_range(isl_map_get_space(map));
5020 range_dim = isl_space_from_range(range_dim);
5021 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5022 map->dim = isl_space_join(map->dim, range_dim);
5023 if (!map->dim)
5024 goto error;
5025 for (i = 0; i < map->n; ++i) {
5026 map->p[i] = isl_basic_map_range_map(map->p[i]);
5027 if (!map->p[i])
5028 goto error;
5030 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5031 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5032 return map;
5033 error:
5034 isl_map_free(map);
5035 return NULL;
5038 /* Given a wrapped map of the form A[B -> C],
5039 * return the map A[B -> C] -> B.
5041 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5043 isl_id *id;
5044 isl_map *map;
5046 if (!set)
5047 return NULL;
5048 if (!isl_set_has_tuple_id(set))
5049 return isl_map_domain_map(isl_set_unwrap(set));
5051 id = isl_set_get_tuple_id(set);
5052 map = isl_map_domain_map(isl_set_unwrap(set));
5053 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5055 return map;
5058 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5059 __isl_take isl_space *dim)
5061 int i;
5062 struct isl_map *map = NULL;
5064 set = isl_set_cow(set);
5065 if (!set || !dim)
5066 goto error;
5067 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5068 map = (struct isl_map *)set;
5069 for (i = 0; i < set->n; ++i) {
5070 map->p[i] = isl_basic_map_from_basic_set(
5071 set->p[i], isl_space_copy(dim));
5072 if (!map->p[i])
5073 goto error;
5075 isl_space_free(map->dim);
5076 map->dim = dim;
5077 return map;
5078 error:
5079 isl_space_free(dim);
5080 isl_set_free(set);
5081 return NULL;
5084 __isl_give isl_basic_map *isl_basic_map_from_domain(
5085 __isl_take isl_basic_set *bset)
5087 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5090 __isl_give isl_basic_map *isl_basic_map_from_range(
5091 __isl_take isl_basic_set *bset)
5093 isl_space *space;
5094 space = isl_basic_set_get_space(bset);
5095 space = isl_space_from_range(space);
5096 bset = isl_basic_set_reset_space(bset, space);
5097 return (isl_basic_map *)bset;
5100 /* Create a relation with the given set as range.
5101 * The domain of the created relation is a zero-dimensional
5102 * flat anonymous space.
5104 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5106 isl_space *space;
5107 space = isl_set_get_space(set);
5108 space = isl_space_from_range(space);
5109 set = isl_set_reset_space(set, space);
5110 return (struct isl_map *)set;
5113 /* Create a relation with the given set as domain.
5114 * The range of the created relation is a zero-dimensional
5115 * flat anonymous space.
5117 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5119 return isl_map_reverse(isl_map_from_range(set));
5122 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5123 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5125 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5128 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5129 __isl_take isl_set *range)
5131 return isl_map_apply_range(isl_map_reverse(domain), range);
5134 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5135 unsigned flags)
5137 struct isl_map *map;
5139 if (!dim)
5140 return NULL;
5141 if (n < 0)
5142 isl_die(dim->ctx, isl_error_internal,
5143 "negative number of basic maps", goto error);
5144 map = isl_alloc(dim->ctx, struct isl_map,
5145 sizeof(struct isl_map) +
5146 (n - 1) * sizeof(struct isl_basic_map *));
5147 if (!map)
5148 goto error;
5150 map->ctx = dim->ctx;
5151 isl_ctx_ref(map->ctx);
5152 map->ref = 1;
5153 map->size = n;
5154 map->n = 0;
5155 map->dim = dim;
5156 map->flags = flags;
5157 return map;
5158 error:
5159 isl_space_free(dim);
5160 return NULL;
5163 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5164 unsigned nparam, unsigned in, unsigned out, int n,
5165 unsigned flags)
5167 struct isl_map *map;
5168 isl_space *dims;
5170 dims = isl_space_alloc(ctx, nparam, in, out);
5171 if (!dims)
5172 return NULL;
5174 map = isl_map_alloc_space(dims, n, flags);
5175 return map;
5178 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5180 struct isl_basic_map *bmap;
5181 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5182 bmap = isl_basic_map_set_to_empty(bmap);
5183 return bmap;
5186 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5188 struct isl_basic_set *bset;
5189 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5190 bset = isl_basic_set_set_to_empty(bset);
5191 return bset;
5194 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5196 struct isl_basic_map *bmap;
5197 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5198 bmap = isl_basic_map_finalize(bmap);
5199 return bmap;
5202 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5204 struct isl_basic_set *bset;
5205 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5206 bset = isl_basic_set_finalize(bset);
5207 return bset;
5210 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5212 int i;
5213 unsigned total = isl_space_dim(dim, isl_dim_all);
5214 isl_basic_map *bmap;
5216 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5217 for (i = 0; i < total; ++i) {
5218 int k = isl_basic_map_alloc_inequality(bmap);
5219 if (k < 0)
5220 goto error;
5221 isl_seq_clr(bmap->ineq[k], 1 + total);
5222 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5224 return bmap;
5225 error:
5226 isl_basic_map_free(bmap);
5227 return NULL;
5230 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5232 return isl_basic_map_nat_universe(dim);
5235 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5237 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5240 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5242 return isl_map_nat_universe(dim);
5245 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5247 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5250 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5252 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5255 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5257 struct isl_map *map;
5258 if (!dim)
5259 return NULL;
5260 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5261 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5262 return map;
5265 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5267 struct isl_set *set;
5268 if (!dim)
5269 return NULL;
5270 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5271 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5272 return set;
5275 struct isl_map *isl_map_dup(struct isl_map *map)
5277 int i;
5278 struct isl_map *dup;
5280 if (!map)
5281 return NULL;
5282 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5283 for (i = 0; i < map->n; ++i)
5284 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5285 return dup;
5288 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5289 __isl_take isl_basic_map *bmap)
5291 if (!bmap || !map)
5292 goto error;
5293 if (isl_basic_map_plain_is_empty(bmap)) {
5294 isl_basic_map_free(bmap);
5295 return map;
5297 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5298 isl_assert(map->ctx, map->n < map->size, goto error);
5299 map->p[map->n] = bmap;
5300 map->n++;
5301 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5302 return map;
5303 error:
5304 if (map)
5305 isl_map_free(map);
5306 if (bmap)
5307 isl_basic_map_free(bmap);
5308 return NULL;
5311 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5313 int i;
5315 if (!map)
5316 return NULL;
5318 if (--map->ref > 0)
5319 return NULL;
5321 isl_ctx_deref(map->ctx);
5322 for (i = 0; i < map->n; ++i)
5323 isl_basic_map_free(map->p[i]);
5324 isl_space_free(map->dim);
5325 free(map);
5327 return NULL;
5330 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5331 struct isl_basic_map *bmap, unsigned pos, int value)
5333 int j;
5335 bmap = isl_basic_map_cow(bmap);
5336 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5337 j = isl_basic_map_alloc_equality(bmap);
5338 if (j < 0)
5339 goto error;
5340 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5341 isl_int_set_si(bmap->eq[j][pos], -1);
5342 isl_int_set_si(bmap->eq[j][0], value);
5343 bmap = isl_basic_map_simplify(bmap);
5344 return isl_basic_map_finalize(bmap);
5345 error:
5346 isl_basic_map_free(bmap);
5347 return NULL;
5350 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5351 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5353 int j;
5355 bmap = isl_basic_map_cow(bmap);
5356 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5357 j = isl_basic_map_alloc_equality(bmap);
5358 if (j < 0)
5359 goto error;
5360 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5361 isl_int_set_si(bmap->eq[j][pos], -1);
5362 isl_int_set(bmap->eq[j][0], value);
5363 bmap = isl_basic_map_simplify(bmap);
5364 return isl_basic_map_finalize(bmap);
5365 error:
5366 isl_basic_map_free(bmap);
5367 return NULL;
5370 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5371 enum isl_dim_type type, unsigned pos, int value)
5373 if (!bmap)
5374 return NULL;
5375 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5376 return isl_basic_map_fix_pos_si(bmap,
5377 isl_basic_map_offset(bmap, type) + pos, value);
5378 error:
5379 isl_basic_map_free(bmap);
5380 return NULL;
5383 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5384 enum isl_dim_type type, unsigned pos, isl_int value)
5386 if (!bmap)
5387 return NULL;
5388 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5389 return isl_basic_map_fix_pos(bmap,
5390 isl_basic_map_offset(bmap, type) + pos, value);
5391 error:
5392 isl_basic_map_free(bmap);
5393 return NULL;
5396 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5397 * to be equal to "v".
5399 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5400 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5402 if (!bmap || !v)
5403 goto error;
5404 if (!isl_val_is_int(v))
5405 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5406 "expecting integer value", goto error);
5407 if (pos >= isl_basic_map_dim(bmap, type))
5408 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5409 "index out of bounds", goto error);
5410 pos += isl_basic_map_offset(bmap, type);
5411 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5412 isl_val_free(v);
5413 return bmap;
5414 error:
5415 isl_basic_map_free(bmap);
5416 isl_val_free(v);
5417 return NULL;
5420 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5421 * to be equal to "v".
5423 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5424 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5426 return isl_basic_map_fix_val(bset, type, pos, v);
5429 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5430 enum isl_dim_type type, unsigned pos, int value)
5432 return (struct isl_basic_set *)
5433 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5434 type, pos, value);
5437 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5438 enum isl_dim_type type, unsigned pos, isl_int value)
5440 return (struct isl_basic_set *)
5441 isl_basic_map_fix((struct isl_basic_map *)bset,
5442 type, pos, value);
5445 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5446 unsigned input, int value)
5448 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5451 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5452 unsigned dim, int value)
5454 return (struct isl_basic_set *)
5455 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5456 isl_dim_set, dim, value);
5459 static int remove_if_empty(__isl_keep isl_map *map, int i)
5461 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5463 if (empty < 0)
5464 return -1;
5465 if (!empty)
5466 return 0;
5468 isl_basic_map_free(map->p[i]);
5469 if (i != map->n - 1) {
5470 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5471 map->p[i] = map->p[map->n - 1];
5473 map->n--;
5475 return 0;
5478 /* Perform "fn" on each basic map of "map", where we may not be holding
5479 * the only reference to "map".
5480 * In particular, "fn" should be a semantics preserving operation
5481 * that we want to apply to all copies of "map". We therefore need
5482 * to be careful not to modify "map" in a way that breaks "map"
5483 * in case anything goes wrong.
5485 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5486 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5488 struct isl_basic_map *bmap;
5489 int i;
5491 if (!map)
5492 return NULL;
5494 for (i = map->n - 1; i >= 0; --i) {
5495 bmap = isl_basic_map_copy(map->p[i]);
5496 bmap = fn(bmap);
5497 if (!bmap)
5498 goto error;
5499 isl_basic_map_free(map->p[i]);
5500 map->p[i] = bmap;
5501 if (remove_if_empty(map, i) < 0)
5502 goto error;
5505 return map;
5506 error:
5507 isl_map_free(map);
5508 return NULL;
5511 struct isl_map *isl_map_fix_si(struct isl_map *map,
5512 enum isl_dim_type type, unsigned pos, int value)
5514 int i;
5516 map = isl_map_cow(map);
5517 if (!map)
5518 return NULL;
5520 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5521 for (i = map->n - 1; i >= 0; --i) {
5522 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5523 if (remove_if_empty(map, i) < 0)
5524 goto error;
5526 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5527 return map;
5528 error:
5529 isl_map_free(map);
5530 return NULL;
5533 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5534 enum isl_dim_type type, unsigned pos, int value)
5536 return (struct isl_set *)
5537 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5540 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5541 enum isl_dim_type type, unsigned pos, isl_int value)
5543 int i;
5545 map = isl_map_cow(map);
5546 if (!map)
5547 return NULL;
5549 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5550 for (i = 0; i < map->n; ++i) {
5551 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5552 if (!map->p[i])
5553 goto error;
5555 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5556 return map;
5557 error:
5558 isl_map_free(map);
5559 return NULL;
5562 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5563 enum isl_dim_type type, unsigned pos, isl_int value)
5565 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5568 /* Fix the value of the variable at position "pos" of type "type" of "map"
5569 * to be equal to "v".
5571 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5572 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5574 int i;
5576 map = isl_map_cow(map);
5577 if (!map || !v)
5578 goto error;
5580 if (!isl_val_is_int(v))
5581 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5582 "expecting integer value", goto error);
5583 if (pos >= isl_map_dim(map, type))
5584 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5585 "index out of bounds", goto error);
5586 for (i = map->n - 1; i >= 0; --i) {
5587 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5588 isl_val_copy(v));
5589 if (remove_if_empty(map, i) < 0)
5590 goto error;
5592 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5593 isl_val_free(v);
5594 return map;
5595 error:
5596 isl_map_free(map);
5597 isl_val_free(v);
5598 return NULL;
5601 /* Fix the value of the variable at position "pos" of type "type" of "set"
5602 * to be equal to "v".
5604 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5605 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5607 return isl_map_fix_val(set, type, pos, v);
5610 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5611 unsigned input, int value)
5613 return isl_map_fix_si(map, isl_dim_in, input, value);
5616 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5618 return (struct isl_set *)
5619 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5622 static __isl_give isl_basic_map *basic_map_bound_si(
5623 __isl_take isl_basic_map *bmap,
5624 enum isl_dim_type type, unsigned pos, int value, int upper)
5626 int j;
5628 if (!bmap)
5629 return NULL;
5630 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5631 pos += isl_basic_map_offset(bmap, type);
5632 bmap = isl_basic_map_cow(bmap);
5633 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5634 j = isl_basic_map_alloc_inequality(bmap);
5635 if (j < 0)
5636 goto error;
5637 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5638 if (upper) {
5639 isl_int_set_si(bmap->ineq[j][pos], -1);
5640 isl_int_set_si(bmap->ineq[j][0], value);
5641 } else {
5642 isl_int_set_si(bmap->ineq[j][pos], 1);
5643 isl_int_set_si(bmap->ineq[j][0], -value);
5645 bmap = isl_basic_map_simplify(bmap);
5646 return isl_basic_map_finalize(bmap);
5647 error:
5648 isl_basic_map_free(bmap);
5649 return NULL;
5652 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5653 __isl_take isl_basic_map *bmap,
5654 enum isl_dim_type type, unsigned pos, int value)
5656 return basic_map_bound_si(bmap, type, pos, value, 0);
5659 /* Constrain the values of the given dimension to be no greater than "value".
5661 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5662 __isl_take isl_basic_map *bmap,
5663 enum isl_dim_type type, unsigned pos, int value)
5665 return basic_map_bound_si(bmap, type, pos, value, 1);
5668 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5669 unsigned dim, isl_int value)
5671 int j;
5673 bset = isl_basic_set_cow(bset);
5674 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5675 j = isl_basic_set_alloc_inequality(bset);
5676 if (j < 0)
5677 goto error;
5678 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5679 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5680 isl_int_neg(bset->ineq[j][0], value);
5681 bset = isl_basic_set_simplify(bset);
5682 return isl_basic_set_finalize(bset);
5683 error:
5684 isl_basic_set_free(bset);
5685 return NULL;
5688 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5689 enum isl_dim_type type, unsigned pos, int value, int upper)
5691 int i;
5693 map = isl_map_cow(map);
5694 if (!map)
5695 return NULL;
5697 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5698 for (i = 0; i < map->n; ++i) {
5699 map->p[i] = basic_map_bound_si(map->p[i],
5700 type, pos, value, upper);
5701 if (!map->p[i])
5702 goto error;
5704 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5705 return map;
5706 error:
5707 isl_map_free(map);
5708 return NULL;
5711 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5712 enum isl_dim_type type, unsigned pos, int value)
5714 return map_bound_si(map, type, pos, value, 0);
5717 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5718 enum isl_dim_type type, unsigned pos, int value)
5720 return map_bound_si(map, type, pos, value, 1);
5723 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5724 enum isl_dim_type type, unsigned pos, int value)
5726 return (struct isl_set *)
5727 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5730 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5731 enum isl_dim_type type, unsigned pos, int value)
5733 return isl_map_upper_bound_si(set, type, pos, value);
5736 /* Bound the given variable of "bmap" from below (or above is "upper"
5737 * is set) to "value".
5739 static __isl_give isl_basic_map *basic_map_bound(
5740 __isl_take isl_basic_map *bmap,
5741 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5743 int j;
5745 if (!bmap)
5746 return NULL;
5747 if (pos >= isl_basic_map_dim(bmap, type))
5748 isl_die(bmap->ctx, isl_error_invalid,
5749 "index out of bounds", goto error);
5750 pos += isl_basic_map_offset(bmap, type);
5751 bmap = isl_basic_map_cow(bmap);
5752 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5753 j = isl_basic_map_alloc_inequality(bmap);
5754 if (j < 0)
5755 goto error;
5756 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5757 if (upper) {
5758 isl_int_set_si(bmap->ineq[j][pos], -1);
5759 isl_int_set(bmap->ineq[j][0], value);
5760 } else {
5761 isl_int_set_si(bmap->ineq[j][pos], 1);
5762 isl_int_neg(bmap->ineq[j][0], value);
5764 bmap = isl_basic_map_simplify(bmap);
5765 return isl_basic_map_finalize(bmap);
5766 error:
5767 isl_basic_map_free(bmap);
5768 return NULL;
5771 /* Bound the given variable of "map" from below (or above is "upper"
5772 * is set) to "value".
5774 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5775 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5777 int i;
5779 map = isl_map_cow(map);
5780 if (!map)
5781 return NULL;
5783 if (pos >= isl_map_dim(map, type))
5784 isl_die(map->ctx, isl_error_invalid,
5785 "index out of bounds", goto error);
5786 for (i = map->n - 1; i >= 0; --i) {
5787 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5788 if (remove_if_empty(map, i) < 0)
5789 goto error;
5791 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5792 return map;
5793 error:
5794 isl_map_free(map);
5795 return NULL;
5798 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5799 enum isl_dim_type type, unsigned pos, isl_int value)
5801 return map_bound(map, type, pos, value, 0);
5804 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5805 enum isl_dim_type type, unsigned pos, isl_int value)
5807 return map_bound(map, type, pos, value, 1);
5810 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5811 enum isl_dim_type type, unsigned pos, isl_int value)
5813 return isl_map_lower_bound(set, type, pos, value);
5816 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5817 enum isl_dim_type type, unsigned pos, isl_int value)
5819 return isl_map_upper_bound(set, type, pos, value);
5822 /* Force the values of the variable at position "pos" of type "type" of "set"
5823 * to be no smaller than "value".
5825 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5826 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5828 if (!value)
5829 goto error;
5830 if (!isl_val_is_int(value))
5831 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5832 "expecting integer value", goto error);
5833 set = isl_set_lower_bound(set, type, pos, value->n);
5834 isl_val_free(value);
5835 return set;
5836 error:
5837 isl_val_free(value);
5838 isl_set_free(set);
5839 return NULL;
5842 /* Force the values of the variable at position "pos" of type "type" of "set"
5843 * to be no greater than "value".
5845 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5846 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5848 if (!value)
5849 goto error;
5850 if (!isl_val_is_int(value))
5851 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5852 "expecting integer value", goto error);
5853 set = isl_set_upper_bound(set, type, pos, value->n);
5854 isl_val_free(value);
5855 return set;
5856 error:
5857 isl_val_free(value);
5858 isl_set_free(set);
5859 return NULL;
5862 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5863 isl_int value)
5865 int i;
5867 set = isl_set_cow(set);
5868 if (!set)
5869 return NULL;
5871 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5872 for (i = 0; i < set->n; ++i) {
5873 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5874 if (!set->p[i])
5875 goto error;
5877 return set;
5878 error:
5879 isl_set_free(set);
5880 return NULL;
5883 struct isl_map *isl_map_reverse(struct isl_map *map)
5885 int i;
5887 map = isl_map_cow(map);
5888 if (!map)
5889 return NULL;
5891 map->dim = isl_space_reverse(map->dim);
5892 if (!map->dim)
5893 goto error;
5894 for (i = 0; i < map->n; ++i) {
5895 map->p[i] = isl_basic_map_reverse(map->p[i]);
5896 if (!map->p[i])
5897 goto error;
5899 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5900 return map;
5901 error:
5902 isl_map_free(map);
5903 return NULL;
5906 static struct isl_map *isl_basic_map_partial_lexopt(
5907 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5908 struct isl_set **empty, int max)
5910 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5913 struct isl_map *isl_basic_map_partial_lexmax(
5914 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5915 struct isl_set **empty)
5917 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5920 struct isl_map *isl_basic_map_partial_lexmin(
5921 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5922 struct isl_set **empty)
5924 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5927 struct isl_set *isl_basic_set_partial_lexmin(
5928 struct isl_basic_set *bset, struct isl_basic_set *dom,
5929 struct isl_set **empty)
5931 return (struct isl_set *)
5932 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5933 dom, empty);
5936 struct isl_set *isl_basic_set_partial_lexmax(
5937 struct isl_basic_set *bset, struct isl_basic_set *dom,
5938 struct isl_set **empty)
5940 return (struct isl_set *)
5941 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5942 dom, empty);
5945 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5946 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5947 __isl_give isl_set **empty)
5949 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5952 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5953 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5954 __isl_give isl_set **empty)
5956 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5959 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5960 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5961 __isl_give isl_set **empty)
5963 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5966 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5967 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5968 __isl_give isl_set **empty)
5970 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5973 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5974 __isl_take isl_basic_map *bmap, int max)
5976 isl_basic_set *dom = NULL;
5977 isl_space *dom_space;
5979 if (!bmap)
5980 goto error;
5981 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5982 dom = isl_basic_set_universe(dom_space);
5983 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5984 error:
5985 isl_basic_map_free(bmap);
5986 return NULL;
5989 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5990 __isl_take isl_basic_map *bmap)
5992 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5995 #undef TYPE
5996 #define TYPE isl_pw_multi_aff
5997 #undef SUFFIX
5998 #define SUFFIX _pw_multi_aff
5999 #undef EMPTY
6000 #define EMPTY isl_pw_multi_aff_empty
6001 #undef ADD
6002 #define ADD isl_pw_multi_aff_union_add
6003 #include "isl_map_lexopt_templ.c"
6005 /* Given a map "map", compute the lexicographically minimal
6006 * (or maximal) image element for each domain element in dom,
6007 * in the form of an isl_pw_multi_aff.
6008 * Set *empty to those elements in dom that do not have an image element.
6010 * We first compute the lexicographically minimal or maximal element
6011 * in the first basic map. This results in a partial solution "res"
6012 * and a subset "todo" of dom that still need to be handled.
6013 * We then consider each of the remaining maps in "map" and successively
6014 * update both "res" and "todo".
6016 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6017 __isl_take isl_map *map, __isl_take isl_set *dom,
6018 __isl_give isl_set **empty, int max)
6020 int i;
6021 isl_pw_multi_aff *res;
6022 isl_set *todo;
6024 if (!map || !dom)
6025 goto error;
6027 if (isl_map_plain_is_empty(map)) {
6028 if (empty)
6029 *empty = dom;
6030 else
6031 isl_set_free(dom);
6032 return isl_pw_multi_aff_from_map(map);
6035 res = basic_map_partial_lexopt_pw_multi_aff(
6036 isl_basic_map_copy(map->p[0]),
6037 isl_set_copy(dom), &todo, max);
6039 for (i = 1; i < map->n; ++i) {
6040 isl_pw_multi_aff *res_i;
6041 isl_set *todo_i;
6043 res_i = basic_map_partial_lexopt_pw_multi_aff(
6044 isl_basic_map_copy(map->p[i]),
6045 isl_set_copy(dom), &todo_i, max);
6047 if (max)
6048 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6049 else
6050 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6052 todo = isl_set_intersect(todo, todo_i);
6055 isl_set_free(dom);
6056 isl_map_free(map);
6058 if (empty)
6059 *empty = todo;
6060 else
6061 isl_set_free(todo);
6063 return res;
6064 error:
6065 if (empty)
6066 *empty = NULL;
6067 isl_set_free(dom);
6068 isl_map_free(map);
6069 return NULL;
6072 #undef TYPE
6073 #define TYPE isl_map
6074 #undef SUFFIX
6075 #define SUFFIX
6076 #undef EMPTY
6077 #define EMPTY isl_map_empty
6078 #undef ADD
6079 #define ADD isl_map_union_disjoint
6080 #include "isl_map_lexopt_templ.c"
6082 /* Given a map "map", compute the lexicographically minimal
6083 * (or maximal) image element for each domain element in dom.
6084 * Set *empty to those elements in dom that do not have an image element.
6086 * We first compute the lexicographically minimal or maximal element
6087 * in the first basic map. This results in a partial solution "res"
6088 * and a subset "todo" of dom that still need to be handled.
6089 * We then consider each of the remaining maps in "map" and successively
6090 * update both "res" and "todo".
6092 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6093 * Assume we are computing the lexicographical maximum.
6094 * We first compute the lexicographically maximal element in basic map i.
6095 * This results in a partial solution res_i and a subset todo_i.
6096 * Then we combine these results with those obtain for the first k basic maps
6097 * to obtain a result that is valid for the first k+1 basic maps.
6098 * In particular, the set where there is no solution is the set where
6099 * there is no solution for the first k basic maps and also no solution
6100 * for the ith basic map, i.e.,
6102 * todo^i = todo^k * todo_i
6104 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6105 * solutions, arbitrarily breaking ties in favor of res^k.
6106 * That is, when res^k(a) >= res_i(a), we pick res^k and
6107 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6108 * the lexicographic order.)
6109 * In practice, we compute
6111 * res^k * (res_i . "<=")
6113 * and
6115 * res_i * (res^k . "<")
6117 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6118 * where only one of res^k and res_i provides a solution and we simply pick
6119 * that one, i.e.,
6121 * res^k * todo_i
6122 * and
6123 * res_i * todo^k
6125 * Note that we only compute these intersections when dom(res^k) intersects
6126 * dom(res_i). Otherwise, the only effect of these intersections is to
6127 * potentially break up res^k and res_i into smaller pieces.
6128 * We want to avoid such splintering as much as possible.
6129 * In fact, an earlier implementation of this function would look for
6130 * better results in the domain of res^k and for extra results in todo^k,
6131 * but this would always result in a splintering according to todo^k,
6132 * even when the domain of basic map i is disjoint from the domains of
6133 * the previous basic maps.
6135 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6136 __isl_take isl_map *map, __isl_take isl_set *dom,
6137 __isl_give isl_set **empty, int max)
6139 int i;
6140 struct isl_map *res;
6141 struct isl_set *todo;
6143 if (!map || !dom)
6144 goto error;
6146 if (isl_map_plain_is_empty(map)) {
6147 if (empty)
6148 *empty = dom;
6149 else
6150 isl_set_free(dom);
6151 return map;
6154 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6155 isl_set_copy(dom), &todo, max);
6157 for (i = 1; i < map->n; ++i) {
6158 isl_map *lt, *le;
6159 isl_map *res_i;
6160 isl_set *todo_i;
6161 isl_space *dim = isl_space_range(isl_map_get_space(res));
6163 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6164 isl_set_copy(dom), &todo_i, max);
6166 if (max) {
6167 lt = isl_map_lex_lt(isl_space_copy(dim));
6168 le = isl_map_lex_le(dim);
6169 } else {
6170 lt = isl_map_lex_gt(isl_space_copy(dim));
6171 le = isl_map_lex_ge(dim);
6173 lt = isl_map_apply_range(isl_map_copy(res), lt);
6174 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6175 le = isl_map_apply_range(isl_map_copy(res_i), le);
6176 le = isl_map_intersect(le, isl_map_copy(res));
6178 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6179 res = isl_map_intersect_domain(res,
6180 isl_set_copy(todo_i));
6181 res_i = isl_map_intersect_domain(res_i,
6182 isl_set_copy(todo));
6185 res = isl_map_union_disjoint(res, res_i);
6186 res = isl_map_union_disjoint(res, lt);
6187 res = isl_map_union_disjoint(res, le);
6189 todo = isl_set_intersect(todo, todo_i);
6192 isl_set_free(dom);
6193 isl_map_free(map);
6195 if (empty)
6196 *empty = todo;
6197 else
6198 isl_set_free(todo);
6200 return res;
6201 error:
6202 if (empty)
6203 *empty = NULL;
6204 isl_set_free(dom);
6205 isl_map_free(map);
6206 return NULL;
6209 __isl_give isl_map *isl_map_partial_lexmax(
6210 __isl_take isl_map *map, __isl_take isl_set *dom,
6211 __isl_give isl_set **empty)
6213 return isl_map_partial_lexopt(map, dom, empty, 1);
6216 __isl_give isl_map *isl_map_partial_lexmin(
6217 __isl_take isl_map *map, __isl_take isl_set *dom,
6218 __isl_give isl_set **empty)
6220 return isl_map_partial_lexopt(map, dom, empty, 0);
6223 __isl_give isl_set *isl_set_partial_lexmin(
6224 __isl_take isl_set *set, __isl_take isl_set *dom,
6225 __isl_give isl_set **empty)
6227 return (struct isl_set *)
6228 isl_map_partial_lexmin((struct isl_map *)set,
6229 dom, empty);
6232 __isl_give isl_set *isl_set_partial_lexmax(
6233 __isl_take isl_set *set, __isl_take isl_set *dom,
6234 __isl_give isl_set **empty)
6236 return (struct isl_set *)
6237 isl_map_partial_lexmax((struct isl_map *)set,
6238 dom, empty);
6241 /* Compute the lexicographic minimum (or maximum if "max" is set)
6242 * of "bmap" over its domain.
6244 * Since we are not interested in the part of the domain space where
6245 * there is no solution, we initialize the domain to those constraints
6246 * of "bmap" that only involve the parameters and the input dimensions.
6247 * This relieves the parametric programming engine from detecting those
6248 * inequalities and transferring them to the context. More importantly,
6249 * it ensures that those inequalities are transferred first and not
6250 * intermixed with inequalities that actually split the domain.
6252 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6254 int n_div;
6255 int n_out;
6256 isl_basic_map *copy;
6257 isl_basic_set *dom;
6259 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6260 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6261 copy = isl_basic_map_copy(bmap);
6262 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6263 isl_dim_div, 0, n_div);
6264 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6265 isl_dim_out, 0, n_out);
6266 dom = isl_basic_map_domain(copy);
6267 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6270 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6272 return isl_basic_map_lexopt(bmap, 0);
6275 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6277 return isl_basic_map_lexopt(bmap, 1);
6280 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6282 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6285 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6287 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6290 /* Extract the first and only affine expression from list
6291 * and then add it to *pwaff with the given dom.
6292 * This domain is known to be disjoint from other domains
6293 * because of the way isl_basic_map_foreach_lexmax works.
6295 static int update_dim_opt(__isl_take isl_basic_set *dom,
6296 __isl_take isl_aff_list *list, void *user)
6298 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6299 isl_aff *aff;
6300 isl_pw_aff **pwaff = user;
6301 isl_pw_aff *pwaff_i;
6303 if (!list)
6304 goto error;
6305 if (isl_aff_list_n_aff(list) != 1)
6306 isl_die(ctx, isl_error_internal,
6307 "expecting single element list", goto error);
6309 aff = isl_aff_list_get_aff(list, 0);
6310 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6312 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6314 isl_aff_list_free(list);
6316 return 0;
6317 error:
6318 isl_basic_set_free(dom);
6319 isl_aff_list_free(list);
6320 return -1;
6323 /* Given a basic map with one output dimension, compute the minimum or
6324 * maximum of that dimension as an isl_pw_aff.
6326 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6327 * call update_dim_opt on each leaf of the result.
6329 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6330 int max)
6332 isl_space *dim = isl_basic_map_get_space(bmap);
6333 isl_pw_aff *pwaff;
6334 int r;
6336 dim = isl_space_from_domain(isl_space_domain(dim));
6337 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6338 pwaff = isl_pw_aff_empty(dim);
6340 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6341 if (r < 0)
6342 return isl_pw_aff_free(pwaff);
6344 return pwaff;
6347 /* Compute the minimum or maximum of the given output dimension
6348 * as a function of the parameters and the input dimensions,
6349 * but independently of the other output dimensions.
6351 * We first project out the other output dimension and then compute
6352 * the "lexicographic" maximum in each basic map, combining the results
6353 * using isl_pw_aff_union_max.
6355 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6356 int max)
6358 int i;
6359 isl_pw_aff *pwaff;
6360 unsigned n_out;
6362 n_out = isl_map_dim(map, isl_dim_out);
6363 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6364 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6365 if (!map)
6366 return NULL;
6368 if (map->n == 0) {
6369 isl_space *dim = isl_map_get_space(map);
6370 isl_map_free(map);
6371 return isl_pw_aff_empty(dim);
6374 pwaff = basic_map_dim_opt(map->p[0], max);
6375 for (i = 1; i < map->n; ++i) {
6376 isl_pw_aff *pwaff_i;
6378 pwaff_i = basic_map_dim_opt(map->p[i], max);
6379 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6382 isl_map_free(map);
6384 return pwaff;
6387 /* Compute the maximum of the given output dimension as a function of the
6388 * parameters and input dimensions, but independently of
6389 * the other output dimensions.
6391 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6393 return map_dim_opt(map, pos, 1);
6396 /* Compute the minimum or maximum of the given set dimension
6397 * as a function of the parameters,
6398 * but independently of the other set dimensions.
6400 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6401 int max)
6403 return map_dim_opt(set, pos, max);
6406 /* Compute the maximum of the given set dimension as a function of the
6407 * parameters, but independently of the other set dimensions.
6409 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6411 return set_dim_opt(set, pos, 1);
6414 /* Compute the minimum of the given set dimension as a function of the
6415 * parameters, but independently of the other set dimensions.
6417 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6419 return set_dim_opt(set, pos, 0);
6422 /* Apply a preimage specified by "mat" on the parameters of "bset".
6423 * bset is assumed to have only parameters and divs.
6425 static struct isl_basic_set *basic_set_parameter_preimage(
6426 struct isl_basic_set *bset, struct isl_mat *mat)
6428 unsigned nparam;
6430 if (!bset || !mat)
6431 goto error;
6433 bset->dim = isl_space_cow(bset->dim);
6434 if (!bset->dim)
6435 goto error;
6437 nparam = isl_basic_set_dim(bset, isl_dim_param);
6439 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6441 bset->dim->nparam = 0;
6442 bset->dim->n_out = nparam;
6443 bset = isl_basic_set_preimage(bset, mat);
6444 if (bset) {
6445 bset->dim->nparam = bset->dim->n_out;
6446 bset->dim->n_out = 0;
6448 return bset;
6449 error:
6450 isl_mat_free(mat);
6451 isl_basic_set_free(bset);
6452 return NULL;
6455 /* Apply a preimage specified by "mat" on the parameters of "set".
6456 * set is assumed to have only parameters and divs.
6458 static struct isl_set *set_parameter_preimage(
6459 struct isl_set *set, struct isl_mat *mat)
6461 isl_space *dim = NULL;
6462 unsigned nparam;
6464 if (!set || !mat)
6465 goto error;
6467 dim = isl_space_copy(set->dim);
6468 dim = isl_space_cow(dim);
6469 if (!dim)
6470 goto error;
6472 nparam = isl_set_dim(set, isl_dim_param);
6474 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6476 dim->nparam = 0;
6477 dim->n_out = nparam;
6478 isl_set_reset_space(set, dim);
6479 set = isl_set_preimage(set, mat);
6480 if (!set)
6481 goto error2;
6482 dim = isl_space_copy(set->dim);
6483 dim = isl_space_cow(dim);
6484 if (!dim)
6485 goto error2;
6486 dim->nparam = dim->n_out;
6487 dim->n_out = 0;
6488 isl_set_reset_space(set, dim);
6489 return set;
6490 error:
6491 isl_space_free(dim);
6492 isl_mat_free(mat);
6493 error2:
6494 isl_set_free(set);
6495 return NULL;
6498 /* Intersect the basic set "bset" with the affine space specified by the
6499 * equalities in "eq".
6501 static struct isl_basic_set *basic_set_append_equalities(
6502 struct isl_basic_set *bset, struct isl_mat *eq)
6504 int i, k;
6505 unsigned len;
6507 if (!bset || !eq)
6508 goto error;
6510 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6511 eq->n_row, 0);
6512 if (!bset)
6513 goto error;
6515 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6516 for (i = 0; i < eq->n_row; ++i) {
6517 k = isl_basic_set_alloc_equality(bset);
6518 if (k < 0)
6519 goto error;
6520 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6521 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6523 isl_mat_free(eq);
6525 bset = isl_basic_set_gauss(bset, NULL);
6526 bset = isl_basic_set_finalize(bset);
6528 return bset;
6529 error:
6530 isl_mat_free(eq);
6531 isl_basic_set_free(bset);
6532 return NULL;
6535 /* Intersect the set "set" with the affine space specified by the
6536 * equalities in "eq".
6538 static struct isl_set *set_append_equalities(struct isl_set *set,
6539 struct isl_mat *eq)
6541 int i;
6543 if (!set || !eq)
6544 goto error;
6546 for (i = 0; i < set->n; ++i) {
6547 set->p[i] = basic_set_append_equalities(set->p[i],
6548 isl_mat_copy(eq));
6549 if (!set->p[i])
6550 goto error;
6552 isl_mat_free(eq);
6553 return set;
6554 error:
6555 isl_mat_free(eq);
6556 isl_set_free(set);
6557 return NULL;
6560 /* Given a basic set "bset" that only involves parameters and existentially
6561 * quantified variables, return the index of the first equality
6562 * that only involves parameters. If there is no such equality then
6563 * return bset->n_eq.
6565 * This function assumes that isl_basic_set_gauss has been called on "bset".
6567 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6569 int i, j;
6570 unsigned nparam, n_div;
6572 if (!bset)
6573 return -1;
6575 nparam = isl_basic_set_dim(bset, isl_dim_param);
6576 n_div = isl_basic_set_dim(bset, isl_dim_div);
6578 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6579 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6580 ++i;
6583 return i;
6586 /* Compute an explicit representation for the existentially quantified
6587 * variables in "bset" by computing the "minimal value" of the set
6588 * variables. Since there are no set variables, the computation of
6589 * the minimal value essentially computes an explicit representation
6590 * of the non-empty part(s) of "bset".
6592 * The input only involves parameters and existentially quantified variables.
6593 * All equalities among parameters have been removed.
6595 * Since the existentially quantified variables in the result are in general
6596 * going to be different from those in the input, we first replace
6597 * them by the minimal number of variables based on their equalities.
6598 * This should simplify the parametric integer programming.
6600 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6602 isl_morph *morph1, *morph2;
6603 isl_set *set;
6604 unsigned n;
6606 if (!bset)
6607 return NULL;
6608 if (bset->n_eq == 0)
6609 return isl_basic_set_lexmin(bset);
6611 morph1 = isl_basic_set_parameter_compression(bset);
6612 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6613 bset = isl_basic_set_lift(bset);
6614 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6615 bset = isl_morph_basic_set(morph2, bset);
6616 n = isl_basic_set_dim(bset, isl_dim_set);
6617 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6619 set = isl_basic_set_lexmin(bset);
6621 set = isl_morph_set(isl_morph_inverse(morph1), set);
6623 return set;
6626 /* Project the given basic set onto its parameter domain, possibly introducing
6627 * new, explicit, existential variables in the constraints.
6628 * The input has parameters and (possibly implicit) existential variables.
6629 * The output has the same parameters, but only
6630 * explicit existentially quantified variables.
6632 * The actual projection is performed by pip, but pip doesn't seem
6633 * to like equalities very much, so we first remove the equalities
6634 * among the parameters by performing a variable compression on
6635 * the parameters. Afterward, an inverse transformation is performed
6636 * and the equalities among the parameters are inserted back in.
6638 * The variable compression on the parameters may uncover additional
6639 * equalities that were only implicit before. We therefore check
6640 * if there are any new parameter equalities in the result and
6641 * if so recurse. The removal of parameter equalities is required
6642 * for the parameter compression performed by base_compute_divs.
6644 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6646 int i;
6647 struct isl_mat *eq;
6648 struct isl_mat *T, *T2;
6649 struct isl_set *set;
6650 unsigned nparam;
6652 bset = isl_basic_set_cow(bset);
6653 if (!bset)
6654 return NULL;
6656 if (bset->n_eq == 0)
6657 return base_compute_divs(bset);
6659 bset = isl_basic_set_gauss(bset, NULL);
6660 if (!bset)
6661 return NULL;
6662 if (isl_basic_set_plain_is_empty(bset))
6663 return isl_set_from_basic_set(bset);
6665 i = first_parameter_equality(bset);
6666 if (i == bset->n_eq)
6667 return base_compute_divs(bset);
6669 nparam = isl_basic_set_dim(bset, isl_dim_param);
6670 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6671 0, 1 + nparam);
6672 eq = isl_mat_cow(eq);
6673 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6674 if (T && T->n_col == 0) {
6675 isl_mat_free(T);
6676 isl_mat_free(T2);
6677 isl_mat_free(eq);
6678 bset = isl_basic_set_set_to_empty(bset);
6679 return isl_set_from_basic_set(bset);
6681 bset = basic_set_parameter_preimage(bset, T);
6683 i = first_parameter_equality(bset);
6684 if (!bset)
6685 set = NULL;
6686 else if (i == bset->n_eq)
6687 set = base_compute_divs(bset);
6688 else
6689 set = parameter_compute_divs(bset);
6690 set = set_parameter_preimage(set, T2);
6691 set = set_append_equalities(set, eq);
6692 return set;
6695 /* Insert the divs from "ls" before those of "bmap".
6697 * The number of columns is not changed, which means that the last
6698 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6699 * The caller is responsible for removing the same number of dimensions
6700 * from the space of "bmap".
6702 static __isl_give isl_basic_map *insert_divs_from_local_space(
6703 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6705 int i;
6706 int n_div;
6707 int old_n_div;
6709 n_div = isl_local_space_dim(ls, isl_dim_div);
6710 if (n_div == 0)
6711 return bmap;
6713 old_n_div = bmap->n_div;
6714 bmap = insert_div_rows(bmap, n_div);
6715 if (!bmap)
6716 return NULL;
6718 for (i = 0; i < n_div; ++i) {
6719 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6720 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6723 return bmap;
6726 /* Replace the space of "bmap" by the space and divs of "ls".
6728 * If "ls" has any divs, then we simplify the result since we may
6729 * have discovered some additional equalities that could simplify
6730 * the div expressions.
6732 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6733 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6735 int n_div;
6737 bmap = isl_basic_map_cow(bmap);
6738 if (!bmap || !ls)
6739 goto error;
6741 n_div = isl_local_space_dim(ls, isl_dim_div);
6742 bmap = insert_divs_from_local_space(bmap, ls);
6743 if (!bmap)
6744 goto error;
6746 isl_space_free(bmap->dim);
6747 bmap->dim = isl_local_space_get_space(ls);
6748 if (!bmap->dim)
6749 goto error;
6751 isl_local_space_free(ls);
6752 if (n_div > 0)
6753 bmap = isl_basic_map_simplify(bmap);
6754 bmap = isl_basic_map_finalize(bmap);
6755 return bmap;
6756 error:
6757 isl_basic_map_free(bmap);
6758 isl_local_space_free(ls);
6759 return NULL;
6762 /* Replace the space of "map" by the space and divs of "ls".
6764 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6765 __isl_take isl_local_space *ls)
6767 int i;
6769 map = isl_map_cow(map);
6770 if (!map || !ls)
6771 goto error;
6773 for (i = 0; i < map->n; ++i) {
6774 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6775 isl_local_space_copy(ls));
6776 if (!map->p[i])
6777 goto error;
6779 isl_space_free(map->dim);
6780 map->dim = isl_local_space_get_space(ls);
6781 if (!map->dim)
6782 goto error;
6784 isl_local_space_free(ls);
6785 return map;
6786 error:
6787 isl_local_space_free(ls);
6788 isl_map_free(map);
6789 return NULL;
6792 /* Compute an explicit representation for the existentially
6793 * quantified variables for which do not know any explicit representation yet.
6795 * We first sort the existentially quantified variables so that the
6796 * existentially quantified variables for which we already have an explicit
6797 * representation are placed before those for which we do not.
6798 * The input dimensions, the output dimensions and the existentially
6799 * quantified variables for which we already have an explicit
6800 * representation are then turned into parameters.
6801 * compute_divs returns a map with the same parameters and
6802 * no input or output dimensions and the dimension specification
6803 * is reset to that of the input, including the existentially quantified
6804 * variables for which we already had an explicit representation.
6806 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6808 struct isl_basic_set *bset;
6809 struct isl_set *set;
6810 struct isl_map *map;
6811 isl_space *dim;
6812 isl_local_space *ls;
6813 unsigned nparam;
6814 unsigned n_in;
6815 unsigned n_out;
6816 unsigned n_known;
6817 int i;
6819 bmap = isl_basic_map_sort_divs(bmap);
6820 bmap = isl_basic_map_cow(bmap);
6821 if (!bmap)
6822 return NULL;
6824 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6825 if (isl_int_is_zero(bmap->div[n_known][0]))
6826 break;
6828 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6829 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6830 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6831 dim = isl_space_set_alloc(bmap->ctx,
6832 nparam + n_in + n_out + n_known, 0);
6833 if (!dim)
6834 goto error;
6836 ls = isl_basic_map_get_local_space(bmap);
6837 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6838 n_known, bmap->n_div - n_known);
6839 if (n_known > 0) {
6840 for (i = n_known; i < bmap->n_div; ++i)
6841 swap_div(bmap, i - n_known, i);
6842 bmap->n_div -= n_known;
6843 bmap->extra -= n_known;
6845 bmap = isl_basic_map_reset_space(bmap, dim);
6846 bset = (struct isl_basic_set *)bmap;
6848 set = parameter_compute_divs(bset);
6849 map = (struct isl_map *)set;
6850 map = replace_space_by_local_space(map, ls);
6852 return map;
6853 error:
6854 isl_basic_map_free(bmap);
6855 return NULL;
6858 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6860 int i;
6861 unsigned off;
6863 if (!bmap)
6864 return -1;
6866 off = isl_space_dim(bmap->dim, isl_dim_all);
6867 for (i = 0; i < bmap->n_div; ++i) {
6868 if (isl_int_is_zero(bmap->div[i][0]))
6869 return 0;
6870 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6871 return -1);
6873 return 1;
6876 static int map_divs_known(__isl_keep isl_map *map)
6878 int i;
6880 if (!map)
6881 return -1;
6883 for (i = 0; i < map->n; ++i) {
6884 int known = isl_basic_map_divs_known(map->p[i]);
6885 if (known <= 0)
6886 return known;
6889 return 1;
6892 /* If bmap contains any unknown divs, then compute explicit
6893 * expressions for them. However, this computation may be
6894 * quite expensive, so first try to remove divs that aren't
6895 * strictly needed.
6897 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6899 int known;
6900 struct isl_map *map;
6902 known = isl_basic_map_divs_known(bmap);
6903 if (known < 0)
6904 goto error;
6905 if (known)
6906 return isl_map_from_basic_map(bmap);
6908 bmap = isl_basic_map_drop_redundant_divs(bmap);
6910 known = isl_basic_map_divs_known(bmap);
6911 if (known < 0)
6912 goto error;
6913 if (known)
6914 return isl_map_from_basic_map(bmap);
6916 map = compute_divs(bmap);
6917 return map;
6918 error:
6919 isl_basic_map_free(bmap);
6920 return NULL;
6923 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6925 int i;
6926 int known;
6927 struct isl_map *res;
6929 if (!map)
6930 return NULL;
6931 if (map->n == 0)
6932 return map;
6934 known = map_divs_known(map);
6935 if (known < 0) {
6936 isl_map_free(map);
6937 return NULL;
6939 if (known)
6940 return map;
6942 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6943 for (i = 1 ; i < map->n; ++i) {
6944 struct isl_map *r2;
6945 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6946 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6947 res = isl_map_union_disjoint(res, r2);
6948 else
6949 res = isl_map_union(res, r2);
6951 isl_map_free(map);
6953 return res;
6956 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6958 return (struct isl_set *)
6959 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6962 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6964 return (struct isl_set *)
6965 isl_map_compute_divs((struct isl_map *)set);
6968 struct isl_set *isl_map_domain(struct isl_map *map)
6970 int i;
6971 struct isl_set *set;
6973 if (!map)
6974 goto error;
6976 map = isl_map_cow(map);
6977 if (!map)
6978 return NULL;
6980 set = (struct isl_set *)map;
6981 set->dim = isl_space_domain(set->dim);
6982 if (!set->dim)
6983 goto error;
6984 for (i = 0; i < map->n; ++i) {
6985 set->p[i] = isl_basic_map_domain(map->p[i]);
6986 if (!set->p[i])
6987 goto error;
6989 ISL_F_CLR(set, ISL_MAP_DISJOINT);
6990 ISL_F_CLR(set, ISL_SET_NORMALIZED);
6991 return set;
6992 error:
6993 isl_map_free(map);
6994 return NULL;
6997 /* Return the union of "map1" and "map2", where we assume for now that
6998 * "map1" and "map2" are disjoint. Note that the basic maps inside
6999 * "map1" or "map2" may not be disjoint from each other.
7000 * Also note that this function is also called from isl_map_union,
7001 * which takes care of handling the situation where "map1" and "map2"
7002 * may not be disjoint.
7004 * If one of the inputs is empty, we can simply return the other input.
7005 * Similarly, if one of the inputs is universal, then it is equal to the union.
7007 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7008 __isl_take isl_map *map2)
7010 int i;
7011 unsigned flags = 0;
7012 struct isl_map *map = NULL;
7013 int is_universe;
7015 if (!map1 || !map2)
7016 goto error;
7018 if (!isl_space_is_equal(map1->dim, map2->dim))
7019 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7020 "spaces don't match", goto error);
7022 if (map1->n == 0) {
7023 isl_map_free(map1);
7024 return map2;
7026 if (map2->n == 0) {
7027 isl_map_free(map2);
7028 return map1;
7031 is_universe = isl_map_plain_is_universe(map1);
7032 if (is_universe < 0)
7033 goto error;
7034 if (is_universe) {
7035 isl_map_free(map2);
7036 return map1;
7039 is_universe = isl_map_plain_is_universe(map2);
7040 if (is_universe < 0)
7041 goto error;
7042 if (is_universe) {
7043 isl_map_free(map1);
7044 return map2;
7047 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7048 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7049 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7051 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7052 map1->n + map2->n, flags);
7053 if (!map)
7054 goto error;
7055 for (i = 0; i < map1->n; ++i) {
7056 map = isl_map_add_basic_map(map,
7057 isl_basic_map_copy(map1->p[i]));
7058 if (!map)
7059 goto error;
7061 for (i = 0; i < map2->n; ++i) {
7062 map = isl_map_add_basic_map(map,
7063 isl_basic_map_copy(map2->p[i]));
7064 if (!map)
7065 goto error;
7067 isl_map_free(map1);
7068 isl_map_free(map2);
7069 return map;
7070 error:
7071 isl_map_free(map);
7072 isl_map_free(map1);
7073 isl_map_free(map2);
7074 return NULL;
7077 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7078 * guaranteed to be disjoint by the caller.
7080 * Note that this functions is called from within isl_map_make_disjoint,
7081 * so we have to be careful not to touch the constraints of the inputs
7082 * in any way.
7084 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7085 __isl_take isl_map *map2)
7087 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7090 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7091 * not be disjoint. The parameters are assumed to have been aligned.
7093 * We currently simply call map_union_disjoint, the internal operation
7094 * of which does not really depend on the inputs being disjoint.
7095 * If the result contains more than one basic map, then we clear
7096 * the disjoint flag since the result may contain basic maps from
7097 * both inputs and these are not guaranteed to be disjoint.
7099 * As a special case, if "map1" and "map2" are obviously equal,
7100 * then we simply return "map1".
7102 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7103 __isl_take isl_map *map2)
7105 int equal;
7107 if (!map1 || !map2)
7108 goto error;
7110 equal = isl_map_plain_is_equal(map1, map2);
7111 if (equal < 0)
7112 goto error;
7113 if (equal) {
7114 isl_map_free(map2);
7115 return map1;
7118 map1 = map_union_disjoint(map1, map2);
7119 if (!map1)
7120 return NULL;
7121 if (map1->n > 1)
7122 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7123 return map1;
7124 error:
7125 isl_map_free(map1);
7126 isl_map_free(map2);
7127 return NULL;
7130 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7131 * not be disjoint.
7133 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7134 __isl_take isl_map *map2)
7136 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7139 struct isl_set *isl_set_union_disjoint(
7140 struct isl_set *set1, struct isl_set *set2)
7142 return (struct isl_set *)
7143 isl_map_union_disjoint(
7144 (struct isl_map *)set1, (struct isl_map *)set2);
7147 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7149 return (struct isl_set *)
7150 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7153 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7154 * the results.
7156 * "map" and "set" are assumed to be compatible and non-NULL.
7158 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7159 __isl_take isl_set *set,
7160 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7161 __isl_take isl_basic_set *bset))
7163 unsigned flags = 0;
7164 struct isl_map *result;
7165 int i, j;
7167 if (isl_set_plain_is_universe(set)) {
7168 isl_set_free(set);
7169 return map;
7172 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7173 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7174 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7176 result = isl_map_alloc_space(isl_space_copy(map->dim),
7177 map->n * set->n, flags);
7178 for (i = 0; result && i < map->n; ++i)
7179 for (j = 0; j < set->n; ++j) {
7180 result = isl_map_add_basic_map(result,
7181 fn(isl_basic_map_copy(map->p[i]),
7182 isl_basic_set_copy(set->p[j])));
7183 if (!result)
7184 break;
7187 isl_map_free(map);
7188 isl_set_free(set);
7189 return result;
7192 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7193 __isl_take isl_set *set)
7195 if (!map || !set)
7196 goto error;
7198 if (!isl_map_compatible_range(map, set))
7199 isl_die(set->ctx, isl_error_invalid,
7200 "incompatible spaces", goto error);
7202 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7203 error:
7204 isl_map_free(map);
7205 isl_set_free(set);
7206 return NULL;
7209 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7210 __isl_take isl_set *set)
7212 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7215 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7216 __isl_take isl_set *set)
7218 if (!map || !set)
7219 goto error;
7221 if (!isl_map_compatible_domain(map, set))
7222 isl_die(set->ctx, isl_error_invalid,
7223 "incompatible spaces", goto error);
7225 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7226 error:
7227 isl_map_free(map);
7228 isl_set_free(set);
7229 return NULL;
7232 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7233 __isl_take isl_set *set)
7235 return isl_map_align_params_map_map_and(map, set,
7236 &map_intersect_domain);
7239 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7240 __isl_take isl_map *map2)
7242 if (!map1 || !map2)
7243 goto error;
7244 map1 = isl_map_reverse(map1);
7245 map1 = isl_map_apply_range(map1, map2);
7246 return isl_map_reverse(map1);
7247 error:
7248 isl_map_free(map1);
7249 isl_map_free(map2);
7250 return NULL;
7253 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7254 __isl_take isl_map *map2)
7256 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7259 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7260 __isl_take isl_map *map2)
7262 isl_space *dim_result;
7263 struct isl_map *result;
7264 int i, j;
7266 if (!map1 || !map2)
7267 goto error;
7269 dim_result = isl_space_join(isl_space_copy(map1->dim),
7270 isl_space_copy(map2->dim));
7272 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7273 if (!result)
7274 goto error;
7275 for (i = 0; i < map1->n; ++i)
7276 for (j = 0; j < map2->n; ++j) {
7277 result = isl_map_add_basic_map(result,
7278 isl_basic_map_apply_range(
7279 isl_basic_map_copy(map1->p[i]),
7280 isl_basic_map_copy(map2->p[j])));
7281 if (!result)
7282 goto error;
7284 isl_map_free(map1);
7285 isl_map_free(map2);
7286 if (result && result->n <= 1)
7287 ISL_F_SET(result, ISL_MAP_DISJOINT);
7288 return result;
7289 error:
7290 isl_map_free(map1);
7291 isl_map_free(map2);
7292 return NULL;
7295 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7296 __isl_take isl_map *map2)
7298 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7302 * returns range - domain
7304 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7306 isl_space *target_space;
7307 struct isl_basic_set *bset;
7308 unsigned dim;
7309 unsigned nparam;
7310 int i;
7312 if (!bmap)
7313 goto error;
7314 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7315 bmap->dim, isl_dim_out),
7316 goto error);
7317 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7318 dim = isl_basic_map_n_in(bmap);
7319 nparam = isl_basic_map_n_param(bmap);
7320 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7321 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7322 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7323 for (i = 0; i < dim; ++i) {
7324 int j = isl_basic_map_alloc_equality(bmap);
7325 if (j < 0) {
7326 bmap = isl_basic_map_free(bmap);
7327 break;
7329 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7330 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7331 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7332 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7334 bset = isl_basic_map_domain(bmap);
7335 bset = isl_basic_set_reset_space(bset, target_space);
7336 return bset;
7337 error:
7338 isl_basic_map_free(bmap);
7339 return NULL;
7343 * returns range - domain
7345 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7347 int i;
7348 isl_space *dim;
7349 struct isl_set *result;
7351 if (!map)
7352 return NULL;
7354 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7355 map->dim, isl_dim_out),
7356 goto error);
7357 dim = isl_map_get_space(map);
7358 dim = isl_space_domain(dim);
7359 result = isl_set_alloc_space(dim, map->n, 0);
7360 if (!result)
7361 goto error;
7362 for (i = 0; i < map->n; ++i)
7363 result = isl_set_add_basic_set(result,
7364 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7365 isl_map_free(map);
7366 return result;
7367 error:
7368 isl_map_free(map);
7369 return NULL;
7373 * returns [domain -> range] -> range - domain
7375 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7376 __isl_take isl_basic_map *bmap)
7378 int i, k;
7379 isl_space *dim;
7380 isl_basic_map *domain;
7381 int nparam, n;
7382 unsigned total;
7384 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7385 bmap->dim, isl_dim_out))
7386 isl_die(bmap->ctx, isl_error_invalid,
7387 "domain and range don't match", goto error);
7389 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7390 n = isl_basic_map_dim(bmap, isl_dim_in);
7392 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7393 domain = isl_basic_map_universe(dim);
7395 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7396 bmap = isl_basic_map_apply_range(bmap, domain);
7397 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7399 total = isl_basic_map_total_dim(bmap);
7401 for (i = 0; i < n; ++i) {
7402 k = isl_basic_map_alloc_equality(bmap);
7403 if (k < 0)
7404 goto error;
7405 isl_seq_clr(bmap->eq[k], 1 + total);
7406 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7407 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7408 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7411 bmap = isl_basic_map_gauss(bmap, NULL);
7412 return isl_basic_map_finalize(bmap);
7413 error:
7414 isl_basic_map_free(bmap);
7415 return NULL;
7419 * returns [domain -> range] -> range - domain
7421 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7423 int i;
7424 isl_space *domain_dim;
7426 if (!map)
7427 return NULL;
7429 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7430 map->dim, isl_dim_out))
7431 isl_die(map->ctx, isl_error_invalid,
7432 "domain and range don't match", goto error);
7434 map = isl_map_cow(map);
7435 if (!map)
7436 return NULL;
7438 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7439 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7440 map->dim = isl_space_join(map->dim, domain_dim);
7441 if (!map->dim)
7442 goto error;
7443 for (i = 0; i < map->n; ++i) {
7444 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7445 if (!map->p[i])
7446 goto error;
7448 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7449 return map;
7450 error:
7451 isl_map_free(map);
7452 return NULL;
7455 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7457 struct isl_basic_map *bmap;
7458 unsigned nparam;
7459 unsigned dim;
7460 int i;
7462 if (!dims)
7463 return NULL;
7465 nparam = dims->nparam;
7466 dim = dims->n_out;
7467 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7468 if (!bmap)
7469 goto error;
7471 for (i = 0; i < dim; ++i) {
7472 int j = isl_basic_map_alloc_equality(bmap);
7473 if (j < 0)
7474 goto error;
7475 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7476 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7477 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7479 return isl_basic_map_finalize(bmap);
7480 error:
7481 isl_basic_map_free(bmap);
7482 return NULL;
7485 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7487 if (!dim)
7488 return NULL;
7489 if (dim->n_in != dim->n_out)
7490 isl_die(dim->ctx, isl_error_invalid,
7491 "number of input and output dimensions needs to be "
7492 "the same", goto error);
7493 return basic_map_identity(dim);
7494 error:
7495 isl_space_free(dim);
7496 return NULL;
7499 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7501 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7504 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7506 isl_space *dim = isl_set_get_space(set);
7507 isl_map *id;
7508 id = isl_map_identity(isl_space_map_from_set(dim));
7509 return isl_map_intersect_range(id, set);
7512 /* Construct a basic set with all set dimensions having only non-negative
7513 * values.
7515 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7516 __isl_take isl_space *space)
7518 int i;
7519 unsigned nparam;
7520 unsigned dim;
7521 struct isl_basic_set *bset;
7523 if (!space)
7524 return NULL;
7525 nparam = space->nparam;
7526 dim = space->n_out;
7527 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7528 if (!bset)
7529 return NULL;
7530 for (i = 0; i < dim; ++i) {
7531 int k = isl_basic_set_alloc_inequality(bset);
7532 if (k < 0)
7533 goto error;
7534 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7535 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7537 return bset;
7538 error:
7539 isl_basic_set_free(bset);
7540 return NULL;
7543 /* Construct the half-space x_pos >= 0.
7545 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7546 int pos)
7548 int k;
7549 isl_basic_set *nonneg;
7551 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7552 k = isl_basic_set_alloc_inequality(nonneg);
7553 if (k < 0)
7554 goto error;
7555 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7556 isl_int_set_si(nonneg->ineq[k][pos], 1);
7558 return isl_basic_set_finalize(nonneg);
7559 error:
7560 isl_basic_set_free(nonneg);
7561 return NULL;
7564 /* Construct the half-space x_pos <= -1.
7566 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7568 int k;
7569 isl_basic_set *neg;
7571 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7572 k = isl_basic_set_alloc_inequality(neg);
7573 if (k < 0)
7574 goto error;
7575 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7576 isl_int_set_si(neg->ineq[k][0], -1);
7577 isl_int_set_si(neg->ineq[k][pos], -1);
7579 return isl_basic_set_finalize(neg);
7580 error:
7581 isl_basic_set_free(neg);
7582 return NULL;
7585 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7586 enum isl_dim_type type, unsigned first, unsigned n)
7588 int i;
7589 unsigned offset;
7590 isl_basic_set *nonneg;
7591 isl_basic_set *neg;
7593 if (!set)
7594 return NULL;
7595 if (n == 0)
7596 return set;
7598 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7600 offset = pos(set->dim, type);
7601 for (i = 0; i < n; ++i) {
7602 nonneg = nonneg_halfspace(isl_set_get_space(set),
7603 offset + first + i);
7604 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7606 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7609 return set;
7610 error:
7611 isl_set_free(set);
7612 return NULL;
7615 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7616 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7617 void *user)
7619 isl_set *half;
7621 if (!set)
7622 return -1;
7623 if (isl_set_plain_is_empty(set)) {
7624 isl_set_free(set);
7625 return 0;
7627 if (first == len)
7628 return fn(set, signs, user);
7630 signs[first] = 1;
7631 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7632 1 + first));
7633 half = isl_set_intersect(half, isl_set_copy(set));
7634 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7635 goto error;
7637 signs[first] = -1;
7638 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7639 1 + first));
7640 half = isl_set_intersect(half, set);
7641 return foreach_orthant(half, signs, first + 1, len, fn, user);
7642 error:
7643 isl_set_free(set);
7644 return -1;
7647 /* Call "fn" on the intersections of "set" with each of the orthants
7648 * (except for obviously empty intersections). The orthant is identified
7649 * by the signs array, with each entry having value 1 or -1 according
7650 * to the sign of the corresponding variable.
7652 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7653 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7654 void *user)
7656 unsigned nparam;
7657 unsigned nvar;
7658 int *signs;
7659 int r;
7661 if (!set)
7662 return -1;
7663 if (isl_set_plain_is_empty(set))
7664 return 0;
7666 nparam = isl_set_dim(set, isl_dim_param);
7667 nvar = isl_set_dim(set, isl_dim_set);
7669 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7671 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7672 fn, user);
7674 free(signs);
7676 return r;
7679 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7681 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7684 int isl_basic_map_is_subset(
7685 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7687 int is_subset;
7688 struct isl_map *map1;
7689 struct isl_map *map2;
7691 if (!bmap1 || !bmap2)
7692 return -1;
7694 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7695 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7697 is_subset = isl_map_is_subset(map1, map2);
7699 isl_map_free(map1);
7700 isl_map_free(map2);
7702 return is_subset;
7705 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7706 __isl_keep isl_basic_set *bset2)
7708 return isl_basic_map_is_subset(bset1, bset2);
7711 int isl_basic_map_is_equal(
7712 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7714 int is_subset;
7716 if (!bmap1 || !bmap2)
7717 return -1;
7718 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7719 if (is_subset != 1)
7720 return is_subset;
7721 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7722 return is_subset;
7725 int isl_basic_set_is_equal(
7726 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7728 return isl_basic_map_is_equal(
7729 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7732 int isl_map_is_empty(struct isl_map *map)
7734 int i;
7735 int is_empty;
7737 if (!map)
7738 return -1;
7739 for (i = 0; i < map->n; ++i) {
7740 is_empty = isl_basic_map_is_empty(map->p[i]);
7741 if (is_empty < 0)
7742 return -1;
7743 if (!is_empty)
7744 return 0;
7746 return 1;
7749 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7751 return map ? map->n == 0 : -1;
7754 int isl_set_plain_is_empty(struct isl_set *set)
7756 return set ? set->n == 0 : -1;
7759 int isl_set_is_empty(struct isl_set *set)
7761 return isl_map_is_empty((struct isl_map *)set);
7764 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7766 if (!map1 || !map2)
7767 return -1;
7769 return isl_space_is_equal(map1->dim, map2->dim);
7772 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7774 if (!set1 || !set2)
7775 return -1;
7777 return isl_space_is_equal(set1->dim, set2->dim);
7780 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7782 int is_subset;
7784 if (!map1 || !map2)
7785 return -1;
7786 is_subset = isl_map_is_subset(map1, map2);
7787 if (is_subset != 1)
7788 return is_subset;
7789 is_subset = isl_map_is_subset(map2, map1);
7790 return is_subset;
7793 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7795 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7798 int isl_basic_map_is_strict_subset(
7799 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7801 int is_subset;
7803 if (!bmap1 || !bmap2)
7804 return -1;
7805 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7806 if (is_subset != 1)
7807 return is_subset;
7808 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7809 if (is_subset == -1)
7810 return is_subset;
7811 return !is_subset;
7814 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7816 int is_subset;
7818 if (!map1 || !map2)
7819 return -1;
7820 is_subset = isl_map_is_subset(map1, map2);
7821 if (is_subset != 1)
7822 return is_subset;
7823 is_subset = isl_map_is_subset(map2, map1);
7824 if (is_subset == -1)
7825 return is_subset;
7826 return !is_subset;
7829 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7831 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7834 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7836 if (!bmap)
7837 return -1;
7838 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7841 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7843 if (!bset)
7844 return -1;
7845 return bset->n_eq == 0 && bset->n_ineq == 0;
7848 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7850 int i;
7852 if (!map)
7853 return -1;
7855 for (i = 0; i < map->n; ++i) {
7856 int r = isl_basic_map_is_universe(map->p[i]);
7857 if (r < 0 || r)
7858 return r;
7861 return 0;
7864 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7866 return isl_map_plain_is_universe((isl_map *) set);
7869 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7871 struct isl_basic_set *bset = NULL;
7872 struct isl_vec *sample = NULL;
7873 int empty;
7874 unsigned total;
7876 if (!bmap)
7877 return -1;
7879 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7880 return 1;
7882 if (isl_basic_map_is_universe(bmap))
7883 return 0;
7885 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7886 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7887 copy = isl_basic_map_remove_redundancies(copy);
7888 empty = isl_basic_map_plain_is_empty(copy);
7889 isl_basic_map_free(copy);
7890 return empty;
7893 total = 1 + isl_basic_map_total_dim(bmap);
7894 if (bmap->sample && bmap->sample->size == total) {
7895 int contains = isl_basic_map_contains(bmap, bmap->sample);
7896 if (contains < 0)
7897 return -1;
7898 if (contains)
7899 return 0;
7901 isl_vec_free(bmap->sample);
7902 bmap->sample = NULL;
7903 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7904 if (!bset)
7905 return -1;
7906 sample = isl_basic_set_sample_vec(bset);
7907 if (!sample)
7908 return -1;
7909 empty = sample->size == 0;
7910 isl_vec_free(bmap->sample);
7911 bmap->sample = sample;
7912 if (empty)
7913 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7915 return empty;
7918 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7920 if (!bmap)
7921 return -1;
7922 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7925 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7927 if (!bset)
7928 return -1;
7929 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7932 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7934 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7937 struct isl_map *isl_basic_map_union(
7938 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7940 struct isl_map *map;
7941 if (!bmap1 || !bmap2)
7942 goto error;
7944 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7946 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7947 if (!map)
7948 goto error;
7949 map = isl_map_add_basic_map(map, bmap1);
7950 map = isl_map_add_basic_map(map, bmap2);
7951 return map;
7952 error:
7953 isl_basic_map_free(bmap1);
7954 isl_basic_map_free(bmap2);
7955 return NULL;
7958 struct isl_set *isl_basic_set_union(
7959 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7961 return (struct isl_set *)isl_basic_map_union(
7962 (struct isl_basic_map *)bset1,
7963 (struct isl_basic_map *)bset2);
7966 /* Order divs such that any div only depends on previous divs */
7967 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7969 int i;
7970 unsigned off;
7972 if (!bmap)
7973 return NULL;
7975 off = isl_space_dim(bmap->dim, isl_dim_all);
7977 for (i = 0; i < bmap->n_div; ++i) {
7978 int pos;
7979 if (isl_int_is_zero(bmap->div[i][0]))
7980 continue;
7981 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7982 bmap->n_div-i);
7983 if (pos == -1)
7984 continue;
7985 isl_basic_map_swap_div(bmap, i, i + pos);
7986 --i;
7988 return bmap;
7991 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7993 return (struct isl_basic_set *)
7994 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7997 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7999 int i;
8001 if (!map)
8002 return 0;
8004 for (i = 0; i < map->n; ++i) {
8005 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8006 if (!map->p[i])
8007 goto error;
8010 return map;
8011 error:
8012 isl_map_free(map);
8013 return NULL;
8016 /* Apply the expansion computed by isl_merge_divs.
8017 * The expansion itself is given by "exp" while the resulting
8018 * list of divs is given by "div".
8020 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8021 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8023 int i, j;
8024 int n_div;
8026 bset = isl_basic_set_cow(bset);
8027 if (!bset || !div)
8028 goto error;
8030 if (div->n_row < bset->n_div)
8031 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8032 "not an expansion", goto error);
8034 n_div = bset->n_div;
8035 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8036 div->n_row - n_div, 0,
8037 2 * (div->n_row - n_div));
8039 for (i = n_div; i < div->n_row; ++i)
8040 if (isl_basic_set_alloc_div(bset) < 0)
8041 goto error;
8043 j = n_div - 1;
8044 for (i = div->n_row - 1; i >= 0; --i) {
8045 if (j >= 0 && exp[j] == i) {
8046 if (i != j)
8047 isl_basic_map_swap_div(bset, i, j);
8048 j--;
8049 } else {
8050 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8051 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8052 goto error;
8056 isl_mat_free(div);
8057 return bset;
8058 error:
8059 isl_basic_set_free(bset);
8060 isl_mat_free(div);
8061 return NULL;
8064 /* Look for a div in dst that corresponds to the div "div" in src.
8065 * The divs before "div" in src and dst are assumed to be the same.
8067 * Returns -1 if no corresponding div was found and the position
8068 * of the corresponding div in dst otherwise.
8070 static int find_div(struct isl_basic_map *dst,
8071 struct isl_basic_map *src, unsigned div)
8073 int i;
8075 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8077 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8078 for (i = div; i < dst->n_div; ++i)
8079 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8080 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8081 dst->n_div - div) == -1)
8082 return i;
8083 return -1;
8086 /* Align the divs of "dst" to those of "src", adding divs from "src"
8087 * if needed. That is, make sure that the first src->n_div divs
8088 * of the result are equal to those of src.
8090 * The result is not finalized as by design it will have redundant
8091 * divs if any divs from "src" were copied.
8093 __isl_give isl_basic_map *isl_basic_map_align_divs(
8094 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8096 int i;
8097 int known, extended;
8098 unsigned total;
8100 if (!dst || !src)
8101 return isl_basic_map_free(dst);
8103 if (src->n_div == 0)
8104 return dst;
8106 known = isl_basic_map_divs_known(src);
8107 if (known < 0)
8108 return isl_basic_map_free(dst);
8109 if (!known)
8110 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8111 "some src divs are unknown",
8112 return isl_basic_map_free(dst));
8114 src = isl_basic_map_order_divs(src);
8116 extended = 0;
8117 total = isl_space_dim(src->dim, isl_dim_all);
8118 for (i = 0; i < src->n_div; ++i) {
8119 int j = find_div(dst, src, i);
8120 if (j < 0) {
8121 if (!extended) {
8122 int extra = src->n_div - i;
8123 dst = isl_basic_map_cow(dst);
8124 if (!dst)
8125 return NULL;
8126 dst = isl_basic_map_extend_space(dst,
8127 isl_space_copy(dst->dim),
8128 extra, 0, 2 * extra);
8129 extended = 1;
8131 j = isl_basic_map_alloc_div(dst);
8132 if (j < 0)
8133 return isl_basic_map_free(dst);
8134 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8135 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8136 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8137 return isl_basic_map_free(dst);
8139 if (j != i)
8140 isl_basic_map_swap_div(dst, i, j);
8142 return dst;
8145 struct isl_basic_set *isl_basic_set_align_divs(
8146 struct isl_basic_set *dst, struct isl_basic_set *src)
8148 return (struct isl_basic_set *)isl_basic_map_align_divs(
8149 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8152 struct isl_map *isl_map_align_divs(struct isl_map *map)
8154 int i;
8156 if (!map)
8157 return NULL;
8158 if (map->n == 0)
8159 return map;
8160 map = isl_map_compute_divs(map);
8161 map = isl_map_cow(map);
8162 if (!map)
8163 return NULL;
8165 for (i = 1; i < map->n; ++i)
8166 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8167 for (i = 1; i < map->n; ++i) {
8168 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8169 if (!map->p[i])
8170 return isl_map_free(map);
8173 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8174 return map;
8177 struct isl_set *isl_set_align_divs(struct isl_set *set)
8179 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8182 /* Align the divs of the basic maps in "map" to those
8183 * of the basic maps in "list", as well as to the other basic maps in "map".
8184 * The elements in "list" are assumed to have known divs.
8186 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8187 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8189 int i, n;
8191 map = isl_map_compute_divs(map);
8192 map = isl_map_cow(map);
8193 if (!map || !list)
8194 return isl_map_free(map);
8195 if (map->n == 0)
8196 return map;
8198 n = isl_basic_map_list_n_basic_map(list);
8199 for (i = 0; i < n; ++i) {
8200 isl_basic_map *bmap;
8202 bmap = isl_basic_map_list_get_basic_map(list, i);
8203 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8204 isl_basic_map_free(bmap);
8206 if (!map->p[0])
8207 return isl_map_free(map);
8209 return isl_map_align_divs(map);
8212 /* Align the divs of each element of "list" to those of "bmap".
8213 * Both "bmap" and the elements of "list" are assumed to have known divs.
8215 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8216 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8218 int i, n;
8220 if (!list || !bmap)
8221 return isl_basic_map_list_free(list);
8223 n = isl_basic_map_list_n_basic_map(list);
8224 for (i = 0; i < n; ++i) {
8225 isl_basic_map *bmap_i;
8227 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8228 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8229 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8232 return list;
8235 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8236 __isl_take isl_map *map)
8238 if (!set || !map)
8239 goto error;
8240 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8241 map = isl_map_intersect_domain(map, set);
8242 set = isl_map_range(map);
8243 return set;
8244 error:
8245 isl_set_free(set);
8246 isl_map_free(map);
8247 return NULL;
8250 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8251 __isl_take isl_map *map)
8253 return isl_map_align_params_map_map_and(set, map, &set_apply);
8256 /* There is no need to cow as removing empty parts doesn't change
8257 * the meaning of the set.
8259 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8261 int i;
8263 if (!map)
8264 return NULL;
8266 for (i = map->n - 1; i >= 0; --i)
8267 remove_if_empty(map, i);
8269 return map;
8272 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8274 return (struct isl_set *)
8275 isl_map_remove_empty_parts((struct isl_map *)set);
8278 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8280 struct isl_basic_map *bmap;
8281 if (!map || map->n == 0)
8282 return NULL;
8283 bmap = map->p[map->n-1];
8284 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8285 return isl_basic_map_copy(bmap);
8288 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8290 return (struct isl_basic_set *)
8291 isl_map_copy_basic_map((struct isl_map *)set);
8294 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8295 __isl_keep isl_basic_map *bmap)
8297 int i;
8299 if (!map || !bmap)
8300 goto error;
8301 for (i = map->n-1; i >= 0; --i) {
8302 if (map->p[i] != bmap)
8303 continue;
8304 map = isl_map_cow(map);
8305 if (!map)
8306 goto error;
8307 isl_basic_map_free(map->p[i]);
8308 if (i != map->n-1) {
8309 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8310 map->p[i] = map->p[map->n-1];
8312 map->n--;
8313 return map;
8315 return map;
8316 error:
8317 isl_map_free(map);
8318 return NULL;
8321 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8322 struct isl_basic_set *bset)
8324 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8325 (struct isl_basic_map *)bset);
8328 /* Given two basic sets bset1 and bset2, compute the maximal difference
8329 * between the values of dimension pos in bset1 and those in bset2
8330 * for any common value of the parameters and dimensions preceding pos.
8332 static enum isl_lp_result basic_set_maximal_difference_at(
8333 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8334 int pos, isl_int *opt)
8336 isl_space *dims;
8337 struct isl_basic_map *bmap1 = NULL;
8338 struct isl_basic_map *bmap2 = NULL;
8339 struct isl_ctx *ctx;
8340 struct isl_vec *obj;
8341 unsigned total;
8342 unsigned nparam;
8343 unsigned dim1, dim2;
8344 enum isl_lp_result res;
8346 if (!bset1 || !bset2)
8347 return isl_lp_error;
8349 nparam = isl_basic_set_n_param(bset1);
8350 dim1 = isl_basic_set_n_dim(bset1);
8351 dim2 = isl_basic_set_n_dim(bset2);
8352 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8353 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8354 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8355 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8356 if (!bmap1 || !bmap2)
8357 goto error;
8358 bmap1 = isl_basic_map_cow(bmap1);
8359 bmap1 = isl_basic_map_extend(bmap1, nparam,
8360 pos, (dim1 - pos) + (dim2 - pos),
8361 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8362 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8363 if (!bmap1)
8364 goto error2;
8365 total = isl_basic_map_total_dim(bmap1);
8366 ctx = bmap1->ctx;
8367 obj = isl_vec_alloc(ctx, 1 + total);
8368 if (!obj)
8369 goto error2;
8370 isl_seq_clr(obj->block.data, 1 + total);
8371 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8372 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8373 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8374 opt, NULL, NULL);
8375 isl_basic_map_free(bmap1);
8376 isl_vec_free(obj);
8377 return res;
8378 error:
8379 isl_basic_map_free(bmap2);
8380 error2:
8381 isl_basic_map_free(bmap1);
8382 return isl_lp_error;
8385 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8386 * for any common value of the parameters and dimensions preceding pos
8387 * in both basic sets, the values of dimension pos in bset1 are
8388 * smaller or larger than those in bset2.
8390 * Returns
8391 * 1 if bset1 follows bset2
8392 * -1 if bset1 precedes bset2
8393 * 0 if bset1 and bset2 are incomparable
8394 * -2 if some error occurred.
8396 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8397 struct isl_basic_set *bset2, int pos)
8399 isl_int opt;
8400 enum isl_lp_result res;
8401 int cmp;
8403 isl_int_init(opt);
8405 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8407 if (res == isl_lp_empty)
8408 cmp = 0;
8409 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8410 res == isl_lp_unbounded)
8411 cmp = 1;
8412 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8413 cmp = -1;
8414 else
8415 cmp = -2;
8417 isl_int_clear(opt);
8418 return cmp;
8421 /* Given two basic sets bset1 and bset2, check whether
8422 * for any common value of the parameters and dimensions preceding pos
8423 * there is a value of dimension pos in bset1 that is larger
8424 * than a value of the same dimension in bset2.
8426 * Return
8427 * 1 if there exists such a pair
8428 * 0 if there is no such pair, but there is a pair of equal values
8429 * -1 otherwise
8430 * -2 if some error occurred.
8432 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8433 __isl_keep isl_basic_set *bset2, int pos)
8435 isl_int opt;
8436 enum isl_lp_result res;
8437 int cmp;
8439 isl_int_init(opt);
8441 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8443 if (res == isl_lp_empty)
8444 cmp = -1;
8445 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8446 res == isl_lp_unbounded)
8447 cmp = 1;
8448 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8449 cmp = -1;
8450 else if (res == isl_lp_ok)
8451 cmp = 0;
8452 else
8453 cmp = -2;
8455 isl_int_clear(opt);
8456 return cmp;
8459 /* Given two sets set1 and set2, check whether
8460 * for any common value of the parameters and dimensions preceding pos
8461 * there is a value of dimension pos in set1 that is larger
8462 * than a value of the same dimension in set2.
8464 * Return
8465 * 1 if there exists such a pair
8466 * 0 if there is no such pair, but there is a pair of equal values
8467 * -1 otherwise
8468 * -2 if some error occurred.
8470 int isl_set_follows_at(__isl_keep isl_set *set1,
8471 __isl_keep isl_set *set2, int pos)
8473 int i, j;
8474 int follows = -1;
8476 if (!set1 || !set2)
8477 return -2;
8479 for (i = 0; i < set1->n; ++i)
8480 for (j = 0; j < set2->n; ++j) {
8481 int f;
8482 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8483 if (f == 1 || f == -2)
8484 return f;
8485 if (f > follows)
8486 follows = f;
8489 return follows;
8492 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8493 unsigned pos, isl_int *val)
8495 int i;
8496 int d;
8497 unsigned total;
8499 if (!bmap)
8500 return -1;
8501 total = isl_basic_map_total_dim(bmap);
8502 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8503 for (; d+1 > pos; --d)
8504 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8505 break;
8506 if (d != pos)
8507 continue;
8508 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8509 return 0;
8510 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8511 return 0;
8512 if (!isl_int_is_one(bmap->eq[i][1+d]))
8513 return 0;
8514 if (val)
8515 isl_int_neg(*val, bmap->eq[i][0]);
8516 return 1;
8518 return 0;
8521 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8522 unsigned pos, isl_int *val)
8524 int i;
8525 isl_int v;
8526 isl_int tmp;
8527 int fixed;
8529 if (!map)
8530 return -1;
8531 if (map->n == 0)
8532 return 0;
8533 if (map->n == 1)
8534 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8535 isl_int_init(v);
8536 isl_int_init(tmp);
8537 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8538 for (i = 1; fixed == 1 && i < map->n; ++i) {
8539 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8540 if (fixed == 1 && isl_int_ne(tmp, v))
8541 fixed = 0;
8543 if (val)
8544 isl_int_set(*val, v);
8545 isl_int_clear(tmp);
8546 isl_int_clear(v);
8547 return fixed;
8550 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8551 unsigned pos, isl_int *val)
8553 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8554 pos, val);
8557 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8558 isl_int *val)
8560 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8563 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8564 enum isl_dim_type type, unsigned pos, isl_int *val)
8566 if (pos >= isl_basic_map_dim(bmap, type))
8567 return -1;
8568 return isl_basic_map_plain_has_fixed_var(bmap,
8569 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8572 /* If "bmap" obviously lies on a hyperplane where the given dimension
8573 * has a fixed value, then return that value.
8574 * Otherwise return NaN.
8576 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8577 __isl_keep isl_basic_map *bmap,
8578 enum isl_dim_type type, unsigned pos)
8580 isl_ctx *ctx;
8581 isl_val *v;
8582 int fixed;
8584 if (!bmap)
8585 return NULL;
8586 ctx = isl_basic_map_get_ctx(bmap);
8587 v = isl_val_alloc(ctx);
8588 if (!v)
8589 return NULL;
8590 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8591 if (fixed < 0)
8592 return isl_val_free(v);
8593 if (fixed) {
8594 isl_int_set_si(v->d, 1);
8595 return v;
8597 isl_val_free(v);
8598 return isl_val_nan(ctx);
8601 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8602 enum isl_dim_type type, unsigned pos, isl_int *val)
8604 if (pos >= isl_map_dim(map, type))
8605 return -1;
8606 return isl_map_plain_has_fixed_var(map,
8607 map_offset(map, type) - 1 + pos, val);
8610 /* If "map" obviously lies on a hyperplane where the given dimension
8611 * has a fixed value, then return that value.
8612 * Otherwise return NaN.
8614 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8615 enum isl_dim_type type, unsigned pos)
8617 isl_ctx *ctx;
8618 isl_val *v;
8619 int fixed;
8621 if (!map)
8622 return NULL;
8623 ctx = isl_map_get_ctx(map);
8624 v = isl_val_alloc(ctx);
8625 if (!v)
8626 return NULL;
8627 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8628 if (fixed < 0)
8629 return isl_val_free(v);
8630 if (fixed) {
8631 isl_int_set_si(v->d, 1);
8632 return v;
8634 isl_val_free(v);
8635 return isl_val_nan(ctx);
8638 /* If "set" obviously lies on a hyperplane where the given dimension
8639 * has a fixed value, then return that value.
8640 * Otherwise return NaN.
8642 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8643 enum isl_dim_type type, unsigned pos)
8645 return isl_map_plain_get_val_if_fixed(set, type, pos);
8648 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8649 enum isl_dim_type type, unsigned pos, isl_int *val)
8651 return isl_map_plain_is_fixed(set, type, pos, val);
8654 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8655 * then return this fixed value in *val.
8657 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8658 unsigned dim, isl_int *val)
8660 return isl_basic_set_plain_has_fixed_var(bset,
8661 isl_basic_set_n_param(bset) + dim, val);
8664 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8665 * then return this fixed value in *val.
8667 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8668 unsigned dim, isl_int *val)
8670 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8673 /* Check if input variable in has fixed value and if so and if val is not NULL,
8674 * then return this fixed value in *val.
8676 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8677 unsigned in, isl_int *val)
8679 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8682 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8683 * and if val is not NULL, then return this lower bound in *val.
8685 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8686 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8688 int i, i_eq = -1, i_ineq = -1;
8689 isl_int *c;
8690 unsigned total;
8691 unsigned nparam;
8693 if (!bset)
8694 return -1;
8695 total = isl_basic_set_total_dim(bset);
8696 nparam = isl_basic_set_n_param(bset);
8697 for (i = 0; i < bset->n_eq; ++i) {
8698 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8699 continue;
8700 if (i_eq != -1)
8701 return 0;
8702 i_eq = i;
8704 for (i = 0; i < bset->n_ineq; ++i) {
8705 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8706 continue;
8707 if (i_eq != -1 || i_ineq != -1)
8708 return 0;
8709 i_ineq = i;
8711 if (i_eq == -1 && i_ineq == -1)
8712 return 0;
8713 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8714 /* The coefficient should always be one due to normalization. */
8715 if (!isl_int_is_one(c[1+nparam+dim]))
8716 return 0;
8717 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8718 return 0;
8719 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8720 total - nparam - dim - 1) != -1)
8721 return 0;
8722 if (val)
8723 isl_int_neg(*val, c[0]);
8724 return 1;
8727 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8728 unsigned dim, isl_int *val)
8730 int i;
8731 isl_int v;
8732 isl_int tmp;
8733 int fixed;
8735 if (!set)
8736 return -1;
8737 if (set->n == 0)
8738 return 0;
8739 if (set->n == 1)
8740 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8741 dim, val);
8742 isl_int_init(v);
8743 isl_int_init(tmp);
8744 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8745 dim, &v);
8746 for (i = 1; fixed == 1 && i < set->n; ++i) {
8747 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8748 dim, &tmp);
8749 if (fixed == 1 && isl_int_ne(tmp, v))
8750 fixed = 0;
8752 if (val)
8753 isl_int_set(*val, v);
8754 isl_int_clear(tmp);
8755 isl_int_clear(v);
8756 return fixed;
8759 /* uset_gist depends on constraints without existentially quantified
8760 * variables sorting first.
8762 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8764 isl_int **c1 = (isl_int **) p1;
8765 isl_int **c2 = (isl_int **) p2;
8766 int l1, l2;
8767 unsigned size = *(unsigned *) arg;
8769 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8770 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8772 if (l1 != l2)
8773 return l1 - l2;
8775 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8778 static struct isl_basic_map *isl_basic_map_sort_constraints(
8779 struct isl_basic_map *bmap)
8781 unsigned total;
8783 if (!bmap)
8784 return NULL;
8785 if (bmap->n_ineq == 0)
8786 return bmap;
8787 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8788 return bmap;
8789 total = isl_basic_map_total_dim(bmap);
8790 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
8791 &sort_constraint_cmp, &total) < 0)
8792 return isl_basic_map_free(bmap);
8793 return bmap;
8796 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8797 __isl_take isl_basic_set *bset)
8799 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8800 (struct isl_basic_map *)bset);
8803 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8805 if (!bmap)
8806 return NULL;
8807 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8808 return bmap;
8809 bmap = isl_basic_map_remove_redundancies(bmap);
8810 bmap = isl_basic_map_sort_constraints(bmap);
8811 if (bmap)
8812 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8813 return bmap;
8816 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8818 return (struct isl_basic_set *)isl_basic_map_normalize(
8819 (struct isl_basic_map *)bset);
8822 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8823 const __isl_keep isl_basic_map *bmap2)
8825 int i, cmp;
8826 unsigned total;
8828 if (!bmap1 || !bmap2)
8829 return -1;
8831 if (bmap1 == bmap2)
8832 return 0;
8833 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8834 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8835 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8836 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8837 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8838 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8839 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8840 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8841 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8842 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8843 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8844 return 0;
8845 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8846 return 1;
8847 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8848 return -1;
8849 if (bmap1->n_eq != bmap2->n_eq)
8850 return bmap1->n_eq - bmap2->n_eq;
8851 if (bmap1->n_ineq != bmap2->n_ineq)
8852 return bmap1->n_ineq - bmap2->n_ineq;
8853 if (bmap1->n_div != bmap2->n_div)
8854 return bmap1->n_div - bmap2->n_div;
8855 total = isl_basic_map_total_dim(bmap1);
8856 for (i = 0; i < bmap1->n_eq; ++i) {
8857 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8858 if (cmp)
8859 return cmp;
8861 for (i = 0; i < bmap1->n_ineq; ++i) {
8862 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8863 if (cmp)
8864 return cmp;
8866 for (i = 0; i < bmap1->n_div; ++i) {
8867 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8868 if (cmp)
8869 return cmp;
8871 return 0;
8874 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8875 const __isl_keep isl_basic_set *bset2)
8877 return isl_basic_map_plain_cmp(bset1, bset2);
8880 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8882 int i, cmp;
8884 if (set1 == set2)
8885 return 0;
8886 if (set1->n != set2->n)
8887 return set1->n - set2->n;
8889 for (i = 0; i < set1->n; ++i) {
8890 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8891 if (cmp)
8892 return cmp;
8895 return 0;
8898 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8899 __isl_keep isl_basic_map *bmap2)
8901 if (!bmap1 || !bmap2)
8902 return -1;
8903 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8906 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8907 __isl_keep isl_basic_set *bset2)
8909 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8910 (isl_basic_map *)bset2);
8913 static int qsort_bmap_cmp(const void *p1, const void *p2)
8915 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8916 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8918 return isl_basic_map_plain_cmp(bmap1, bmap2);
8921 /* Sort the basic maps of "map" and remove duplicate basic maps.
8923 * While removing basic maps, we make sure that the basic maps remain
8924 * sorted because isl_map_normalize expects the basic maps of the result
8925 * to be sorted.
8927 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
8929 int i, j;
8931 map = isl_map_remove_empty_parts(map);
8932 if (!map)
8933 return NULL;
8934 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8935 for (i = map->n - 1; i >= 1; --i) {
8936 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
8937 continue;
8938 isl_basic_map_free(map->p[i-1]);
8939 for (j = i; j < map->n; ++j)
8940 map->p[j - 1] = map->p[j];
8941 map->n--;
8944 return map;
8947 /* Remove obvious duplicates among the basic maps of "map".
8949 * Unlike isl_map_normalize, this function does not remove redundant
8950 * constraints and only removes duplicates that have exactly the same
8951 * constraints in the input. It does sort the constraints and
8952 * the basic maps to ease the detection of duplicates.
8954 * If "map" has already been normalized or if the basic maps are
8955 * disjoint, then there can be no duplicates.
8957 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
8959 int i;
8960 isl_basic_map *bmap;
8962 if (!map)
8963 return NULL;
8964 if (map->n <= 1)
8965 return map;
8966 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
8967 return map;
8968 for (i = 0; i < map->n; ++i) {
8969 bmap = isl_basic_map_copy(map->p[i]);
8970 bmap = isl_basic_map_sort_constraints(bmap);
8971 if (!bmap)
8972 return isl_map_free(map);
8973 isl_basic_map_free(map->p[i]);
8974 map->p[i] = bmap;
8977 map = sort_and_remove_duplicates(map);
8978 return map;
8981 /* We normalize in place, but if anything goes wrong we need
8982 * to return NULL, so we need to make sure we don't change the
8983 * meaning of any possible other copies of map.
8985 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
8987 int i;
8988 struct isl_basic_map *bmap;
8990 if (!map)
8991 return NULL;
8992 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8993 return map;
8994 for (i = 0; i < map->n; ++i) {
8995 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8996 if (!bmap)
8997 goto error;
8998 isl_basic_map_free(map->p[i]);
8999 map->p[i] = bmap;
9002 map = sort_and_remove_duplicates(map);
9003 if (map)
9004 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9005 return map;
9006 error:
9007 isl_map_free(map);
9008 return NULL;
9011 struct isl_set *isl_set_normalize(struct isl_set *set)
9013 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9016 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9018 int i;
9019 int equal;
9021 if (!map1 || !map2)
9022 return -1;
9024 if (map1 == map2)
9025 return 1;
9026 if (!isl_space_is_equal(map1->dim, map2->dim))
9027 return 0;
9029 map1 = isl_map_copy(map1);
9030 map2 = isl_map_copy(map2);
9031 map1 = isl_map_normalize(map1);
9032 map2 = isl_map_normalize(map2);
9033 if (!map1 || !map2)
9034 goto error;
9035 equal = map1->n == map2->n;
9036 for (i = 0; equal && i < map1->n; ++i) {
9037 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9038 if (equal < 0)
9039 goto error;
9041 isl_map_free(map1);
9042 isl_map_free(map2);
9043 return equal;
9044 error:
9045 isl_map_free(map1);
9046 isl_map_free(map2);
9047 return -1;
9050 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9052 return isl_map_plain_is_equal((struct isl_map *)set1,
9053 (struct isl_map *)set2);
9056 /* Return an interval that ranges from min to max (inclusive)
9058 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9059 isl_int min, isl_int max)
9061 int k;
9062 struct isl_basic_set *bset = NULL;
9064 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9065 if (!bset)
9066 goto error;
9068 k = isl_basic_set_alloc_inequality(bset);
9069 if (k < 0)
9070 goto error;
9071 isl_int_set_si(bset->ineq[k][1], 1);
9072 isl_int_neg(bset->ineq[k][0], min);
9074 k = isl_basic_set_alloc_inequality(bset);
9075 if (k < 0)
9076 goto error;
9077 isl_int_set_si(bset->ineq[k][1], -1);
9078 isl_int_set(bset->ineq[k][0], max);
9080 return bset;
9081 error:
9082 isl_basic_set_free(bset);
9083 return NULL;
9086 /* Return the basic maps in "map" as a list.
9088 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9089 __isl_keep isl_map *map)
9091 int i;
9092 isl_ctx *ctx;
9093 isl_basic_map_list *list;
9095 if (!map)
9096 return NULL;
9097 ctx = isl_map_get_ctx(map);
9098 list = isl_basic_map_list_alloc(ctx, map->n);
9100 for (i = 0; i < map->n; ++i) {
9101 isl_basic_map *bmap;
9103 bmap = isl_basic_map_copy(map->p[i]);
9104 list = isl_basic_map_list_add(list, bmap);
9107 return list;
9110 /* Return the intersection of the elements in the non-empty list "list".
9111 * All elements are assumed to live in the same space.
9113 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9114 __isl_take isl_basic_map_list *list)
9116 int i, n;
9117 isl_basic_map *bmap;
9119 if (!list)
9120 return NULL;
9121 n = isl_basic_map_list_n_basic_map(list);
9122 if (n < 1)
9123 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9124 "expecting non-empty list", goto error);
9126 bmap = isl_basic_map_list_get_basic_map(list, 0);
9127 for (i = 1; i < n; ++i) {
9128 isl_basic_map *bmap_i;
9130 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9131 bmap = isl_basic_map_intersect(bmap, bmap_i);
9134 isl_basic_map_list_free(list);
9135 return bmap;
9136 error:
9137 isl_basic_map_list_free(list);
9138 return NULL;
9141 /* Return the intersection of the elements in the non-empty list "list".
9142 * All elements are assumed to live in the same space.
9144 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9145 __isl_take isl_basic_set_list *list)
9147 return isl_basic_map_list_intersect(list);
9150 /* Return the Cartesian product of the basic sets in list (in the given order).
9152 __isl_give isl_basic_set *isl_basic_set_list_product(
9153 __isl_take struct isl_basic_set_list *list)
9155 int i;
9156 unsigned dim;
9157 unsigned nparam;
9158 unsigned extra;
9159 unsigned n_eq;
9160 unsigned n_ineq;
9161 struct isl_basic_set *product = NULL;
9163 if (!list)
9164 goto error;
9165 isl_assert(list->ctx, list->n > 0, goto error);
9166 isl_assert(list->ctx, list->p[0], goto error);
9167 nparam = isl_basic_set_n_param(list->p[0]);
9168 dim = isl_basic_set_n_dim(list->p[0]);
9169 extra = list->p[0]->n_div;
9170 n_eq = list->p[0]->n_eq;
9171 n_ineq = list->p[0]->n_ineq;
9172 for (i = 1; i < list->n; ++i) {
9173 isl_assert(list->ctx, list->p[i], goto error);
9174 isl_assert(list->ctx,
9175 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9176 dim += isl_basic_set_n_dim(list->p[i]);
9177 extra += list->p[i]->n_div;
9178 n_eq += list->p[i]->n_eq;
9179 n_ineq += list->p[i]->n_ineq;
9181 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9182 n_eq, n_ineq);
9183 if (!product)
9184 goto error;
9185 dim = 0;
9186 for (i = 0; i < list->n; ++i) {
9187 isl_basic_set_add_constraints(product,
9188 isl_basic_set_copy(list->p[i]), dim);
9189 dim += isl_basic_set_n_dim(list->p[i]);
9191 isl_basic_set_list_free(list);
9192 return product;
9193 error:
9194 isl_basic_set_free(product);
9195 isl_basic_set_list_free(list);
9196 return NULL;
9199 struct isl_basic_map *isl_basic_map_product(
9200 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9202 isl_space *dim_result = NULL;
9203 struct isl_basic_map *bmap;
9204 unsigned in1, in2, out1, out2, nparam, total, pos;
9205 struct isl_dim_map *dim_map1, *dim_map2;
9207 if (!bmap1 || !bmap2)
9208 goto error;
9210 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9211 bmap2->dim, isl_dim_param), goto error);
9212 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9213 isl_space_copy(bmap2->dim));
9215 in1 = isl_basic_map_n_in(bmap1);
9216 in2 = isl_basic_map_n_in(bmap2);
9217 out1 = isl_basic_map_n_out(bmap1);
9218 out2 = isl_basic_map_n_out(bmap2);
9219 nparam = isl_basic_map_n_param(bmap1);
9221 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9222 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9223 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9224 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9225 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9226 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9227 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9228 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9229 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9230 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9231 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9233 bmap = isl_basic_map_alloc_space(dim_result,
9234 bmap1->n_div + bmap2->n_div,
9235 bmap1->n_eq + bmap2->n_eq,
9236 bmap1->n_ineq + bmap2->n_ineq);
9237 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9238 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9239 bmap = isl_basic_map_simplify(bmap);
9240 return isl_basic_map_finalize(bmap);
9241 error:
9242 isl_basic_map_free(bmap1);
9243 isl_basic_map_free(bmap2);
9244 return NULL;
9247 __isl_give isl_basic_map *isl_basic_map_flat_product(
9248 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9250 isl_basic_map *prod;
9252 prod = isl_basic_map_product(bmap1, bmap2);
9253 prod = isl_basic_map_flatten(prod);
9254 return prod;
9257 __isl_give isl_basic_set *isl_basic_set_flat_product(
9258 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9260 return isl_basic_map_flat_range_product(bset1, bset2);
9263 __isl_give isl_basic_map *isl_basic_map_domain_product(
9264 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9266 isl_space *space_result = NULL;
9267 isl_basic_map *bmap;
9268 unsigned in1, in2, out, nparam, total, pos;
9269 struct isl_dim_map *dim_map1, *dim_map2;
9271 if (!bmap1 || !bmap2)
9272 goto error;
9274 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9275 isl_space_copy(bmap2->dim));
9277 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9278 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9279 out = isl_basic_map_dim(bmap1, isl_dim_out);
9280 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9282 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9283 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9284 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9285 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9286 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9287 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9288 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9289 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9290 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9291 isl_dim_map_div(dim_map1, bmap1, pos += out);
9292 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9294 bmap = isl_basic_map_alloc_space(space_result,
9295 bmap1->n_div + bmap2->n_div,
9296 bmap1->n_eq + bmap2->n_eq,
9297 bmap1->n_ineq + bmap2->n_ineq);
9298 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9299 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9300 bmap = isl_basic_map_simplify(bmap);
9301 return isl_basic_map_finalize(bmap);
9302 error:
9303 isl_basic_map_free(bmap1);
9304 isl_basic_map_free(bmap2);
9305 return NULL;
9308 __isl_give isl_basic_map *isl_basic_map_range_product(
9309 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9311 isl_space *dim_result = NULL;
9312 isl_basic_map *bmap;
9313 unsigned in, out1, out2, nparam, total, pos;
9314 struct isl_dim_map *dim_map1, *dim_map2;
9316 if (!bmap1 || !bmap2)
9317 goto error;
9319 if (!isl_space_match(bmap1->dim, isl_dim_param,
9320 bmap2->dim, isl_dim_param))
9321 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9322 "parameters don't match", goto error);
9324 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9325 isl_space_copy(bmap2->dim));
9327 in = isl_basic_map_dim(bmap1, isl_dim_in);
9328 out1 = isl_basic_map_n_out(bmap1);
9329 out2 = isl_basic_map_n_out(bmap2);
9330 nparam = isl_basic_map_n_param(bmap1);
9332 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9333 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9334 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9335 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9336 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9337 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9338 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9339 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9340 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9341 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9342 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9344 bmap = isl_basic_map_alloc_space(dim_result,
9345 bmap1->n_div + bmap2->n_div,
9346 bmap1->n_eq + bmap2->n_eq,
9347 bmap1->n_ineq + bmap2->n_ineq);
9348 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9349 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9350 bmap = isl_basic_map_simplify(bmap);
9351 return isl_basic_map_finalize(bmap);
9352 error:
9353 isl_basic_map_free(bmap1);
9354 isl_basic_map_free(bmap2);
9355 return NULL;
9358 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9359 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9361 isl_basic_map *prod;
9363 prod = isl_basic_map_range_product(bmap1, bmap2);
9364 prod = isl_basic_map_flatten_range(prod);
9365 return prod;
9368 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9369 * and collect the results.
9370 * The result live in the space obtained by calling "space_product"
9371 * on the spaces of "map1" and "map2".
9372 * If "remove_duplicates" is set then the result may contain duplicates
9373 * (even if the inputs do not) and so we try and remove the obvious
9374 * duplicates.
9376 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9377 __isl_take isl_map *map2,
9378 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9379 __isl_take isl_space *right),
9380 __isl_give isl_basic_map *(*basic_map_product)(
9381 __isl_take isl_basic_map *left,
9382 __isl_take isl_basic_map *right),
9383 int remove_duplicates)
9385 unsigned flags = 0;
9386 struct isl_map *result;
9387 int i, j;
9389 if (!map1 || !map2)
9390 goto error;
9392 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9393 map2->dim, isl_dim_param), goto error);
9395 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9396 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9397 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9399 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9400 isl_space_copy(map2->dim)),
9401 map1->n * map2->n, flags);
9402 if (!result)
9403 goto error;
9404 for (i = 0; i < map1->n; ++i)
9405 for (j = 0; j < map2->n; ++j) {
9406 struct isl_basic_map *part;
9407 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9408 isl_basic_map_copy(map2->p[j]));
9409 if (isl_basic_map_is_empty(part))
9410 isl_basic_map_free(part);
9411 else
9412 result = isl_map_add_basic_map(result, part);
9413 if (!result)
9414 goto error;
9416 if (remove_duplicates)
9417 result = isl_map_remove_obvious_duplicates(result);
9418 isl_map_free(map1);
9419 isl_map_free(map2);
9420 return result;
9421 error:
9422 isl_map_free(map1);
9423 isl_map_free(map2);
9424 return NULL;
9427 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9429 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9430 __isl_take isl_map *map2)
9432 return map_product(map1, map2, &isl_space_product,
9433 &isl_basic_map_product, 0);
9436 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9437 __isl_take isl_map *map2)
9439 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9442 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9444 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9445 __isl_take isl_map *map2)
9447 isl_map *prod;
9449 prod = isl_map_product(map1, map2);
9450 prod = isl_map_flatten(prod);
9451 return prod;
9454 /* Given two set A and B, construct its Cartesian product A x B.
9456 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9458 return isl_map_range_product(set1, set2);
9461 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9462 __isl_take isl_set *set2)
9464 return isl_map_flat_range_product(set1, set2);
9467 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9469 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9470 __isl_take isl_map *map2)
9472 return map_product(map1, map2, &isl_space_domain_product,
9473 &isl_basic_map_domain_product, 1);
9476 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9478 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9479 __isl_take isl_map *map2)
9481 return map_product(map1, map2, &isl_space_range_product,
9482 &isl_basic_map_range_product, 1);
9485 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9486 __isl_take isl_map *map2)
9488 return isl_map_align_params_map_map_and(map1, map2,
9489 &map_domain_product_aligned);
9492 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9493 __isl_take isl_map *map2)
9495 return isl_map_align_params_map_map_and(map1, map2,
9496 &map_range_product_aligned);
9499 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9501 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9503 isl_space *space;
9504 int total1, keep1, total2, keep2;
9506 if (!map)
9507 return NULL;
9508 if (!isl_space_domain_is_wrapping(map->dim) ||
9509 !isl_space_range_is_wrapping(map->dim))
9510 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9511 "not a product", return isl_map_free(map));
9513 space = isl_map_get_space(map);
9514 total1 = isl_space_dim(space, isl_dim_in);
9515 total2 = isl_space_dim(space, isl_dim_out);
9516 space = isl_space_factor_domain(space);
9517 keep1 = isl_space_dim(space, isl_dim_in);
9518 keep2 = isl_space_dim(space, isl_dim_out);
9519 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9520 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9521 map = isl_map_reset_space(map, space);
9523 return map;
9526 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9528 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9530 isl_space *space;
9531 int total1, keep1, total2, keep2;
9533 if (!map)
9534 return NULL;
9535 if (!isl_space_domain_is_wrapping(map->dim) ||
9536 !isl_space_range_is_wrapping(map->dim))
9537 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9538 "not a product", return isl_map_free(map));
9540 space = isl_map_get_space(map);
9541 total1 = isl_space_dim(space, isl_dim_in);
9542 total2 = isl_space_dim(space, isl_dim_out);
9543 space = isl_space_factor_range(space);
9544 keep1 = isl_space_dim(space, isl_dim_in);
9545 keep2 = isl_space_dim(space, isl_dim_out);
9546 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9547 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9548 map = isl_map_reset_space(map, space);
9550 return map;
9553 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9555 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9557 isl_space *space;
9558 int total, keep;
9560 if (!map)
9561 return NULL;
9562 if (!isl_space_domain_is_wrapping(map->dim))
9563 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9564 "domain is not a product", return isl_map_free(map));
9566 space = isl_map_get_space(map);
9567 total = isl_space_dim(space, isl_dim_in);
9568 space = isl_space_domain_factor_domain(space);
9569 keep = isl_space_dim(space, isl_dim_in);
9570 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9571 map = isl_map_reset_space(map, space);
9573 return map;
9576 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9578 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9580 isl_space *space;
9581 int total, keep;
9583 if (!map)
9584 return NULL;
9585 if (!isl_space_domain_is_wrapping(map->dim))
9586 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9587 "domain is not a product", return isl_map_free(map));
9589 space = isl_map_get_space(map);
9590 total = isl_space_dim(space, isl_dim_in);
9591 space = isl_space_domain_factor_range(space);
9592 keep = isl_space_dim(space, isl_dim_in);
9593 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9594 map = isl_map_reset_space(map, space);
9596 return map;
9599 /* Given a map A -> [B -> C], extract the map A -> B.
9601 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9603 isl_space *space;
9604 int total, keep;
9606 if (!map)
9607 return NULL;
9608 if (!isl_space_range_is_wrapping(map->dim))
9609 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9610 "range is not a product", return isl_map_free(map));
9612 space = isl_map_get_space(map);
9613 total = isl_space_dim(space, isl_dim_out);
9614 space = isl_space_range_factor_domain(space);
9615 keep = isl_space_dim(space, isl_dim_out);
9616 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9617 map = isl_map_reset_space(map, space);
9619 return map;
9622 /* Given a map A -> [B -> C], extract the map A -> C.
9624 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9626 isl_space *space;
9627 int total, keep;
9629 if (!map)
9630 return NULL;
9631 if (!isl_space_range_is_wrapping(map->dim))
9632 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9633 "range is not a product", return isl_map_free(map));
9635 space = isl_map_get_space(map);
9636 total = isl_space_dim(space, isl_dim_out);
9637 space = isl_space_range_factor_range(space);
9638 keep = isl_space_dim(space, isl_dim_out);
9639 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9640 map = isl_map_reset_space(map, space);
9642 return map;
9645 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9647 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9648 __isl_take isl_map *map2)
9650 isl_map *prod;
9652 prod = isl_map_domain_product(map1, map2);
9653 prod = isl_map_flatten_domain(prod);
9654 return prod;
9657 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9659 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9660 __isl_take isl_map *map2)
9662 isl_map *prod;
9664 prod = isl_map_range_product(map1, map2);
9665 prod = isl_map_flatten_range(prod);
9666 return prod;
9669 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9671 int i;
9672 uint32_t hash = isl_hash_init();
9673 unsigned total;
9675 if (!bmap)
9676 return 0;
9677 bmap = isl_basic_map_copy(bmap);
9678 bmap = isl_basic_map_normalize(bmap);
9679 if (!bmap)
9680 return 0;
9681 total = isl_basic_map_total_dim(bmap);
9682 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9683 for (i = 0; i < bmap->n_eq; ++i) {
9684 uint32_t c_hash;
9685 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9686 isl_hash_hash(hash, c_hash);
9688 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9689 for (i = 0; i < bmap->n_ineq; ++i) {
9690 uint32_t c_hash;
9691 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9692 isl_hash_hash(hash, c_hash);
9694 isl_hash_byte(hash, bmap->n_div & 0xFF);
9695 for (i = 0; i < bmap->n_div; ++i) {
9696 uint32_t c_hash;
9697 if (isl_int_is_zero(bmap->div[i][0]))
9698 continue;
9699 isl_hash_byte(hash, i & 0xFF);
9700 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9701 isl_hash_hash(hash, c_hash);
9703 isl_basic_map_free(bmap);
9704 return hash;
9707 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9709 return isl_basic_map_get_hash((isl_basic_map *)bset);
9712 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9714 int i;
9715 uint32_t hash;
9717 if (!map)
9718 return 0;
9719 map = isl_map_copy(map);
9720 map = isl_map_normalize(map);
9721 if (!map)
9722 return 0;
9724 hash = isl_hash_init();
9725 for (i = 0; i < map->n; ++i) {
9726 uint32_t bmap_hash;
9727 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9728 isl_hash_hash(hash, bmap_hash);
9731 isl_map_free(map);
9733 return hash;
9736 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9738 return isl_map_get_hash((isl_map *)set);
9741 /* Check if the value for dimension dim is completely determined
9742 * by the values of the other parameters and variables.
9743 * That is, check if dimension dim is involved in an equality.
9745 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9747 int i;
9748 unsigned nparam;
9750 if (!bset)
9751 return -1;
9752 nparam = isl_basic_set_n_param(bset);
9753 for (i = 0; i < bset->n_eq; ++i)
9754 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9755 return 1;
9756 return 0;
9759 /* Check if the value for dimension dim is completely determined
9760 * by the values of the other parameters and variables.
9761 * That is, check if dimension dim is involved in an equality
9762 * for each of the subsets.
9764 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9766 int i;
9768 if (!set)
9769 return -1;
9770 for (i = 0; i < set->n; ++i) {
9771 int unique;
9772 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9773 if (unique != 1)
9774 return unique;
9776 return 1;
9779 /* Return the number of basic maps in the (current) representation of "map".
9781 int isl_map_n_basic_map(__isl_keep isl_map *map)
9783 return map ? map->n : 0;
9786 int isl_set_n_basic_set(__isl_keep isl_set *set)
9788 return set ? set->n : 0;
9791 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9792 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9794 int i;
9796 if (!map)
9797 return -1;
9799 for (i = 0; i < map->n; ++i)
9800 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9801 return -1;
9803 return 0;
9806 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9807 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9809 int i;
9811 if (!set)
9812 return -1;
9814 for (i = 0; i < set->n; ++i)
9815 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9816 return -1;
9818 return 0;
9821 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9823 isl_space *dim;
9825 if (!bset)
9826 return NULL;
9828 bset = isl_basic_set_cow(bset);
9829 if (!bset)
9830 return NULL;
9832 dim = isl_basic_set_get_space(bset);
9833 dim = isl_space_lift(dim, bset->n_div);
9834 if (!dim)
9835 goto error;
9836 isl_space_free(bset->dim);
9837 bset->dim = dim;
9838 bset->extra -= bset->n_div;
9839 bset->n_div = 0;
9841 bset = isl_basic_set_finalize(bset);
9843 return bset;
9844 error:
9845 isl_basic_set_free(bset);
9846 return NULL;
9849 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9851 int i;
9852 isl_space *dim;
9853 unsigned n_div;
9855 set = isl_set_align_divs(set);
9857 if (!set)
9858 return NULL;
9860 set = isl_set_cow(set);
9861 if (!set)
9862 return NULL;
9864 n_div = set->p[0]->n_div;
9865 dim = isl_set_get_space(set);
9866 dim = isl_space_lift(dim, n_div);
9867 if (!dim)
9868 goto error;
9869 isl_space_free(set->dim);
9870 set->dim = dim;
9872 for (i = 0; i < set->n; ++i) {
9873 set->p[i] = isl_basic_set_lift(set->p[i]);
9874 if (!set->p[i])
9875 goto error;
9878 return set;
9879 error:
9880 isl_set_free(set);
9881 return NULL;
9884 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9886 isl_space *dim;
9887 struct isl_basic_map *bmap;
9888 unsigned n_set;
9889 unsigned n_div;
9890 unsigned n_param;
9891 unsigned total;
9892 int i, k, l;
9894 set = isl_set_align_divs(set);
9896 if (!set)
9897 return NULL;
9899 dim = isl_set_get_space(set);
9900 if (set->n == 0 || set->p[0]->n_div == 0) {
9901 isl_set_free(set);
9902 return isl_map_identity(isl_space_map_from_set(dim));
9905 n_div = set->p[0]->n_div;
9906 dim = isl_space_map_from_set(dim);
9907 n_param = isl_space_dim(dim, isl_dim_param);
9908 n_set = isl_space_dim(dim, isl_dim_in);
9909 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9910 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9911 for (i = 0; i < n_set; ++i)
9912 bmap = var_equal(bmap, i);
9914 total = n_param + n_set + n_set + n_div;
9915 for (i = 0; i < n_div; ++i) {
9916 k = isl_basic_map_alloc_inequality(bmap);
9917 if (k < 0)
9918 goto error;
9919 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9920 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9921 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9922 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9923 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9924 set->p[0]->div[i][0]);
9926 l = isl_basic_map_alloc_inequality(bmap);
9927 if (l < 0)
9928 goto error;
9929 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9930 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9931 set->p[0]->div[i][0]);
9932 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9935 isl_set_free(set);
9936 bmap = isl_basic_map_simplify(bmap);
9937 bmap = isl_basic_map_finalize(bmap);
9938 return isl_map_from_basic_map(bmap);
9939 error:
9940 isl_set_free(set);
9941 isl_basic_map_free(bmap);
9942 return NULL;
9945 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9947 unsigned dim;
9948 int size = 0;
9950 if (!bset)
9951 return -1;
9953 dim = isl_basic_set_total_dim(bset);
9954 size += bset->n_eq * (1 + dim);
9955 size += bset->n_ineq * (1 + dim);
9956 size += bset->n_div * (2 + dim);
9958 return size;
9961 int isl_set_size(__isl_keep isl_set *set)
9963 int i;
9964 int size = 0;
9966 if (!set)
9967 return -1;
9969 for (i = 0; i < set->n; ++i)
9970 size += isl_basic_set_size(set->p[i]);
9972 return size;
9975 /* Check if there is any lower bound (if lower == 0) and/or upper
9976 * bound (if upper == 0) on the specified dim.
9978 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9979 enum isl_dim_type type, unsigned pos, int lower, int upper)
9981 int i;
9983 if (!bmap)
9984 return -1;
9986 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9988 pos += isl_basic_map_offset(bmap, type);
9990 for (i = 0; i < bmap->n_div; ++i) {
9991 if (isl_int_is_zero(bmap->div[i][0]))
9992 continue;
9993 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9994 return 1;
9997 for (i = 0; i < bmap->n_eq; ++i)
9998 if (!isl_int_is_zero(bmap->eq[i][pos]))
9999 return 1;
10001 for (i = 0; i < bmap->n_ineq; ++i) {
10002 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10003 if (sgn > 0)
10004 lower = 1;
10005 if (sgn < 0)
10006 upper = 1;
10009 return lower && upper;
10012 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10013 enum isl_dim_type type, unsigned pos)
10015 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10018 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10019 enum isl_dim_type type, unsigned pos)
10021 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10024 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10025 enum isl_dim_type type, unsigned pos)
10027 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10030 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10031 enum isl_dim_type type, unsigned pos)
10033 int i;
10035 if (!map)
10036 return -1;
10038 for (i = 0; i < map->n; ++i) {
10039 int bounded;
10040 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10041 if (bounded < 0 || !bounded)
10042 return bounded;
10045 return 1;
10048 /* Return 1 if the specified dim is involved in both an upper bound
10049 * and a lower bound.
10051 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10052 enum isl_dim_type type, unsigned pos)
10054 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10057 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10059 static int has_any_bound(__isl_keep isl_map *map,
10060 enum isl_dim_type type, unsigned pos,
10061 int (*fn)(__isl_keep isl_basic_map *bmap,
10062 enum isl_dim_type type, unsigned pos))
10064 int i;
10066 if (!map)
10067 return -1;
10069 for (i = 0; i < map->n; ++i) {
10070 int bounded;
10071 bounded = fn(map->p[i], type, pos);
10072 if (bounded < 0 || bounded)
10073 return bounded;
10076 return 0;
10079 /* Return 1 if the specified dim is involved in any lower bound.
10081 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10082 enum isl_dim_type type, unsigned pos)
10084 return has_any_bound(set, type, pos,
10085 &isl_basic_map_dim_has_lower_bound);
10088 /* Return 1 if the specified dim is involved in any upper bound.
10090 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10091 enum isl_dim_type type, unsigned pos)
10093 return has_any_bound(set, type, pos,
10094 &isl_basic_map_dim_has_upper_bound);
10097 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10099 static int has_bound(__isl_keep isl_map *map,
10100 enum isl_dim_type type, unsigned pos,
10101 int (*fn)(__isl_keep isl_basic_map *bmap,
10102 enum isl_dim_type type, unsigned pos))
10104 int i;
10106 if (!map)
10107 return -1;
10109 for (i = 0; i < map->n; ++i) {
10110 int bounded;
10111 bounded = fn(map->p[i], type, pos);
10112 if (bounded < 0 || !bounded)
10113 return bounded;
10116 return 1;
10119 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10121 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10122 enum isl_dim_type type, unsigned pos)
10124 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10127 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10129 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10130 enum isl_dim_type type, unsigned pos)
10132 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10135 /* For each of the "n" variables starting at "first", determine
10136 * the sign of the variable and put the results in the first "n"
10137 * elements of the array "signs".
10138 * Sign
10139 * 1 means that the variable is non-negative
10140 * -1 means that the variable is non-positive
10141 * 0 means the variable attains both positive and negative values.
10143 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10144 unsigned first, unsigned n, int *signs)
10146 isl_vec *bound = NULL;
10147 struct isl_tab *tab = NULL;
10148 struct isl_tab_undo *snap;
10149 int i;
10151 if (!bset || !signs)
10152 return -1;
10154 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10155 tab = isl_tab_from_basic_set(bset, 0);
10156 if (!bound || !tab)
10157 goto error;
10159 isl_seq_clr(bound->el, bound->size);
10160 isl_int_set_si(bound->el[0], -1);
10162 snap = isl_tab_snap(tab);
10163 for (i = 0; i < n; ++i) {
10164 int empty;
10166 isl_int_set_si(bound->el[1 + first + i], -1);
10167 if (isl_tab_add_ineq(tab, bound->el) < 0)
10168 goto error;
10169 empty = tab->empty;
10170 isl_int_set_si(bound->el[1 + first + i], 0);
10171 if (isl_tab_rollback(tab, snap) < 0)
10172 goto error;
10174 if (empty) {
10175 signs[i] = 1;
10176 continue;
10179 isl_int_set_si(bound->el[1 + first + i], 1);
10180 if (isl_tab_add_ineq(tab, bound->el) < 0)
10181 goto error;
10182 empty = tab->empty;
10183 isl_int_set_si(bound->el[1 + first + i], 0);
10184 if (isl_tab_rollback(tab, snap) < 0)
10185 goto error;
10187 signs[i] = empty ? -1 : 0;
10190 isl_tab_free(tab);
10191 isl_vec_free(bound);
10192 return 0;
10193 error:
10194 isl_tab_free(tab);
10195 isl_vec_free(bound);
10196 return -1;
10199 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10200 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10202 if (!bset || !signs)
10203 return -1;
10204 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10205 return -1);
10207 first += pos(bset->dim, type) - 1;
10208 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10211 /* Is it possible for the integer division "div" to depend (possibly
10212 * indirectly) on any output dimensions?
10214 * If the div is undefined, then we conservatively assume that it
10215 * may depend on them.
10216 * Otherwise, we check if it actually depends on them or on any integer
10217 * divisions that may depend on them.
10219 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10221 int i;
10222 unsigned n_out, o_out;
10223 unsigned n_div, o_div;
10225 if (isl_int_is_zero(bmap->div[div][0]))
10226 return 1;
10228 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10229 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10231 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10232 return 1;
10234 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10235 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10237 for (i = 0; i < n_div; ++i) {
10238 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10239 continue;
10240 if (div_may_involve_output(bmap, i))
10241 return 1;
10244 return 0;
10247 /* Return the index of the equality of "bmap" that defines
10248 * the output dimension "pos" in terms of earlier dimensions.
10249 * The equality may also involve integer divisions, as long
10250 * as those integer divisions are defined in terms of
10251 * parameters or input dimensions.
10252 * Return bmap->n_eq if there is no such equality.
10253 * Return -1 on error.
10255 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10256 int pos)
10258 int j, k;
10259 unsigned n_out, o_out;
10260 unsigned n_div, o_div;
10262 if (!bmap)
10263 return -1;
10265 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10266 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10267 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10268 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10270 for (j = 0; j < bmap->n_eq; ++j) {
10271 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10272 continue;
10273 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10274 n_out - (pos + 1)) != -1)
10275 continue;
10276 for (k = 0; k < n_div; ++k) {
10277 if (isl_int_is_zero(bmap->eq[j][o_div + k]))
10278 continue;
10279 if (div_may_involve_output(bmap, k))
10280 break;
10282 if (k >= n_div)
10283 return j;
10286 return bmap->n_eq;
10289 /* Check if the given basic map is obviously single-valued.
10290 * In particular, for each output dimension, check that there is
10291 * an equality that defines the output dimension in terms of
10292 * earlier dimensions.
10294 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10296 int i;
10297 unsigned n_out;
10299 if (!bmap)
10300 return -1;
10302 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10304 for (i = 0; i < n_out; ++i) {
10305 int eq;
10307 eq = isl_basic_map_output_defining_equality(bmap, i);
10308 if (eq < 0)
10309 return -1;
10310 if (eq >= bmap->n_eq)
10311 return 0;
10314 return 1;
10317 /* Check if the given basic map is single-valued.
10318 * We simply compute
10320 * M \circ M^-1
10322 * and check if the result is a subset of the identity mapping.
10324 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10326 isl_space *space;
10327 isl_basic_map *test;
10328 isl_basic_map *id;
10329 int sv;
10331 sv = isl_basic_map_plain_is_single_valued(bmap);
10332 if (sv < 0 || sv)
10333 return sv;
10335 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10336 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10338 space = isl_basic_map_get_space(bmap);
10339 space = isl_space_map_from_set(isl_space_range(space));
10340 id = isl_basic_map_identity(space);
10342 sv = isl_basic_map_is_subset(test, id);
10344 isl_basic_map_free(test);
10345 isl_basic_map_free(id);
10347 return sv;
10350 /* Check if the given map is obviously single-valued.
10352 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10354 if (!map)
10355 return -1;
10356 if (map->n == 0)
10357 return 1;
10358 if (map->n >= 2)
10359 return 0;
10361 return isl_basic_map_plain_is_single_valued(map->p[0]);
10364 /* Check if the given map is single-valued.
10365 * We simply compute
10367 * M \circ M^-1
10369 * and check if the result is a subset of the identity mapping.
10371 int isl_map_is_single_valued(__isl_keep isl_map *map)
10373 isl_space *dim;
10374 isl_map *test;
10375 isl_map *id;
10376 int sv;
10378 sv = isl_map_plain_is_single_valued(map);
10379 if (sv < 0 || sv)
10380 return sv;
10382 test = isl_map_reverse(isl_map_copy(map));
10383 test = isl_map_apply_range(test, isl_map_copy(map));
10385 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10386 id = isl_map_identity(dim);
10388 sv = isl_map_is_subset(test, id);
10390 isl_map_free(test);
10391 isl_map_free(id);
10393 return sv;
10396 int isl_map_is_injective(__isl_keep isl_map *map)
10398 int in;
10400 map = isl_map_copy(map);
10401 map = isl_map_reverse(map);
10402 in = isl_map_is_single_valued(map);
10403 isl_map_free(map);
10405 return in;
10408 /* Check if the given map is obviously injective.
10410 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10412 int in;
10414 map = isl_map_copy(map);
10415 map = isl_map_reverse(map);
10416 in = isl_map_plain_is_single_valued(map);
10417 isl_map_free(map);
10419 return in;
10422 int isl_map_is_bijective(__isl_keep isl_map *map)
10424 int sv;
10426 sv = isl_map_is_single_valued(map);
10427 if (sv < 0 || !sv)
10428 return sv;
10430 return isl_map_is_injective(map);
10433 int isl_set_is_singleton(__isl_keep isl_set *set)
10435 return isl_map_is_single_valued((isl_map *)set);
10438 int isl_map_is_translation(__isl_keep isl_map *map)
10440 int ok;
10441 isl_set *delta;
10443 delta = isl_map_deltas(isl_map_copy(map));
10444 ok = isl_set_is_singleton(delta);
10445 isl_set_free(delta);
10447 return ok;
10450 static int unique(isl_int *p, unsigned pos, unsigned len)
10452 if (isl_seq_first_non_zero(p, pos) != -1)
10453 return 0;
10454 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10455 return 0;
10456 return 1;
10459 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10461 int i, j;
10462 unsigned nvar;
10463 unsigned ovar;
10465 if (!bset)
10466 return -1;
10468 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10469 return 0;
10471 nvar = isl_basic_set_dim(bset, isl_dim_set);
10472 ovar = isl_space_offset(bset->dim, isl_dim_set);
10473 for (j = 0; j < nvar; ++j) {
10474 int lower = 0, upper = 0;
10475 for (i = 0; i < bset->n_eq; ++i) {
10476 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10477 continue;
10478 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10479 return 0;
10480 break;
10482 if (i < bset->n_eq)
10483 continue;
10484 for (i = 0; i < bset->n_ineq; ++i) {
10485 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10486 continue;
10487 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10488 return 0;
10489 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10490 lower = 1;
10491 else
10492 upper = 1;
10494 if (!lower || !upper)
10495 return 0;
10498 return 1;
10501 int isl_set_is_box(__isl_keep isl_set *set)
10503 if (!set)
10504 return -1;
10505 if (set->n != 1)
10506 return 0;
10508 return isl_basic_set_is_box(set->p[0]);
10511 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10513 if (!bset)
10514 return -1;
10516 return isl_space_is_wrapping(bset->dim);
10519 int isl_set_is_wrapping(__isl_keep isl_set *set)
10521 if (!set)
10522 return -1;
10524 return isl_space_is_wrapping(set->dim);
10527 /* Is the domain of "map" a wrapped relation?
10529 int isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10531 if (!map)
10532 return -1;
10534 return isl_space_domain_is_wrapping(map->dim);
10537 /* Is the range of "map" a wrapped relation?
10539 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10541 if (!map)
10542 return -1;
10544 return isl_space_range_is_wrapping(map->dim);
10547 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10549 bmap = isl_basic_map_cow(bmap);
10550 if (!bmap)
10551 return NULL;
10553 bmap->dim = isl_space_wrap(bmap->dim);
10554 if (!bmap->dim)
10555 goto error;
10557 bmap = isl_basic_map_finalize(bmap);
10559 return (isl_basic_set *)bmap;
10560 error:
10561 isl_basic_map_free(bmap);
10562 return NULL;
10565 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10567 int i;
10569 map = isl_map_cow(map);
10570 if (!map)
10571 return NULL;
10573 for (i = 0; i < map->n; ++i) {
10574 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10575 if (!map->p[i])
10576 goto error;
10578 map->dim = isl_space_wrap(map->dim);
10579 if (!map->dim)
10580 goto error;
10582 return (isl_set *)map;
10583 error:
10584 isl_map_free(map);
10585 return NULL;
10588 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10590 bset = isl_basic_set_cow(bset);
10591 if (!bset)
10592 return NULL;
10594 bset->dim = isl_space_unwrap(bset->dim);
10595 if (!bset->dim)
10596 goto error;
10598 bset = isl_basic_set_finalize(bset);
10600 return (isl_basic_map *)bset;
10601 error:
10602 isl_basic_set_free(bset);
10603 return NULL;
10606 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10608 int i;
10610 if (!set)
10611 return NULL;
10613 if (!isl_set_is_wrapping(set))
10614 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10615 goto error);
10617 set = isl_set_cow(set);
10618 if (!set)
10619 return NULL;
10621 for (i = 0; i < set->n; ++i) {
10622 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10623 if (!set->p[i])
10624 goto error;
10627 set->dim = isl_space_unwrap(set->dim);
10628 if (!set->dim)
10629 goto error;
10631 return (isl_map *)set;
10632 error:
10633 isl_set_free(set);
10634 return NULL;
10637 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10638 enum isl_dim_type type)
10640 if (!bmap)
10641 return NULL;
10643 if (!isl_space_is_named_or_nested(bmap->dim, type))
10644 return bmap;
10646 bmap = isl_basic_map_cow(bmap);
10647 if (!bmap)
10648 return NULL;
10650 bmap->dim = isl_space_reset(bmap->dim, type);
10651 if (!bmap->dim)
10652 goto error;
10654 bmap = isl_basic_map_finalize(bmap);
10656 return bmap;
10657 error:
10658 isl_basic_map_free(bmap);
10659 return NULL;
10662 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10663 enum isl_dim_type type)
10665 int i;
10667 if (!map)
10668 return NULL;
10670 if (!isl_space_is_named_or_nested(map->dim, type))
10671 return map;
10673 map = isl_map_cow(map);
10674 if (!map)
10675 return NULL;
10677 for (i = 0; i < map->n; ++i) {
10678 map->p[i] = isl_basic_map_reset(map->p[i], type);
10679 if (!map->p[i])
10680 goto error;
10682 map->dim = isl_space_reset(map->dim, type);
10683 if (!map->dim)
10684 goto error;
10686 return map;
10687 error:
10688 isl_map_free(map);
10689 return NULL;
10692 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10694 if (!bmap)
10695 return NULL;
10697 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10698 return bmap;
10700 bmap = isl_basic_map_cow(bmap);
10701 if (!bmap)
10702 return NULL;
10704 bmap->dim = isl_space_flatten(bmap->dim);
10705 if (!bmap->dim)
10706 goto error;
10708 bmap = isl_basic_map_finalize(bmap);
10710 return bmap;
10711 error:
10712 isl_basic_map_free(bmap);
10713 return NULL;
10716 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10718 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10721 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10722 __isl_take isl_basic_map *bmap)
10724 if (!bmap)
10725 return NULL;
10727 if (!bmap->dim->nested[0])
10728 return bmap;
10730 bmap = isl_basic_map_cow(bmap);
10731 if (!bmap)
10732 return NULL;
10734 bmap->dim = isl_space_flatten_domain(bmap->dim);
10735 if (!bmap->dim)
10736 goto error;
10738 bmap = isl_basic_map_finalize(bmap);
10740 return bmap;
10741 error:
10742 isl_basic_map_free(bmap);
10743 return NULL;
10746 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10747 __isl_take isl_basic_map *bmap)
10749 if (!bmap)
10750 return NULL;
10752 if (!bmap->dim->nested[1])
10753 return bmap;
10755 bmap = isl_basic_map_cow(bmap);
10756 if (!bmap)
10757 return NULL;
10759 bmap->dim = isl_space_flatten_range(bmap->dim);
10760 if (!bmap->dim)
10761 goto error;
10763 bmap = isl_basic_map_finalize(bmap);
10765 return bmap;
10766 error:
10767 isl_basic_map_free(bmap);
10768 return NULL;
10771 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10773 int i;
10775 if (!map)
10776 return NULL;
10778 if (!map->dim->nested[0] && !map->dim->nested[1])
10779 return map;
10781 map = isl_map_cow(map);
10782 if (!map)
10783 return NULL;
10785 for (i = 0; i < map->n; ++i) {
10786 map->p[i] = isl_basic_map_flatten(map->p[i]);
10787 if (!map->p[i])
10788 goto error;
10790 map->dim = isl_space_flatten(map->dim);
10791 if (!map->dim)
10792 goto error;
10794 return map;
10795 error:
10796 isl_map_free(map);
10797 return NULL;
10800 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10802 return (isl_set *)isl_map_flatten((isl_map *)set);
10805 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10807 isl_space *dim, *flat_dim;
10808 isl_map *map;
10810 dim = isl_set_get_space(set);
10811 flat_dim = isl_space_flatten(isl_space_copy(dim));
10812 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10813 map = isl_map_intersect_domain(map, set);
10815 return map;
10818 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10820 int i;
10822 if (!map)
10823 return NULL;
10825 if (!map->dim->nested[0])
10826 return map;
10828 map = isl_map_cow(map);
10829 if (!map)
10830 return NULL;
10832 for (i = 0; i < map->n; ++i) {
10833 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10834 if (!map->p[i])
10835 goto error;
10837 map->dim = isl_space_flatten_domain(map->dim);
10838 if (!map->dim)
10839 goto error;
10841 return map;
10842 error:
10843 isl_map_free(map);
10844 return NULL;
10847 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10849 int i;
10851 if (!map)
10852 return NULL;
10854 if (!map->dim->nested[1])
10855 return map;
10857 map = isl_map_cow(map);
10858 if (!map)
10859 return NULL;
10861 for (i = 0; i < map->n; ++i) {
10862 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10863 if (!map->p[i])
10864 goto error;
10866 map->dim = isl_space_flatten_range(map->dim);
10867 if (!map->dim)
10868 goto error;
10870 return map;
10871 error:
10872 isl_map_free(map);
10873 return NULL;
10876 /* Reorder the dimensions of "bmap" according to the given dim_map
10877 * and set the dimension specification to "dim".
10879 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10880 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10882 isl_basic_map *res;
10883 unsigned flags;
10885 bmap = isl_basic_map_cow(bmap);
10886 if (!bmap || !dim || !dim_map)
10887 goto error;
10889 flags = bmap->flags;
10890 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10891 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10892 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10893 res = isl_basic_map_alloc_space(dim,
10894 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10895 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10896 if (res)
10897 res->flags = flags;
10898 res = isl_basic_map_finalize(res);
10899 return res;
10900 error:
10901 free(dim_map);
10902 isl_basic_map_free(bmap);
10903 isl_space_free(dim);
10904 return NULL;
10907 /* Reorder the dimensions of "map" according to given reordering.
10909 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10910 __isl_take isl_reordering *r)
10912 int i;
10913 struct isl_dim_map *dim_map;
10915 map = isl_map_cow(map);
10916 dim_map = isl_dim_map_from_reordering(r);
10917 if (!map || !r || !dim_map)
10918 goto error;
10920 for (i = 0; i < map->n; ++i) {
10921 struct isl_dim_map *dim_map_i;
10923 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10925 map->p[i] = isl_basic_map_realign(map->p[i],
10926 isl_space_copy(r->dim), dim_map_i);
10928 if (!map->p[i])
10929 goto error;
10932 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10934 isl_reordering_free(r);
10935 free(dim_map);
10936 return map;
10937 error:
10938 free(dim_map);
10939 isl_map_free(map);
10940 isl_reordering_free(r);
10941 return NULL;
10944 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10945 __isl_take isl_reordering *r)
10947 return (isl_set *)isl_map_realign((isl_map *)set, r);
10950 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10951 __isl_take isl_space *model)
10953 isl_ctx *ctx;
10955 if (!map || !model)
10956 goto error;
10958 ctx = isl_space_get_ctx(model);
10959 if (!isl_space_has_named_params(model))
10960 isl_die(ctx, isl_error_invalid,
10961 "model has unnamed parameters", goto error);
10962 if (!isl_space_has_named_params(map->dim))
10963 isl_die(ctx, isl_error_invalid,
10964 "relation has unnamed parameters", goto error);
10965 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10966 isl_reordering *exp;
10968 model = isl_space_drop_dims(model, isl_dim_in,
10969 0, isl_space_dim(model, isl_dim_in));
10970 model = isl_space_drop_dims(model, isl_dim_out,
10971 0, isl_space_dim(model, isl_dim_out));
10972 exp = isl_parameter_alignment_reordering(map->dim, model);
10973 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10974 map = isl_map_realign(map, exp);
10977 isl_space_free(model);
10978 return map;
10979 error:
10980 isl_space_free(model);
10981 isl_map_free(map);
10982 return NULL;
10985 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10986 __isl_take isl_space *model)
10988 return isl_map_align_params(set, model);
10991 /* Align the parameters of "bmap" to those of "model", introducing
10992 * additional parameters if needed.
10994 __isl_give isl_basic_map *isl_basic_map_align_params(
10995 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10997 isl_ctx *ctx;
10999 if (!bmap || !model)
11000 goto error;
11002 ctx = isl_space_get_ctx(model);
11003 if (!isl_space_has_named_params(model))
11004 isl_die(ctx, isl_error_invalid,
11005 "model has unnamed parameters", goto error);
11006 if (!isl_space_has_named_params(bmap->dim))
11007 isl_die(ctx, isl_error_invalid,
11008 "relation has unnamed parameters", goto error);
11009 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11010 isl_reordering *exp;
11011 struct isl_dim_map *dim_map;
11013 model = isl_space_drop_dims(model, isl_dim_in,
11014 0, isl_space_dim(model, isl_dim_in));
11015 model = isl_space_drop_dims(model, isl_dim_out,
11016 0, isl_space_dim(model, isl_dim_out));
11017 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11018 exp = isl_reordering_extend_space(exp,
11019 isl_basic_map_get_space(bmap));
11020 dim_map = isl_dim_map_from_reordering(exp);
11021 bmap = isl_basic_map_realign(bmap,
11022 exp ? isl_space_copy(exp->dim) : NULL,
11023 isl_dim_map_extend(dim_map, bmap));
11024 isl_reordering_free(exp);
11025 free(dim_map);
11028 isl_space_free(model);
11029 return bmap;
11030 error:
11031 isl_space_free(model);
11032 isl_basic_map_free(bmap);
11033 return NULL;
11036 /* Align the parameters of "bset" to those of "model", introducing
11037 * additional parameters if needed.
11039 __isl_give isl_basic_set *isl_basic_set_align_params(
11040 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11042 return isl_basic_map_align_params(bset, model);
11045 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11046 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11047 enum isl_dim_type c2, enum isl_dim_type c3,
11048 enum isl_dim_type c4, enum isl_dim_type c5)
11050 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11051 struct isl_mat *mat;
11052 int i, j, k;
11053 int pos;
11055 if (!bmap)
11056 return NULL;
11057 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11058 isl_basic_map_total_dim(bmap) + 1);
11059 if (!mat)
11060 return NULL;
11061 for (i = 0; i < bmap->n_eq; ++i)
11062 for (j = 0, pos = 0; j < 5; ++j) {
11063 int off = isl_basic_map_offset(bmap, c[j]);
11064 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11065 isl_int_set(mat->row[i][pos],
11066 bmap->eq[i][off + k]);
11067 ++pos;
11071 return mat;
11074 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11075 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11076 enum isl_dim_type c2, enum isl_dim_type c3,
11077 enum isl_dim_type c4, enum isl_dim_type c5)
11079 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11080 struct isl_mat *mat;
11081 int i, j, k;
11082 int pos;
11084 if (!bmap)
11085 return NULL;
11086 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11087 isl_basic_map_total_dim(bmap) + 1);
11088 if (!mat)
11089 return NULL;
11090 for (i = 0; i < bmap->n_ineq; ++i)
11091 for (j = 0, pos = 0; j < 5; ++j) {
11092 int off = isl_basic_map_offset(bmap, c[j]);
11093 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11094 isl_int_set(mat->row[i][pos],
11095 bmap->ineq[i][off + k]);
11096 ++pos;
11100 return mat;
11103 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11104 __isl_take isl_space *dim,
11105 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11106 enum isl_dim_type c2, enum isl_dim_type c3,
11107 enum isl_dim_type c4, enum isl_dim_type c5)
11109 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11110 isl_basic_map *bmap;
11111 unsigned total;
11112 unsigned extra;
11113 int i, j, k, l;
11114 int pos;
11116 if (!dim || !eq || !ineq)
11117 goto error;
11119 if (eq->n_col != ineq->n_col)
11120 isl_die(dim->ctx, isl_error_invalid,
11121 "equalities and inequalities matrices should have "
11122 "same number of columns", goto error);
11124 total = 1 + isl_space_dim(dim, isl_dim_all);
11126 if (eq->n_col < total)
11127 isl_die(dim->ctx, isl_error_invalid,
11128 "number of columns too small", goto error);
11130 extra = eq->n_col - total;
11132 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11133 eq->n_row, ineq->n_row);
11134 if (!bmap)
11135 goto error;
11136 for (i = 0; i < extra; ++i) {
11137 k = isl_basic_map_alloc_div(bmap);
11138 if (k < 0)
11139 goto error;
11140 isl_int_set_si(bmap->div[k][0], 0);
11142 for (i = 0; i < eq->n_row; ++i) {
11143 l = isl_basic_map_alloc_equality(bmap);
11144 if (l < 0)
11145 goto error;
11146 for (j = 0, pos = 0; j < 5; ++j) {
11147 int off = isl_basic_map_offset(bmap, c[j]);
11148 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11149 isl_int_set(bmap->eq[l][off + k],
11150 eq->row[i][pos]);
11151 ++pos;
11155 for (i = 0; i < ineq->n_row; ++i) {
11156 l = isl_basic_map_alloc_inequality(bmap);
11157 if (l < 0)
11158 goto error;
11159 for (j = 0, pos = 0; j < 5; ++j) {
11160 int off = isl_basic_map_offset(bmap, c[j]);
11161 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11162 isl_int_set(bmap->ineq[l][off + k],
11163 ineq->row[i][pos]);
11164 ++pos;
11169 isl_space_free(dim);
11170 isl_mat_free(eq);
11171 isl_mat_free(ineq);
11173 bmap = isl_basic_map_simplify(bmap);
11174 return isl_basic_map_finalize(bmap);
11175 error:
11176 isl_space_free(dim);
11177 isl_mat_free(eq);
11178 isl_mat_free(ineq);
11179 return NULL;
11182 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11183 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11184 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11186 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11187 c1, c2, c3, c4, isl_dim_in);
11190 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11191 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11192 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11194 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11195 c1, c2, c3, c4, isl_dim_in);
11198 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11199 __isl_take isl_space *dim,
11200 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11201 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11203 return (isl_basic_set*)
11204 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11205 c1, c2, c3, c4, isl_dim_in);
11208 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11210 if (!bmap)
11211 return -1;
11213 return isl_space_can_zip(bmap->dim);
11216 int isl_map_can_zip(__isl_keep isl_map *map)
11218 if (!map)
11219 return -1;
11221 return isl_space_can_zip(map->dim);
11224 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11225 * (A -> C) -> (B -> D).
11227 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11229 unsigned pos;
11230 unsigned n1;
11231 unsigned n2;
11233 if (!bmap)
11234 return NULL;
11236 if (!isl_basic_map_can_zip(bmap))
11237 isl_die(bmap->ctx, isl_error_invalid,
11238 "basic map cannot be zipped", goto error);
11239 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11240 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11241 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11242 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11243 bmap = isl_basic_map_cow(bmap);
11244 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11245 if (!bmap)
11246 return NULL;
11247 bmap->dim = isl_space_zip(bmap->dim);
11248 if (!bmap->dim)
11249 goto error;
11250 return bmap;
11251 error:
11252 isl_basic_map_free(bmap);
11253 return NULL;
11256 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11257 * (A -> C) -> (B -> D).
11259 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11261 int i;
11263 if (!map)
11264 return NULL;
11266 if (!isl_map_can_zip(map))
11267 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11268 goto error);
11270 map = isl_map_cow(map);
11271 if (!map)
11272 return NULL;
11274 for (i = 0; i < map->n; ++i) {
11275 map->p[i] = isl_basic_map_zip(map->p[i]);
11276 if (!map->p[i])
11277 goto error;
11280 map->dim = isl_space_zip(map->dim);
11281 if (!map->dim)
11282 goto error;
11284 return map;
11285 error:
11286 isl_map_free(map);
11287 return NULL;
11290 /* Can we apply isl_basic_map_curry to "bmap"?
11291 * That is, does it have a nested relation in its domain?
11293 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11295 if (!bmap)
11296 return -1;
11298 return isl_space_can_curry(bmap->dim);
11301 /* Can we apply isl_map_curry to "map"?
11302 * That is, does it have a nested relation in its domain?
11304 int isl_map_can_curry(__isl_keep isl_map *map)
11306 if (!map)
11307 return -1;
11309 return isl_space_can_curry(map->dim);
11312 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11313 * A -> (B -> C).
11315 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11318 if (!bmap)
11319 return NULL;
11321 if (!isl_basic_map_can_curry(bmap))
11322 isl_die(bmap->ctx, isl_error_invalid,
11323 "basic map cannot be curried", goto error);
11324 bmap = isl_basic_map_cow(bmap);
11325 if (!bmap)
11326 return NULL;
11327 bmap->dim = isl_space_curry(bmap->dim);
11328 if (!bmap->dim)
11329 goto error;
11330 return bmap;
11331 error:
11332 isl_basic_map_free(bmap);
11333 return NULL;
11336 /* Given a map (A -> B) -> C, return the corresponding map
11337 * A -> (B -> C).
11339 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11341 int i;
11343 if (!map)
11344 return NULL;
11346 if (!isl_map_can_curry(map))
11347 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11348 goto error);
11350 map = isl_map_cow(map);
11351 if (!map)
11352 return NULL;
11354 for (i = 0; i < map->n; ++i) {
11355 map->p[i] = isl_basic_map_curry(map->p[i]);
11356 if (!map->p[i])
11357 goto error;
11360 map->dim = isl_space_curry(map->dim);
11361 if (!map->dim)
11362 goto error;
11364 return map;
11365 error:
11366 isl_map_free(map);
11367 return NULL;
11370 /* Can we apply isl_basic_map_uncurry to "bmap"?
11371 * That is, does it have a nested relation in its domain?
11373 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11375 if (!bmap)
11376 return -1;
11378 return isl_space_can_uncurry(bmap->dim);
11381 /* Can we apply isl_map_uncurry to "map"?
11382 * That is, does it have a nested relation in its domain?
11384 int isl_map_can_uncurry(__isl_keep isl_map *map)
11386 if (!map)
11387 return -1;
11389 return isl_space_can_uncurry(map->dim);
11392 /* Given a basic map A -> (B -> C), return the corresponding basic map
11393 * (A -> B) -> C.
11395 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11398 if (!bmap)
11399 return NULL;
11401 if (!isl_basic_map_can_uncurry(bmap))
11402 isl_die(bmap->ctx, isl_error_invalid,
11403 "basic map cannot be uncurried",
11404 return isl_basic_map_free(bmap));
11405 bmap = isl_basic_map_cow(bmap);
11406 if (!bmap)
11407 return NULL;
11408 bmap->dim = isl_space_uncurry(bmap->dim);
11409 if (!bmap->dim)
11410 return isl_basic_map_free(bmap);
11411 return bmap;
11414 /* Given a map A -> (B -> C), return the corresponding map
11415 * (A -> B) -> C.
11417 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11419 int i;
11421 if (!map)
11422 return NULL;
11424 if (!isl_map_can_uncurry(map))
11425 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11426 return isl_map_free(map));
11428 map = isl_map_cow(map);
11429 if (!map)
11430 return NULL;
11432 for (i = 0; i < map->n; ++i) {
11433 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11434 if (!map->p[i])
11435 return isl_map_free(map);
11438 map->dim = isl_space_uncurry(map->dim);
11439 if (!map->dim)
11440 return isl_map_free(map);
11442 return map;
11445 /* Construct a basic map mapping the domain of the affine expression
11446 * to a one-dimensional range prescribed by the affine expression.
11448 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11450 int k;
11451 int pos;
11452 isl_local_space *ls;
11453 isl_basic_map *bmap;
11455 if (!aff)
11456 return NULL;
11458 ls = isl_aff_get_local_space(aff);
11459 bmap = isl_basic_map_from_local_space(ls);
11460 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11461 k = isl_basic_map_alloc_equality(bmap);
11462 if (k < 0)
11463 goto error;
11465 pos = isl_basic_map_offset(bmap, isl_dim_out);
11466 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11467 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11468 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11469 aff->v->size - (pos + 1));
11471 isl_aff_free(aff);
11472 bmap = isl_basic_map_finalize(bmap);
11473 return bmap;
11474 error:
11475 isl_aff_free(aff);
11476 isl_basic_map_free(bmap);
11477 return NULL;
11480 /* Construct a map mapping the domain of the affine expression
11481 * to a one-dimensional range prescribed by the affine expression.
11483 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11485 isl_basic_map *bmap;
11487 bmap = isl_basic_map_from_aff(aff);
11488 return isl_map_from_basic_map(bmap);
11491 /* Construct a basic map mapping the domain the multi-affine expression
11492 * to its range, with each dimension in the range equated to the
11493 * corresponding affine expression.
11495 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11496 __isl_take isl_multi_aff *maff)
11498 int i;
11499 isl_space *space;
11500 isl_basic_map *bmap;
11502 if (!maff)
11503 return NULL;
11505 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11506 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11507 "invalid space", goto error);
11509 space = isl_space_domain(isl_multi_aff_get_space(maff));
11510 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11512 for (i = 0; i < maff->n; ++i) {
11513 isl_aff *aff;
11514 isl_basic_map *bmap_i;
11516 aff = isl_aff_copy(maff->p[i]);
11517 bmap_i = isl_basic_map_from_aff(aff);
11519 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11522 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11524 isl_multi_aff_free(maff);
11525 return bmap;
11526 error:
11527 isl_multi_aff_free(maff);
11528 return NULL;
11531 /* Construct a map mapping the domain the multi-affine expression
11532 * to its range, with each dimension in the range equated to the
11533 * corresponding affine expression.
11535 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11537 isl_basic_map *bmap;
11539 bmap = isl_basic_map_from_multi_aff(maff);
11540 return isl_map_from_basic_map(bmap);
11543 /* Construct a basic map mapping a domain in the given space to
11544 * to an n-dimensional range, with n the number of elements in the list,
11545 * where each coordinate in the range is prescribed by the
11546 * corresponding affine expression.
11547 * The domains of all affine expressions in the list are assumed to match
11548 * domain_dim.
11550 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11551 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11553 int i;
11554 isl_space *dim;
11555 isl_basic_map *bmap;
11557 if (!list)
11558 return NULL;
11560 dim = isl_space_from_domain(domain_dim);
11561 bmap = isl_basic_map_universe(dim);
11563 for (i = 0; i < list->n; ++i) {
11564 isl_aff *aff;
11565 isl_basic_map *bmap_i;
11567 aff = isl_aff_copy(list->p[i]);
11568 bmap_i = isl_basic_map_from_aff(aff);
11570 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11573 isl_aff_list_free(list);
11574 return bmap;
11577 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11578 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11580 return isl_map_equate(set, type1, pos1, type2, pos2);
11583 /* Construct a basic map where the given dimensions are equal to each other.
11585 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11586 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11588 isl_basic_map *bmap = NULL;
11589 int i;
11591 if (!space)
11592 return NULL;
11594 if (pos1 >= isl_space_dim(space, type1))
11595 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11596 "index out of bounds", goto error);
11597 if (pos2 >= isl_space_dim(space, type2))
11598 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11599 "index out of bounds", goto error);
11601 if (type1 == type2 && pos1 == pos2)
11602 return isl_basic_map_universe(space);
11604 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11605 i = isl_basic_map_alloc_equality(bmap);
11606 if (i < 0)
11607 goto error;
11608 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11609 pos1 += isl_basic_map_offset(bmap, type1);
11610 pos2 += isl_basic_map_offset(bmap, type2);
11611 isl_int_set_si(bmap->eq[i][pos1], -1);
11612 isl_int_set_si(bmap->eq[i][pos2], 1);
11613 bmap = isl_basic_map_finalize(bmap);
11614 isl_space_free(space);
11615 return bmap;
11616 error:
11617 isl_space_free(space);
11618 isl_basic_map_free(bmap);
11619 return NULL;
11622 /* Add a constraint imposing that the given two dimensions are equal.
11624 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11625 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11627 isl_basic_map *eq;
11629 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11631 bmap = isl_basic_map_intersect(bmap, eq);
11633 return bmap;
11636 /* Add a constraint imposing that the given two dimensions are equal.
11638 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11639 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11641 isl_basic_map *bmap;
11643 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11645 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11647 return map;
11650 /* Add a constraint imposing that the given two dimensions have opposite values.
11652 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11653 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11655 isl_basic_map *bmap = NULL;
11656 int i;
11658 if (!map)
11659 return NULL;
11661 if (pos1 >= isl_map_dim(map, type1))
11662 isl_die(map->ctx, isl_error_invalid,
11663 "index out of bounds", goto error);
11664 if (pos2 >= isl_map_dim(map, type2))
11665 isl_die(map->ctx, isl_error_invalid,
11666 "index out of bounds", goto error);
11668 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11669 i = isl_basic_map_alloc_equality(bmap);
11670 if (i < 0)
11671 goto error;
11672 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11673 pos1 += isl_basic_map_offset(bmap, type1);
11674 pos2 += isl_basic_map_offset(bmap, type2);
11675 isl_int_set_si(bmap->eq[i][pos1], 1);
11676 isl_int_set_si(bmap->eq[i][pos2], 1);
11677 bmap = isl_basic_map_finalize(bmap);
11679 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11681 return map;
11682 error:
11683 isl_basic_map_free(bmap);
11684 isl_map_free(map);
11685 return NULL;
11688 /* Construct a constraint imposing that the value of the first dimension is
11689 * greater than or equal to that of the second.
11691 static __isl_give isl_constraint *constraint_order_ge(
11692 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11693 enum isl_dim_type type2, int pos2)
11695 isl_constraint *c;
11697 if (!space)
11698 return NULL;
11700 c = isl_inequality_alloc(isl_local_space_from_space(space));
11702 if (pos1 >= isl_constraint_dim(c, type1))
11703 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11704 "index out of bounds", return isl_constraint_free(c));
11705 if (pos2 >= isl_constraint_dim(c, type2))
11706 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11707 "index out of bounds", return isl_constraint_free(c));
11709 if (type1 == type2 && pos1 == pos2)
11710 return c;
11712 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11713 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11715 return c;
11718 /* Add a constraint imposing that the value of the first dimension is
11719 * greater than or equal to that of the second.
11721 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11722 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11724 isl_constraint *c;
11725 isl_space *space;
11727 if (type1 == type2 && pos1 == pos2)
11728 return bmap;
11729 space = isl_basic_map_get_space(bmap);
11730 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11731 bmap = isl_basic_map_add_constraint(bmap, c);
11733 return bmap;
11736 /* Add a constraint imposing that the value of the first dimension is
11737 * greater than or equal to that of the second.
11739 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11740 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11742 isl_constraint *c;
11743 isl_space *space;
11745 if (type1 == type2 && pos1 == pos2)
11746 return map;
11747 space = isl_map_get_space(map);
11748 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11749 map = isl_map_add_constraint(map, c);
11751 return map;
11754 /* Add a constraint imposing that the value of the first dimension is
11755 * less than or equal to that of the second.
11757 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11758 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11760 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11763 /* Construct a basic map where the value of the first dimension is
11764 * greater than that of the second.
11766 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11767 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11769 isl_basic_map *bmap = NULL;
11770 int i;
11772 if (!space)
11773 return NULL;
11775 if (pos1 >= isl_space_dim(space, type1))
11776 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11777 "index out of bounds", goto error);
11778 if (pos2 >= isl_space_dim(space, type2))
11779 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11780 "index out of bounds", goto error);
11782 if (type1 == type2 && pos1 == pos2)
11783 return isl_basic_map_empty(space);
11785 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11786 i = isl_basic_map_alloc_inequality(bmap);
11787 if (i < 0)
11788 return isl_basic_map_free(bmap);
11789 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11790 pos1 += isl_basic_map_offset(bmap, type1);
11791 pos2 += isl_basic_map_offset(bmap, type2);
11792 isl_int_set_si(bmap->ineq[i][pos1], 1);
11793 isl_int_set_si(bmap->ineq[i][pos2], -1);
11794 isl_int_set_si(bmap->ineq[i][0], -1);
11795 bmap = isl_basic_map_finalize(bmap);
11797 return bmap;
11798 error:
11799 isl_space_free(space);
11800 isl_basic_map_free(bmap);
11801 return NULL;
11804 /* Add a constraint imposing that the value of the first dimension is
11805 * greater than that of the second.
11807 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11808 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11810 isl_basic_map *gt;
11812 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11814 bmap = isl_basic_map_intersect(bmap, gt);
11816 return bmap;
11819 /* Add a constraint imposing that the value of the first dimension is
11820 * greater than that of the second.
11822 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11823 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11825 isl_basic_map *bmap;
11827 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11829 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11831 return map;
11834 /* Add a constraint imposing that the value of the first dimension is
11835 * smaller than that of the second.
11837 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11838 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11840 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11843 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11844 int pos)
11846 isl_aff *div;
11847 isl_local_space *ls;
11849 if (!bmap)
11850 return NULL;
11852 if (!isl_basic_map_divs_known(bmap))
11853 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11854 "some divs are unknown", return NULL);
11856 ls = isl_basic_map_get_local_space(bmap);
11857 div = isl_local_space_get_div(ls, pos);
11858 isl_local_space_free(ls);
11860 return div;
11863 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11864 int pos)
11866 return isl_basic_map_get_div(bset, pos);
11869 /* Plug in "subs" for dimension "type", "pos" of "bset".
11871 * Let i be the dimension to replace and let "subs" be of the form
11873 * f/d
11875 * Any integer division with a non-zero coefficient for i,
11877 * floor((a i + g)/m)
11879 * is replaced by
11881 * floor((a f + d g)/(m d))
11883 * Constraints of the form
11885 * a i + g
11887 * are replaced by
11889 * a f + d g
11891 * We currently require that "subs" is an integral expression.
11892 * Handling rational expressions may require us to add stride constraints
11893 * as we do in isl_basic_set_preimage_multi_aff.
11895 __isl_give isl_basic_set *isl_basic_set_substitute(
11896 __isl_take isl_basic_set *bset,
11897 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11899 int i;
11900 isl_int v;
11901 isl_ctx *ctx;
11903 if (bset && isl_basic_set_plain_is_empty(bset))
11904 return bset;
11906 bset = isl_basic_set_cow(bset);
11907 if (!bset || !subs)
11908 goto error;
11910 ctx = isl_basic_set_get_ctx(bset);
11911 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11912 isl_die(ctx, isl_error_invalid,
11913 "spaces don't match", goto error);
11914 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11915 isl_die(ctx, isl_error_unsupported,
11916 "cannot handle divs yet", goto error);
11917 if (!isl_int_is_one(subs->v->el[0]))
11918 isl_die(ctx, isl_error_invalid,
11919 "can only substitute integer expressions", goto error);
11921 pos += isl_basic_set_offset(bset, type);
11923 isl_int_init(v);
11925 for (i = 0; i < bset->n_eq; ++i) {
11926 if (isl_int_is_zero(bset->eq[i][pos]))
11927 continue;
11928 isl_int_set(v, bset->eq[i][pos]);
11929 isl_int_set_si(bset->eq[i][pos], 0);
11930 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11931 v, subs->v->el + 1, subs->v->size - 1);
11934 for (i = 0; i < bset->n_ineq; ++i) {
11935 if (isl_int_is_zero(bset->ineq[i][pos]))
11936 continue;
11937 isl_int_set(v, bset->ineq[i][pos]);
11938 isl_int_set_si(bset->ineq[i][pos], 0);
11939 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11940 v, subs->v->el + 1, subs->v->size - 1);
11943 for (i = 0; i < bset->n_div; ++i) {
11944 if (isl_int_is_zero(bset->div[i][1 + pos]))
11945 continue;
11946 isl_int_set(v, bset->div[i][1 + pos]);
11947 isl_int_set_si(bset->div[i][1 + pos], 0);
11948 isl_seq_combine(bset->div[i] + 1,
11949 subs->v->el[0], bset->div[i] + 1,
11950 v, subs->v->el + 1, subs->v->size - 1);
11951 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11954 isl_int_clear(v);
11956 bset = isl_basic_set_simplify(bset);
11957 return isl_basic_set_finalize(bset);
11958 error:
11959 isl_basic_set_free(bset);
11960 return NULL;
11963 /* Plug in "subs" for dimension "type", "pos" of "set".
11965 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11966 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11968 int i;
11970 if (set && isl_set_plain_is_empty(set))
11971 return set;
11973 set = isl_set_cow(set);
11974 if (!set || !subs)
11975 goto error;
11977 for (i = set->n - 1; i >= 0; --i) {
11978 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11979 if (remove_if_empty(set, i) < 0)
11980 goto error;
11983 return set;
11984 error:
11985 isl_set_free(set);
11986 return NULL;
11989 /* Check if the range of "ma" is compatible with the domain or range
11990 * (depending on "type") of "bmap".
11991 * Return -1 if anything is wrong.
11993 static int check_basic_map_compatible_range_multi_aff(
11994 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11995 __isl_keep isl_multi_aff *ma)
11997 int m;
11998 isl_space *ma_space;
12000 ma_space = isl_multi_aff_get_space(ma);
12002 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12003 if (m < 0)
12004 goto error;
12005 if (!m)
12006 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12007 "parameters don't match", goto error);
12008 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12009 if (m < 0)
12010 goto error;
12011 if (!m)
12012 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12013 "spaces don't match", goto error);
12015 isl_space_free(ma_space);
12016 return m;
12017 error:
12018 isl_space_free(ma_space);
12019 return -1;
12022 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12023 * coefficients before the transformed range of dimensions,
12024 * the "n_after" coefficients after the transformed range of dimensions
12025 * and the coefficients of the other divs in "bmap".
12027 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12028 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12030 int i;
12031 int n_param;
12032 int n_set;
12033 isl_local_space *ls;
12035 if (n_div == 0)
12036 return 0;
12038 ls = isl_aff_get_domain_local_space(ma->p[0]);
12039 if (!ls)
12040 return -1;
12042 n_param = isl_local_space_dim(ls, isl_dim_param);
12043 n_set = isl_local_space_dim(ls, isl_dim_set);
12044 for (i = 0; i < n_div; ++i) {
12045 int o_bmap = 0, o_ls = 0;
12047 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12048 o_bmap += 1 + 1 + n_param;
12049 o_ls += 1 + 1 + n_param;
12050 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12051 o_bmap += n_before;
12052 isl_seq_cpy(bmap->div[i] + o_bmap,
12053 ls->div->row[i] + o_ls, n_set);
12054 o_bmap += n_set;
12055 o_ls += n_set;
12056 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12057 o_bmap += n_after;
12058 isl_seq_cpy(bmap->div[i] + o_bmap,
12059 ls->div->row[i] + o_ls, n_div);
12060 o_bmap += n_div;
12061 o_ls += n_div;
12062 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12063 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12064 goto error;
12067 isl_local_space_free(ls);
12068 return 0;
12069 error:
12070 isl_local_space_free(ls);
12071 return -1;
12074 /* How many stride constraints does "ma" enforce?
12075 * That is, how many of the affine expressions have a denominator
12076 * different from one?
12078 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12080 int i;
12081 int strides = 0;
12083 for (i = 0; i < ma->n; ++i)
12084 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12085 strides++;
12087 return strides;
12090 /* For each affine expression in ma of the form
12092 * x_i = (f_i y + h_i)/m_i
12094 * with m_i different from one, add a constraint to "bmap"
12095 * of the form
12097 * f_i y + h_i = m_i alpha_i
12099 * with alpha_i an additional existentially quantified variable.
12101 static __isl_give isl_basic_map *add_ma_strides(
12102 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12103 int n_before, int n_after)
12105 int i, k;
12106 int div;
12107 int total;
12108 int n_param;
12109 int n_in;
12110 int n_div;
12112 total = isl_basic_map_total_dim(bmap);
12113 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12114 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12115 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12116 for (i = 0; i < ma->n; ++i) {
12117 int o_bmap = 0, o_ma = 1;
12119 if (isl_int_is_one(ma->p[i]->v->el[0]))
12120 continue;
12121 div = isl_basic_map_alloc_div(bmap);
12122 k = isl_basic_map_alloc_equality(bmap);
12123 if (div < 0 || k < 0)
12124 goto error;
12125 isl_int_set_si(bmap->div[div][0], 0);
12126 isl_seq_cpy(bmap->eq[k] + o_bmap,
12127 ma->p[i]->v->el + o_ma, 1 + n_param);
12128 o_bmap += 1 + n_param;
12129 o_ma += 1 + n_param;
12130 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12131 o_bmap += n_before;
12132 isl_seq_cpy(bmap->eq[k] + o_bmap,
12133 ma->p[i]->v->el + o_ma, n_in);
12134 o_bmap += n_in;
12135 o_ma += n_in;
12136 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12137 o_bmap += n_after;
12138 isl_seq_cpy(bmap->eq[k] + o_bmap,
12139 ma->p[i]->v->el + o_ma, n_div);
12140 o_bmap += n_div;
12141 o_ma += n_div;
12142 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12143 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12144 total++;
12147 return bmap;
12148 error:
12149 isl_basic_map_free(bmap);
12150 return NULL;
12153 /* Replace the domain or range space (depending on "type) of "space" by "set".
12155 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12156 enum isl_dim_type type, __isl_take isl_space *set)
12158 if (type == isl_dim_in) {
12159 space = isl_space_range(space);
12160 space = isl_space_map_from_domain_and_range(set, space);
12161 } else {
12162 space = isl_space_domain(space);
12163 space = isl_space_map_from_domain_and_range(space, set);
12166 return space;
12169 /* Compute the preimage of the domain or range (depending on "type")
12170 * of "bmap" under the function represented by "ma".
12171 * In other words, plug in "ma" in the domain or range of "bmap".
12172 * The result is a basic map that lives in the same space as "bmap"
12173 * except that the domain or range has been replaced by
12174 * the domain space of "ma".
12176 * If bmap is represented by
12178 * A(p) + S u + B x + T v + C(divs) >= 0,
12180 * where u and x are input and output dimensions if type == isl_dim_out
12181 * while x and v are input and output dimensions if type == isl_dim_in,
12182 * and ma is represented by
12184 * x = D(p) + F(y) + G(divs')
12186 * then the result is
12188 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12190 * The divs in the input set are similarly adjusted.
12191 * In particular
12193 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12195 * becomes
12197 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12198 * B_i G(divs') + c_i(divs))/n_i)
12200 * If bmap is not a rational map and if F(y) involves any denominators
12202 * x_i = (f_i y + h_i)/m_i
12204 * then additional constraints are added to ensure that we only
12205 * map back integer points. That is we enforce
12207 * f_i y + h_i = m_i alpha_i
12209 * with alpha_i an additional existentially quantified variable.
12211 * We first copy over the divs from "ma".
12212 * Then we add the modified constraints and divs from "bmap".
12213 * Finally, we add the stride constraints, if needed.
12215 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12216 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12217 __isl_take isl_multi_aff *ma)
12219 int i, k;
12220 isl_space *space;
12221 isl_basic_map *res = NULL;
12222 int n_before, n_after, n_div_bmap, n_div_ma;
12223 isl_int f, c1, c2, g;
12224 int rational, strides;
12226 isl_int_init(f);
12227 isl_int_init(c1);
12228 isl_int_init(c2);
12229 isl_int_init(g);
12231 ma = isl_multi_aff_align_divs(ma);
12232 if (!bmap || !ma)
12233 goto error;
12234 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12235 goto error;
12237 if (type == isl_dim_in) {
12238 n_before = 0;
12239 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12240 } else {
12241 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12242 n_after = 0;
12244 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12245 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12247 space = isl_multi_aff_get_domain_space(ma);
12248 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12249 rational = isl_basic_map_is_rational(bmap);
12250 strides = rational ? 0 : multi_aff_strides(ma);
12251 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12252 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12253 if (rational)
12254 res = isl_basic_map_set_rational(res);
12256 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12257 if (isl_basic_map_alloc_div(res) < 0)
12258 goto error;
12260 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12261 goto error;
12263 for (i = 0; i < bmap->n_eq; ++i) {
12264 k = isl_basic_map_alloc_equality(res);
12265 if (k < 0)
12266 goto error;
12267 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12268 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12271 for (i = 0; i < bmap->n_ineq; ++i) {
12272 k = isl_basic_map_alloc_inequality(res);
12273 if (k < 0)
12274 goto error;
12275 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12276 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12279 for (i = 0; i < bmap->n_div; ++i) {
12280 if (isl_int_is_zero(bmap->div[i][0])) {
12281 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12282 continue;
12284 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12285 n_before, n_after, n_div_ma, n_div_bmap,
12286 f, c1, c2, g, 1);
12289 if (strides)
12290 res = add_ma_strides(res, ma, n_before, n_after);
12292 isl_int_clear(f);
12293 isl_int_clear(c1);
12294 isl_int_clear(c2);
12295 isl_int_clear(g);
12296 isl_basic_map_free(bmap);
12297 isl_multi_aff_free(ma);
12298 res = isl_basic_set_simplify(res);
12299 return isl_basic_map_finalize(res);
12300 error:
12301 isl_int_clear(f);
12302 isl_int_clear(c1);
12303 isl_int_clear(c2);
12304 isl_int_clear(g);
12305 isl_basic_map_free(bmap);
12306 isl_multi_aff_free(ma);
12307 isl_basic_map_free(res);
12308 return NULL;
12311 /* Compute the preimage of "bset" under the function represented by "ma".
12312 * In other words, plug in "ma" in "bset". The result is a basic set
12313 * that lives in the domain space of "ma".
12315 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12316 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12318 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12321 /* Compute the preimage of the domain of "bmap" under the function
12322 * represented by "ma".
12323 * In other words, plug in "ma" in the domain of "bmap".
12324 * The result is a basic map that lives in the same space as "bmap"
12325 * except that the domain has been replaced by the domain space of "ma".
12327 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12328 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12330 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12333 /* Compute the preimage of the range of "bmap" under the function
12334 * represented by "ma".
12335 * In other words, plug in "ma" in the range of "bmap".
12336 * The result is a basic map that lives in the same space as "bmap"
12337 * except that the range has been replaced by the domain space of "ma".
12339 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12340 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12342 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12345 /* Check if the range of "ma" is compatible with the domain or range
12346 * (depending on "type") of "map".
12347 * Return -1 if anything is wrong.
12349 static int check_map_compatible_range_multi_aff(
12350 __isl_keep isl_map *map, enum isl_dim_type type,
12351 __isl_keep isl_multi_aff *ma)
12353 int m;
12354 isl_space *ma_space;
12356 ma_space = isl_multi_aff_get_space(ma);
12357 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12358 isl_space_free(ma_space);
12359 if (m >= 0 && !m)
12360 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12361 "spaces don't match", return -1);
12362 return m;
12365 /* Compute the preimage of the domain or range (depending on "type")
12366 * of "map" under the function represented by "ma".
12367 * In other words, plug in "ma" in the domain or range of "map".
12368 * The result is a map that lives in the same space as "map"
12369 * except that the domain or range has been replaced by
12370 * the domain space of "ma".
12372 * The parameters are assumed to have been aligned.
12374 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12375 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12377 int i;
12378 isl_space *space;
12380 map = isl_map_cow(map);
12381 ma = isl_multi_aff_align_divs(ma);
12382 if (!map || !ma)
12383 goto error;
12384 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12385 goto error;
12387 for (i = 0; i < map->n; ++i) {
12388 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12389 isl_multi_aff_copy(ma));
12390 if (!map->p[i])
12391 goto error;
12394 space = isl_multi_aff_get_domain_space(ma);
12395 space = isl_space_set(isl_map_get_space(map), type, space);
12397 isl_space_free(map->dim);
12398 map->dim = space;
12399 if (!map->dim)
12400 goto error;
12402 isl_multi_aff_free(ma);
12403 if (map->n > 1)
12404 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12405 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12406 return map;
12407 error:
12408 isl_multi_aff_free(ma);
12409 isl_map_free(map);
12410 return NULL;
12413 /* Compute the preimage of the domain or range (depending on "type")
12414 * of "map" under the function represented by "ma".
12415 * In other words, plug in "ma" in the domain or range of "map".
12416 * The result is a map that lives in the same space as "map"
12417 * except that the domain or range has been replaced by
12418 * the domain space of "ma".
12420 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12421 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12423 if (!map || !ma)
12424 goto error;
12426 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12427 return map_preimage_multi_aff(map, type, ma);
12429 if (!isl_space_has_named_params(map->dim) ||
12430 !isl_space_has_named_params(ma->space))
12431 isl_die(map->ctx, isl_error_invalid,
12432 "unaligned unnamed parameters", goto error);
12433 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12434 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12436 return map_preimage_multi_aff(map, type, ma);
12437 error:
12438 isl_multi_aff_free(ma);
12439 return isl_map_free(map);
12442 /* Compute the preimage of "set" under the function represented by "ma".
12443 * In other words, plug in "ma" in "set". The result is a set
12444 * that lives in the domain space of "ma".
12446 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12447 __isl_take isl_multi_aff *ma)
12449 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12452 /* Compute the preimage of the domain of "map" under the function
12453 * represented by "ma".
12454 * In other words, plug in "ma" in the domain of "map".
12455 * The result is a map that lives in the same space as "map"
12456 * except that the domain has been replaced by the domain space of "ma".
12458 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12459 __isl_take isl_multi_aff *ma)
12461 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12464 /* Compute the preimage of the range of "map" under the function
12465 * represented by "ma".
12466 * In other words, plug in "ma" in the range of "map".
12467 * The result is a map that lives in the same space as "map"
12468 * except that the range has been replaced by the domain space of "ma".
12470 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12471 __isl_take isl_multi_aff *ma)
12473 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12476 /* Compute the preimage of "map" under the function represented by "pma".
12477 * In other words, plug in "pma" in the domain or range of "map".
12478 * The result is a map that lives in the same space as "map",
12479 * except that the space of type "type" has been replaced by
12480 * the domain space of "pma".
12482 * The parameters of "map" and "pma" are assumed to have been aligned.
12484 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12485 __isl_take isl_map *map, enum isl_dim_type type,
12486 __isl_take isl_pw_multi_aff *pma)
12488 int i;
12489 isl_map *res;
12491 if (!pma)
12492 goto error;
12494 if (pma->n == 0) {
12495 isl_pw_multi_aff_free(pma);
12496 res = isl_map_empty(isl_map_get_space(map));
12497 isl_map_free(map);
12498 return res;
12501 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12502 isl_multi_aff_copy(pma->p[0].maff));
12503 if (type == isl_dim_in)
12504 res = isl_map_intersect_domain(res,
12505 isl_map_copy(pma->p[0].set));
12506 else
12507 res = isl_map_intersect_range(res,
12508 isl_map_copy(pma->p[0].set));
12510 for (i = 1; i < pma->n; ++i) {
12511 isl_map *res_i;
12513 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12514 isl_multi_aff_copy(pma->p[i].maff));
12515 if (type == isl_dim_in)
12516 res_i = isl_map_intersect_domain(res_i,
12517 isl_map_copy(pma->p[i].set));
12518 else
12519 res_i = isl_map_intersect_range(res_i,
12520 isl_map_copy(pma->p[i].set));
12521 res = isl_map_union(res, res_i);
12524 isl_pw_multi_aff_free(pma);
12525 isl_map_free(map);
12526 return res;
12527 error:
12528 isl_pw_multi_aff_free(pma);
12529 isl_map_free(map);
12530 return NULL;
12533 /* Compute the preimage of "map" under the function represented by "pma".
12534 * In other words, plug in "pma" in the domain or range of "map".
12535 * The result is a map that lives in the same space as "map",
12536 * except that the space of type "type" has been replaced by
12537 * the domain space of "pma".
12539 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12540 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12542 if (!map || !pma)
12543 goto error;
12545 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12546 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12548 if (!isl_space_has_named_params(map->dim) ||
12549 !isl_space_has_named_params(pma->dim))
12550 isl_die(map->ctx, isl_error_invalid,
12551 "unaligned unnamed parameters", goto error);
12552 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12553 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12555 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12556 error:
12557 isl_pw_multi_aff_free(pma);
12558 return isl_map_free(map);
12561 /* Compute the preimage of "set" under the function represented by "pma".
12562 * In other words, plug in "pma" in "set". The result is a set
12563 * that lives in the domain space of "pma".
12565 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12566 __isl_take isl_pw_multi_aff *pma)
12568 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12571 /* Compute the preimage of the domain of "map" under the function
12572 * represented by "pma".
12573 * In other words, plug in "pma" in the domain of "map".
12574 * The result is a map that lives in the same space as "map",
12575 * except that domain space has been replaced by the domain space of "pma".
12577 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12578 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12580 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12583 /* Compute the preimage of the range of "map" under the function
12584 * represented by "pma".
12585 * In other words, plug in "pma" in the range of "map".
12586 * The result is a map that lives in the same space as "map",
12587 * except that range space has been replaced by the domain space of "pma".
12589 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12590 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12592 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12595 /* Compute the preimage of "map" under the function represented by "mpa".
12596 * In other words, plug in "mpa" in the domain or range of "map".
12597 * The result is a map that lives in the same space as "map",
12598 * except that the space of type "type" has been replaced by
12599 * the domain space of "mpa".
12601 * If the map does not involve any constraints that refer to the
12602 * dimensions of the substituted space, then the only possible
12603 * effect of "mpa" on the map is to map the space to a different space.
12604 * We create a separate isl_multi_aff to effectuate this change
12605 * in order to avoid spurious splitting of the map along the pieces
12606 * of "mpa".
12608 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12609 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12611 int n;
12612 isl_pw_multi_aff *pma;
12614 if (!map || !mpa)
12615 goto error;
12617 n = isl_map_dim(map, type);
12618 if (!isl_map_involves_dims(map, type, 0, n)) {
12619 isl_space *space;
12620 isl_multi_aff *ma;
12622 space = isl_multi_pw_aff_get_space(mpa);
12623 isl_multi_pw_aff_free(mpa);
12624 ma = isl_multi_aff_zero(space);
12625 return isl_map_preimage_multi_aff(map, type, ma);
12628 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12629 return isl_map_preimage_pw_multi_aff(map, type, pma);
12630 error:
12631 isl_map_free(map);
12632 isl_multi_pw_aff_free(mpa);
12633 return NULL;
12636 /* Compute the preimage of "map" under the function represented by "mpa".
12637 * In other words, plug in "mpa" in the domain "map".
12638 * The result is a map that lives in the same space as "map",
12639 * except that domain space has been replaced by the domain space of "mpa".
12641 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12642 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12644 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12647 /* Compute the preimage of "set" by the function represented by "mpa".
12648 * In other words, plug in "mpa" in "set".
12650 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12651 __isl_take isl_multi_pw_aff *mpa)
12653 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);