introduce schedule tree representation
[isl.git] / isl_map.c
blob7bc6d14b099c8a0781f2d482f21f1b54be367754
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_space_private.h"
23 #include "isl_equalities.h"
24 #include <isl_lp_private.h>
25 #include <isl_seq.h>
26 #include <isl/set.h>
27 #include <isl/map.h>
28 #include <isl_reordering.h>
29 #include "isl_sample.h"
30 #include <isl_sort.h>
31 #include "isl_tab.h"
32 #include <isl/vec.h>
33 #include <isl_mat_private.h>
34 #include <isl_vec_private.h>
35 #include <isl_dim_map.h>
36 #include <isl_local_space_private.h>
37 #include <isl_aff_private.h>
38 #include <isl_options_private.h>
39 #include <isl_morph.h>
40 #include <isl_val_private.h>
41 #include <isl/deprecated/map_int.h>
42 #include <isl/deprecated/set_int.h>
44 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
46 switch (type) {
47 case isl_dim_param: return dim->nparam;
48 case isl_dim_in: return dim->n_in;
49 case isl_dim_out: return dim->n_out;
50 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
51 default: return 0;
55 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
57 switch (type) {
58 case isl_dim_param: return 1;
59 case isl_dim_in: return 1 + dim->nparam;
60 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
61 default: return 0;
65 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
66 enum isl_dim_type type)
68 if (!bmap)
69 return 0;
70 switch (type) {
71 case isl_dim_cst: return 1;
72 case isl_dim_param:
73 case isl_dim_in:
74 case isl_dim_out: return isl_space_dim(bmap->dim, type);
75 case isl_dim_div: return bmap->n_div;
76 case isl_dim_all: return isl_basic_map_total_dim(bmap);
77 default: return 0;
81 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
83 return map ? n(map->dim, type) : 0;
86 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
88 return set ? n(set->dim, type) : 0;
91 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
92 enum isl_dim_type type)
94 isl_space *dim = bmap->dim;
95 switch (type) {
96 case isl_dim_cst: return 0;
97 case isl_dim_param: return 1;
98 case isl_dim_in: return 1 + dim->nparam;
99 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
100 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
101 default: return 0;
105 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
106 enum isl_dim_type type)
108 return isl_basic_map_offset(bset, type);
111 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
113 return pos(map->dim, type);
116 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
117 enum isl_dim_type type)
119 return isl_basic_map_dim(bset, type);
122 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
124 return isl_basic_set_dim(bset, isl_dim_set);
127 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
129 return isl_basic_set_dim(bset, isl_dim_param);
132 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
134 if (!bset)
135 return 0;
136 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
139 unsigned isl_set_n_dim(__isl_keep isl_set *set)
141 return isl_set_dim(set, isl_dim_set);
144 unsigned isl_set_n_param(__isl_keep isl_set *set)
146 return isl_set_dim(set, isl_dim_param);
149 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
151 return bmap ? bmap->dim->n_in : 0;
154 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
156 return bmap ? bmap->dim->n_out : 0;
159 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
161 return bmap ? bmap->dim->nparam : 0;
164 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
166 return bmap ? bmap->n_div : 0;
169 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
171 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
174 unsigned isl_map_n_in(const struct isl_map *map)
176 return map ? map->dim->n_in : 0;
179 unsigned isl_map_n_out(const struct isl_map *map)
181 return map ? map->dim->n_out : 0;
184 unsigned isl_map_n_param(const struct isl_map *map)
186 return map ? map->dim->nparam : 0;
189 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
191 int m;
192 if (!map || !set)
193 return -1;
194 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
195 if (m < 0 || !m)
196 return m;
197 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
198 set->dim, isl_dim_set);
201 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
202 struct isl_basic_set *bset)
204 int m;
205 if (!bmap || !bset)
206 return -1;
207 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
208 if (m < 0 || !m)
209 return m;
210 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
211 bset->dim, isl_dim_set);
214 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
216 int m;
217 if (!map || !set)
218 return -1;
219 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
220 if (m < 0 || !m)
221 return m;
222 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
223 set->dim, isl_dim_set);
226 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
227 struct isl_basic_set *bset)
229 int m;
230 if (!bmap || !bset)
231 return -1;
232 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
233 if (m < 0 || !m)
234 return m;
235 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
236 bset->dim, isl_dim_set);
239 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
241 return bmap ? bmap->ctx : NULL;
244 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
246 return bset ? bset->ctx : NULL;
249 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
251 return map ? map->ctx : NULL;
254 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
256 return set ? set->ctx : NULL;
259 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
261 if (!bmap)
262 return NULL;
263 return isl_space_copy(bmap->dim);
266 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
268 if (!bset)
269 return NULL;
270 return isl_space_copy(bset->dim);
273 /* Extract the divs in "bmap" as a matrix.
275 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
277 int i;
278 isl_ctx *ctx;
279 isl_mat *div;
280 unsigned total;
281 unsigned cols;
283 if (!bmap)
284 return NULL;
286 ctx = isl_basic_map_get_ctx(bmap);
287 total = isl_space_dim(bmap->dim, isl_dim_all);
288 cols = 1 + 1 + total + bmap->n_div;
289 div = isl_mat_alloc(ctx, bmap->n_div, cols);
290 if (!div)
291 return NULL;
293 for (i = 0; i < bmap->n_div; ++i)
294 isl_seq_cpy(div->row[i], bmap->div[i], cols);
296 return div;
299 /* Extract the divs in "bset" as a matrix.
301 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
303 return isl_basic_map_get_divs(bset);
306 __isl_give isl_local_space *isl_basic_map_get_local_space(
307 __isl_keep isl_basic_map *bmap)
309 isl_mat *div;
311 if (!bmap)
312 return NULL;
314 div = isl_basic_map_get_divs(bmap);
315 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
318 __isl_give isl_local_space *isl_basic_set_get_local_space(
319 __isl_keep isl_basic_set *bset)
321 return isl_basic_map_get_local_space(bset);
324 __isl_give isl_basic_map *isl_basic_map_from_local_space(
325 __isl_take isl_local_space *ls)
327 int i;
328 int n_div;
329 isl_basic_map *bmap;
331 if (!ls)
332 return NULL;
334 n_div = isl_local_space_dim(ls, isl_dim_div);
335 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
336 n_div, 0, 2 * n_div);
338 for (i = 0; i < n_div; ++i)
339 if (isl_basic_map_alloc_div(bmap) < 0)
340 goto error;
342 for (i = 0; i < n_div; ++i) {
343 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
344 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
345 goto error;
348 isl_local_space_free(ls);
349 return bmap;
350 error:
351 isl_local_space_free(ls);
352 isl_basic_map_free(bmap);
353 return NULL;
356 __isl_give isl_basic_set *isl_basic_set_from_local_space(
357 __isl_take isl_local_space *ls)
359 return isl_basic_map_from_local_space(ls);
362 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
364 if (!map)
365 return NULL;
366 return isl_space_copy(map->dim);
369 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
371 if (!set)
372 return NULL;
373 return isl_space_copy(set->dim);
376 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
377 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
379 bmap = isl_basic_map_cow(bmap);
380 if (!bmap)
381 return NULL;
382 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
383 if (!bmap->dim)
384 goto error;
385 bmap = isl_basic_map_finalize(bmap);
386 return bmap;
387 error:
388 isl_basic_map_free(bmap);
389 return NULL;
392 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
393 __isl_take isl_basic_set *bset, const char *s)
395 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
398 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
399 enum isl_dim_type type)
401 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
404 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
405 enum isl_dim_type type, const char *s)
407 int i;
409 map = isl_map_cow(map);
410 if (!map)
411 return NULL;
413 map->dim = isl_space_set_tuple_name(map->dim, type, s);
414 if (!map->dim)
415 goto error;
417 for (i = 0; i < map->n; ++i) {
418 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
419 if (!map->p[i])
420 goto error;
423 return map;
424 error:
425 isl_map_free(map);
426 return NULL;
429 /* Replace the identifier of the tuple of type "type" by "id".
431 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
432 __isl_take isl_basic_map *bmap,
433 enum isl_dim_type type, __isl_take isl_id *id)
435 bmap = isl_basic_map_cow(bmap);
436 if (!bmap)
437 goto error;
438 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
439 if (!bmap->dim)
440 return isl_basic_map_free(bmap);
441 bmap = isl_basic_map_finalize(bmap);
442 return bmap;
443 error:
444 isl_id_free(id);
445 return NULL;
448 /* Replace the identifier of the tuple by "id".
450 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
451 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
453 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
456 /* Does the input or output tuple have a name?
458 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
460 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
463 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
464 enum isl_dim_type type)
466 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
469 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
470 const char *s)
472 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
475 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
476 enum isl_dim_type type, __isl_take isl_id *id)
478 map = isl_map_cow(map);
479 if (!map)
480 goto error;
482 map->dim = isl_space_set_tuple_id(map->dim, type, id);
484 return isl_map_reset_space(map, isl_space_copy(map->dim));
485 error:
486 isl_id_free(id);
487 return NULL;
490 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
491 __isl_take isl_id *id)
493 return isl_map_set_tuple_id(set, isl_dim_set, id);
496 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
497 enum isl_dim_type type)
499 map = isl_map_cow(map);
500 if (!map)
501 return NULL;
503 map->dim = isl_space_reset_tuple_id(map->dim, type);
505 return isl_map_reset_space(map, isl_space_copy(map->dim));
508 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
510 return isl_map_reset_tuple_id(set, isl_dim_set);
513 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
515 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
518 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
519 enum isl_dim_type type)
521 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
524 int isl_set_has_tuple_id(__isl_keep isl_set *set)
526 return isl_map_has_tuple_id(set, isl_dim_set);
529 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
531 return isl_map_get_tuple_id(set, isl_dim_set);
534 /* Does the set tuple have a name?
536 int isl_set_has_tuple_name(__isl_keep isl_set *set)
538 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
542 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
544 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
547 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
549 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
552 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
553 enum isl_dim_type type, unsigned pos)
555 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
558 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
559 enum isl_dim_type type, unsigned pos)
561 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
564 /* Does the given dimension have a name?
566 int isl_map_has_dim_name(__isl_keep isl_map *map,
567 enum isl_dim_type type, unsigned pos)
569 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
572 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
573 enum isl_dim_type type, unsigned pos)
575 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
578 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
579 enum isl_dim_type type, unsigned pos)
581 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
584 /* Does the given dimension have a name?
586 int isl_set_has_dim_name(__isl_keep isl_set *set,
587 enum isl_dim_type type, unsigned pos)
589 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
592 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
593 __isl_take isl_basic_map *bmap,
594 enum isl_dim_type type, unsigned pos, const char *s)
596 bmap = isl_basic_map_cow(bmap);
597 if (!bmap)
598 return NULL;
599 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
600 if (!bmap->dim)
601 goto error;
602 return isl_basic_map_finalize(bmap);
603 error:
604 isl_basic_map_free(bmap);
605 return NULL;
608 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
609 enum isl_dim_type type, unsigned pos, const char *s)
611 int i;
613 map = isl_map_cow(map);
614 if (!map)
615 return NULL;
617 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
618 if (!map->dim)
619 goto error;
621 for (i = 0; i < map->n; ++i) {
622 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
623 if (!map->p[i])
624 goto error;
627 return map;
628 error:
629 isl_map_free(map);
630 return NULL;
633 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
634 __isl_take isl_basic_set *bset,
635 enum isl_dim_type type, unsigned pos, const char *s)
637 return (isl_basic_set *)isl_basic_map_set_dim_name(
638 (isl_basic_map *)bset, type, pos, s);
641 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
642 enum isl_dim_type type, unsigned pos, const char *s)
644 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
647 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
648 enum isl_dim_type type, unsigned pos)
650 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
653 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
654 enum isl_dim_type type, unsigned pos)
656 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
659 int isl_map_has_dim_id(__isl_keep isl_map *map,
660 enum isl_dim_type type, unsigned pos)
662 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
665 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
666 enum isl_dim_type type, unsigned pos)
668 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
671 int isl_set_has_dim_id(__isl_keep isl_set *set,
672 enum isl_dim_type type, unsigned pos)
674 return isl_map_has_dim_id(set, type, pos);
677 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
678 enum isl_dim_type type, unsigned pos)
680 return isl_map_get_dim_id(set, type, pos);
683 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
684 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
686 map = isl_map_cow(map);
687 if (!map)
688 goto error;
690 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
692 return isl_map_reset_space(map, isl_space_copy(map->dim));
693 error:
694 isl_id_free(id);
695 return NULL;
698 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
699 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
701 return isl_map_set_dim_id(set, type, pos, id);
704 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
705 __isl_keep isl_id *id)
707 if (!map)
708 return -1;
709 return isl_space_find_dim_by_id(map->dim, type, id);
712 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
713 __isl_keep isl_id *id)
715 return isl_map_find_dim_by_id(set, type, id);
718 /* Return the position of the dimension of the given type and name
719 * in "bmap".
720 * Return -1 if no such dimension can be found.
722 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
723 enum isl_dim_type type, const char *name)
725 if (!bmap)
726 return -1;
727 return isl_space_find_dim_by_name(bmap->dim, type, name);
730 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
731 const char *name)
733 if (!map)
734 return -1;
735 return isl_space_find_dim_by_name(map->dim, type, name);
738 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
739 const char *name)
741 return isl_map_find_dim_by_name(set, type, name);
744 /* Reset the user pointer on all identifiers of parameters and tuples
745 * of the space of "map".
747 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
749 isl_space *space;
751 space = isl_map_get_space(map);
752 space = isl_space_reset_user(space);
753 map = isl_map_reset_space(map, space);
755 return map;
758 /* Reset the user pointer on all identifiers of parameters and tuples
759 * of the space of "set".
761 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
763 return isl_map_reset_user(set);
766 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
768 if (!bmap)
769 return -1;
770 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
773 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
775 return isl_basic_map_is_rational(bset);
778 /* Does "bmap" contain any rational points?
780 * If "bmap" has an equality for each dimension, equating the dimension
781 * to an integer constant, then it has no rational points, even if it
782 * is marked as rational.
784 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
786 int has_rational = 1;
787 unsigned total;
789 if (!bmap)
790 return -1;
791 if (isl_basic_map_plain_is_empty(bmap))
792 return 0;
793 if (!isl_basic_map_is_rational(bmap))
794 return 0;
795 bmap = isl_basic_map_copy(bmap);
796 bmap = isl_basic_map_implicit_equalities(bmap);
797 if (!bmap)
798 return -1;
799 total = isl_basic_map_total_dim(bmap);
800 if (bmap->n_eq == total) {
801 int i, j;
802 for (i = 0; i < bmap->n_eq; ++i) {
803 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
804 if (j < 0)
805 break;
806 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
807 !isl_int_is_negone(bmap->eq[i][1 + j]))
808 break;
809 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
810 total - j - 1);
811 if (j >= 0)
812 break;
814 if (i == bmap->n_eq)
815 has_rational = 0;
817 isl_basic_map_free(bmap);
819 return has_rational;
822 /* Does "map" contain any rational points?
824 int isl_map_has_rational(__isl_keep isl_map *map)
826 int i;
827 int has_rational;
829 if (!map)
830 return -1;
831 for (i = 0; i < map->n; ++i) {
832 has_rational = isl_basic_map_has_rational(map->p[i]);
833 if (has_rational < 0)
834 return -1;
835 if (has_rational)
836 return 1;
838 return 0;
841 /* Does "set" contain any rational points?
843 int isl_set_has_rational(__isl_keep isl_set *set)
845 return isl_map_has_rational(set);
848 /* Is this basic set a parameter domain?
850 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
852 if (!bset)
853 return -1;
854 return isl_space_is_params(bset->dim);
857 /* Is this set a parameter domain?
859 int isl_set_is_params(__isl_keep isl_set *set)
861 if (!set)
862 return -1;
863 return isl_space_is_params(set->dim);
866 /* Is this map actually a parameter domain?
867 * Users should never call this function. Outside of isl,
868 * a map can never be a parameter domain.
870 int isl_map_is_params(__isl_keep isl_map *map)
872 if (!map)
873 return -1;
874 return isl_space_is_params(map->dim);
877 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
878 struct isl_basic_map *bmap, unsigned extra,
879 unsigned n_eq, unsigned n_ineq)
881 int i;
882 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
884 bmap->ctx = ctx;
885 isl_ctx_ref(ctx);
887 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
888 if (isl_blk_is_error(bmap->block))
889 goto error;
891 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
892 if ((n_ineq + n_eq) && !bmap->ineq)
893 goto error;
895 if (extra == 0) {
896 bmap->block2 = isl_blk_empty();
897 bmap->div = NULL;
898 } else {
899 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
900 if (isl_blk_is_error(bmap->block2))
901 goto error;
903 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
904 if (!bmap->div)
905 goto error;
908 for (i = 0; i < n_ineq + n_eq; ++i)
909 bmap->ineq[i] = bmap->block.data + i * row_size;
911 for (i = 0; i < extra; ++i)
912 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
914 bmap->ref = 1;
915 bmap->flags = 0;
916 bmap->c_size = n_eq + n_ineq;
917 bmap->eq = bmap->ineq + n_ineq;
918 bmap->extra = extra;
919 bmap->n_eq = 0;
920 bmap->n_ineq = 0;
921 bmap->n_div = 0;
922 bmap->sample = NULL;
924 return bmap;
925 error:
926 isl_basic_map_free(bmap);
927 return NULL;
930 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
931 unsigned nparam, unsigned dim, unsigned extra,
932 unsigned n_eq, unsigned n_ineq)
934 struct isl_basic_map *bmap;
935 isl_space *space;
937 space = isl_space_set_alloc(ctx, nparam, dim);
938 if (!space)
939 return NULL;
941 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
942 return (struct isl_basic_set *)bmap;
945 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
946 unsigned extra, unsigned n_eq, unsigned n_ineq)
948 struct isl_basic_map *bmap;
949 if (!dim)
950 return NULL;
951 isl_assert(dim->ctx, dim->n_in == 0, goto error);
952 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
953 return (struct isl_basic_set *)bmap;
954 error:
955 isl_space_free(dim);
956 return NULL;
959 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
960 unsigned extra, unsigned n_eq, unsigned n_ineq)
962 struct isl_basic_map *bmap;
964 if (!dim)
965 return NULL;
966 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
967 if (!bmap)
968 goto error;
969 bmap->dim = dim;
971 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
972 error:
973 isl_space_free(dim);
974 return NULL;
977 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
978 unsigned nparam, unsigned in, unsigned out, unsigned extra,
979 unsigned n_eq, unsigned n_ineq)
981 struct isl_basic_map *bmap;
982 isl_space *dim;
984 dim = isl_space_alloc(ctx, nparam, in, out);
985 if (!dim)
986 return NULL;
988 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
989 return bmap;
992 static void dup_constraints(
993 struct isl_basic_map *dst, struct isl_basic_map *src)
995 int i;
996 unsigned total = isl_basic_map_total_dim(src);
998 for (i = 0; i < src->n_eq; ++i) {
999 int j = isl_basic_map_alloc_equality(dst);
1000 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1003 for (i = 0; i < src->n_ineq; ++i) {
1004 int j = isl_basic_map_alloc_inequality(dst);
1005 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1008 for (i = 0; i < src->n_div; ++i) {
1009 int j = isl_basic_map_alloc_div(dst);
1010 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1012 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1015 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1017 struct isl_basic_map *dup;
1019 if (!bmap)
1020 return NULL;
1021 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1022 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1023 if (!dup)
1024 return NULL;
1025 dup_constraints(dup, bmap);
1026 dup->flags = bmap->flags;
1027 dup->sample = isl_vec_copy(bmap->sample);
1028 return dup;
1031 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1033 struct isl_basic_map *dup;
1035 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1036 return (struct isl_basic_set *)dup;
1039 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1041 if (!bset)
1042 return NULL;
1044 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1045 bset->ref++;
1046 return bset;
1048 return isl_basic_set_dup(bset);
1051 struct isl_set *isl_set_copy(struct isl_set *set)
1053 if (!set)
1054 return NULL;
1056 set->ref++;
1057 return set;
1060 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1062 if (!bmap)
1063 return NULL;
1065 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1066 bmap->ref++;
1067 return bmap;
1069 bmap = isl_basic_map_dup(bmap);
1070 if (bmap)
1071 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1072 return bmap;
1075 struct isl_map *isl_map_copy(struct isl_map *map)
1077 if (!map)
1078 return NULL;
1080 map->ref++;
1081 return map;
1084 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1086 if (!bmap)
1087 return NULL;
1089 if (--bmap->ref > 0)
1090 return NULL;
1092 isl_ctx_deref(bmap->ctx);
1093 free(bmap->div);
1094 isl_blk_free(bmap->ctx, bmap->block2);
1095 free(bmap->ineq);
1096 isl_blk_free(bmap->ctx, bmap->block);
1097 isl_vec_free(bmap->sample);
1098 isl_space_free(bmap->dim);
1099 free(bmap);
1101 return NULL;
1104 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1106 return isl_basic_map_free((struct isl_basic_map *)bset);
1109 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1111 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1114 __isl_give isl_map *isl_map_align_params_map_map_and(
1115 __isl_take isl_map *map1, __isl_take isl_map *map2,
1116 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1117 __isl_take isl_map *map2))
1119 if (!map1 || !map2)
1120 goto error;
1121 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1122 return fn(map1, map2);
1123 if (!isl_space_has_named_params(map1->dim) ||
1124 !isl_space_has_named_params(map2->dim))
1125 isl_die(map1->ctx, isl_error_invalid,
1126 "unaligned unnamed parameters", goto error);
1127 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1128 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1129 return fn(map1, map2);
1130 error:
1131 isl_map_free(map1);
1132 isl_map_free(map2);
1133 return NULL;
1136 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1137 __isl_keep isl_map *map2,
1138 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1140 int r;
1142 if (!map1 || !map2)
1143 return -1;
1144 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1145 return fn(map1, map2);
1146 if (!isl_space_has_named_params(map1->dim) ||
1147 !isl_space_has_named_params(map2->dim))
1148 isl_die(map1->ctx, isl_error_invalid,
1149 "unaligned unnamed parameters", return -1);
1150 map1 = isl_map_copy(map1);
1151 map2 = isl_map_copy(map2);
1152 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1153 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1154 r = fn(map1, map2);
1155 isl_map_free(map1);
1156 isl_map_free(map2);
1157 return r;
1160 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1162 struct isl_ctx *ctx;
1163 if (!bmap)
1164 return -1;
1165 ctx = bmap->ctx;
1166 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1167 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1168 return -1);
1169 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1170 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1171 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1172 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1173 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1174 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1175 isl_int *t;
1176 int j = isl_basic_map_alloc_inequality(bmap);
1177 if (j < 0)
1178 return -1;
1179 t = bmap->ineq[j];
1180 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1181 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1182 bmap->eq[-1] = t;
1183 bmap->n_eq++;
1184 bmap->n_ineq--;
1185 bmap->eq--;
1186 return 0;
1188 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1189 bmap->extra - bmap->n_div);
1190 return bmap->n_eq++;
1193 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1195 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1198 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1200 if (!bmap)
1201 return -1;
1202 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1203 bmap->n_eq -= n;
1204 return 0;
1207 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1209 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1212 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1214 isl_int *t;
1215 if (!bmap)
1216 return -1;
1217 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1219 if (pos != bmap->n_eq - 1) {
1220 t = bmap->eq[pos];
1221 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1222 bmap->eq[bmap->n_eq - 1] = t;
1224 bmap->n_eq--;
1225 return 0;
1228 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1230 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1233 /* Turn inequality "pos" of "bmap" into an equality.
1235 * In particular, we move the inequality in front of the equalities
1236 * and move the last inequality in the position of the moved inequality.
1237 * Note that isl_tab_make_equalities_explicit depends on this particular
1238 * change in the ordering of the constraints.
1240 void isl_basic_map_inequality_to_equality(
1241 struct isl_basic_map *bmap, unsigned pos)
1243 isl_int *t;
1245 t = bmap->ineq[pos];
1246 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1247 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1248 bmap->eq[-1] = t;
1249 bmap->n_eq++;
1250 bmap->n_ineq--;
1251 bmap->eq--;
1252 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1253 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1254 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1255 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1258 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1260 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1263 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1265 struct isl_ctx *ctx;
1266 if (!bmap)
1267 return -1;
1268 ctx = bmap->ctx;
1269 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1270 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1271 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1272 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1273 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1274 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1275 1 + isl_basic_map_total_dim(bmap),
1276 bmap->extra - bmap->n_div);
1277 return bmap->n_ineq++;
1280 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1282 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1285 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1287 if (!bmap)
1288 return -1;
1289 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1290 bmap->n_ineq -= n;
1291 return 0;
1294 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1296 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1299 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1301 isl_int *t;
1302 if (!bmap)
1303 return -1;
1304 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1306 if (pos != bmap->n_ineq - 1) {
1307 t = bmap->ineq[pos];
1308 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1309 bmap->ineq[bmap->n_ineq - 1] = t;
1310 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1312 bmap->n_ineq--;
1313 return 0;
1316 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1318 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1321 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1322 isl_int *eq)
1324 int k;
1326 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1327 if (!bmap)
1328 return NULL;
1329 k = isl_basic_map_alloc_equality(bmap);
1330 if (k < 0)
1331 goto error;
1332 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1333 return bmap;
1334 error:
1335 isl_basic_map_free(bmap);
1336 return NULL;
1339 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1340 isl_int *eq)
1342 return (isl_basic_set *)
1343 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1346 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1347 isl_int *ineq)
1349 int k;
1351 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1352 if (!bmap)
1353 return NULL;
1354 k = isl_basic_map_alloc_inequality(bmap);
1355 if (k < 0)
1356 goto error;
1357 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1358 return bmap;
1359 error:
1360 isl_basic_map_free(bmap);
1361 return NULL;
1364 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1365 isl_int *ineq)
1367 return (isl_basic_set *)
1368 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1371 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1373 if (!bmap)
1374 return -1;
1375 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1376 isl_seq_clr(bmap->div[bmap->n_div] +
1377 1 + 1 + isl_basic_map_total_dim(bmap),
1378 bmap->extra - bmap->n_div);
1379 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1380 return bmap->n_div++;
1383 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1385 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1388 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1390 if (!bmap)
1391 return -1;
1392 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1393 bmap->n_div -= n;
1394 return 0;
1397 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1399 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1402 /* Copy constraint from src to dst, putting the vars of src at offset
1403 * dim_off in dst and the divs of src at offset div_off in dst.
1404 * If both sets are actually map, then dim_off applies to the input
1405 * variables.
1407 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1408 struct isl_basic_map *src_map, isl_int *src,
1409 unsigned in_off, unsigned out_off, unsigned div_off)
1411 unsigned src_nparam = isl_basic_map_n_param(src_map);
1412 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1413 unsigned src_in = isl_basic_map_n_in(src_map);
1414 unsigned dst_in = isl_basic_map_n_in(dst_map);
1415 unsigned src_out = isl_basic_map_n_out(src_map);
1416 unsigned dst_out = isl_basic_map_n_out(dst_map);
1417 isl_int_set(dst[0], src[0]);
1418 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1419 if (dst_nparam > src_nparam)
1420 isl_seq_clr(dst+1+src_nparam,
1421 dst_nparam - src_nparam);
1422 isl_seq_clr(dst+1+dst_nparam, in_off);
1423 isl_seq_cpy(dst+1+dst_nparam+in_off,
1424 src+1+src_nparam,
1425 isl_min(dst_in-in_off, src_in));
1426 if (dst_in-in_off > src_in)
1427 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1428 dst_in - in_off - src_in);
1429 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1430 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1431 src+1+src_nparam+src_in,
1432 isl_min(dst_out-out_off, src_out));
1433 if (dst_out-out_off > src_out)
1434 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1435 dst_out - out_off - src_out);
1436 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1437 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1438 src+1+src_nparam+src_in+src_out,
1439 isl_min(dst_map->extra-div_off, src_map->n_div));
1440 if (dst_map->n_div-div_off > src_map->n_div)
1441 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1442 div_off+src_map->n_div,
1443 dst_map->n_div - div_off - src_map->n_div);
1446 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1447 struct isl_basic_map *src_map, isl_int *src,
1448 unsigned in_off, unsigned out_off, unsigned div_off)
1450 isl_int_set(dst[0], src[0]);
1451 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1454 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1455 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1457 int i;
1458 unsigned div_off;
1460 if (!bmap1 || !bmap2)
1461 goto error;
1463 div_off = bmap1->n_div;
1465 for (i = 0; i < bmap2->n_eq; ++i) {
1466 int i1 = isl_basic_map_alloc_equality(bmap1);
1467 if (i1 < 0)
1468 goto error;
1469 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1470 i_pos, o_pos, div_off);
1473 for (i = 0; i < bmap2->n_ineq; ++i) {
1474 int i1 = isl_basic_map_alloc_inequality(bmap1);
1475 if (i1 < 0)
1476 goto error;
1477 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1478 i_pos, o_pos, div_off);
1481 for (i = 0; i < bmap2->n_div; ++i) {
1482 int i1 = isl_basic_map_alloc_div(bmap1);
1483 if (i1 < 0)
1484 goto error;
1485 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1486 i_pos, o_pos, div_off);
1489 isl_basic_map_free(bmap2);
1491 return bmap1;
1493 error:
1494 isl_basic_map_free(bmap1);
1495 isl_basic_map_free(bmap2);
1496 return NULL;
1499 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1500 struct isl_basic_set *bset2, unsigned pos)
1502 return (struct isl_basic_set *)
1503 add_constraints((struct isl_basic_map *)bset1,
1504 (struct isl_basic_map *)bset2, 0, pos);
1507 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1508 __isl_take isl_space *dim, unsigned extra,
1509 unsigned n_eq, unsigned n_ineq)
1511 struct isl_basic_map *ext;
1512 unsigned flags;
1513 int dims_ok;
1515 if (!dim)
1516 goto error;
1518 if (!base)
1519 goto error;
1521 dims_ok = isl_space_is_equal(base->dim, dim) &&
1522 base->extra >= base->n_div + extra;
1524 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1525 room_for_ineq(base, n_ineq)) {
1526 isl_space_free(dim);
1527 return base;
1530 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1531 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1532 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1533 extra += base->extra;
1534 n_eq += base->n_eq;
1535 n_ineq += base->n_ineq;
1537 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1538 dim = NULL;
1539 if (!ext)
1540 goto error;
1542 if (dims_ok)
1543 ext->sample = isl_vec_copy(base->sample);
1544 flags = base->flags;
1545 ext = add_constraints(ext, base, 0, 0);
1546 if (ext) {
1547 ext->flags = flags;
1548 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1551 return ext;
1553 error:
1554 isl_space_free(dim);
1555 isl_basic_map_free(base);
1556 return NULL;
1559 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1560 __isl_take isl_space *dim, unsigned extra,
1561 unsigned n_eq, unsigned n_ineq)
1563 return (struct isl_basic_set *)
1564 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1565 extra, n_eq, n_ineq);
1568 struct isl_basic_map *isl_basic_map_extend_constraints(
1569 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1571 if (!base)
1572 return NULL;
1573 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1574 0, n_eq, n_ineq);
1577 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1578 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1579 unsigned n_eq, unsigned n_ineq)
1581 struct isl_basic_map *bmap;
1582 isl_space *dim;
1584 if (!base)
1585 return NULL;
1586 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1587 if (!dim)
1588 goto error;
1590 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1591 return bmap;
1592 error:
1593 isl_basic_map_free(base);
1594 return NULL;
1597 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1598 unsigned nparam, unsigned dim, unsigned extra,
1599 unsigned n_eq, unsigned n_ineq)
1601 return (struct isl_basic_set *)
1602 isl_basic_map_extend((struct isl_basic_map *)base,
1603 nparam, 0, dim, extra, n_eq, n_ineq);
1606 struct isl_basic_set *isl_basic_set_extend_constraints(
1607 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1609 return (struct isl_basic_set *)
1610 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1611 n_eq, n_ineq);
1614 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1616 return (struct isl_basic_set *)
1617 isl_basic_map_cow((struct isl_basic_map *)bset);
1620 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1622 if (!bmap)
1623 return NULL;
1625 if (bmap->ref > 1) {
1626 bmap->ref--;
1627 bmap = isl_basic_map_dup(bmap);
1629 if (bmap) {
1630 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1631 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1633 return bmap;
1636 struct isl_set *isl_set_cow(struct isl_set *set)
1638 if (!set)
1639 return NULL;
1641 if (set->ref == 1)
1642 return set;
1643 set->ref--;
1644 return isl_set_dup(set);
1647 struct isl_map *isl_map_cow(struct isl_map *map)
1649 if (!map)
1650 return NULL;
1652 if (map->ref == 1)
1653 return map;
1654 map->ref--;
1655 return isl_map_dup(map);
1658 static void swap_vars(struct isl_blk blk, isl_int *a,
1659 unsigned a_len, unsigned b_len)
1661 isl_seq_cpy(blk.data, a+a_len, b_len);
1662 isl_seq_cpy(blk.data+b_len, a, a_len);
1663 isl_seq_cpy(a, blk.data, b_len+a_len);
1666 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1667 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1669 int i;
1670 struct isl_blk blk;
1672 if (!bmap)
1673 goto error;
1675 isl_assert(bmap->ctx,
1676 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1678 if (n1 == 0 || n2 == 0)
1679 return bmap;
1681 bmap = isl_basic_map_cow(bmap);
1682 if (!bmap)
1683 return NULL;
1685 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1686 if (isl_blk_is_error(blk))
1687 goto error;
1689 for (i = 0; i < bmap->n_eq; ++i)
1690 swap_vars(blk,
1691 bmap->eq[i] + pos, n1, n2);
1693 for (i = 0; i < bmap->n_ineq; ++i)
1694 swap_vars(blk,
1695 bmap->ineq[i] + pos, n1, n2);
1697 for (i = 0; i < bmap->n_div; ++i)
1698 swap_vars(blk,
1699 bmap->div[i]+1 + pos, n1, n2);
1701 isl_blk_free(bmap->ctx, blk);
1703 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1704 bmap = isl_basic_map_gauss(bmap, NULL);
1705 return isl_basic_map_finalize(bmap);
1706 error:
1707 isl_basic_map_free(bmap);
1708 return NULL;
1711 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1712 __isl_take isl_basic_set *bset, unsigned n)
1714 unsigned dim;
1715 unsigned nparam;
1717 if (!bset)
1718 return NULL;
1720 nparam = isl_basic_set_n_param(bset);
1721 dim = isl_basic_set_n_dim(bset);
1722 isl_assert(bset->ctx, n <= dim, goto error);
1724 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1725 error:
1726 isl_basic_set_free(bset);
1727 return NULL;
1730 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1732 int i = 0;
1733 unsigned total;
1734 if (!bmap)
1735 goto error;
1736 total = isl_basic_map_total_dim(bmap);
1737 isl_basic_map_free_div(bmap, bmap->n_div);
1738 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1739 if (bmap->n_eq > 0)
1740 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1741 else {
1742 i = isl_basic_map_alloc_equality(bmap);
1743 if (i < 0)
1744 goto error;
1746 isl_int_set_si(bmap->eq[i][0], 1);
1747 isl_seq_clr(bmap->eq[i]+1, total);
1748 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1749 isl_vec_free(bmap->sample);
1750 bmap->sample = NULL;
1751 return isl_basic_map_finalize(bmap);
1752 error:
1753 isl_basic_map_free(bmap);
1754 return NULL;
1757 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1759 return (struct isl_basic_set *)
1760 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1763 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1764 * of "bmap").
1766 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1768 isl_int *t = bmap->div[a];
1769 bmap->div[a] = bmap->div[b];
1770 bmap->div[b] = t;
1773 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1774 * div definitions accordingly.
1776 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1778 int i;
1779 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1781 swap_div(bmap, a, b);
1783 for (i = 0; i < bmap->n_eq; ++i)
1784 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1786 for (i = 0; i < bmap->n_ineq; ++i)
1787 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1789 for (i = 0; i < bmap->n_div; ++i)
1790 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1791 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1794 /* Eliminate the specified n dimensions starting at first from the
1795 * constraints, without removing the dimensions from the space.
1796 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1798 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1799 enum isl_dim_type type, unsigned first, unsigned n)
1801 int i;
1803 if (!map)
1804 return NULL;
1805 if (n == 0)
1806 return map;
1808 if (first + n > isl_map_dim(map, type) || first + n < first)
1809 isl_die(map->ctx, isl_error_invalid,
1810 "index out of bounds", goto error);
1812 map = isl_map_cow(map);
1813 if (!map)
1814 return NULL;
1816 for (i = 0; i < map->n; ++i) {
1817 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1818 if (!map->p[i])
1819 goto error;
1821 return map;
1822 error:
1823 isl_map_free(map);
1824 return NULL;
1827 /* Eliminate the specified n dimensions starting at first from the
1828 * constraints, without removing the dimensions from the space.
1829 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1831 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1832 enum isl_dim_type type, unsigned first, unsigned n)
1834 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1837 /* Eliminate the specified n dimensions starting at first from the
1838 * constraints, without removing the dimensions from the space.
1839 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1841 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1842 unsigned first, unsigned n)
1844 return isl_set_eliminate(set, isl_dim_set, first, n);
1847 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1848 __isl_take isl_basic_map *bmap)
1850 if (!bmap)
1851 return NULL;
1852 bmap = isl_basic_map_eliminate_vars(bmap,
1853 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1854 if (!bmap)
1855 return NULL;
1856 bmap->n_div = 0;
1857 return isl_basic_map_finalize(bmap);
1860 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1861 __isl_take isl_basic_set *bset)
1863 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1864 (struct isl_basic_map *)bset);
1867 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1869 int i;
1871 if (!map)
1872 return NULL;
1873 if (map->n == 0)
1874 return map;
1876 map = isl_map_cow(map);
1877 if (!map)
1878 return NULL;
1880 for (i = 0; i < map->n; ++i) {
1881 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1882 if (!map->p[i])
1883 goto error;
1885 return map;
1886 error:
1887 isl_map_free(map);
1888 return NULL;
1891 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1893 return isl_map_remove_divs(set);
1896 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1897 enum isl_dim_type type, unsigned first, unsigned n)
1899 if (!bmap)
1900 return NULL;
1901 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1902 goto error);
1903 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1904 return bmap;
1905 bmap = isl_basic_map_eliminate_vars(bmap,
1906 isl_basic_map_offset(bmap, type) - 1 + first, n);
1907 if (!bmap)
1908 return bmap;
1909 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1910 return bmap;
1911 bmap = isl_basic_map_drop(bmap, type, first, n);
1912 return bmap;
1913 error:
1914 isl_basic_map_free(bmap);
1915 return NULL;
1918 /* Return true if the definition of the given div (recursively) involves
1919 * any of the given variables.
1921 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1922 unsigned first, unsigned n)
1924 int i;
1925 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1927 if (isl_int_is_zero(bmap->div[div][0]))
1928 return 0;
1929 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1930 return 1;
1932 for (i = bmap->n_div - 1; i >= 0; --i) {
1933 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1934 continue;
1935 if (div_involves_vars(bmap, i, first, n))
1936 return 1;
1939 return 0;
1942 /* Try and add a lower and/or upper bound on "div" to "bmap"
1943 * based on inequality "i".
1944 * "total" is the total number of variables (excluding the divs).
1945 * "v" is a temporary object that can be used during the calculations.
1946 * If "lb" is set, then a lower bound should be constructed.
1947 * If "ub" is set, then an upper bound should be constructed.
1949 * The calling function has already checked that the inequality does not
1950 * reference "div", but we still need to check that the inequality is
1951 * of the right form. We'll consider the case where we want to construct
1952 * a lower bound. The construction of upper bounds is similar.
1954 * Let "div" be of the form
1956 * q = floor((a + f(x))/d)
1958 * We essentially check if constraint "i" is of the form
1960 * b + f(x) >= 0
1962 * so that we can use it to derive a lower bound on "div".
1963 * However, we allow a slightly more general form
1965 * b + g(x) >= 0
1967 * with the condition that the coefficients of g(x) - f(x) are all
1968 * divisible by d.
1969 * Rewriting this constraint as
1971 * 0 >= -b - g(x)
1973 * adding a + f(x) to both sides and dividing by d, we obtain
1975 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1977 * Taking the floor on both sides, we obtain
1979 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1981 * or
1983 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1985 * In the case of an upper bound, we construct the constraint
1987 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1990 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1991 __isl_take isl_basic_map *bmap, int div, int i,
1992 unsigned total, isl_int v, int lb, int ub)
1994 int j;
1996 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1997 if (lb) {
1998 isl_int_sub(v, bmap->ineq[i][1 + j],
1999 bmap->div[div][1 + 1 + j]);
2000 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2002 if (ub) {
2003 isl_int_add(v, bmap->ineq[i][1 + j],
2004 bmap->div[div][1 + 1 + j]);
2005 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2008 if (!lb && !ub)
2009 return bmap;
2011 bmap = isl_basic_map_cow(bmap);
2012 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2013 if (lb) {
2014 int k = isl_basic_map_alloc_inequality(bmap);
2015 if (k < 0)
2016 goto error;
2017 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2018 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2019 bmap->div[div][1 + j]);
2020 isl_int_cdiv_q(bmap->ineq[k][j],
2021 bmap->ineq[k][j], bmap->div[div][0]);
2023 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2025 if (ub) {
2026 int k = isl_basic_map_alloc_inequality(bmap);
2027 if (k < 0)
2028 goto error;
2029 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2030 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2031 bmap->div[div][1 + j]);
2032 isl_int_fdiv_q(bmap->ineq[k][j],
2033 bmap->ineq[k][j], bmap->div[div][0]);
2035 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2038 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2039 return bmap;
2040 error:
2041 isl_basic_map_free(bmap);
2042 return NULL;
2045 /* This function is called right before "div" is eliminated from "bmap"
2046 * using Fourier-Motzkin.
2047 * Look through the constraints of "bmap" for constraints on the argument
2048 * of the integer division and use them to construct constraints on the
2049 * integer division itself. These constraints can then be combined
2050 * during the Fourier-Motzkin elimination.
2051 * Note that it is only useful to introduce lower bounds on "div"
2052 * if "bmap" already contains upper bounds on "div" as the newly
2053 * introduce lower bounds can then be combined with the pre-existing
2054 * upper bounds. Similarly for upper bounds.
2055 * We therefore first check if "bmap" contains any lower and/or upper bounds
2056 * on "div".
2058 * It is interesting to note that the introduction of these constraints
2059 * can indeed lead to more accurate results, even when compared to
2060 * deriving constraints on the argument of "div" from constraints on "div".
2061 * Consider, for example, the set
2063 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2065 * The second constraint can be rewritten as
2067 * 2 * [(-i-2j+3)/4] + k >= 0
2069 * from which we can derive
2071 * -i - 2j + 3 >= -2k
2073 * or
2075 * i + 2j <= 3 + 2k
2077 * Combined with the first constraint, we obtain
2079 * -3 <= 3 + 2k or k >= -3
2081 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2082 * the first constraint, we obtain
2084 * [(i + 2j)/4] >= [-3/4] = -1
2086 * Combining this constraint with the second constraint, we obtain
2088 * k >= -2
2090 static __isl_give isl_basic_map *insert_bounds_on_div(
2091 __isl_take isl_basic_map *bmap, int div)
2093 int i;
2094 int check_lb, check_ub;
2095 isl_int v;
2096 unsigned total;
2098 if (!bmap)
2099 return NULL;
2101 if (isl_int_is_zero(bmap->div[div][0]))
2102 return bmap;
2104 total = isl_space_dim(bmap->dim, isl_dim_all);
2106 check_lb = 0;
2107 check_ub = 0;
2108 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2109 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2110 if (s > 0)
2111 check_ub = 1;
2112 if (s < 0)
2113 check_lb = 1;
2116 if (!check_lb && !check_ub)
2117 return bmap;
2119 isl_int_init(v);
2121 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2122 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2123 continue;
2125 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2126 check_lb, check_ub);
2129 isl_int_clear(v);
2131 return bmap;
2134 /* Remove all divs (recursively) involving any of the given dimensions
2135 * in their definitions.
2137 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2138 __isl_take isl_basic_map *bmap,
2139 enum isl_dim_type type, unsigned first, unsigned n)
2141 int i;
2143 if (!bmap)
2144 return NULL;
2145 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2146 goto error);
2147 first += isl_basic_map_offset(bmap, type);
2149 for (i = bmap->n_div - 1; i >= 0; --i) {
2150 if (!div_involves_vars(bmap, i, first, n))
2151 continue;
2152 bmap = insert_bounds_on_div(bmap, i);
2153 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2154 if (!bmap)
2155 return NULL;
2156 i = bmap->n_div;
2159 return bmap;
2160 error:
2161 isl_basic_map_free(bmap);
2162 return NULL;
2165 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2166 __isl_take isl_basic_set *bset,
2167 enum isl_dim_type type, unsigned first, unsigned n)
2169 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2172 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2173 enum isl_dim_type type, unsigned first, unsigned n)
2175 int i;
2177 if (!map)
2178 return NULL;
2179 if (map->n == 0)
2180 return map;
2182 map = isl_map_cow(map);
2183 if (!map)
2184 return NULL;
2186 for (i = 0; i < map->n; ++i) {
2187 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2188 type, first, n);
2189 if (!map->p[i])
2190 goto error;
2192 return map;
2193 error:
2194 isl_map_free(map);
2195 return NULL;
2198 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2199 enum isl_dim_type type, unsigned first, unsigned n)
2201 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2202 type, first, n);
2205 /* Does the desciption of "bmap" depend on the specified dimensions?
2206 * We also check whether the dimensions appear in any of the div definitions.
2207 * In principle there is no need for this check. If the dimensions appear
2208 * in a div definition, they also appear in the defining constraints of that
2209 * div.
2211 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2212 enum isl_dim_type type, unsigned first, unsigned n)
2214 int i;
2216 if (!bmap)
2217 return -1;
2219 if (first + n > isl_basic_map_dim(bmap, type))
2220 isl_die(bmap->ctx, isl_error_invalid,
2221 "index out of bounds", return -1);
2223 first += isl_basic_map_offset(bmap, type);
2224 for (i = 0; i < bmap->n_eq; ++i)
2225 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2226 return 1;
2227 for (i = 0; i < bmap->n_ineq; ++i)
2228 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2229 return 1;
2230 for (i = 0; i < bmap->n_div; ++i) {
2231 if (isl_int_is_zero(bmap->div[i][0]))
2232 continue;
2233 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2234 return 1;
2237 return 0;
2240 int isl_map_involves_dims(__isl_keep isl_map *map,
2241 enum isl_dim_type type, unsigned first, unsigned n)
2243 int i;
2245 if (!map)
2246 return -1;
2248 if (first + n > isl_map_dim(map, type))
2249 isl_die(map->ctx, isl_error_invalid,
2250 "index out of bounds", return -1);
2252 for (i = 0; i < map->n; ++i) {
2253 int involves = isl_basic_map_involves_dims(map->p[i],
2254 type, first, n);
2255 if (involves < 0 || involves)
2256 return involves;
2259 return 0;
2262 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2263 enum isl_dim_type type, unsigned first, unsigned n)
2265 return isl_basic_map_involves_dims(bset, type, first, n);
2268 int isl_set_involves_dims(__isl_keep isl_set *set,
2269 enum isl_dim_type type, unsigned first, unsigned n)
2271 return isl_map_involves_dims(set, type, first, n);
2274 /* Return true if the definition of the given div is unknown or depends
2275 * on unknown divs.
2277 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2279 int i;
2280 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2282 if (isl_int_is_zero(bmap->div[div][0]))
2283 return 1;
2285 for (i = bmap->n_div - 1; i >= 0; --i) {
2286 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2287 continue;
2288 if (div_is_unknown(bmap, i))
2289 return 1;
2292 return 0;
2295 /* Remove all divs that are unknown or defined in terms of unknown divs.
2297 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2298 __isl_take isl_basic_map *bmap)
2300 int i;
2302 if (!bmap)
2303 return NULL;
2305 for (i = bmap->n_div - 1; i >= 0; --i) {
2306 if (!div_is_unknown(bmap, i))
2307 continue;
2308 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2309 if (!bmap)
2310 return NULL;
2311 i = bmap->n_div;
2314 return bmap;
2317 /* Remove all divs that are unknown or defined in terms of unknown divs.
2319 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2320 __isl_take isl_basic_set *bset)
2322 return isl_basic_map_remove_unknown_divs(bset);
2325 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2327 int i;
2329 if (!map)
2330 return NULL;
2331 if (map->n == 0)
2332 return map;
2334 map = isl_map_cow(map);
2335 if (!map)
2336 return NULL;
2338 for (i = 0; i < map->n; ++i) {
2339 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2340 if (!map->p[i])
2341 goto error;
2343 return map;
2344 error:
2345 isl_map_free(map);
2346 return NULL;
2349 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2351 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2354 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2355 __isl_take isl_basic_set *bset,
2356 enum isl_dim_type type, unsigned first, unsigned n)
2358 return (isl_basic_set *)
2359 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2362 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2363 enum isl_dim_type type, unsigned first, unsigned n)
2365 int i;
2367 if (n == 0)
2368 return map;
2370 map = isl_map_cow(map);
2371 if (!map)
2372 return NULL;
2373 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2375 for (i = 0; i < map->n; ++i) {
2376 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2377 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2378 if (!map->p[i])
2379 goto error;
2381 map = isl_map_drop(map, type, first, n);
2382 return map;
2383 error:
2384 isl_map_free(map);
2385 return NULL;
2388 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2389 enum isl_dim_type type, unsigned first, unsigned n)
2391 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2394 /* Project out n inputs starting at first using Fourier-Motzkin */
2395 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2396 unsigned first, unsigned n)
2398 return isl_map_remove_dims(map, isl_dim_in, first, n);
2401 static void dump_term(struct isl_basic_map *bmap,
2402 isl_int c, int pos, FILE *out)
2404 const char *name;
2405 unsigned in = isl_basic_map_n_in(bmap);
2406 unsigned dim = in + isl_basic_map_n_out(bmap);
2407 unsigned nparam = isl_basic_map_n_param(bmap);
2408 if (!pos)
2409 isl_int_print(out, c, 0);
2410 else {
2411 if (!isl_int_is_one(c))
2412 isl_int_print(out, c, 0);
2413 if (pos < 1 + nparam) {
2414 name = isl_space_get_dim_name(bmap->dim,
2415 isl_dim_param, pos - 1);
2416 if (name)
2417 fprintf(out, "%s", name);
2418 else
2419 fprintf(out, "p%d", pos - 1);
2420 } else if (pos < 1 + nparam + in)
2421 fprintf(out, "i%d", pos - 1 - nparam);
2422 else if (pos < 1 + nparam + dim)
2423 fprintf(out, "o%d", pos - 1 - nparam - in);
2424 else
2425 fprintf(out, "e%d", pos - 1 - nparam - dim);
2429 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2430 int sign, FILE *out)
2432 int i;
2433 int first;
2434 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2435 isl_int v;
2437 isl_int_init(v);
2438 for (i = 0, first = 1; i < len; ++i) {
2439 if (isl_int_sgn(c[i]) * sign <= 0)
2440 continue;
2441 if (!first)
2442 fprintf(out, " + ");
2443 first = 0;
2444 isl_int_abs(v, c[i]);
2445 dump_term(bmap, v, i, out);
2447 isl_int_clear(v);
2448 if (first)
2449 fprintf(out, "0");
2452 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2453 const char *op, FILE *out, int indent)
2455 int i;
2457 fprintf(out, "%*s", indent, "");
2459 dump_constraint_sign(bmap, c, 1, out);
2460 fprintf(out, " %s ", op);
2461 dump_constraint_sign(bmap, c, -1, out);
2463 fprintf(out, "\n");
2465 for (i = bmap->n_div; i < bmap->extra; ++i) {
2466 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2467 continue;
2468 fprintf(out, "%*s", indent, "");
2469 fprintf(out, "ERROR: unused div coefficient not zero\n");
2470 abort();
2474 static void dump_constraints(struct isl_basic_map *bmap,
2475 isl_int **c, unsigned n,
2476 const char *op, FILE *out, int indent)
2478 int i;
2480 for (i = 0; i < n; ++i)
2481 dump_constraint(bmap, c[i], op, out, indent);
2484 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2486 int j;
2487 int first = 1;
2488 unsigned total = isl_basic_map_total_dim(bmap);
2490 for (j = 0; j < 1 + total; ++j) {
2491 if (isl_int_is_zero(exp[j]))
2492 continue;
2493 if (!first && isl_int_is_pos(exp[j]))
2494 fprintf(out, "+");
2495 dump_term(bmap, exp[j], j, out);
2496 first = 0;
2500 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2502 int i;
2504 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2505 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2507 for (i = 0; i < bmap->n_div; ++i) {
2508 fprintf(out, "%*s", indent, "");
2509 fprintf(out, "e%d = [(", i);
2510 dump_affine(bmap, bmap->div[i]+1, out);
2511 fprintf(out, ")/");
2512 isl_int_print(out, bmap->div[i][0], 0);
2513 fprintf(out, "]\n");
2517 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2518 FILE *out, int indent)
2520 if (!bset) {
2521 fprintf(out, "null basic set\n");
2522 return;
2525 fprintf(out, "%*s", indent, "");
2526 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2527 bset->ref, bset->dim->nparam, bset->dim->n_out,
2528 bset->extra, bset->flags);
2529 dump((struct isl_basic_map *)bset, out, indent);
2532 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2533 FILE *out, int indent)
2535 if (!bmap) {
2536 fprintf(out, "null basic map\n");
2537 return;
2540 fprintf(out, "%*s", indent, "");
2541 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2542 "flags: %x, n_name: %d\n",
2543 bmap->ref,
2544 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2545 bmap->extra, bmap->flags, bmap->dim->n_id);
2546 dump(bmap, out, indent);
2549 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2551 unsigned total;
2552 if (!bmap)
2553 return -1;
2554 total = isl_basic_map_total_dim(bmap);
2555 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2556 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2557 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2558 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2559 return 0;
2562 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2563 unsigned flags)
2565 struct isl_set *set;
2567 if (!dim)
2568 return NULL;
2569 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2570 isl_assert(dim->ctx, n >= 0, goto error);
2571 set = isl_alloc(dim->ctx, struct isl_set,
2572 sizeof(struct isl_set) +
2573 (n - 1) * sizeof(struct isl_basic_set *));
2574 if (!set)
2575 goto error;
2577 set->ctx = dim->ctx;
2578 isl_ctx_ref(set->ctx);
2579 set->ref = 1;
2580 set->size = n;
2581 set->n = 0;
2582 set->dim = dim;
2583 set->flags = flags;
2584 return set;
2585 error:
2586 isl_space_free(dim);
2587 return NULL;
2590 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2591 unsigned nparam, unsigned dim, int n, unsigned flags)
2593 struct isl_set *set;
2594 isl_space *dims;
2596 dims = isl_space_alloc(ctx, nparam, 0, dim);
2597 if (!dims)
2598 return NULL;
2600 set = isl_set_alloc_space(dims, n, flags);
2601 return set;
2604 /* Make sure "map" has room for at least "n" more basic maps.
2606 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2608 int i;
2609 struct isl_map *grown = NULL;
2611 if (!map)
2612 return NULL;
2613 isl_assert(map->ctx, n >= 0, goto error);
2614 if (map->n + n <= map->size)
2615 return map;
2616 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2617 if (!grown)
2618 goto error;
2619 for (i = 0; i < map->n; ++i) {
2620 grown->p[i] = isl_basic_map_copy(map->p[i]);
2621 if (!grown->p[i])
2622 goto error;
2623 grown->n++;
2625 isl_map_free(map);
2626 return grown;
2627 error:
2628 isl_map_free(grown);
2629 isl_map_free(map);
2630 return NULL;
2633 /* Make sure "set" has room for at least "n" more basic sets.
2635 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2637 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2640 struct isl_set *isl_set_dup(struct isl_set *set)
2642 int i;
2643 struct isl_set *dup;
2645 if (!set)
2646 return NULL;
2648 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2649 if (!dup)
2650 return NULL;
2651 for (i = 0; i < set->n; ++i)
2652 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2653 return dup;
2656 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2658 return isl_map_from_basic_map(bset);
2661 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2663 struct isl_map *map;
2665 if (!bmap)
2666 return NULL;
2668 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2669 return isl_map_add_basic_map(map, bmap);
2672 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2673 __isl_take isl_basic_set *bset)
2675 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2676 (struct isl_basic_map *)bset);
2679 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2681 int i;
2683 if (!set)
2684 return NULL;
2686 if (--set->ref > 0)
2687 return NULL;
2689 isl_ctx_deref(set->ctx);
2690 for (i = 0; i < set->n; ++i)
2691 isl_basic_set_free(set->p[i]);
2692 isl_space_free(set->dim);
2693 free(set);
2695 return NULL;
2698 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2700 int i;
2702 if (!set) {
2703 fprintf(out, "null set\n");
2704 return;
2707 fprintf(out, "%*s", indent, "");
2708 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2709 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2710 set->flags);
2711 for (i = 0; i < set->n; ++i) {
2712 fprintf(out, "%*s", indent, "");
2713 fprintf(out, "basic set %d:\n", i);
2714 isl_basic_set_print_internal(set->p[i], out, indent+4);
2718 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2720 int i;
2722 if (!map) {
2723 fprintf(out, "null map\n");
2724 return;
2727 fprintf(out, "%*s", indent, "");
2728 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2729 "flags: %x, n_name: %d\n",
2730 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2731 map->dim->n_out, map->flags, map->dim->n_id);
2732 for (i = 0; i < map->n; ++i) {
2733 fprintf(out, "%*s", indent, "");
2734 fprintf(out, "basic map %d:\n", i);
2735 isl_basic_map_print_internal(map->p[i], out, indent+4);
2739 struct isl_basic_map *isl_basic_map_intersect_domain(
2740 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2742 struct isl_basic_map *bmap_domain;
2744 if (!bmap || !bset)
2745 goto error;
2747 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2748 bset->dim, isl_dim_param), goto error);
2750 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2751 isl_assert(bset->ctx,
2752 isl_basic_map_compatible_domain(bmap, bset), goto error);
2754 bmap = isl_basic_map_cow(bmap);
2755 if (!bmap)
2756 goto error;
2757 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2758 bset->n_div, bset->n_eq, bset->n_ineq);
2759 bmap_domain = isl_basic_map_from_domain(bset);
2760 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2762 bmap = isl_basic_map_simplify(bmap);
2763 return isl_basic_map_finalize(bmap);
2764 error:
2765 isl_basic_map_free(bmap);
2766 isl_basic_set_free(bset);
2767 return NULL;
2770 struct isl_basic_map *isl_basic_map_intersect_range(
2771 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2773 struct isl_basic_map *bmap_range;
2775 if (!bmap || !bset)
2776 goto error;
2778 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2779 bset->dim, isl_dim_param), goto error);
2781 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2782 isl_assert(bset->ctx,
2783 isl_basic_map_compatible_range(bmap, bset), goto error);
2785 if (isl_basic_set_is_universe(bset)) {
2786 isl_basic_set_free(bset);
2787 return bmap;
2790 bmap = isl_basic_map_cow(bmap);
2791 if (!bmap)
2792 goto error;
2793 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2794 bset->n_div, bset->n_eq, bset->n_ineq);
2795 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2796 bmap = add_constraints(bmap, bmap_range, 0, 0);
2798 bmap = isl_basic_map_simplify(bmap);
2799 return isl_basic_map_finalize(bmap);
2800 error:
2801 isl_basic_map_free(bmap);
2802 isl_basic_set_free(bset);
2803 return NULL;
2806 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2808 int i;
2809 unsigned total;
2810 isl_int s;
2812 if (!bmap || !vec)
2813 return -1;
2815 total = 1 + isl_basic_map_total_dim(bmap);
2816 if (total != vec->size)
2817 return -1;
2819 isl_int_init(s);
2821 for (i = 0; i < bmap->n_eq; ++i) {
2822 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2823 if (!isl_int_is_zero(s)) {
2824 isl_int_clear(s);
2825 return 0;
2829 for (i = 0; i < bmap->n_ineq; ++i) {
2830 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2831 if (isl_int_is_neg(s)) {
2832 isl_int_clear(s);
2833 return 0;
2837 isl_int_clear(s);
2839 return 1;
2842 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2844 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2847 struct isl_basic_map *isl_basic_map_intersect(
2848 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2850 struct isl_vec *sample = NULL;
2852 if (!bmap1 || !bmap2)
2853 goto error;
2855 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2856 bmap2->dim, isl_dim_param), goto error);
2857 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2858 isl_space_dim(bmap1->dim, isl_dim_param) &&
2859 isl_space_dim(bmap2->dim, isl_dim_all) !=
2860 isl_space_dim(bmap2->dim, isl_dim_param))
2861 return isl_basic_map_intersect(bmap2, bmap1);
2863 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2864 isl_space_dim(bmap2->dim, isl_dim_param))
2865 isl_assert(bmap1->ctx,
2866 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2868 if (bmap1->sample &&
2869 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2870 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2871 sample = isl_vec_copy(bmap1->sample);
2872 else if (bmap2->sample &&
2873 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2874 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2875 sample = isl_vec_copy(bmap2->sample);
2877 bmap1 = isl_basic_map_cow(bmap1);
2878 if (!bmap1)
2879 goto error;
2880 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2881 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2882 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2884 if (!bmap1)
2885 isl_vec_free(sample);
2886 else if (sample) {
2887 isl_vec_free(bmap1->sample);
2888 bmap1->sample = sample;
2891 bmap1 = isl_basic_map_simplify(bmap1);
2892 return isl_basic_map_finalize(bmap1);
2893 error:
2894 if (sample)
2895 isl_vec_free(sample);
2896 isl_basic_map_free(bmap1);
2897 isl_basic_map_free(bmap2);
2898 return NULL;
2901 struct isl_basic_set *isl_basic_set_intersect(
2902 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2904 return (struct isl_basic_set *)
2905 isl_basic_map_intersect(
2906 (struct isl_basic_map *)bset1,
2907 (struct isl_basic_map *)bset2);
2910 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2911 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2913 return isl_basic_set_intersect(bset1, bset2);
2916 /* Special case of isl_map_intersect, where both map1 and map2
2917 * are convex, without any divs and such that either map1 or map2
2918 * contains a single constraint. This constraint is then simply
2919 * added to the other map.
2921 static __isl_give isl_map *map_intersect_add_constraint(
2922 __isl_take isl_map *map1, __isl_take isl_map *map2)
2924 isl_assert(map1->ctx, map1->n == 1, goto error);
2925 isl_assert(map2->ctx, map1->n == 1, goto error);
2926 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2927 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2929 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2930 return isl_map_intersect(map2, map1);
2932 isl_assert(map2->ctx,
2933 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2935 map1 = isl_map_cow(map1);
2936 if (!map1)
2937 goto error;
2938 if (isl_map_plain_is_empty(map1)) {
2939 isl_map_free(map2);
2940 return map1;
2942 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2943 if (map2->p[0]->n_eq == 1)
2944 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2945 else
2946 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2947 map2->p[0]->ineq[0]);
2949 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2950 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2951 if (!map1->p[0])
2952 goto error;
2954 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2955 isl_basic_map_free(map1->p[0]);
2956 map1->n = 0;
2959 isl_map_free(map2);
2961 return map1;
2962 error:
2963 isl_map_free(map1);
2964 isl_map_free(map2);
2965 return NULL;
2968 /* map2 may be either a parameter domain or a map living in the same
2969 * space as map1.
2971 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2972 __isl_take isl_map *map2)
2974 unsigned flags = 0;
2975 isl_map *result;
2976 int i, j;
2978 if (!map1 || !map2)
2979 goto error;
2981 if ((isl_map_plain_is_empty(map1) ||
2982 isl_map_plain_is_universe(map2)) &&
2983 isl_space_is_equal(map1->dim, map2->dim)) {
2984 isl_map_free(map2);
2985 return map1;
2987 if ((isl_map_plain_is_empty(map2) ||
2988 isl_map_plain_is_universe(map1)) &&
2989 isl_space_is_equal(map1->dim, map2->dim)) {
2990 isl_map_free(map1);
2991 return map2;
2994 if (map1->n == 1 && map2->n == 1 &&
2995 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2996 isl_space_is_equal(map1->dim, map2->dim) &&
2997 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2998 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2999 return map_intersect_add_constraint(map1, map2);
3001 if (isl_space_dim(map2->dim, isl_dim_all) !=
3002 isl_space_dim(map2->dim, isl_dim_param))
3003 isl_assert(map1->ctx,
3004 isl_space_is_equal(map1->dim, map2->dim), goto error);
3006 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3007 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3008 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3010 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3011 map1->n * map2->n, flags);
3012 if (!result)
3013 goto error;
3014 for (i = 0; i < map1->n; ++i)
3015 for (j = 0; j < map2->n; ++j) {
3016 struct isl_basic_map *part;
3017 part = isl_basic_map_intersect(
3018 isl_basic_map_copy(map1->p[i]),
3019 isl_basic_map_copy(map2->p[j]));
3020 if (isl_basic_map_is_empty(part) < 0)
3021 part = isl_basic_map_free(part);
3022 result = isl_map_add_basic_map(result, part);
3023 if (!result)
3024 goto error;
3026 isl_map_free(map1);
3027 isl_map_free(map2);
3028 return result;
3029 error:
3030 isl_map_free(map1);
3031 isl_map_free(map2);
3032 return NULL;
3035 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3036 __isl_take isl_map *map2)
3038 if (!map1 || !map2)
3039 goto error;
3040 if (!isl_space_is_equal(map1->dim, map2->dim))
3041 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3042 "spaces don't match", goto error);
3043 return map_intersect_internal(map1, map2);
3044 error:
3045 isl_map_free(map1);
3046 isl_map_free(map2);
3047 return NULL;
3050 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3051 __isl_take isl_map *map2)
3053 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3056 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3058 return (struct isl_set *)
3059 isl_map_intersect((struct isl_map *)set1,
3060 (struct isl_map *)set2);
3063 /* map_intersect_internal accepts intersections
3064 * with parameter domains, so we can just call that function.
3066 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3067 __isl_take isl_set *params)
3069 return map_intersect_internal(map, params);
3072 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3073 __isl_take isl_map *map2)
3075 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3078 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3079 __isl_take isl_set *params)
3081 return isl_map_intersect_params(set, params);
3084 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3086 isl_space *dim;
3087 struct isl_basic_set *bset;
3088 unsigned in;
3090 if (!bmap)
3091 return NULL;
3092 bmap = isl_basic_map_cow(bmap);
3093 if (!bmap)
3094 return NULL;
3095 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3096 in = isl_basic_map_n_in(bmap);
3097 bset = isl_basic_set_from_basic_map(bmap);
3098 bset = isl_basic_set_swap_vars(bset, in);
3099 return isl_basic_map_from_basic_set(bset, dim);
3102 static __isl_give isl_basic_map *basic_map_space_reset(
3103 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3105 isl_space *space;
3107 if (!bmap)
3108 return NULL;
3109 if (!isl_space_is_named_or_nested(bmap->dim, type))
3110 return bmap;
3112 space = isl_basic_map_get_space(bmap);
3113 space = isl_space_reset(space, type);
3114 bmap = isl_basic_map_reset_space(bmap, space);
3115 return bmap;
3118 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3119 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3120 unsigned pos, unsigned n)
3122 isl_space *res_dim;
3123 struct isl_basic_map *res;
3124 struct isl_dim_map *dim_map;
3125 unsigned total, off;
3126 enum isl_dim_type t;
3128 if (n == 0)
3129 return basic_map_space_reset(bmap, type);
3131 if (!bmap)
3132 return NULL;
3134 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3136 total = isl_basic_map_total_dim(bmap) + n;
3137 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3138 off = 0;
3139 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3140 if (t != type) {
3141 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3142 } else {
3143 unsigned size = isl_basic_map_dim(bmap, t);
3144 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3145 0, pos, off);
3146 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3147 pos, size - pos, off + pos + n);
3149 off += isl_space_dim(res_dim, t);
3151 isl_dim_map_div(dim_map, bmap, off);
3153 res = isl_basic_map_alloc_space(res_dim,
3154 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3155 if (isl_basic_map_is_rational(bmap))
3156 res = isl_basic_map_set_rational(res);
3157 if (isl_basic_map_plain_is_empty(bmap)) {
3158 isl_basic_map_free(bmap);
3159 free(dim_map);
3160 return isl_basic_map_set_to_empty(res);
3162 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3163 return isl_basic_map_finalize(res);
3166 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3167 __isl_take isl_basic_set *bset,
3168 enum isl_dim_type type, unsigned pos, unsigned n)
3170 return isl_basic_map_insert_dims(bset, type, pos, n);
3173 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3174 enum isl_dim_type type, unsigned n)
3176 if (!bmap)
3177 return NULL;
3178 return isl_basic_map_insert_dims(bmap, type,
3179 isl_basic_map_dim(bmap, type), n);
3182 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3183 enum isl_dim_type type, unsigned n)
3185 if (!bset)
3186 return NULL;
3187 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3188 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3189 error:
3190 isl_basic_set_free(bset);
3191 return NULL;
3194 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3195 enum isl_dim_type type)
3197 isl_space *space;
3199 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3200 return map;
3202 space = isl_map_get_space(map);
3203 space = isl_space_reset(space, type);
3204 map = isl_map_reset_space(map, space);
3205 return map;
3208 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3209 enum isl_dim_type type, unsigned pos, unsigned n)
3211 int i;
3213 if (n == 0)
3214 return map_space_reset(map, type);
3216 map = isl_map_cow(map);
3217 if (!map)
3218 return NULL;
3220 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3221 if (!map->dim)
3222 goto error;
3224 for (i = 0; i < map->n; ++i) {
3225 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3226 if (!map->p[i])
3227 goto error;
3230 return map;
3231 error:
3232 isl_map_free(map);
3233 return NULL;
3236 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3237 enum isl_dim_type type, unsigned pos, unsigned n)
3239 return isl_map_insert_dims(set, type, pos, n);
3242 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3243 enum isl_dim_type type, unsigned n)
3245 if (!map)
3246 return NULL;
3247 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3250 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3251 enum isl_dim_type type, unsigned n)
3253 if (!set)
3254 return NULL;
3255 isl_assert(set->ctx, type != isl_dim_in, goto error);
3256 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3257 error:
3258 isl_set_free(set);
3259 return NULL;
3262 __isl_give isl_basic_map *isl_basic_map_move_dims(
3263 __isl_take isl_basic_map *bmap,
3264 enum isl_dim_type dst_type, unsigned dst_pos,
3265 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3267 struct isl_dim_map *dim_map;
3268 struct isl_basic_map *res;
3269 enum isl_dim_type t;
3270 unsigned total, off;
3272 if (!bmap)
3273 return NULL;
3274 if (n == 0)
3275 return bmap;
3277 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3278 goto error);
3280 if (dst_type == src_type && dst_pos == src_pos)
3281 return bmap;
3283 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3285 if (pos(bmap->dim, dst_type) + dst_pos ==
3286 pos(bmap->dim, src_type) + src_pos +
3287 ((src_type < dst_type) ? n : 0)) {
3288 bmap = isl_basic_map_cow(bmap);
3289 if (!bmap)
3290 return NULL;
3292 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3293 src_type, src_pos, n);
3294 if (!bmap->dim)
3295 goto error;
3297 bmap = isl_basic_map_finalize(bmap);
3299 return bmap;
3302 total = isl_basic_map_total_dim(bmap);
3303 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3305 off = 0;
3306 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3307 unsigned size = isl_space_dim(bmap->dim, t);
3308 if (t == dst_type) {
3309 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3310 0, dst_pos, off);
3311 off += dst_pos;
3312 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3313 src_pos, n, off);
3314 off += n;
3315 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3316 dst_pos, size - dst_pos, off);
3317 off += size - dst_pos;
3318 } else if (t == src_type) {
3319 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3320 0, src_pos, off);
3321 off += src_pos;
3322 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3323 src_pos + n, size - src_pos - n, off);
3324 off += size - src_pos - n;
3325 } else {
3326 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3327 off += size;
3330 isl_dim_map_div(dim_map, bmap, off);
3332 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3333 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3334 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3335 if (!bmap)
3336 goto error;
3338 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3339 src_type, src_pos, n);
3340 if (!bmap->dim)
3341 goto error;
3343 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3344 bmap = isl_basic_map_gauss(bmap, NULL);
3345 bmap = isl_basic_map_finalize(bmap);
3347 return bmap;
3348 error:
3349 isl_basic_map_free(bmap);
3350 return NULL;
3353 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3354 enum isl_dim_type dst_type, unsigned dst_pos,
3355 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3357 return (isl_basic_set *)isl_basic_map_move_dims(
3358 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3361 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3362 enum isl_dim_type dst_type, unsigned dst_pos,
3363 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3365 if (!set)
3366 return NULL;
3367 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3368 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3369 src_type, src_pos, n);
3370 error:
3371 isl_set_free(set);
3372 return NULL;
3375 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3376 enum isl_dim_type dst_type, unsigned dst_pos,
3377 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3379 int i;
3381 if (!map)
3382 return NULL;
3383 if (n == 0)
3384 return map;
3386 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3387 goto error);
3389 if (dst_type == src_type && dst_pos == src_pos)
3390 return map;
3392 isl_assert(map->ctx, dst_type != src_type, goto error);
3394 map = isl_map_cow(map);
3395 if (!map)
3396 return NULL;
3398 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3399 if (!map->dim)
3400 goto error;
3402 for (i = 0; i < map->n; ++i) {
3403 map->p[i] = isl_basic_map_move_dims(map->p[i],
3404 dst_type, dst_pos,
3405 src_type, src_pos, n);
3406 if (!map->p[i])
3407 goto error;
3410 return map;
3411 error:
3412 isl_map_free(map);
3413 return NULL;
3416 /* Move the specified dimensions to the last columns right before
3417 * the divs. Don't change the dimension specification of bmap.
3418 * That's the responsibility of the caller.
3420 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3421 enum isl_dim_type type, unsigned first, unsigned n)
3423 struct isl_dim_map *dim_map;
3424 struct isl_basic_map *res;
3425 enum isl_dim_type t;
3426 unsigned total, off;
3428 if (!bmap)
3429 return NULL;
3430 if (pos(bmap->dim, type) + first + n ==
3431 1 + isl_space_dim(bmap->dim, isl_dim_all))
3432 return bmap;
3434 total = isl_basic_map_total_dim(bmap);
3435 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3437 off = 0;
3438 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3439 unsigned size = isl_space_dim(bmap->dim, t);
3440 if (t == type) {
3441 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3442 0, first, off);
3443 off += first;
3444 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3445 first, n, total - bmap->n_div - n);
3446 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3447 first + n, size - (first + n), off);
3448 off += size - (first + n);
3449 } else {
3450 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3451 off += size;
3454 isl_dim_map_div(dim_map, bmap, off + n);
3456 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3457 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3458 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3459 return res;
3462 /* Insert "n" rows in the divs of "bmap".
3464 * The number of columns is not changed, which means that the last
3465 * dimensions of "bmap" are being reintepreted as the new divs.
3466 * The space of "bmap" is not adjusted, however, which means
3467 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3468 * from the space of "bmap" is the responsibility of the caller.
3470 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3471 int n)
3473 int i;
3474 size_t row_size;
3475 isl_int **new_div;
3476 isl_int *old;
3478 bmap = isl_basic_map_cow(bmap);
3479 if (!bmap)
3480 return NULL;
3482 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3483 old = bmap->block2.data;
3484 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3485 (bmap->extra + n) * (1 + row_size));
3486 if (!bmap->block2.data)
3487 return isl_basic_map_free(bmap);
3488 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3489 if (!new_div)
3490 return isl_basic_map_free(bmap);
3491 for (i = 0; i < n; ++i) {
3492 new_div[i] = bmap->block2.data +
3493 (bmap->extra + i) * (1 + row_size);
3494 isl_seq_clr(new_div[i], 1 + row_size);
3496 for (i = 0; i < bmap->extra; ++i)
3497 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3498 free(bmap->div);
3499 bmap->div = new_div;
3500 bmap->n_div += n;
3501 bmap->extra += n;
3503 return bmap;
3506 /* Turn the n dimensions of type type, starting at first
3507 * into existentially quantified variables.
3509 __isl_give isl_basic_map *isl_basic_map_project_out(
3510 __isl_take isl_basic_map *bmap,
3511 enum isl_dim_type type, unsigned first, unsigned n)
3513 if (n == 0)
3514 return basic_map_space_reset(bmap, type);
3516 if (!bmap)
3517 return NULL;
3519 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3520 return isl_basic_map_remove_dims(bmap, type, first, n);
3522 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3523 goto error);
3525 bmap = move_last(bmap, type, first, n);
3526 bmap = isl_basic_map_cow(bmap);
3527 bmap = insert_div_rows(bmap, n);
3528 if (!bmap)
3529 return NULL;
3531 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3532 if (!bmap->dim)
3533 goto error;
3534 bmap = isl_basic_map_simplify(bmap);
3535 bmap = isl_basic_map_drop_redundant_divs(bmap);
3536 return isl_basic_map_finalize(bmap);
3537 error:
3538 isl_basic_map_free(bmap);
3539 return NULL;
3542 /* Turn the n dimensions of type type, starting at first
3543 * into existentially quantified variables.
3545 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3546 enum isl_dim_type type, unsigned first, unsigned n)
3548 return (isl_basic_set *)isl_basic_map_project_out(
3549 (isl_basic_map *)bset, type, first, n);
3552 /* Turn the n dimensions of type type, starting at first
3553 * into existentially quantified variables.
3555 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3556 enum isl_dim_type type, unsigned first, unsigned n)
3558 int i;
3560 if (!map)
3561 return NULL;
3563 if (n == 0)
3564 return map_space_reset(map, type);
3566 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3568 map = isl_map_cow(map);
3569 if (!map)
3570 return NULL;
3572 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3573 if (!map->dim)
3574 goto error;
3576 for (i = 0; i < map->n; ++i) {
3577 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3578 if (!map->p[i])
3579 goto error;
3582 return map;
3583 error:
3584 isl_map_free(map);
3585 return NULL;
3588 /* Turn the n dimensions of type type, starting at first
3589 * into existentially quantified variables.
3591 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3592 enum isl_dim_type type, unsigned first, unsigned n)
3594 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3597 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3599 int i, j;
3601 for (i = 0; i < n; ++i) {
3602 j = isl_basic_map_alloc_div(bmap);
3603 if (j < 0)
3604 goto error;
3605 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3607 return bmap;
3608 error:
3609 isl_basic_map_free(bmap);
3610 return NULL;
3613 struct isl_basic_map *isl_basic_map_apply_range(
3614 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3616 isl_space *dim_result = NULL;
3617 struct isl_basic_map *bmap;
3618 unsigned n_in, n_out, n, nparam, total, pos;
3619 struct isl_dim_map *dim_map1, *dim_map2;
3621 if (!bmap1 || !bmap2)
3622 goto error;
3623 if (!isl_space_match(bmap1->dim, isl_dim_param,
3624 bmap2->dim, isl_dim_param))
3625 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3626 "parameters don't match", goto error);
3627 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3628 bmap2->dim, isl_dim_in))
3629 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3630 "spaces don't match", goto error);
3632 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3633 isl_space_copy(bmap2->dim));
3635 n_in = isl_basic_map_n_in(bmap1);
3636 n_out = isl_basic_map_n_out(bmap2);
3637 n = isl_basic_map_n_out(bmap1);
3638 nparam = isl_basic_map_n_param(bmap1);
3640 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3641 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3642 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3643 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3644 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3645 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3646 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3647 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3648 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3649 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3650 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3652 bmap = isl_basic_map_alloc_space(dim_result,
3653 bmap1->n_div + bmap2->n_div + n,
3654 bmap1->n_eq + bmap2->n_eq,
3655 bmap1->n_ineq + bmap2->n_ineq);
3656 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3657 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3658 bmap = add_divs(bmap, n);
3659 bmap = isl_basic_map_simplify(bmap);
3660 bmap = isl_basic_map_drop_redundant_divs(bmap);
3661 return isl_basic_map_finalize(bmap);
3662 error:
3663 isl_basic_map_free(bmap1);
3664 isl_basic_map_free(bmap2);
3665 return NULL;
3668 struct isl_basic_set *isl_basic_set_apply(
3669 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3671 if (!bset || !bmap)
3672 goto error;
3674 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3675 goto error);
3677 return (struct isl_basic_set *)
3678 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3679 error:
3680 isl_basic_set_free(bset);
3681 isl_basic_map_free(bmap);
3682 return NULL;
3685 struct isl_basic_map *isl_basic_map_apply_domain(
3686 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3688 if (!bmap1 || !bmap2)
3689 goto error;
3691 isl_assert(bmap1->ctx,
3692 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3693 isl_assert(bmap1->ctx,
3694 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3695 goto error);
3697 bmap1 = isl_basic_map_reverse(bmap1);
3698 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3699 return isl_basic_map_reverse(bmap1);
3700 error:
3701 isl_basic_map_free(bmap1);
3702 isl_basic_map_free(bmap2);
3703 return NULL;
3706 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3707 * A \cap B -> f(A) + f(B)
3709 struct isl_basic_map *isl_basic_map_sum(
3710 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3712 unsigned n_in, n_out, nparam, total, pos;
3713 struct isl_basic_map *bmap = NULL;
3714 struct isl_dim_map *dim_map1, *dim_map2;
3715 int i;
3717 if (!bmap1 || !bmap2)
3718 goto error;
3720 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3721 goto error);
3723 nparam = isl_basic_map_n_param(bmap1);
3724 n_in = isl_basic_map_n_in(bmap1);
3725 n_out = isl_basic_map_n_out(bmap1);
3727 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3728 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3729 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3730 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3731 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3732 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3733 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3734 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3735 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3736 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3737 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3739 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3740 bmap1->n_div + bmap2->n_div + 2 * n_out,
3741 bmap1->n_eq + bmap2->n_eq + n_out,
3742 bmap1->n_ineq + bmap2->n_ineq);
3743 for (i = 0; i < n_out; ++i) {
3744 int j = isl_basic_map_alloc_equality(bmap);
3745 if (j < 0)
3746 goto error;
3747 isl_seq_clr(bmap->eq[j], 1+total);
3748 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3749 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3750 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3752 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3753 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3754 bmap = add_divs(bmap, 2 * n_out);
3756 bmap = isl_basic_map_simplify(bmap);
3757 return isl_basic_map_finalize(bmap);
3758 error:
3759 isl_basic_map_free(bmap);
3760 isl_basic_map_free(bmap1);
3761 isl_basic_map_free(bmap2);
3762 return NULL;
3765 /* Given two maps A -> f(A) and B -> g(B), construct a map
3766 * A \cap B -> f(A) + f(B)
3768 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3770 struct isl_map *result;
3771 int i, j;
3773 if (!map1 || !map2)
3774 goto error;
3776 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3778 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3779 map1->n * map2->n, 0);
3780 if (!result)
3781 goto error;
3782 for (i = 0; i < map1->n; ++i)
3783 for (j = 0; j < map2->n; ++j) {
3784 struct isl_basic_map *part;
3785 part = isl_basic_map_sum(
3786 isl_basic_map_copy(map1->p[i]),
3787 isl_basic_map_copy(map2->p[j]));
3788 if (isl_basic_map_is_empty(part))
3789 isl_basic_map_free(part);
3790 else
3791 result = isl_map_add_basic_map(result, part);
3792 if (!result)
3793 goto error;
3795 isl_map_free(map1);
3796 isl_map_free(map2);
3797 return result;
3798 error:
3799 isl_map_free(map1);
3800 isl_map_free(map2);
3801 return NULL;
3804 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3805 __isl_take isl_set *set2)
3807 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3810 /* Given a basic map A -> f(A), construct A -> -f(A).
3812 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3814 int i, j;
3815 unsigned off, n;
3817 bmap = isl_basic_map_cow(bmap);
3818 if (!bmap)
3819 return NULL;
3821 n = isl_basic_map_dim(bmap, isl_dim_out);
3822 off = isl_basic_map_offset(bmap, isl_dim_out);
3823 for (i = 0; i < bmap->n_eq; ++i)
3824 for (j = 0; j < n; ++j)
3825 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3826 for (i = 0; i < bmap->n_ineq; ++i)
3827 for (j = 0; j < n; ++j)
3828 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3829 for (i = 0; i < bmap->n_div; ++i)
3830 for (j = 0; j < n; ++j)
3831 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3832 bmap = isl_basic_map_gauss(bmap, NULL);
3833 return isl_basic_map_finalize(bmap);
3836 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3838 return isl_basic_map_neg(bset);
3841 /* Given a map A -> f(A), construct A -> -f(A).
3843 struct isl_map *isl_map_neg(struct isl_map *map)
3845 int i;
3847 map = isl_map_cow(map);
3848 if (!map)
3849 return NULL;
3851 for (i = 0; i < map->n; ++i) {
3852 map->p[i] = isl_basic_map_neg(map->p[i]);
3853 if (!map->p[i])
3854 goto error;
3857 return map;
3858 error:
3859 isl_map_free(map);
3860 return NULL;
3863 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3865 return (isl_set *)isl_map_neg((isl_map *)set);
3868 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3869 * A -> floor(f(A)/d).
3871 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3872 isl_int d)
3874 unsigned n_in, n_out, nparam, total, pos;
3875 struct isl_basic_map *result = NULL;
3876 struct isl_dim_map *dim_map;
3877 int i;
3879 if (!bmap)
3880 return NULL;
3882 nparam = isl_basic_map_n_param(bmap);
3883 n_in = isl_basic_map_n_in(bmap);
3884 n_out = isl_basic_map_n_out(bmap);
3886 total = nparam + n_in + n_out + bmap->n_div + n_out;
3887 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3888 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3889 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3890 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3891 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3893 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3894 bmap->n_div + n_out,
3895 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3896 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3897 result = add_divs(result, n_out);
3898 for (i = 0; i < n_out; ++i) {
3899 int j;
3900 j = isl_basic_map_alloc_inequality(result);
3901 if (j < 0)
3902 goto error;
3903 isl_seq_clr(result->ineq[j], 1+total);
3904 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3905 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3906 j = isl_basic_map_alloc_inequality(result);
3907 if (j < 0)
3908 goto error;
3909 isl_seq_clr(result->ineq[j], 1+total);
3910 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3911 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3912 isl_int_sub_ui(result->ineq[j][0], d, 1);
3915 result = isl_basic_map_simplify(result);
3916 return isl_basic_map_finalize(result);
3917 error:
3918 isl_basic_map_free(result);
3919 return NULL;
3922 /* Given a map A -> f(A) and an integer d, construct a map
3923 * A -> floor(f(A)/d).
3925 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3927 int i;
3929 map = isl_map_cow(map);
3930 if (!map)
3931 return NULL;
3933 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3934 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3935 for (i = 0; i < map->n; ++i) {
3936 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3937 if (!map->p[i])
3938 goto error;
3941 return map;
3942 error:
3943 isl_map_free(map);
3944 return NULL;
3947 /* Given a map A -> f(A) and an integer d, construct a map
3948 * A -> floor(f(A)/d).
3950 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3951 __isl_take isl_val *d)
3953 if (!map || !d)
3954 goto error;
3955 if (!isl_val_is_int(d))
3956 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3957 "expecting integer denominator", goto error);
3958 map = isl_map_floordiv(map, d->n);
3959 isl_val_free(d);
3960 return map;
3961 error:
3962 isl_map_free(map);
3963 isl_val_free(d);
3964 return NULL;
3967 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3969 int i;
3970 unsigned nparam;
3971 unsigned n_in;
3973 i = isl_basic_map_alloc_equality(bmap);
3974 if (i < 0)
3975 goto error;
3976 nparam = isl_basic_map_n_param(bmap);
3977 n_in = isl_basic_map_n_in(bmap);
3978 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3979 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3980 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3981 return isl_basic_map_finalize(bmap);
3982 error:
3983 isl_basic_map_free(bmap);
3984 return NULL;
3987 /* Add a constraints to "bmap" expressing i_pos < o_pos
3989 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3991 int i;
3992 unsigned nparam;
3993 unsigned n_in;
3995 i = isl_basic_map_alloc_inequality(bmap);
3996 if (i < 0)
3997 goto error;
3998 nparam = isl_basic_map_n_param(bmap);
3999 n_in = isl_basic_map_n_in(bmap);
4000 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4001 isl_int_set_si(bmap->ineq[i][0], -1);
4002 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4003 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4004 return isl_basic_map_finalize(bmap);
4005 error:
4006 isl_basic_map_free(bmap);
4007 return NULL;
4010 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4012 static __isl_give isl_basic_map *var_less_or_equal(
4013 __isl_take isl_basic_map *bmap, unsigned pos)
4015 int i;
4016 unsigned nparam;
4017 unsigned n_in;
4019 i = isl_basic_map_alloc_inequality(bmap);
4020 if (i < 0)
4021 goto error;
4022 nparam = isl_basic_map_n_param(bmap);
4023 n_in = isl_basic_map_n_in(bmap);
4024 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4025 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4026 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4027 return isl_basic_map_finalize(bmap);
4028 error:
4029 isl_basic_map_free(bmap);
4030 return NULL;
4033 /* Add a constraints to "bmap" expressing i_pos > o_pos
4035 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4037 int i;
4038 unsigned nparam;
4039 unsigned n_in;
4041 i = isl_basic_map_alloc_inequality(bmap);
4042 if (i < 0)
4043 goto error;
4044 nparam = isl_basic_map_n_param(bmap);
4045 n_in = isl_basic_map_n_in(bmap);
4046 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4047 isl_int_set_si(bmap->ineq[i][0], -1);
4048 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4049 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4050 return isl_basic_map_finalize(bmap);
4051 error:
4052 isl_basic_map_free(bmap);
4053 return NULL;
4056 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4058 static __isl_give isl_basic_map *var_more_or_equal(
4059 __isl_take isl_basic_map *bmap, unsigned pos)
4061 int i;
4062 unsigned nparam;
4063 unsigned n_in;
4065 i = isl_basic_map_alloc_inequality(bmap);
4066 if (i < 0)
4067 goto error;
4068 nparam = isl_basic_map_n_param(bmap);
4069 n_in = isl_basic_map_n_in(bmap);
4070 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4071 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4072 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4073 return isl_basic_map_finalize(bmap);
4074 error:
4075 isl_basic_map_free(bmap);
4076 return NULL;
4079 __isl_give isl_basic_map *isl_basic_map_equal(
4080 __isl_take isl_space *dim, unsigned n_equal)
4082 int i;
4083 struct isl_basic_map *bmap;
4084 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4085 if (!bmap)
4086 return NULL;
4087 for (i = 0; i < n_equal && bmap; ++i)
4088 bmap = var_equal(bmap, i);
4089 return isl_basic_map_finalize(bmap);
4092 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4094 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4095 unsigned pos)
4097 int i;
4098 struct isl_basic_map *bmap;
4099 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4100 if (!bmap)
4101 return NULL;
4102 for (i = 0; i < pos && bmap; ++i)
4103 bmap = var_equal(bmap, i);
4104 if (bmap)
4105 bmap = var_less(bmap, pos);
4106 return isl_basic_map_finalize(bmap);
4109 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4111 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4112 __isl_take isl_space *dim, unsigned pos)
4114 int i;
4115 isl_basic_map *bmap;
4117 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4118 for (i = 0; i < pos; ++i)
4119 bmap = var_equal(bmap, i);
4120 bmap = var_less_or_equal(bmap, pos);
4121 return isl_basic_map_finalize(bmap);
4124 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4126 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4127 unsigned pos)
4129 int i;
4130 struct isl_basic_map *bmap;
4131 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4132 if (!bmap)
4133 return NULL;
4134 for (i = 0; i < pos && bmap; ++i)
4135 bmap = var_equal(bmap, i);
4136 if (bmap)
4137 bmap = var_more(bmap, pos);
4138 return isl_basic_map_finalize(bmap);
4141 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4143 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4144 __isl_take isl_space *dim, unsigned pos)
4146 int i;
4147 isl_basic_map *bmap;
4149 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4150 for (i = 0; i < pos; ++i)
4151 bmap = var_equal(bmap, i);
4152 bmap = var_more_or_equal(bmap, pos);
4153 return isl_basic_map_finalize(bmap);
4156 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4157 unsigned n, int equal)
4159 struct isl_map *map;
4160 int i;
4162 if (n == 0 && equal)
4163 return isl_map_universe(dims);
4165 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4167 for (i = 0; i + 1 < n; ++i)
4168 map = isl_map_add_basic_map(map,
4169 isl_basic_map_less_at(isl_space_copy(dims), i));
4170 if (n > 0) {
4171 if (equal)
4172 map = isl_map_add_basic_map(map,
4173 isl_basic_map_less_or_equal_at(dims, n - 1));
4174 else
4175 map = isl_map_add_basic_map(map,
4176 isl_basic_map_less_at(dims, n - 1));
4177 } else
4178 isl_space_free(dims);
4180 return map;
4183 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4185 if (!dims)
4186 return NULL;
4187 return map_lex_lte_first(dims, dims->n_out, equal);
4190 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4192 return map_lex_lte_first(dim, n, 0);
4195 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4197 return map_lex_lte_first(dim, n, 1);
4200 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4202 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4205 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4207 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4210 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4211 unsigned n, int equal)
4213 struct isl_map *map;
4214 int i;
4216 if (n == 0 && equal)
4217 return isl_map_universe(dims);
4219 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4221 for (i = 0; i + 1 < n; ++i)
4222 map = isl_map_add_basic_map(map,
4223 isl_basic_map_more_at(isl_space_copy(dims), i));
4224 if (n > 0) {
4225 if (equal)
4226 map = isl_map_add_basic_map(map,
4227 isl_basic_map_more_or_equal_at(dims, n - 1));
4228 else
4229 map = isl_map_add_basic_map(map,
4230 isl_basic_map_more_at(dims, n - 1));
4231 } else
4232 isl_space_free(dims);
4234 return map;
4237 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4239 if (!dims)
4240 return NULL;
4241 return map_lex_gte_first(dims, dims->n_out, equal);
4244 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4246 return map_lex_gte_first(dim, n, 0);
4249 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4251 return map_lex_gte_first(dim, n, 1);
4254 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4256 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4259 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4261 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4264 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4265 __isl_take isl_set *set2)
4267 isl_map *map;
4268 map = isl_map_lex_le(isl_set_get_space(set1));
4269 map = isl_map_intersect_domain(map, set1);
4270 map = isl_map_intersect_range(map, set2);
4271 return map;
4274 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4275 __isl_take isl_set *set2)
4277 isl_map *map;
4278 map = isl_map_lex_lt(isl_set_get_space(set1));
4279 map = isl_map_intersect_domain(map, set1);
4280 map = isl_map_intersect_range(map, set2);
4281 return map;
4284 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4285 __isl_take isl_set *set2)
4287 isl_map *map;
4288 map = isl_map_lex_ge(isl_set_get_space(set1));
4289 map = isl_map_intersect_domain(map, set1);
4290 map = isl_map_intersect_range(map, set2);
4291 return map;
4294 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4295 __isl_take isl_set *set2)
4297 isl_map *map;
4298 map = isl_map_lex_gt(isl_set_get_space(set1));
4299 map = isl_map_intersect_domain(map, set1);
4300 map = isl_map_intersect_range(map, set2);
4301 return map;
4304 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4305 __isl_take isl_map *map2)
4307 isl_map *map;
4308 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4309 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4310 map = isl_map_apply_range(map, isl_map_reverse(map2));
4311 return map;
4314 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4315 __isl_take isl_map *map2)
4317 isl_map *map;
4318 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4319 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4320 map = isl_map_apply_range(map, isl_map_reverse(map2));
4321 return map;
4324 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4325 __isl_take isl_map *map2)
4327 isl_map *map;
4328 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4329 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4330 map = isl_map_apply_range(map, isl_map_reverse(map2));
4331 return map;
4334 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4335 __isl_take isl_map *map2)
4337 isl_map *map;
4338 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4339 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4340 map = isl_map_apply_range(map, isl_map_reverse(map2));
4341 return map;
4344 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4345 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4347 struct isl_basic_map *bmap;
4349 bset = isl_basic_set_cow(bset);
4350 if (!bset || !dim)
4351 goto error;
4353 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4354 isl_space_free(bset->dim);
4355 bmap = (struct isl_basic_map *) bset;
4356 bmap->dim = dim;
4357 return isl_basic_map_finalize(bmap);
4358 error:
4359 isl_basic_set_free(bset);
4360 isl_space_free(dim);
4361 return NULL;
4364 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4366 if (!bmap)
4367 goto error;
4368 if (bmap->dim->n_in == 0)
4369 return (struct isl_basic_set *)bmap;
4370 bmap = isl_basic_map_cow(bmap);
4371 if (!bmap)
4372 goto error;
4373 bmap->dim = isl_space_as_set_space(bmap->dim);
4374 if (!bmap->dim)
4375 goto error;
4376 bmap = isl_basic_map_finalize(bmap);
4377 return (struct isl_basic_set *)bmap;
4378 error:
4379 isl_basic_map_free(bmap);
4380 return NULL;
4383 /* For a div d = floor(f/m), add the constraint
4385 * f - m d >= 0
4387 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4388 unsigned pos, isl_int *div)
4390 int i;
4391 unsigned total = isl_basic_map_total_dim(bmap);
4393 i = isl_basic_map_alloc_inequality(bmap);
4394 if (i < 0)
4395 return -1;
4396 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4397 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4399 return 0;
4402 /* For a div d = floor(f/m), add the constraint
4404 * -(f-(n-1)) + m d >= 0
4406 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4407 unsigned pos, isl_int *div)
4409 int i;
4410 unsigned total = isl_basic_map_total_dim(bmap);
4412 i = isl_basic_map_alloc_inequality(bmap);
4413 if (i < 0)
4414 return -1;
4415 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4416 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4417 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4418 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4420 return 0;
4423 /* For a div d = floor(f/m), add the constraints
4425 * f - m d >= 0
4426 * -(f-(n-1)) + m d >= 0
4428 * Note that the second constraint is the negation of
4430 * f - m d >= n
4432 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4433 unsigned pos, isl_int *div)
4435 if (add_upper_div_constraint(bmap, pos, div) < 0)
4436 return -1;
4437 if (add_lower_div_constraint(bmap, pos, div) < 0)
4438 return -1;
4439 return 0;
4442 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4443 unsigned pos, isl_int *div)
4445 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4446 pos, div);
4449 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4451 unsigned total = isl_basic_map_total_dim(bmap);
4452 unsigned div_pos = total - bmap->n_div + div;
4454 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4455 bmap->div[div]);
4458 /* For each known div d = floor(f/m), add the constraints
4460 * f - m d >= 0
4461 * -(f-(n-1)) + m d >= 0
4463 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4464 __isl_take isl_basic_map *bmap)
4466 int i;
4467 unsigned n_div;
4469 if (!bmap)
4470 return NULL;
4471 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4472 if (n_div == 0)
4473 return bmap;
4474 bmap = isl_basic_map_cow(bmap);
4475 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4476 if (!bmap)
4477 return NULL;
4478 for (i = 0; i < n_div; ++i) {
4479 if (isl_int_is_zero(bmap->div[i][0]))
4480 continue;
4481 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4482 return isl_basic_map_free(bmap);
4485 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4486 bmap = isl_basic_map_finalize(bmap);
4487 return bmap;
4490 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4492 * In particular, if this div is of the form d = floor(f/m),
4493 * then add the constraint
4495 * f - m d >= 0
4497 * if sign < 0 or the constraint
4499 * -(f-(n-1)) + m d >= 0
4501 * if sign > 0.
4503 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4504 unsigned div, int sign)
4506 unsigned total;
4507 unsigned div_pos;
4509 if (!bmap)
4510 return -1;
4512 total = isl_basic_map_total_dim(bmap);
4513 div_pos = total - bmap->n_div + div;
4515 if (sign < 0)
4516 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4517 else
4518 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4521 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4523 return isl_basic_map_add_div_constraints(bset, div);
4526 struct isl_basic_set *isl_basic_map_underlying_set(
4527 struct isl_basic_map *bmap)
4529 if (!bmap)
4530 goto error;
4531 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4532 bmap->n_div == 0 &&
4533 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4534 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4535 return (struct isl_basic_set *)bmap;
4536 bmap = isl_basic_map_cow(bmap);
4537 if (!bmap)
4538 goto error;
4539 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4540 if (!bmap->dim)
4541 goto error;
4542 bmap->extra -= bmap->n_div;
4543 bmap->n_div = 0;
4544 bmap = isl_basic_map_finalize(bmap);
4545 return (struct isl_basic_set *)bmap;
4546 error:
4547 isl_basic_map_free(bmap);
4548 return NULL;
4551 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4552 __isl_take isl_basic_set *bset)
4554 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4557 /* Replace each element in "list" by the result of applying
4558 * isl_basic_map_underlying_set to the element.
4560 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4561 __isl_take isl_basic_map_list *list)
4563 int i, n;
4565 if (!list)
4566 return NULL;
4568 n = isl_basic_map_list_n_basic_map(list);
4569 for (i = 0; i < n; ++i) {
4570 isl_basic_map *bmap;
4571 isl_basic_set *bset;
4573 bmap = isl_basic_map_list_get_basic_map(list, i);
4574 bset = isl_basic_set_underlying_set(bmap);
4575 list = isl_basic_set_list_set_basic_set(list, i, bset);
4578 return list;
4581 struct isl_basic_map *isl_basic_map_overlying_set(
4582 struct isl_basic_set *bset, struct isl_basic_map *like)
4584 struct isl_basic_map *bmap;
4585 struct isl_ctx *ctx;
4586 unsigned total;
4587 int i;
4589 if (!bset || !like)
4590 goto error;
4591 ctx = bset->ctx;
4592 isl_assert(ctx, bset->n_div == 0, goto error);
4593 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4594 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4595 goto error);
4596 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4597 isl_basic_map_free(like);
4598 return (struct isl_basic_map *)bset;
4600 bset = isl_basic_set_cow(bset);
4601 if (!bset)
4602 goto error;
4603 total = bset->dim->n_out + bset->extra;
4604 bmap = (struct isl_basic_map *)bset;
4605 isl_space_free(bmap->dim);
4606 bmap->dim = isl_space_copy(like->dim);
4607 if (!bmap->dim)
4608 goto error;
4609 bmap->n_div = like->n_div;
4610 bmap->extra += like->n_div;
4611 if (bmap->extra) {
4612 unsigned ltotal;
4613 isl_int **div;
4614 ltotal = total - bmap->extra + like->extra;
4615 if (ltotal > total)
4616 ltotal = total;
4617 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4618 bmap->extra * (1 + 1 + total));
4619 if (isl_blk_is_error(bmap->block2))
4620 goto error;
4621 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4622 if (!div)
4623 goto error;
4624 bmap->div = div;
4625 for (i = 0; i < bmap->extra; ++i)
4626 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4627 for (i = 0; i < like->n_div; ++i) {
4628 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4629 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4631 bmap = isl_basic_map_add_known_div_constraints(bmap);
4633 isl_basic_map_free(like);
4634 bmap = isl_basic_map_simplify(bmap);
4635 bmap = isl_basic_map_finalize(bmap);
4636 return bmap;
4637 error:
4638 isl_basic_map_free(like);
4639 isl_basic_set_free(bset);
4640 return NULL;
4643 struct isl_basic_set *isl_basic_set_from_underlying_set(
4644 struct isl_basic_set *bset, struct isl_basic_set *like)
4646 return (struct isl_basic_set *)
4647 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4650 struct isl_set *isl_set_from_underlying_set(
4651 struct isl_set *set, struct isl_basic_set *like)
4653 int i;
4655 if (!set || !like)
4656 goto error;
4657 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4658 goto error);
4659 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4660 isl_basic_set_free(like);
4661 return set;
4663 set = isl_set_cow(set);
4664 if (!set)
4665 goto error;
4666 for (i = 0; i < set->n; ++i) {
4667 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4668 isl_basic_set_copy(like));
4669 if (!set->p[i])
4670 goto error;
4672 isl_space_free(set->dim);
4673 set->dim = isl_space_copy(like->dim);
4674 if (!set->dim)
4675 goto error;
4676 isl_basic_set_free(like);
4677 return set;
4678 error:
4679 isl_basic_set_free(like);
4680 isl_set_free(set);
4681 return NULL;
4684 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4686 int i;
4688 map = isl_map_cow(map);
4689 if (!map)
4690 return NULL;
4691 map->dim = isl_space_cow(map->dim);
4692 if (!map->dim)
4693 goto error;
4695 for (i = 1; i < map->n; ++i)
4696 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4697 goto error);
4698 for (i = 0; i < map->n; ++i) {
4699 map->p[i] = (struct isl_basic_map *)
4700 isl_basic_map_underlying_set(map->p[i]);
4701 if (!map->p[i])
4702 goto error;
4704 if (map->n == 0)
4705 map->dim = isl_space_underlying(map->dim, 0);
4706 else {
4707 isl_space_free(map->dim);
4708 map->dim = isl_space_copy(map->p[0]->dim);
4710 if (!map->dim)
4711 goto error;
4712 return (struct isl_set *)map;
4713 error:
4714 isl_map_free(map);
4715 return NULL;
4718 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4720 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4723 __isl_give isl_basic_map *isl_basic_map_reset_space(
4724 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4726 bmap = isl_basic_map_cow(bmap);
4727 if (!bmap || !dim)
4728 goto error;
4730 isl_space_free(bmap->dim);
4731 bmap->dim = dim;
4733 bmap = isl_basic_map_finalize(bmap);
4735 return bmap;
4736 error:
4737 isl_basic_map_free(bmap);
4738 isl_space_free(dim);
4739 return NULL;
4742 __isl_give isl_basic_set *isl_basic_set_reset_space(
4743 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4745 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4746 dim);
4749 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4750 __isl_take isl_space *dim)
4752 int i;
4754 map = isl_map_cow(map);
4755 if (!map || !dim)
4756 goto error;
4758 for (i = 0; i < map->n; ++i) {
4759 map->p[i] = isl_basic_map_reset_space(map->p[i],
4760 isl_space_copy(dim));
4761 if (!map->p[i])
4762 goto error;
4764 isl_space_free(map->dim);
4765 map->dim = dim;
4767 return map;
4768 error:
4769 isl_map_free(map);
4770 isl_space_free(dim);
4771 return NULL;
4774 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4775 __isl_take isl_space *dim)
4777 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4780 /* Compute the parameter domain of the given basic set.
4782 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4784 isl_space *space;
4785 unsigned n;
4787 if (isl_basic_set_is_params(bset))
4788 return bset;
4790 n = isl_basic_set_dim(bset, isl_dim_set);
4791 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4792 space = isl_basic_set_get_space(bset);
4793 space = isl_space_params(space);
4794 bset = isl_basic_set_reset_space(bset, space);
4795 return bset;
4798 /* Construct a zero-dimensional basic set with the given parameter domain.
4800 __isl_give isl_basic_set *isl_basic_set_from_params(
4801 __isl_take isl_basic_set *bset)
4803 isl_space *space;
4804 space = isl_basic_set_get_space(bset);
4805 space = isl_space_set_from_params(space);
4806 bset = isl_basic_set_reset_space(bset, space);
4807 return bset;
4810 /* Compute the parameter domain of the given set.
4812 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4814 isl_space *space;
4815 unsigned n;
4817 if (isl_set_is_params(set))
4818 return set;
4820 n = isl_set_dim(set, isl_dim_set);
4821 set = isl_set_project_out(set, isl_dim_set, 0, n);
4822 space = isl_set_get_space(set);
4823 space = isl_space_params(space);
4824 set = isl_set_reset_space(set, space);
4825 return set;
4828 /* Construct a zero-dimensional set with the given parameter domain.
4830 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4832 isl_space *space;
4833 space = isl_set_get_space(set);
4834 space = isl_space_set_from_params(space);
4835 set = isl_set_reset_space(set, space);
4836 return set;
4839 /* Compute the parameter domain of the given map.
4841 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4843 isl_space *space;
4844 unsigned n;
4846 n = isl_map_dim(map, isl_dim_in);
4847 map = isl_map_project_out(map, isl_dim_in, 0, n);
4848 n = isl_map_dim(map, isl_dim_out);
4849 map = isl_map_project_out(map, isl_dim_out, 0, n);
4850 space = isl_map_get_space(map);
4851 space = isl_space_params(space);
4852 map = isl_map_reset_space(map, space);
4853 return map;
4856 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4858 isl_space *dim;
4859 struct isl_basic_set *domain;
4860 unsigned n_in;
4861 unsigned n_out;
4863 if (!bmap)
4864 return NULL;
4865 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4867 n_in = isl_basic_map_n_in(bmap);
4868 n_out = isl_basic_map_n_out(bmap);
4869 domain = isl_basic_set_from_basic_map(bmap);
4870 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4872 domain = isl_basic_set_reset_space(domain, dim);
4874 return domain;
4877 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4879 if (!bmap)
4880 return -1;
4881 return isl_space_may_be_set(bmap->dim);
4884 /* Is this basic map actually a set?
4885 * Users should never call this function. Outside of isl,
4886 * the type should indicate whether something is a set or a map.
4888 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4890 if (!bmap)
4891 return -1;
4892 return isl_space_is_set(bmap->dim);
4895 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4897 if (!bmap)
4898 return NULL;
4899 if (isl_basic_map_is_set(bmap))
4900 return bmap;
4901 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4904 __isl_give isl_basic_map *isl_basic_map_domain_map(
4905 __isl_take isl_basic_map *bmap)
4907 int i, k;
4908 isl_space *dim;
4909 isl_basic_map *domain;
4910 int nparam, n_in, n_out;
4911 unsigned total;
4913 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4914 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4915 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4917 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4918 domain = isl_basic_map_universe(dim);
4920 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4921 bmap = isl_basic_map_apply_range(bmap, domain);
4922 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4924 total = isl_basic_map_total_dim(bmap);
4926 for (i = 0; i < n_in; ++i) {
4927 k = isl_basic_map_alloc_equality(bmap);
4928 if (k < 0)
4929 goto error;
4930 isl_seq_clr(bmap->eq[k], 1 + total);
4931 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4932 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4935 bmap = isl_basic_map_gauss(bmap, NULL);
4936 return isl_basic_map_finalize(bmap);
4937 error:
4938 isl_basic_map_free(bmap);
4939 return NULL;
4942 __isl_give isl_basic_map *isl_basic_map_range_map(
4943 __isl_take isl_basic_map *bmap)
4945 int i, k;
4946 isl_space *dim;
4947 isl_basic_map *range;
4948 int nparam, n_in, n_out;
4949 unsigned total;
4951 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4952 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4953 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4955 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4956 range = isl_basic_map_universe(dim);
4958 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4959 bmap = isl_basic_map_apply_range(bmap, range);
4960 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4962 total = isl_basic_map_total_dim(bmap);
4964 for (i = 0; i < n_out; ++i) {
4965 k = isl_basic_map_alloc_equality(bmap);
4966 if (k < 0)
4967 goto error;
4968 isl_seq_clr(bmap->eq[k], 1 + total);
4969 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4970 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4973 bmap = isl_basic_map_gauss(bmap, NULL);
4974 return isl_basic_map_finalize(bmap);
4975 error:
4976 isl_basic_map_free(bmap);
4977 return NULL;
4980 int isl_map_may_be_set(__isl_keep isl_map *map)
4982 if (!map)
4983 return -1;
4984 return isl_space_may_be_set(map->dim);
4987 /* Is this map actually a set?
4988 * Users should never call this function. Outside of isl,
4989 * the type should indicate whether something is a set or a map.
4991 int isl_map_is_set(__isl_keep isl_map *map)
4993 if (!map)
4994 return -1;
4995 return isl_space_is_set(map->dim);
4998 struct isl_set *isl_map_range(struct isl_map *map)
5000 int i;
5001 struct isl_set *set;
5003 if (!map)
5004 goto error;
5005 if (isl_map_is_set(map))
5006 return (isl_set *)map;
5008 map = isl_map_cow(map);
5009 if (!map)
5010 goto error;
5012 set = (struct isl_set *) map;
5013 set->dim = isl_space_range(set->dim);
5014 if (!set->dim)
5015 goto error;
5016 for (i = 0; i < map->n; ++i) {
5017 set->p[i] = isl_basic_map_range(map->p[i]);
5018 if (!set->p[i])
5019 goto error;
5021 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5022 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5023 return set;
5024 error:
5025 isl_map_free(map);
5026 return NULL;
5029 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5031 int i;
5033 map = isl_map_cow(map);
5034 if (!map)
5035 return NULL;
5037 map->dim = isl_space_domain_map(map->dim);
5038 if (!map->dim)
5039 goto error;
5040 for (i = 0; i < map->n; ++i) {
5041 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5042 if (!map->p[i])
5043 goto error;
5045 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5046 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5047 return map;
5048 error:
5049 isl_map_free(map);
5050 return NULL;
5053 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5055 int i;
5056 isl_space *range_dim;
5058 map = isl_map_cow(map);
5059 if (!map)
5060 return NULL;
5062 range_dim = isl_space_range(isl_map_get_space(map));
5063 range_dim = isl_space_from_range(range_dim);
5064 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5065 map->dim = isl_space_join(map->dim, range_dim);
5066 if (!map->dim)
5067 goto error;
5068 for (i = 0; i < map->n; ++i) {
5069 map->p[i] = isl_basic_map_range_map(map->p[i]);
5070 if (!map->p[i])
5071 goto error;
5073 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5074 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5075 return map;
5076 error:
5077 isl_map_free(map);
5078 return NULL;
5081 /* Given a wrapped map of the form A[B -> C],
5082 * return the map A[B -> C] -> B.
5084 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5086 isl_id *id;
5087 isl_map *map;
5089 if (!set)
5090 return NULL;
5091 if (!isl_set_has_tuple_id(set))
5092 return isl_map_domain_map(isl_set_unwrap(set));
5094 id = isl_set_get_tuple_id(set);
5095 map = isl_map_domain_map(isl_set_unwrap(set));
5096 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5098 return map;
5101 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5102 __isl_take isl_space *dim)
5104 int i;
5105 struct isl_map *map = NULL;
5107 set = isl_set_cow(set);
5108 if (!set || !dim)
5109 goto error;
5110 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5111 map = (struct isl_map *)set;
5112 for (i = 0; i < set->n; ++i) {
5113 map->p[i] = isl_basic_map_from_basic_set(
5114 set->p[i], isl_space_copy(dim));
5115 if (!map->p[i])
5116 goto error;
5118 isl_space_free(map->dim);
5119 map->dim = dim;
5120 return map;
5121 error:
5122 isl_space_free(dim);
5123 isl_set_free(set);
5124 return NULL;
5127 __isl_give isl_basic_map *isl_basic_map_from_domain(
5128 __isl_take isl_basic_set *bset)
5130 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5133 __isl_give isl_basic_map *isl_basic_map_from_range(
5134 __isl_take isl_basic_set *bset)
5136 isl_space *space;
5137 space = isl_basic_set_get_space(bset);
5138 space = isl_space_from_range(space);
5139 bset = isl_basic_set_reset_space(bset, space);
5140 return (isl_basic_map *)bset;
5143 /* Create a relation with the given set as range.
5144 * The domain of the created relation is a zero-dimensional
5145 * flat anonymous space.
5147 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5149 isl_space *space;
5150 space = isl_set_get_space(set);
5151 space = isl_space_from_range(space);
5152 set = isl_set_reset_space(set, space);
5153 return (struct isl_map *)set;
5156 /* Create a relation with the given set as domain.
5157 * The range of the created relation is a zero-dimensional
5158 * flat anonymous space.
5160 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5162 return isl_map_reverse(isl_map_from_range(set));
5165 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5166 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5168 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5171 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5172 __isl_take isl_set *range)
5174 return isl_map_apply_range(isl_map_reverse(domain), range);
5177 struct isl_set *isl_set_from_map(struct isl_map *map)
5179 int i;
5180 struct isl_set *set = NULL;
5182 if (!map)
5183 return NULL;
5184 map = isl_map_cow(map);
5185 if (!map)
5186 return NULL;
5187 map->dim = isl_space_as_set_space(map->dim);
5188 if (!map->dim)
5189 goto error;
5190 set = (struct isl_set *)map;
5191 for (i = 0; i < map->n; ++i) {
5192 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5193 if (!set->p[i])
5194 goto error;
5196 return set;
5197 error:
5198 isl_map_free(map);
5199 return NULL;
5202 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5203 unsigned flags)
5205 struct isl_map *map;
5207 if (!dim)
5208 return NULL;
5209 if (n < 0)
5210 isl_die(dim->ctx, isl_error_internal,
5211 "negative number of basic maps", goto error);
5212 map = isl_alloc(dim->ctx, struct isl_map,
5213 sizeof(struct isl_map) +
5214 (n - 1) * sizeof(struct isl_basic_map *));
5215 if (!map)
5216 goto error;
5218 map->ctx = dim->ctx;
5219 isl_ctx_ref(map->ctx);
5220 map->ref = 1;
5221 map->size = n;
5222 map->n = 0;
5223 map->dim = dim;
5224 map->flags = flags;
5225 return map;
5226 error:
5227 isl_space_free(dim);
5228 return NULL;
5231 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5232 unsigned nparam, unsigned in, unsigned out, int n,
5233 unsigned flags)
5235 struct isl_map *map;
5236 isl_space *dims;
5238 dims = isl_space_alloc(ctx, nparam, in, out);
5239 if (!dims)
5240 return NULL;
5242 map = isl_map_alloc_space(dims, n, flags);
5243 return map;
5246 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5248 struct isl_basic_map *bmap;
5249 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5250 bmap = isl_basic_map_set_to_empty(bmap);
5251 return bmap;
5254 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5256 struct isl_basic_set *bset;
5257 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5258 bset = isl_basic_set_set_to_empty(bset);
5259 return bset;
5262 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5264 struct isl_basic_map *bmap;
5265 if (!model)
5266 return NULL;
5267 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5268 bmap = isl_basic_map_set_to_empty(bmap);
5269 return bmap;
5272 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5274 struct isl_basic_map *bmap;
5275 if (!model)
5276 return NULL;
5277 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5278 bmap = isl_basic_map_set_to_empty(bmap);
5279 return bmap;
5282 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5284 struct isl_basic_set *bset;
5285 if (!model)
5286 return NULL;
5287 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5288 bset = isl_basic_set_set_to_empty(bset);
5289 return bset;
5292 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5294 struct isl_basic_map *bmap;
5295 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5296 bmap = isl_basic_map_finalize(bmap);
5297 return bmap;
5300 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5302 struct isl_basic_set *bset;
5303 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5304 bset = isl_basic_set_finalize(bset);
5305 return bset;
5308 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5310 int i;
5311 unsigned total = isl_space_dim(dim, isl_dim_all);
5312 isl_basic_map *bmap;
5314 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5315 for (i = 0; i < total; ++i) {
5316 int k = isl_basic_map_alloc_inequality(bmap);
5317 if (k < 0)
5318 goto error;
5319 isl_seq_clr(bmap->ineq[k], 1 + total);
5320 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5322 return bmap;
5323 error:
5324 isl_basic_map_free(bmap);
5325 return NULL;
5328 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5330 return isl_basic_map_nat_universe(dim);
5333 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5335 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5338 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5340 return isl_map_nat_universe(dim);
5343 __isl_give isl_basic_map *isl_basic_map_universe_like(
5344 __isl_keep isl_basic_map *model)
5346 if (!model)
5347 return NULL;
5348 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5351 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5353 if (!model)
5354 return NULL;
5355 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5358 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5359 __isl_keep isl_set *model)
5361 if (!model)
5362 return NULL;
5363 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5366 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5368 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5371 struct isl_map *isl_map_empty_like(struct isl_map *model)
5373 if (!model)
5374 return NULL;
5375 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5378 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5380 if (!model)
5381 return NULL;
5382 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5385 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5387 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5390 struct isl_set *isl_set_empty_like(struct isl_set *model)
5392 if (!model)
5393 return NULL;
5394 return isl_set_empty(isl_space_copy(model->dim));
5397 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5399 struct isl_map *map;
5400 if (!dim)
5401 return NULL;
5402 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5403 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5404 return map;
5407 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5409 struct isl_set *set;
5410 if (!dim)
5411 return NULL;
5412 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5413 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5414 return set;
5417 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5419 if (!model)
5420 return NULL;
5421 return isl_set_universe(isl_space_copy(model->dim));
5424 struct isl_map *isl_map_dup(struct isl_map *map)
5426 int i;
5427 struct isl_map *dup;
5429 if (!map)
5430 return NULL;
5431 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5432 for (i = 0; i < map->n; ++i)
5433 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5434 return dup;
5437 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5438 __isl_take isl_basic_map *bmap)
5440 if (!bmap || !map)
5441 goto error;
5442 if (isl_basic_map_plain_is_empty(bmap)) {
5443 isl_basic_map_free(bmap);
5444 return map;
5446 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5447 isl_assert(map->ctx, map->n < map->size, goto error);
5448 map->p[map->n] = bmap;
5449 map->n++;
5450 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5451 return map;
5452 error:
5453 if (map)
5454 isl_map_free(map);
5455 if (bmap)
5456 isl_basic_map_free(bmap);
5457 return NULL;
5460 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5462 int i;
5464 if (!map)
5465 return NULL;
5467 if (--map->ref > 0)
5468 return NULL;
5470 isl_ctx_deref(map->ctx);
5471 for (i = 0; i < map->n; ++i)
5472 isl_basic_map_free(map->p[i]);
5473 isl_space_free(map->dim);
5474 free(map);
5476 return NULL;
5479 struct isl_map *isl_map_extend(struct isl_map *base,
5480 unsigned nparam, unsigned n_in, unsigned n_out)
5482 int i;
5484 base = isl_map_cow(base);
5485 if (!base)
5486 return NULL;
5488 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5489 if (!base->dim)
5490 goto error;
5491 for (i = 0; i < base->n; ++i) {
5492 base->p[i] = isl_basic_map_extend_space(base->p[i],
5493 isl_space_copy(base->dim), 0, 0, 0);
5494 if (!base->p[i])
5495 goto error;
5497 return base;
5498 error:
5499 isl_map_free(base);
5500 return NULL;
5503 struct isl_set *isl_set_extend(struct isl_set *base,
5504 unsigned nparam, unsigned dim)
5506 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5507 nparam, 0, dim);
5510 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5511 struct isl_basic_map *bmap, unsigned pos, int value)
5513 int j;
5515 bmap = isl_basic_map_cow(bmap);
5516 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5517 j = isl_basic_map_alloc_equality(bmap);
5518 if (j < 0)
5519 goto error;
5520 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5521 isl_int_set_si(bmap->eq[j][pos], -1);
5522 isl_int_set_si(bmap->eq[j][0], value);
5523 bmap = isl_basic_map_simplify(bmap);
5524 return isl_basic_map_finalize(bmap);
5525 error:
5526 isl_basic_map_free(bmap);
5527 return NULL;
5530 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5531 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5533 int j;
5535 bmap = isl_basic_map_cow(bmap);
5536 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5537 j = isl_basic_map_alloc_equality(bmap);
5538 if (j < 0)
5539 goto error;
5540 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5541 isl_int_set_si(bmap->eq[j][pos], -1);
5542 isl_int_set(bmap->eq[j][0], value);
5543 bmap = isl_basic_map_simplify(bmap);
5544 return isl_basic_map_finalize(bmap);
5545 error:
5546 isl_basic_map_free(bmap);
5547 return NULL;
5550 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5551 enum isl_dim_type type, unsigned pos, int value)
5553 if (!bmap)
5554 return NULL;
5555 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5556 return isl_basic_map_fix_pos_si(bmap,
5557 isl_basic_map_offset(bmap, type) + pos, value);
5558 error:
5559 isl_basic_map_free(bmap);
5560 return NULL;
5563 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5564 enum isl_dim_type type, unsigned pos, isl_int value)
5566 if (!bmap)
5567 return NULL;
5568 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5569 return isl_basic_map_fix_pos(bmap,
5570 isl_basic_map_offset(bmap, type) + pos, value);
5571 error:
5572 isl_basic_map_free(bmap);
5573 return NULL;
5576 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5577 * to be equal to "v".
5579 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5580 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5582 if (!bmap || !v)
5583 goto error;
5584 if (!isl_val_is_int(v))
5585 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5586 "expecting integer value", goto error);
5587 if (pos >= isl_basic_map_dim(bmap, type))
5588 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5589 "index out of bounds", goto error);
5590 pos += isl_basic_map_offset(bmap, type);
5591 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5592 isl_val_free(v);
5593 return bmap;
5594 error:
5595 isl_basic_map_free(bmap);
5596 isl_val_free(v);
5597 return NULL;
5600 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5601 * to be equal to "v".
5603 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5604 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5606 return isl_basic_map_fix_val(bset, type, pos, v);
5609 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5610 enum isl_dim_type type, unsigned pos, int value)
5612 return (struct isl_basic_set *)
5613 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5614 type, pos, value);
5617 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5618 enum isl_dim_type type, unsigned pos, isl_int value)
5620 return (struct isl_basic_set *)
5621 isl_basic_map_fix((struct isl_basic_map *)bset,
5622 type, pos, value);
5625 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5626 unsigned input, int value)
5628 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5631 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5632 unsigned dim, int value)
5634 return (struct isl_basic_set *)
5635 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5636 isl_dim_set, dim, value);
5639 static int remove_if_empty(__isl_keep isl_map *map, int i)
5641 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5643 if (empty < 0)
5644 return -1;
5645 if (!empty)
5646 return 0;
5648 isl_basic_map_free(map->p[i]);
5649 if (i != map->n - 1) {
5650 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5651 map->p[i] = map->p[map->n - 1];
5653 map->n--;
5655 return 0;
5658 /* Perform "fn" on each basic map of "map", where we may not be holding
5659 * the only reference to "map".
5660 * In particular, "fn" should be a semantics preserving operation
5661 * that we want to apply to all copies of "map". We therefore need
5662 * to be careful not to modify "map" in a way that breaks "map"
5663 * in case anything goes wrong.
5665 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5666 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5668 struct isl_basic_map *bmap;
5669 int i;
5671 if (!map)
5672 return NULL;
5674 for (i = map->n - 1; i >= 0; --i) {
5675 bmap = isl_basic_map_copy(map->p[i]);
5676 bmap = fn(bmap);
5677 if (!bmap)
5678 goto error;
5679 isl_basic_map_free(map->p[i]);
5680 map->p[i] = bmap;
5681 if (remove_if_empty(map, i) < 0)
5682 goto error;
5685 return map;
5686 error:
5687 isl_map_free(map);
5688 return NULL;
5691 struct isl_map *isl_map_fix_si(struct isl_map *map,
5692 enum isl_dim_type type, unsigned pos, int value)
5694 int i;
5696 map = isl_map_cow(map);
5697 if (!map)
5698 return NULL;
5700 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5701 for (i = map->n - 1; i >= 0; --i) {
5702 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5703 if (remove_if_empty(map, i) < 0)
5704 goto error;
5706 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5707 return map;
5708 error:
5709 isl_map_free(map);
5710 return NULL;
5713 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5714 enum isl_dim_type type, unsigned pos, int value)
5716 return (struct isl_set *)
5717 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5720 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5721 enum isl_dim_type type, unsigned pos, isl_int value)
5723 int i;
5725 map = isl_map_cow(map);
5726 if (!map)
5727 return NULL;
5729 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5730 for (i = 0; i < map->n; ++i) {
5731 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5732 if (!map->p[i])
5733 goto error;
5735 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5736 return map;
5737 error:
5738 isl_map_free(map);
5739 return NULL;
5742 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5743 enum isl_dim_type type, unsigned pos, isl_int value)
5745 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5748 /* Fix the value of the variable at position "pos" of type "type" of "map"
5749 * to be equal to "v".
5751 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5752 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5754 int i;
5756 map = isl_map_cow(map);
5757 if (!map || !v)
5758 goto error;
5760 if (!isl_val_is_int(v))
5761 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5762 "expecting integer value", goto error);
5763 if (pos >= isl_map_dim(map, type))
5764 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5765 "index out of bounds", goto error);
5766 for (i = map->n - 1; i >= 0; --i) {
5767 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5768 isl_val_copy(v));
5769 if (remove_if_empty(map, i) < 0)
5770 goto error;
5772 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5773 isl_val_free(v);
5774 return map;
5775 error:
5776 isl_map_free(map);
5777 isl_val_free(v);
5778 return NULL;
5781 /* Fix the value of the variable at position "pos" of type "type" of "set"
5782 * to be equal to "v".
5784 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5785 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5787 return isl_map_fix_val(set, type, pos, v);
5790 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5791 unsigned input, int value)
5793 return isl_map_fix_si(map, isl_dim_in, input, value);
5796 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5798 return (struct isl_set *)
5799 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5802 static __isl_give isl_basic_map *basic_map_bound_si(
5803 __isl_take isl_basic_map *bmap,
5804 enum isl_dim_type type, unsigned pos, int value, int upper)
5806 int j;
5808 if (!bmap)
5809 return NULL;
5810 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5811 pos += isl_basic_map_offset(bmap, type);
5812 bmap = isl_basic_map_cow(bmap);
5813 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5814 j = isl_basic_map_alloc_inequality(bmap);
5815 if (j < 0)
5816 goto error;
5817 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5818 if (upper) {
5819 isl_int_set_si(bmap->ineq[j][pos], -1);
5820 isl_int_set_si(bmap->ineq[j][0], value);
5821 } else {
5822 isl_int_set_si(bmap->ineq[j][pos], 1);
5823 isl_int_set_si(bmap->ineq[j][0], -value);
5825 bmap = isl_basic_map_simplify(bmap);
5826 return isl_basic_map_finalize(bmap);
5827 error:
5828 isl_basic_map_free(bmap);
5829 return NULL;
5832 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5833 __isl_take isl_basic_map *bmap,
5834 enum isl_dim_type type, unsigned pos, int value)
5836 return basic_map_bound_si(bmap, type, pos, value, 0);
5839 /* Constrain the values of the given dimension to be no greater than "value".
5841 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5842 __isl_take isl_basic_map *bmap,
5843 enum isl_dim_type type, unsigned pos, int value)
5845 return basic_map_bound_si(bmap, type, pos, value, 1);
5848 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5849 unsigned dim, isl_int value)
5851 int j;
5853 bset = isl_basic_set_cow(bset);
5854 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5855 j = isl_basic_set_alloc_inequality(bset);
5856 if (j < 0)
5857 goto error;
5858 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5859 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5860 isl_int_neg(bset->ineq[j][0], value);
5861 bset = isl_basic_set_simplify(bset);
5862 return isl_basic_set_finalize(bset);
5863 error:
5864 isl_basic_set_free(bset);
5865 return NULL;
5868 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5869 enum isl_dim_type type, unsigned pos, int value, int upper)
5871 int i;
5873 map = isl_map_cow(map);
5874 if (!map)
5875 return NULL;
5877 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5878 for (i = 0; i < map->n; ++i) {
5879 map->p[i] = basic_map_bound_si(map->p[i],
5880 type, pos, value, upper);
5881 if (!map->p[i])
5882 goto error;
5884 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5885 return map;
5886 error:
5887 isl_map_free(map);
5888 return NULL;
5891 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5892 enum isl_dim_type type, unsigned pos, int value)
5894 return map_bound_si(map, type, pos, value, 0);
5897 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5898 enum isl_dim_type type, unsigned pos, int value)
5900 return map_bound_si(map, type, pos, value, 1);
5903 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5904 enum isl_dim_type type, unsigned pos, int value)
5906 return (struct isl_set *)
5907 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5910 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5911 enum isl_dim_type type, unsigned pos, int value)
5913 return isl_map_upper_bound_si(set, type, pos, value);
5916 /* Bound the given variable of "bmap" from below (or above is "upper"
5917 * is set) to "value".
5919 static __isl_give isl_basic_map *basic_map_bound(
5920 __isl_take isl_basic_map *bmap,
5921 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5923 int j;
5925 if (!bmap)
5926 return NULL;
5927 if (pos >= isl_basic_map_dim(bmap, type))
5928 isl_die(bmap->ctx, isl_error_invalid,
5929 "index out of bounds", goto error);
5930 pos += isl_basic_map_offset(bmap, type);
5931 bmap = isl_basic_map_cow(bmap);
5932 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5933 j = isl_basic_map_alloc_inequality(bmap);
5934 if (j < 0)
5935 goto error;
5936 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5937 if (upper) {
5938 isl_int_set_si(bmap->ineq[j][pos], -1);
5939 isl_int_set(bmap->ineq[j][0], value);
5940 } else {
5941 isl_int_set_si(bmap->ineq[j][pos], 1);
5942 isl_int_neg(bmap->ineq[j][0], value);
5944 bmap = isl_basic_map_simplify(bmap);
5945 return isl_basic_map_finalize(bmap);
5946 error:
5947 isl_basic_map_free(bmap);
5948 return NULL;
5951 /* Bound the given variable of "map" from below (or above is "upper"
5952 * is set) to "value".
5954 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5955 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5957 int i;
5959 map = isl_map_cow(map);
5960 if (!map)
5961 return NULL;
5963 if (pos >= isl_map_dim(map, type))
5964 isl_die(map->ctx, isl_error_invalid,
5965 "index out of bounds", goto error);
5966 for (i = map->n - 1; i >= 0; --i) {
5967 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5968 if (remove_if_empty(map, i) < 0)
5969 goto error;
5971 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5972 return map;
5973 error:
5974 isl_map_free(map);
5975 return NULL;
5978 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5979 enum isl_dim_type type, unsigned pos, isl_int value)
5981 return map_bound(map, type, pos, value, 0);
5984 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5985 enum isl_dim_type type, unsigned pos, isl_int value)
5987 return map_bound(map, type, pos, value, 1);
5990 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5991 enum isl_dim_type type, unsigned pos, isl_int value)
5993 return isl_map_lower_bound(set, type, pos, value);
5996 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5997 enum isl_dim_type type, unsigned pos, isl_int value)
5999 return isl_map_upper_bound(set, type, pos, value);
6002 /* Force the values of the variable at position "pos" of type "type" of "set"
6003 * to be no smaller than "value".
6005 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6006 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6008 if (!value)
6009 goto error;
6010 if (!isl_val_is_int(value))
6011 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6012 "expecting integer value", goto error);
6013 set = isl_set_lower_bound(set, type, pos, value->n);
6014 isl_val_free(value);
6015 return set;
6016 error:
6017 isl_val_free(value);
6018 isl_set_free(set);
6019 return NULL;
6022 /* Force the values of the variable at position "pos" of type "type" of "set"
6023 * to be no greater than "value".
6025 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6026 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6028 if (!value)
6029 goto error;
6030 if (!isl_val_is_int(value))
6031 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6032 "expecting integer value", goto error);
6033 set = isl_set_upper_bound(set, type, pos, value->n);
6034 isl_val_free(value);
6035 return set;
6036 error:
6037 isl_val_free(value);
6038 isl_set_free(set);
6039 return NULL;
6042 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6043 isl_int value)
6045 int i;
6047 set = isl_set_cow(set);
6048 if (!set)
6049 return NULL;
6051 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6052 for (i = 0; i < set->n; ++i) {
6053 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6054 if (!set->p[i])
6055 goto error;
6057 return set;
6058 error:
6059 isl_set_free(set);
6060 return NULL;
6063 struct isl_map *isl_map_reverse(struct isl_map *map)
6065 int i;
6067 map = isl_map_cow(map);
6068 if (!map)
6069 return NULL;
6071 map->dim = isl_space_reverse(map->dim);
6072 if (!map->dim)
6073 goto error;
6074 for (i = 0; i < map->n; ++i) {
6075 map->p[i] = isl_basic_map_reverse(map->p[i]);
6076 if (!map->p[i])
6077 goto error;
6079 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6080 return map;
6081 error:
6082 isl_map_free(map);
6083 return NULL;
6086 static struct isl_map *isl_basic_map_partial_lexopt(
6087 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6088 struct isl_set **empty, int max)
6090 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6093 struct isl_map *isl_basic_map_partial_lexmax(
6094 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6095 struct isl_set **empty)
6097 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6100 struct isl_map *isl_basic_map_partial_lexmin(
6101 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6102 struct isl_set **empty)
6104 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6107 struct isl_set *isl_basic_set_partial_lexmin(
6108 struct isl_basic_set *bset, struct isl_basic_set *dom,
6109 struct isl_set **empty)
6111 return (struct isl_set *)
6112 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6113 dom, empty);
6116 struct isl_set *isl_basic_set_partial_lexmax(
6117 struct isl_basic_set *bset, struct isl_basic_set *dom,
6118 struct isl_set **empty)
6120 return (struct isl_set *)
6121 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6122 dom, empty);
6125 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6126 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6127 __isl_give isl_set **empty)
6129 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6132 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6133 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6134 __isl_give isl_set **empty)
6136 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6139 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6140 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6141 __isl_give isl_set **empty)
6143 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6146 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6147 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6148 __isl_give isl_set **empty)
6150 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6153 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6154 __isl_take isl_basic_map *bmap, int max)
6156 isl_basic_set *dom = NULL;
6157 isl_space *dom_space;
6159 if (!bmap)
6160 goto error;
6161 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6162 dom = isl_basic_set_universe(dom_space);
6163 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6164 error:
6165 isl_basic_map_free(bmap);
6166 return NULL;
6169 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6170 __isl_take isl_basic_map *bmap)
6172 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6175 #undef TYPE
6176 #define TYPE isl_pw_multi_aff
6177 #undef SUFFIX
6178 #define SUFFIX _pw_multi_aff
6179 #undef EMPTY
6180 #define EMPTY isl_pw_multi_aff_empty
6181 #undef ADD
6182 #define ADD isl_pw_multi_aff_union_add
6183 #include "isl_map_lexopt_templ.c"
6185 /* Given a map "map", compute the lexicographically minimal
6186 * (or maximal) image element for each domain element in dom,
6187 * in the form of an isl_pw_multi_aff.
6188 * Set *empty to those elements in dom that do not have an image element.
6190 * We first compute the lexicographically minimal or maximal element
6191 * in the first basic map. This results in a partial solution "res"
6192 * and a subset "todo" of dom that still need to be handled.
6193 * We then consider each of the remaining maps in "map" and successively
6194 * update both "res" and "todo".
6196 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6197 __isl_take isl_map *map, __isl_take isl_set *dom,
6198 __isl_give isl_set **empty, int max)
6200 int i;
6201 isl_pw_multi_aff *res;
6202 isl_set *todo;
6204 if (!map || !dom)
6205 goto error;
6207 if (isl_map_plain_is_empty(map)) {
6208 if (empty)
6209 *empty = dom;
6210 else
6211 isl_set_free(dom);
6212 return isl_pw_multi_aff_from_map(map);
6215 res = basic_map_partial_lexopt_pw_multi_aff(
6216 isl_basic_map_copy(map->p[0]),
6217 isl_set_copy(dom), &todo, max);
6219 for (i = 1; i < map->n; ++i) {
6220 isl_pw_multi_aff *res_i;
6221 isl_set *todo_i;
6223 res_i = basic_map_partial_lexopt_pw_multi_aff(
6224 isl_basic_map_copy(map->p[i]),
6225 isl_set_copy(dom), &todo_i, max);
6227 if (max)
6228 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6229 else
6230 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6232 todo = isl_set_intersect(todo, todo_i);
6235 isl_set_free(dom);
6236 isl_map_free(map);
6238 if (empty)
6239 *empty = todo;
6240 else
6241 isl_set_free(todo);
6243 return res;
6244 error:
6245 if (empty)
6246 *empty = NULL;
6247 isl_set_free(dom);
6248 isl_map_free(map);
6249 return NULL;
6252 #undef TYPE
6253 #define TYPE isl_map
6254 #undef SUFFIX
6255 #define SUFFIX
6256 #undef EMPTY
6257 #define EMPTY isl_map_empty
6258 #undef ADD
6259 #define ADD isl_map_union_disjoint
6260 #include "isl_map_lexopt_templ.c"
6262 /* Given a map "map", compute the lexicographically minimal
6263 * (or maximal) image element for each domain element in dom.
6264 * Set *empty to those elements in dom that do not have an image element.
6266 * We first compute the lexicographically minimal or maximal element
6267 * in the first basic map. This results in a partial solution "res"
6268 * and a subset "todo" of dom that still need to be handled.
6269 * We then consider each of the remaining maps in "map" and successively
6270 * update both "res" and "todo".
6272 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6273 * Assume we are computing the lexicographical maximum.
6274 * We first compute the lexicographically maximal element in basic map i.
6275 * This results in a partial solution res_i and a subset todo_i.
6276 * Then we combine these results with those obtain for the first k basic maps
6277 * to obtain a result that is valid for the first k+1 basic maps.
6278 * In particular, the set where there is no solution is the set where
6279 * there is no solution for the first k basic maps and also no solution
6280 * for the ith basic map, i.e.,
6282 * todo^i = todo^k * todo_i
6284 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6285 * solutions, arbitrarily breaking ties in favor of res^k.
6286 * That is, when res^k(a) >= res_i(a), we pick res^k and
6287 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6288 * the lexicographic order.)
6289 * In practice, we compute
6291 * res^k * (res_i . "<=")
6293 * and
6295 * res_i * (res^k . "<")
6297 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6298 * where only one of res^k and res_i provides a solution and we simply pick
6299 * that one, i.e.,
6301 * res^k * todo_i
6302 * and
6303 * res_i * todo^k
6305 * Note that we only compute these intersections when dom(res^k) intersects
6306 * dom(res_i). Otherwise, the only effect of these intersections is to
6307 * potentially break up res^k and res_i into smaller pieces.
6308 * We want to avoid such splintering as much as possible.
6309 * In fact, an earlier implementation of this function would look for
6310 * better results in the domain of res^k and for extra results in todo^k,
6311 * but this would always result in a splintering according to todo^k,
6312 * even when the domain of basic map i is disjoint from the domains of
6313 * the previous basic maps.
6315 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6316 __isl_take isl_map *map, __isl_take isl_set *dom,
6317 __isl_give isl_set **empty, int max)
6319 int i;
6320 struct isl_map *res;
6321 struct isl_set *todo;
6323 if (!map || !dom)
6324 goto error;
6326 if (isl_map_plain_is_empty(map)) {
6327 if (empty)
6328 *empty = dom;
6329 else
6330 isl_set_free(dom);
6331 return map;
6334 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6335 isl_set_copy(dom), &todo, max);
6337 for (i = 1; i < map->n; ++i) {
6338 isl_map *lt, *le;
6339 isl_map *res_i;
6340 isl_set *todo_i;
6341 isl_space *dim = isl_space_range(isl_map_get_space(res));
6343 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6344 isl_set_copy(dom), &todo_i, max);
6346 if (max) {
6347 lt = isl_map_lex_lt(isl_space_copy(dim));
6348 le = isl_map_lex_le(dim);
6349 } else {
6350 lt = isl_map_lex_gt(isl_space_copy(dim));
6351 le = isl_map_lex_ge(dim);
6353 lt = isl_map_apply_range(isl_map_copy(res), lt);
6354 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6355 le = isl_map_apply_range(isl_map_copy(res_i), le);
6356 le = isl_map_intersect(le, isl_map_copy(res));
6358 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6359 res = isl_map_intersect_domain(res,
6360 isl_set_copy(todo_i));
6361 res_i = isl_map_intersect_domain(res_i,
6362 isl_set_copy(todo));
6365 res = isl_map_union_disjoint(res, res_i);
6366 res = isl_map_union_disjoint(res, lt);
6367 res = isl_map_union_disjoint(res, le);
6369 todo = isl_set_intersect(todo, todo_i);
6372 isl_set_free(dom);
6373 isl_map_free(map);
6375 if (empty)
6376 *empty = todo;
6377 else
6378 isl_set_free(todo);
6380 return res;
6381 error:
6382 if (empty)
6383 *empty = NULL;
6384 isl_set_free(dom);
6385 isl_map_free(map);
6386 return NULL;
6389 __isl_give isl_map *isl_map_partial_lexmax(
6390 __isl_take isl_map *map, __isl_take isl_set *dom,
6391 __isl_give isl_set **empty)
6393 return isl_map_partial_lexopt(map, dom, empty, 1);
6396 __isl_give isl_map *isl_map_partial_lexmin(
6397 __isl_take isl_map *map, __isl_take isl_set *dom,
6398 __isl_give isl_set **empty)
6400 return isl_map_partial_lexopt(map, dom, empty, 0);
6403 __isl_give isl_set *isl_set_partial_lexmin(
6404 __isl_take isl_set *set, __isl_take isl_set *dom,
6405 __isl_give isl_set **empty)
6407 return (struct isl_set *)
6408 isl_map_partial_lexmin((struct isl_map *)set,
6409 dom, empty);
6412 __isl_give isl_set *isl_set_partial_lexmax(
6413 __isl_take isl_set *set, __isl_take isl_set *dom,
6414 __isl_give isl_set **empty)
6416 return (struct isl_set *)
6417 isl_map_partial_lexmax((struct isl_map *)set,
6418 dom, empty);
6421 /* Compute the lexicographic minimum (or maximum if "max" is set)
6422 * of "bmap" over its domain.
6424 * Since we are not interested in the part of the domain space where
6425 * there is no solution, we initialize the domain to those constraints
6426 * of "bmap" that only involve the parameters and the input dimensions.
6427 * This relieves the parametric programming engine from detecting those
6428 * inequalities and transferring them to the context. More importantly,
6429 * it ensures that those inequalities are transferred first and not
6430 * intermixed with inequalities that actually split the domain.
6432 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6434 int n_div;
6435 int n_out;
6436 isl_basic_map *copy;
6437 isl_basic_set *dom;
6439 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6440 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6441 copy = isl_basic_map_copy(bmap);
6442 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6443 isl_dim_div, 0, n_div);
6444 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6445 isl_dim_out, 0, n_out);
6446 dom = isl_basic_map_domain(copy);
6447 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6450 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6452 return isl_basic_map_lexopt(bmap, 0);
6455 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6457 return isl_basic_map_lexopt(bmap, 1);
6460 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6462 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6465 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6467 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6470 /* Extract the first and only affine expression from list
6471 * and then add it to *pwaff with the given dom.
6472 * This domain is known to be disjoint from other domains
6473 * because of the way isl_basic_map_foreach_lexmax works.
6475 static int update_dim_opt(__isl_take isl_basic_set *dom,
6476 __isl_take isl_aff_list *list, void *user)
6478 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6479 isl_aff *aff;
6480 isl_pw_aff **pwaff = user;
6481 isl_pw_aff *pwaff_i;
6483 if (!list)
6484 goto error;
6485 if (isl_aff_list_n_aff(list) != 1)
6486 isl_die(ctx, isl_error_internal,
6487 "expecting single element list", goto error);
6489 aff = isl_aff_list_get_aff(list, 0);
6490 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6492 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6494 isl_aff_list_free(list);
6496 return 0;
6497 error:
6498 isl_basic_set_free(dom);
6499 isl_aff_list_free(list);
6500 return -1;
6503 /* Given a basic map with one output dimension, compute the minimum or
6504 * maximum of that dimension as an isl_pw_aff.
6506 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6507 * call update_dim_opt on each leaf of the result.
6509 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6510 int max)
6512 isl_space *dim = isl_basic_map_get_space(bmap);
6513 isl_pw_aff *pwaff;
6514 int r;
6516 dim = isl_space_from_domain(isl_space_domain(dim));
6517 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6518 pwaff = isl_pw_aff_empty(dim);
6520 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6521 if (r < 0)
6522 return isl_pw_aff_free(pwaff);
6524 return pwaff;
6527 /* Compute the minimum or maximum of the given output dimension
6528 * as a function of the parameters and the input dimensions,
6529 * but independently of the other output dimensions.
6531 * We first project out the other output dimension and then compute
6532 * the "lexicographic" maximum in each basic map, combining the results
6533 * using isl_pw_aff_union_max.
6535 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6536 int max)
6538 int i;
6539 isl_pw_aff *pwaff;
6540 unsigned n_out;
6542 n_out = isl_map_dim(map, isl_dim_out);
6543 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6544 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6545 if (!map)
6546 return NULL;
6548 if (map->n == 0) {
6549 isl_space *dim = isl_map_get_space(map);
6550 isl_map_free(map);
6551 return isl_pw_aff_empty(dim);
6554 pwaff = basic_map_dim_opt(map->p[0], max);
6555 for (i = 1; i < map->n; ++i) {
6556 isl_pw_aff *pwaff_i;
6558 pwaff_i = basic_map_dim_opt(map->p[i], max);
6559 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6562 isl_map_free(map);
6564 return pwaff;
6567 /* Compute the maximum of the given output dimension as a function of the
6568 * parameters and input dimensions, but independently of
6569 * the other output dimensions.
6571 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6573 return map_dim_opt(map, pos, 1);
6576 /* Compute the minimum or maximum of the given set dimension
6577 * as a function of the parameters,
6578 * but independently of the other set dimensions.
6580 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6581 int max)
6583 return map_dim_opt(set, pos, max);
6586 /* Compute the maximum of the given set dimension as a function of the
6587 * parameters, but independently of the other set dimensions.
6589 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6591 return set_dim_opt(set, pos, 1);
6594 /* Compute the minimum of the given set dimension as a function of the
6595 * parameters, but independently of the other set dimensions.
6597 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6599 return set_dim_opt(set, pos, 0);
6602 /* Apply a preimage specified by "mat" on the parameters of "bset".
6603 * bset is assumed to have only parameters and divs.
6605 static struct isl_basic_set *basic_set_parameter_preimage(
6606 struct isl_basic_set *bset, struct isl_mat *mat)
6608 unsigned nparam;
6610 if (!bset || !mat)
6611 goto error;
6613 bset->dim = isl_space_cow(bset->dim);
6614 if (!bset->dim)
6615 goto error;
6617 nparam = isl_basic_set_dim(bset, isl_dim_param);
6619 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6621 bset->dim->nparam = 0;
6622 bset->dim->n_out = nparam;
6623 bset = isl_basic_set_preimage(bset, mat);
6624 if (bset) {
6625 bset->dim->nparam = bset->dim->n_out;
6626 bset->dim->n_out = 0;
6628 return bset;
6629 error:
6630 isl_mat_free(mat);
6631 isl_basic_set_free(bset);
6632 return NULL;
6635 /* Apply a preimage specified by "mat" on the parameters of "set".
6636 * set is assumed to have only parameters and divs.
6638 static struct isl_set *set_parameter_preimage(
6639 struct isl_set *set, struct isl_mat *mat)
6641 isl_space *dim = NULL;
6642 unsigned nparam;
6644 if (!set || !mat)
6645 goto error;
6647 dim = isl_space_copy(set->dim);
6648 dim = isl_space_cow(dim);
6649 if (!dim)
6650 goto error;
6652 nparam = isl_set_dim(set, isl_dim_param);
6654 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6656 dim->nparam = 0;
6657 dim->n_out = nparam;
6658 isl_set_reset_space(set, dim);
6659 set = isl_set_preimage(set, mat);
6660 if (!set)
6661 goto error2;
6662 dim = isl_space_copy(set->dim);
6663 dim = isl_space_cow(dim);
6664 if (!dim)
6665 goto error2;
6666 dim->nparam = dim->n_out;
6667 dim->n_out = 0;
6668 isl_set_reset_space(set, dim);
6669 return set;
6670 error:
6671 isl_space_free(dim);
6672 isl_mat_free(mat);
6673 error2:
6674 isl_set_free(set);
6675 return NULL;
6678 /* Intersect the basic set "bset" with the affine space specified by the
6679 * equalities in "eq".
6681 static struct isl_basic_set *basic_set_append_equalities(
6682 struct isl_basic_set *bset, struct isl_mat *eq)
6684 int i, k;
6685 unsigned len;
6687 if (!bset || !eq)
6688 goto error;
6690 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6691 eq->n_row, 0);
6692 if (!bset)
6693 goto error;
6695 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6696 for (i = 0; i < eq->n_row; ++i) {
6697 k = isl_basic_set_alloc_equality(bset);
6698 if (k < 0)
6699 goto error;
6700 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6701 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6703 isl_mat_free(eq);
6705 bset = isl_basic_set_gauss(bset, NULL);
6706 bset = isl_basic_set_finalize(bset);
6708 return bset;
6709 error:
6710 isl_mat_free(eq);
6711 isl_basic_set_free(bset);
6712 return NULL;
6715 /* Intersect the set "set" with the affine space specified by the
6716 * equalities in "eq".
6718 static struct isl_set *set_append_equalities(struct isl_set *set,
6719 struct isl_mat *eq)
6721 int i;
6723 if (!set || !eq)
6724 goto error;
6726 for (i = 0; i < set->n; ++i) {
6727 set->p[i] = basic_set_append_equalities(set->p[i],
6728 isl_mat_copy(eq));
6729 if (!set->p[i])
6730 goto error;
6732 isl_mat_free(eq);
6733 return set;
6734 error:
6735 isl_mat_free(eq);
6736 isl_set_free(set);
6737 return NULL;
6740 /* Given a basic set "bset" that only involves parameters and existentially
6741 * quantified variables, return the index of the first equality
6742 * that only involves parameters. If there is no such equality then
6743 * return bset->n_eq.
6745 * This function assumes that isl_basic_set_gauss has been called on "bset".
6747 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6749 int i, j;
6750 unsigned nparam, n_div;
6752 if (!bset)
6753 return -1;
6755 nparam = isl_basic_set_dim(bset, isl_dim_param);
6756 n_div = isl_basic_set_dim(bset, isl_dim_div);
6758 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6759 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6760 ++i;
6763 return i;
6766 /* Compute an explicit representation for the existentially quantified
6767 * variables in "bset" by computing the "minimal value" of the set
6768 * variables. Since there are no set variables, the computation of
6769 * the minimal value essentially computes an explicit representation
6770 * of the non-empty part(s) of "bset".
6772 * The input only involves parameters and existentially quantified variables.
6773 * All equalities among parameters have been removed.
6775 * Since the existentially quantified variables in the result are in general
6776 * going to be different from those in the input, we first replace
6777 * them by the minimal number of variables based on their equalities.
6778 * This should simplify the parametric integer programming.
6780 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6782 isl_morph *morph1, *morph2;
6783 isl_set *set;
6784 unsigned n;
6786 if (!bset)
6787 return NULL;
6788 if (bset->n_eq == 0)
6789 return isl_basic_set_lexmin(bset);
6791 morph1 = isl_basic_set_parameter_compression(bset);
6792 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6793 bset = isl_basic_set_lift(bset);
6794 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6795 bset = isl_morph_basic_set(morph2, bset);
6796 n = isl_basic_set_dim(bset, isl_dim_set);
6797 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6799 set = isl_basic_set_lexmin(bset);
6801 set = isl_morph_set(isl_morph_inverse(morph1), set);
6803 return set;
6806 /* Project the given basic set onto its parameter domain, possibly introducing
6807 * new, explicit, existential variables in the constraints.
6808 * The input has parameters and (possibly implicit) existential variables.
6809 * The output has the same parameters, but only
6810 * explicit existentially quantified variables.
6812 * The actual projection is performed by pip, but pip doesn't seem
6813 * to like equalities very much, so we first remove the equalities
6814 * among the parameters by performing a variable compression on
6815 * the parameters. Afterward, an inverse transformation is performed
6816 * and the equalities among the parameters are inserted back in.
6818 * The variable compression on the parameters may uncover additional
6819 * equalities that were only implicit before. We therefore check
6820 * if there are any new parameter equalities in the result and
6821 * if so recurse. The removal of parameter equalities is required
6822 * for the parameter compression performed by base_compute_divs.
6824 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6826 int i;
6827 struct isl_mat *eq;
6828 struct isl_mat *T, *T2;
6829 struct isl_set *set;
6830 unsigned nparam;
6832 bset = isl_basic_set_cow(bset);
6833 if (!bset)
6834 return NULL;
6836 if (bset->n_eq == 0)
6837 return base_compute_divs(bset);
6839 bset = isl_basic_set_gauss(bset, NULL);
6840 if (!bset)
6841 return NULL;
6842 if (isl_basic_set_plain_is_empty(bset))
6843 return isl_set_from_basic_set(bset);
6845 i = first_parameter_equality(bset);
6846 if (i == bset->n_eq)
6847 return base_compute_divs(bset);
6849 nparam = isl_basic_set_dim(bset, isl_dim_param);
6850 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6851 0, 1 + nparam);
6852 eq = isl_mat_cow(eq);
6853 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6854 if (T && T->n_col == 0) {
6855 isl_mat_free(T);
6856 isl_mat_free(T2);
6857 isl_mat_free(eq);
6858 bset = isl_basic_set_set_to_empty(bset);
6859 return isl_set_from_basic_set(bset);
6861 bset = basic_set_parameter_preimage(bset, T);
6863 i = first_parameter_equality(bset);
6864 if (!bset)
6865 set = NULL;
6866 else if (i == bset->n_eq)
6867 set = base_compute_divs(bset);
6868 else
6869 set = parameter_compute_divs(bset);
6870 set = set_parameter_preimage(set, T2);
6871 set = set_append_equalities(set, eq);
6872 return set;
6875 /* Insert the divs from "ls" before those of "bmap".
6877 * The number of columns is not changed, which means that the last
6878 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6879 * The caller is responsible for removing the same number of dimensions
6880 * from the space of "bmap".
6882 static __isl_give isl_basic_map *insert_divs_from_local_space(
6883 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6885 int i;
6886 int n_div;
6887 int old_n_div;
6889 n_div = isl_local_space_dim(ls, isl_dim_div);
6890 if (n_div == 0)
6891 return bmap;
6893 old_n_div = bmap->n_div;
6894 bmap = insert_div_rows(bmap, n_div);
6895 if (!bmap)
6896 return NULL;
6898 for (i = 0; i < n_div; ++i) {
6899 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6900 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6903 return bmap;
6906 /* Replace the space of "bmap" by the space and divs of "ls".
6908 * If "ls" has any divs, then we simplify the result since we may
6909 * have discovered some additional equalities that could simplify
6910 * the div expressions.
6912 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6913 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6915 int n_div;
6917 bmap = isl_basic_map_cow(bmap);
6918 if (!bmap || !ls)
6919 goto error;
6921 n_div = isl_local_space_dim(ls, isl_dim_div);
6922 bmap = insert_divs_from_local_space(bmap, ls);
6923 if (!bmap)
6924 goto error;
6926 isl_space_free(bmap->dim);
6927 bmap->dim = isl_local_space_get_space(ls);
6928 if (!bmap->dim)
6929 goto error;
6931 isl_local_space_free(ls);
6932 if (n_div > 0)
6933 bmap = isl_basic_map_simplify(bmap);
6934 bmap = isl_basic_map_finalize(bmap);
6935 return bmap;
6936 error:
6937 isl_basic_map_free(bmap);
6938 isl_local_space_free(ls);
6939 return NULL;
6942 /* Replace the space of "map" by the space and divs of "ls".
6944 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6945 __isl_take isl_local_space *ls)
6947 int i;
6949 map = isl_map_cow(map);
6950 if (!map || !ls)
6951 goto error;
6953 for (i = 0; i < map->n; ++i) {
6954 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6955 isl_local_space_copy(ls));
6956 if (!map->p[i])
6957 goto error;
6959 isl_space_free(map->dim);
6960 map->dim = isl_local_space_get_space(ls);
6961 if (!map->dim)
6962 goto error;
6964 isl_local_space_free(ls);
6965 return map;
6966 error:
6967 isl_local_space_free(ls);
6968 isl_map_free(map);
6969 return NULL;
6972 /* Compute an explicit representation for the existentially
6973 * quantified variables for which do not know any explicit representation yet.
6975 * We first sort the existentially quantified variables so that the
6976 * existentially quantified variables for which we already have an explicit
6977 * representation are placed before those for which we do not.
6978 * The input dimensions, the output dimensions and the existentially
6979 * quantified variables for which we already have an explicit
6980 * representation are then turned into parameters.
6981 * compute_divs returns a map with the same parameters and
6982 * no input or output dimensions and the dimension specification
6983 * is reset to that of the input, including the existentially quantified
6984 * variables for which we already had an explicit representation.
6986 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6988 struct isl_basic_set *bset;
6989 struct isl_set *set;
6990 struct isl_map *map;
6991 isl_space *dim;
6992 isl_local_space *ls;
6993 unsigned nparam;
6994 unsigned n_in;
6995 unsigned n_out;
6996 unsigned n_known;
6997 int i;
6999 bmap = isl_basic_map_sort_divs(bmap);
7000 bmap = isl_basic_map_cow(bmap);
7001 if (!bmap)
7002 return NULL;
7004 for (n_known = 0; n_known < bmap->n_div; ++n_known)
7005 if (isl_int_is_zero(bmap->div[n_known][0]))
7006 break;
7008 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7009 n_in = isl_basic_map_dim(bmap, isl_dim_in);
7010 n_out = isl_basic_map_dim(bmap, isl_dim_out);
7011 dim = isl_space_set_alloc(bmap->ctx,
7012 nparam + n_in + n_out + n_known, 0);
7013 if (!dim)
7014 goto error;
7016 ls = isl_basic_map_get_local_space(bmap);
7017 ls = isl_local_space_drop_dims(ls, isl_dim_div,
7018 n_known, bmap->n_div - n_known);
7019 if (n_known > 0) {
7020 for (i = n_known; i < bmap->n_div; ++i)
7021 swap_div(bmap, i - n_known, i);
7022 bmap->n_div -= n_known;
7023 bmap->extra -= n_known;
7025 bmap = isl_basic_map_reset_space(bmap, dim);
7026 bset = (struct isl_basic_set *)bmap;
7028 set = parameter_compute_divs(bset);
7029 map = (struct isl_map *)set;
7030 map = replace_space_by_local_space(map, ls);
7032 return map;
7033 error:
7034 isl_basic_map_free(bmap);
7035 return NULL;
7038 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7040 int i;
7041 unsigned off;
7043 if (!bmap)
7044 return -1;
7046 off = isl_space_dim(bmap->dim, isl_dim_all);
7047 for (i = 0; i < bmap->n_div; ++i) {
7048 if (isl_int_is_zero(bmap->div[i][0]))
7049 return 0;
7050 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
7051 return -1);
7053 return 1;
7056 static int map_divs_known(__isl_keep isl_map *map)
7058 int i;
7060 if (!map)
7061 return -1;
7063 for (i = 0; i < map->n; ++i) {
7064 int known = isl_basic_map_divs_known(map->p[i]);
7065 if (known <= 0)
7066 return known;
7069 return 1;
7072 /* If bmap contains any unknown divs, then compute explicit
7073 * expressions for them. However, this computation may be
7074 * quite expensive, so first try to remove divs that aren't
7075 * strictly needed.
7077 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7079 int known;
7080 struct isl_map *map;
7082 known = isl_basic_map_divs_known(bmap);
7083 if (known < 0)
7084 goto error;
7085 if (known)
7086 return isl_map_from_basic_map(bmap);
7088 bmap = isl_basic_map_drop_redundant_divs(bmap);
7090 known = isl_basic_map_divs_known(bmap);
7091 if (known < 0)
7092 goto error;
7093 if (known)
7094 return isl_map_from_basic_map(bmap);
7096 map = compute_divs(bmap);
7097 return map;
7098 error:
7099 isl_basic_map_free(bmap);
7100 return NULL;
7103 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7105 int i;
7106 int known;
7107 struct isl_map *res;
7109 if (!map)
7110 return NULL;
7111 if (map->n == 0)
7112 return map;
7114 known = map_divs_known(map);
7115 if (known < 0) {
7116 isl_map_free(map);
7117 return NULL;
7119 if (known)
7120 return map;
7122 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7123 for (i = 1 ; i < map->n; ++i) {
7124 struct isl_map *r2;
7125 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7126 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7127 res = isl_map_union_disjoint(res, r2);
7128 else
7129 res = isl_map_union(res, r2);
7131 isl_map_free(map);
7133 return res;
7136 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7138 return (struct isl_set *)
7139 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7142 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7144 return (struct isl_set *)
7145 isl_map_compute_divs((struct isl_map *)set);
7148 struct isl_set *isl_map_domain(struct isl_map *map)
7150 int i;
7151 struct isl_set *set;
7153 if (!map)
7154 goto error;
7156 map = isl_map_cow(map);
7157 if (!map)
7158 return NULL;
7160 set = (struct isl_set *)map;
7161 set->dim = isl_space_domain(set->dim);
7162 if (!set->dim)
7163 goto error;
7164 for (i = 0; i < map->n; ++i) {
7165 set->p[i] = isl_basic_map_domain(map->p[i]);
7166 if (!set->p[i])
7167 goto error;
7169 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7170 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7171 return set;
7172 error:
7173 isl_map_free(map);
7174 return NULL;
7177 /* Return the union of "map1" and "map2", where we assume for now that
7178 * "map1" and "map2" are disjoint. Note that the basic maps inside
7179 * "map1" or "map2" may not be disjoint from each other.
7180 * Also note that this function is also called from isl_map_union,
7181 * which takes care of handling the situation where "map1" and "map2"
7182 * may not be disjoint.
7184 * If one of the inputs is empty, we can simply return the other input.
7185 * Similarly, if one of the inputs is universal, then it is equal to the union.
7187 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7188 __isl_take isl_map *map2)
7190 int i;
7191 unsigned flags = 0;
7192 struct isl_map *map = NULL;
7193 int is_universe;
7195 if (!map1 || !map2)
7196 goto error;
7198 if (!isl_space_is_equal(map1->dim, map2->dim))
7199 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7200 "spaces don't match", goto error);
7202 if (map1->n == 0) {
7203 isl_map_free(map1);
7204 return map2;
7206 if (map2->n == 0) {
7207 isl_map_free(map2);
7208 return map1;
7211 is_universe = isl_map_plain_is_universe(map1);
7212 if (is_universe < 0)
7213 goto error;
7214 if (is_universe) {
7215 isl_map_free(map2);
7216 return map1;
7219 is_universe = isl_map_plain_is_universe(map2);
7220 if (is_universe < 0)
7221 goto error;
7222 if (is_universe) {
7223 isl_map_free(map1);
7224 return map2;
7227 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7228 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7229 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7231 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7232 map1->n + map2->n, flags);
7233 if (!map)
7234 goto error;
7235 for (i = 0; i < map1->n; ++i) {
7236 map = isl_map_add_basic_map(map,
7237 isl_basic_map_copy(map1->p[i]));
7238 if (!map)
7239 goto error;
7241 for (i = 0; i < map2->n; ++i) {
7242 map = isl_map_add_basic_map(map,
7243 isl_basic_map_copy(map2->p[i]));
7244 if (!map)
7245 goto error;
7247 isl_map_free(map1);
7248 isl_map_free(map2);
7249 return map;
7250 error:
7251 isl_map_free(map);
7252 isl_map_free(map1);
7253 isl_map_free(map2);
7254 return NULL;
7257 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7258 * guaranteed to be disjoint by the caller.
7260 * Note that this functions is called from within isl_map_make_disjoint,
7261 * so we have to be careful not to touch the constraints of the inputs
7262 * in any way.
7264 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7265 __isl_take isl_map *map2)
7267 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7270 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7271 * not be disjoint. The parameters are assumed to have been aligned.
7273 * We currently simply call map_union_disjoint, the internal operation
7274 * of which does not really depend on the inputs being disjoint.
7275 * If the result contains more than one basic map, then we clear
7276 * the disjoint flag since the result may contain basic maps from
7277 * both inputs and these are not guaranteed to be disjoint.
7279 * As a special case, if "map1" and "map2" are obviously equal,
7280 * then we simply return "map1".
7282 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7283 __isl_take isl_map *map2)
7285 int equal;
7287 if (!map1 || !map2)
7288 goto error;
7290 equal = isl_map_plain_is_equal(map1, map2);
7291 if (equal < 0)
7292 goto error;
7293 if (equal) {
7294 isl_map_free(map2);
7295 return map1;
7298 map1 = map_union_disjoint(map1, map2);
7299 if (!map1)
7300 return NULL;
7301 if (map1->n > 1)
7302 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7303 return map1;
7304 error:
7305 isl_map_free(map1);
7306 isl_map_free(map2);
7307 return NULL;
7310 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7311 * not be disjoint.
7313 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7314 __isl_take isl_map *map2)
7316 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7319 struct isl_set *isl_set_union_disjoint(
7320 struct isl_set *set1, struct isl_set *set2)
7322 return (struct isl_set *)
7323 isl_map_union_disjoint(
7324 (struct isl_map *)set1, (struct isl_map *)set2);
7327 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7329 return (struct isl_set *)
7330 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7333 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7334 * the results.
7336 * "map" and "set" are assumed to be compatible and non-NULL.
7338 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7339 __isl_take isl_set *set,
7340 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7341 __isl_take isl_basic_set *bset))
7343 unsigned flags = 0;
7344 struct isl_map *result;
7345 int i, j;
7347 if (isl_set_plain_is_universe(set)) {
7348 isl_set_free(set);
7349 return map;
7352 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7353 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7354 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7356 result = isl_map_alloc_space(isl_space_copy(map->dim),
7357 map->n * set->n, flags);
7358 for (i = 0; result && i < map->n; ++i)
7359 for (j = 0; j < set->n; ++j) {
7360 result = isl_map_add_basic_map(result,
7361 fn(isl_basic_map_copy(map->p[i]),
7362 isl_basic_set_copy(set->p[j])));
7363 if (!result)
7364 break;
7367 isl_map_free(map);
7368 isl_set_free(set);
7369 return result;
7372 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7373 __isl_take isl_set *set)
7375 if (!map || !set)
7376 goto error;
7378 if (!isl_map_compatible_range(map, set))
7379 isl_die(set->ctx, isl_error_invalid,
7380 "incompatible spaces", goto error);
7382 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7383 error:
7384 isl_map_free(map);
7385 isl_set_free(set);
7386 return NULL;
7389 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7390 __isl_take isl_set *set)
7392 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7395 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7396 __isl_take isl_set *set)
7398 if (!map || !set)
7399 goto error;
7401 if (!isl_map_compatible_domain(map, set))
7402 isl_die(set->ctx, isl_error_invalid,
7403 "incompatible spaces", goto error);
7405 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7406 error:
7407 isl_map_free(map);
7408 isl_set_free(set);
7409 return NULL;
7412 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7413 __isl_take isl_set *set)
7415 return isl_map_align_params_map_map_and(map, set,
7416 &map_intersect_domain);
7419 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7420 __isl_take isl_map *map2)
7422 if (!map1 || !map2)
7423 goto error;
7424 map1 = isl_map_reverse(map1);
7425 map1 = isl_map_apply_range(map1, map2);
7426 return isl_map_reverse(map1);
7427 error:
7428 isl_map_free(map1);
7429 isl_map_free(map2);
7430 return NULL;
7433 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7434 __isl_take isl_map *map2)
7436 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7439 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7440 __isl_take isl_map *map2)
7442 isl_space *dim_result;
7443 struct isl_map *result;
7444 int i, j;
7446 if (!map1 || !map2)
7447 goto error;
7449 dim_result = isl_space_join(isl_space_copy(map1->dim),
7450 isl_space_copy(map2->dim));
7452 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7453 if (!result)
7454 goto error;
7455 for (i = 0; i < map1->n; ++i)
7456 for (j = 0; j < map2->n; ++j) {
7457 result = isl_map_add_basic_map(result,
7458 isl_basic_map_apply_range(
7459 isl_basic_map_copy(map1->p[i]),
7460 isl_basic_map_copy(map2->p[j])));
7461 if (!result)
7462 goto error;
7464 isl_map_free(map1);
7465 isl_map_free(map2);
7466 if (result && result->n <= 1)
7467 ISL_F_SET(result, ISL_MAP_DISJOINT);
7468 return result;
7469 error:
7470 isl_map_free(map1);
7471 isl_map_free(map2);
7472 return NULL;
7475 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7476 __isl_take isl_map *map2)
7478 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7482 * returns range - domain
7484 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7486 isl_space *dims, *target_dim;
7487 struct isl_basic_set *bset;
7488 unsigned dim;
7489 unsigned nparam;
7490 int i;
7492 if (!bmap)
7493 goto error;
7494 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7495 bmap->dim, isl_dim_out),
7496 goto error);
7497 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7498 dim = isl_basic_map_n_in(bmap);
7499 nparam = isl_basic_map_n_param(bmap);
7500 bset = isl_basic_set_from_basic_map(bmap);
7501 bset = isl_basic_set_cow(bset);
7502 dims = isl_basic_set_get_space(bset);
7503 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7504 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7505 bset = isl_basic_set_swap_vars(bset, 2*dim);
7506 for (i = 0; i < dim; ++i) {
7507 int j = isl_basic_map_alloc_equality(
7508 (struct isl_basic_map *)bset);
7509 if (j < 0) {
7510 bset = isl_basic_set_free(bset);
7511 break;
7513 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7514 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7515 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7516 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7518 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7519 bset = isl_basic_set_reset_space(bset, target_dim);
7520 return bset;
7521 error:
7522 isl_basic_map_free(bmap);
7523 return NULL;
7527 * returns range - domain
7529 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7531 int i;
7532 isl_space *dim;
7533 struct isl_set *result;
7535 if (!map)
7536 return NULL;
7538 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7539 map->dim, isl_dim_out),
7540 goto error);
7541 dim = isl_map_get_space(map);
7542 dim = isl_space_domain(dim);
7543 result = isl_set_alloc_space(dim, map->n, 0);
7544 if (!result)
7545 goto error;
7546 for (i = 0; i < map->n; ++i)
7547 result = isl_set_add_basic_set(result,
7548 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7549 isl_map_free(map);
7550 return result;
7551 error:
7552 isl_map_free(map);
7553 return NULL;
7557 * returns [domain -> range] -> range - domain
7559 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7560 __isl_take isl_basic_map *bmap)
7562 int i, k;
7563 isl_space *dim;
7564 isl_basic_map *domain;
7565 int nparam, n;
7566 unsigned total;
7568 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7569 bmap->dim, isl_dim_out))
7570 isl_die(bmap->ctx, isl_error_invalid,
7571 "domain and range don't match", goto error);
7573 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7574 n = isl_basic_map_dim(bmap, isl_dim_in);
7576 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7577 domain = isl_basic_map_universe(dim);
7579 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7580 bmap = isl_basic_map_apply_range(bmap, domain);
7581 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7583 total = isl_basic_map_total_dim(bmap);
7585 for (i = 0; i < n; ++i) {
7586 k = isl_basic_map_alloc_equality(bmap);
7587 if (k < 0)
7588 goto error;
7589 isl_seq_clr(bmap->eq[k], 1 + total);
7590 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7591 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7592 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7595 bmap = isl_basic_map_gauss(bmap, NULL);
7596 return isl_basic_map_finalize(bmap);
7597 error:
7598 isl_basic_map_free(bmap);
7599 return NULL;
7603 * returns [domain -> range] -> range - domain
7605 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7607 int i;
7608 isl_space *domain_dim;
7610 if (!map)
7611 return NULL;
7613 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7614 map->dim, isl_dim_out))
7615 isl_die(map->ctx, isl_error_invalid,
7616 "domain and range don't match", goto error);
7618 map = isl_map_cow(map);
7619 if (!map)
7620 return NULL;
7622 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7623 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7624 map->dim = isl_space_join(map->dim, domain_dim);
7625 if (!map->dim)
7626 goto error;
7627 for (i = 0; i < map->n; ++i) {
7628 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7629 if (!map->p[i])
7630 goto error;
7632 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7633 return map;
7634 error:
7635 isl_map_free(map);
7636 return NULL;
7639 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7641 struct isl_basic_map *bmap;
7642 unsigned nparam;
7643 unsigned dim;
7644 int i;
7646 if (!dims)
7647 return NULL;
7649 nparam = dims->nparam;
7650 dim = dims->n_out;
7651 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7652 if (!bmap)
7653 goto error;
7655 for (i = 0; i < dim; ++i) {
7656 int j = isl_basic_map_alloc_equality(bmap);
7657 if (j < 0)
7658 goto error;
7659 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7660 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7661 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7663 return isl_basic_map_finalize(bmap);
7664 error:
7665 isl_basic_map_free(bmap);
7666 return NULL;
7669 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7671 if (!dim)
7672 return NULL;
7673 if (dim->n_in != dim->n_out)
7674 isl_die(dim->ctx, isl_error_invalid,
7675 "number of input and output dimensions needs to be "
7676 "the same", goto error);
7677 return basic_map_identity(dim);
7678 error:
7679 isl_space_free(dim);
7680 return NULL;
7683 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7685 if (!model || !model->dim)
7686 return NULL;
7687 return isl_basic_map_identity(isl_space_copy(model->dim));
7690 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7692 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7695 struct isl_map *isl_map_identity_like(struct isl_map *model)
7697 if (!model || !model->dim)
7698 return NULL;
7699 return isl_map_identity(isl_space_copy(model->dim));
7702 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7704 if (!model || !model->dim)
7705 return NULL;
7706 return isl_map_identity(isl_space_copy(model->dim));
7709 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7711 isl_space *dim = isl_set_get_space(set);
7712 isl_map *id;
7713 id = isl_map_identity(isl_space_map_from_set(dim));
7714 return isl_map_intersect_range(id, set);
7717 /* Construct a basic set with all set dimensions having only non-negative
7718 * values.
7720 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7721 __isl_take isl_space *space)
7723 int i;
7724 unsigned nparam;
7725 unsigned dim;
7726 struct isl_basic_set *bset;
7728 if (!space)
7729 return NULL;
7730 nparam = space->nparam;
7731 dim = space->n_out;
7732 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7733 if (!bset)
7734 return NULL;
7735 for (i = 0; i < dim; ++i) {
7736 int k = isl_basic_set_alloc_inequality(bset);
7737 if (k < 0)
7738 goto error;
7739 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7740 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7742 return bset;
7743 error:
7744 isl_basic_set_free(bset);
7745 return NULL;
7748 /* Construct the half-space x_pos >= 0.
7750 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7751 int pos)
7753 int k;
7754 isl_basic_set *nonneg;
7756 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7757 k = isl_basic_set_alloc_inequality(nonneg);
7758 if (k < 0)
7759 goto error;
7760 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7761 isl_int_set_si(nonneg->ineq[k][pos], 1);
7763 return isl_basic_set_finalize(nonneg);
7764 error:
7765 isl_basic_set_free(nonneg);
7766 return NULL;
7769 /* Construct the half-space x_pos <= -1.
7771 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7773 int k;
7774 isl_basic_set *neg;
7776 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7777 k = isl_basic_set_alloc_inequality(neg);
7778 if (k < 0)
7779 goto error;
7780 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7781 isl_int_set_si(neg->ineq[k][0], -1);
7782 isl_int_set_si(neg->ineq[k][pos], -1);
7784 return isl_basic_set_finalize(neg);
7785 error:
7786 isl_basic_set_free(neg);
7787 return NULL;
7790 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7791 enum isl_dim_type type, unsigned first, unsigned n)
7793 int i;
7794 isl_basic_set *nonneg;
7795 isl_basic_set *neg;
7797 if (!set)
7798 return NULL;
7799 if (n == 0)
7800 return set;
7802 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7804 for (i = 0; i < n; ++i) {
7805 nonneg = nonneg_halfspace(isl_set_get_space(set),
7806 pos(set->dim, type) + first + i);
7807 neg = neg_halfspace(isl_set_get_space(set),
7808 pos(set->dim, type) + first + i);
7810 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7813 return set;
7814 error:
7815 isl_set_free(set);
7816 return NULL;
7819 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7820 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7821 void *user)
7823 isl_set *half;
7825 if (!set)
7826 return -1;
7827 if (isl_set_plain_is_empty(set)) {
7828 isl_set_free(set);
7829 return 0;
7831 if (first == len)
7832 return fn(set, signs, user);
7834 signs[first] = 1;
7835 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7836 1 + first));
7837 half = isl_set_intersect(half, isl_set_copy(set));
7838 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7839 goto error;
7841 signs[first] = -1;
7842 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7843 1 + first));
7844 half = isl_set_intersect(half, set);
7845 return foreach_orthant(half, signs, first + 1, len, fn, user);
7846 error:
7847 isl_set_free(set);
7848 return -1;
7851 /* Call "fn" on the intersections of "set" with each of the orthants
7852 * (except for obviously empty intersections). The orthant is identified
7853 * by the signs array, with each entry having value 1 or -1 according
7854 * to the sign of the corresponding variable.
7856 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7857 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7858 void *user)
7860 unsigned nparam;
7861 unsigned nvar;
7862 int *signs;
7863 int r;
7865 if (!set)
7866 return -1;
7867 if (isl_set_plain_is_empty(set))
7868 return 0;
7870 nparam = isl_set_dim(set, isl_dim_param);
7871 nvar = isl_set_dim(set, isl_dim_set);
7873 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7875 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7876 fn, user);
7878 free(signs);
7880 return r;
7883 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7885 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7888 int isl_basic_map_is_subset(
7889 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7891 int is_subset;
7892 struct isl_map *map1;
7893 struct isl_map *map2;
7895 if (!bmap1 || !bmap2)
7896 return -1;
7898 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7899 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7901 is_subset = isl_map_is_subset(map1, map2);
7903 isl_map_free(map1);
7904 isl_map_free(map2);
7906 return is_subset;
7909 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7910 __isl_keep isl_basic_set *bset2)
7912 return isl_basic_map_is_subset(bset1, bset2);
7915 int isl_basic_map_is_equal(
7916 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7918 int is_subset;
7920 if (!bmap1 || !bmap2)
7921 return -1;
7922 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7923 if (is_subset != 1)
7924 return is_subset;
7925 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7926 return is_subset;
7929 int isl_basic_set_is_equal(
7930 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7932 return isl_basic_map_is_equal(
7933 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7936 int isl_map_is_empty(struct isl_map *map)
7938 int i;
7939 int is_empty;
7941 if (!map)
7942 return -1;
7943 for (i = 0; i < map->n; ++i) {
7944 is_empty = isl_basic_map_is_empty(map->p[i]);
7945 if (is_empty < 0)
7946 return -1;
7947 if (!is_empty)
7948 return 0;
7950 return 1;
7953 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7955 return map ? map->n == 0 : -1;
7958 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7960 return isl_map_plain_is_empty(map);
7963 int isl_set_plain_is_empty(struct isl_set *set)
7965 return set ? set->n == 0 : -1;
7968 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7970 return isl_set_plain_is_empty(set);
7973 int isl_set_is_empty(struct isl_set *set)
7975 return isl_map_is_empty((struct isl_map *)set);
7978 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7980 if (!map1 || !map2)
7981 return -1;
7983 return isl_space_is_equal(map1->dim, map2->dim);
7986 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7988 if (!set1 || !set2)
7989 return -1;
7991 return isl_space_is_equal(set1->dim, set2->dim);
7994 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7996 int is_subset;
7998 if (!map1 || !map2)
7999 return -1;
8000 is_subset = isl_map_is_subset(map1, map2);
8001 if (is_subset != 1)
8002 return is_subset;
8003 is_subset = isl_map_is_subset(map2, map1);
8004 return is_subset;
8007 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8009 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8012 int isl_basic_map_is_strict_subset(
8013 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8015 int is_subset;
8017 if (!bmap1 || !bmap2)
8018 return -1;
8019 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8020 if (is_subset != 1)
8021 return is_subset;
8022 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8023 if (is_subset == -1)
8024 return is_subset;
8025 return !is_subset;
8028 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
8030 int is_subset;
8032 if (!map1 || !map2)
8033 return -1;
8034 is_subset = isl_map_is_subset(map1, map2);
8035 if (is_subset != 1)
8036 return is_subset;
8037 is_subset = isl_map_is_subset(map2, map1);
8038 if (is_subset == -1)
8039 return is_subset;
8040 return !is_subset;
8043 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8045 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
8048 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
8050 if (!bmap)
8051 return -1;
8052 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8055 int isl_basic_set_is_universe(struct isl_basic_set *bset)
8057 if (!bset)
8058 return -1;
8059 return bset->n_eq == 0 && bset->n_ineq == 0;
8062 int isl_map_plain_is_universe(__isl_keep isl_map *map)
8064 int i;
8066 if (!map)
8067 return -1;
8069 for (i = 0; i < map->n; ++i) {
8070 int r = isl_basic_map_is_universe(map->p[i]);
8071 if (r < 0 || r)
8072 return r;
8075 return 0;
8078 int isl_set_plain_is_universe(__isl_keep isl_set *set)
8080 return isl_map_plain_is_universe((isl_map *) set);
8083 int isl_set_fast_is_universe(__isl_keep isl_set *set)
8085 return isl_set_plain_is_universe(set);
8088 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
8090 struct isl_basic_set *bset = NULL;
8091 struct isl_vec *sample = NULL;
8092 int empty;
8093 unsigned total;
8095 if (!bmap)
8096 return -1;
8098 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8099 return 1;
8101 if (isl_basic_map_is_universe(bmap))
8102 return 0;
8104 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8105 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8106 copy = isl_basic_map_remove_redundancies(copy);
8107 empty = isl_basic_map_plain_is_empty(copy);
8108 isl_basic_map_free(copy);
8109 return empty;
8112 total = 1 + isl_basic_map_total_dim(bmap);
8113 if (bmap->sample && bmap->sample->size == total) {
8114 int contains = isl_basic_map_contains(bmap, bmap->sample);
8115 if (contains < 0)
8116 return -1;
8117 if (contains)
8118 return 0;
8120 isl_vec_free(bmap->sample);
8121 bmap->sample = NULL;
8122 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8123 if (!bset)
8124 return -1;
8125 sample = isl_basic_set_sample_vec(bset);
8126 if (!sample)
8127 return -1;
8128 empty = sample->size == 0;
8129 isl_vec_free(bmap->sample);
8130 bmap->sample = sample;
8131 if (empty)
8132 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8134 return empty;
8137 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8139 if (!bmap)
8140 return -1;
8141 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8144 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
8146 return isl_basic_map_plain_is_empty(bmap);
8149 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8151 if (!bset)
8152 return -1;
8153 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8156 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
8158 return isl_basic_set_plain_is_empty(bset);
8161 int isl_basic_set_is_empty(struct isl_basic_set *bset)
8163 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8166 struct isl_map *isl_basic_map_union(
8167 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8169 struct isl_map *map;
8170 if (!bmap1 || !bmap2)
8171 goto error;
8173 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8175 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8176 if (!map)
8177 goto error;
8178 map = isl_map_add_basic_map(map, bmap1);
8179 map = isl_map_add_basic_map(map, bmap2);
8180 return map;
8181 error:
8182 isl_basic_map_free(bmap1);
8183 isl_basic_map_free(bmap2);
8184 return NULL;
8187 struct isl_set *isl_basic_set_union(
8188 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8190 return (struct isl_set *)isl_basic_map_union(
8191 (struct isl_basic_map *)bset1,
8192 (struct isl_basic_map *)bset2);
8195 /* Order divs such that any div only depends on previous divs */
8196 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8198 int i;
8199 unsigned off;
8201 if (!bmap)
8202 return NULL;
8204 off = isl_space_dim(bmap->dim, isl_dim_all);
8206 for (i = 0; i < bmap->n_div; ++i) {
8207 int pos;
8208 if (isl_int_is_zero(bmap->div[i][0]))
8209 continue;
8210 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8211 bmap->n_div-i);
8212 if (pos == -1)
8213 continue;
8214 isl_basic_map_swap_div(bmap, i, i + pos);
8215 --i;
8217 return bmap;
8220 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8222 return (struct isl_basic_set *)
8223 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8226 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8228 int i;
8230 if (!map)
8231 return 0;
8233 for (i = 0; i < map->n; ++i) {
8234 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8235 if (!map->p[i])
8236 goto error;
8239 return map;
8240 error:
8241 isl_map_free(map);
8242 return NULL;
8245 /* Apply the expansion computed by isl_merge_divs.
8246 * The expansion itself is given by "exp" while the resulting
8247 * list of divs is given by "div".
8249 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8250 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8252 int i, j;
8253 int n_div;
8255 bset = isl_basic_set_cow(bset);
8256 if (!bset || !div)
8257 goto error;
8259 if (div->n_row < bset->n_div)
8260 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8261 "not an expansion", goto error);
8263 n_div = bset->n_div;
8264 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8265 div->n_row - n_div, 0,
8266 2 * (div->n_row - n_div));
8268 for (i = n_div; i < div->n_row; ++i)
8269 if (isl_basic_set_alloc_div(bset) < 0)
8270 goto error;
8272 j = n_div - 1;
8273 for (i = div->n_row - 1; i >= 0; --i) {
8274 if (j >= 0 && exp[j] == i) {
8275 if (i != j)
8276 isl_basic_map_swap_div(bset, i, j);
8277 j--;
8278 } else {
8279 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8280 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8281 goto error;
8285 isl_mat_free(div);
8286 return bset;
8287 error:
8288 isl_basic_set_free(bset);
8289 isl_mat_free(div);
8290 return NULL;
8293 /* Look for a div in dst that corresponds to the div "div" in src.
8294 * The divs before "div" in src and dst are assumed to be the same.
8296 * Returns -1 if no corresponding div was found and the position
8297 * of the corresponding div in dst otherwise.
8299 static int find_div(struct isl_basic_map *dst,
8300 struct isl_basic_map *src, unsigned div)
8302 int i;
8304 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8306 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8307 for (i = div; i < dst->n_div; ++i)
8308 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8309 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8310 dst->n_div - div) == -1)
8311 return i;
8312 return -1;
8315 /* Align the divs of "dst" to those of "src", adding divs from "src"
8316 * if needed. That is, make sure that the first src->n_div divs
8317 * of the result are equal to those of src.
8319 * The result is not finalized as by design it will have redundant
8320 * divs if any divs from "src" were copied.
8322 __isl_give isl_basic_map *isl_basic_map_align_divs(
8323 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8325 int i;
8326 int known, extended;
8327 unsigned total;
8329 if (!dst || !src)
8330 return isl_basic_map_free(dst);
8332 if (src->n_div == 0)
8333 return dst;
8335 known = isl_basic_map_divs_known(src);
8336 if (known < 0)
8337 return isl_basic_map_free(dst);
8338 if (!known)
8339 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8340 "some src divs are unknown",
8341 return isl_basic_map_free(dst));
8343 src = isl_basic_map_order_divs(src);
8345 extended = 0;
8346 total = isl_space_dim(src->dim, isl_dim_all);
8347 for (i = 0; i < src->n_div; ++i) {
8348 int j = find_div(dst, src, i);
8349 if (j < 0) {
8350 if (!extended) {
8351 int extra = src->n_div - i;
8352 dst = isl_basic_map_cow(dst);
8353 if (!dst)
8354 return NULL;
8355 dst = isl_basic_map_extend_space(dst,
8356 isl_space_copy(dst->dim),
8357 extra, 0, 2 * extra);
8358 extended = 1;
8360 j = isl_basic_map_alloc_div(dst);
8361 if (j < 0)
8362 return isl_basic_map_free(dst);
8363 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8364 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8365 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8366 return isl_basic_map_free(dst);
8368 if (j != i)
8369 isl_basic_map_swap_div(dst, i, j);
8371 return dst;
8374 struct isl_basic_set *isl_basic_set_align_divs(
8375 struct isl_basic_set *dst, struct isl_basic_set *src)
8377 return (struct isl_basic_set *)isl_basic_map_align_divs(
8378 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8381 struct isl_map *isl_map_align_divs(struct isl_map *map)
8383 int i;
8385 if (!map)
8386 return NULL;
8387 if (map->n == 0)
8388 return map;
8389 map = isl_map_compute_divs(map);
8390 map = isl_map_cow(map);
8391 if (!map)
8392 return NULL;
8394 for (i = 1; i < map->n; ++i)
8395 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8396 for (i = 1; i < map->n; ++i) {
8397 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8398 if (!map->p[i])
8399 return isl_map_free(map);
8402 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8403 return map;
8406 struct isl_set *isl_set_align_divs(struct isl_set *set)
8408 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8411 /* Align the divs of the basic maps in "map" to those
8412 * of the basic maps in "list", as well as to the other basic maps in "map".
8413 * The elements in "list" are assumed to have known divs.
8415 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8416 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8418 int i, n;
8420 map = isl_map_compute_divs(map);
8421 map = isl_map_cow(map);
8422 if (!map || !list)
8423 return isl_map_free(map);
8424 if (map->n == 0)
8425 return map;
8427 n = isl_basic_map_list_n_basic_map(list);
8428 for (i = 0; i < n; ++i) {
8429 isl_basic_map *bmap;
8431 bmap = isl_basic_map_list_get_basic_map(list, i);
8432 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8433 isl_basic_map_free(bmap);
8435 if (!map->p[0])
8436 return isl_map_free(map);
8438 return isl_map_align_divs(map);
8441 /* Align the divs of each element of "list" to those of "bmap".
8442 * Both "bmap" and the elements of "list" are assumed to have known divs.
8444 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8445 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8447 int i, n;
8449 if (!list || !bmap)
8450 return isl_basic_map_list_free(list);
8452 n = isl_basic_map_list_n_basic_map(list);
8453 for (i = 0; i < n; ++i) {
8454 isl_basic_map *bmap_i;
8456 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8457 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8458 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8461 return list;
8464 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8465 __isl_take isl_map *map)
8467 if (!set || !map)
8468 goto error;
8469 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8470 map = isl_map_intersect_domain(map, set);
8471 set = isl_map_range(map);
8472 return set;
8473 error:
8474 isl_set_free(set);
8475 isl_map_free(map);
8476 return NULL;
8479 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8480 __isl_take isl_map *map)
8482 return isl_map_align_params_map_map_and(set, map, &set_apply);
8485 /* There is no need to cow as removing empty parts doesn't change
8486 * the meaning of the set.
8488 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8490 int i;
8492 if (!map)
8493 return NULL;
8495 for (i = map->n - 1; i >= 0; --i)
8496 remove_if_empty(map, i);
8498 return map;
8501 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8503 return (struct isl_set *)
8504 isl_map_remove_empty_parts((struct isl_map *)set);
8507 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8509 struct isl_basic_map *bmap;
8510 if (!map || map->n == 0)
8511 return NULL;
8512 bmap = map->p[map->n-1];
8513 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8514 return isl_basic_map_copy(bmap);
8517 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8519 return (struct isl_basic_set *)
8520 isl_map_copy_basic_map((struct isl_map *)set);
8523 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8524 __isl_keep isl_basic_map *bmap)
8526 int i;
8528 if (!map || !bmap)
8529 goto error;
8530 for (i = map->n-1; i >= 0; --i) {
8531 if (map->p[i] != bmap)
8532 continue;
8533 map = isl_map_cow(map);
8534 if (!map)
8535 goto error;
8536 isl_basic_map_free(map->p[i]);
8537 if (i != map->n-1) {
8538 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8539 map->p[i] = map->p[map->n-1];
8541 map->n--;
8542 return map;
8544 return map;
8545 error:
8546 isl_map_free(map);
8547 return NULL;
8550 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8551 struct isl_basic_set *bset)
8553 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8554 (struct isl_basic_map *)bset);
8557 /* Given two basic sets bset1 and bset2, compute the maximal difference
8558 * between the values of dimension pos in bset1 and those in bset2
8559 * for any common value of the parameters and dimensions preceding pos.
8561 static enum isl_lp_result basic_set_maximal_difference_at(
8562 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8563 int pos, isl_int *opt)
8565 isl_space *dims;
8566 struct isl_basic_map *bmap1 = NULL;
8567 struct isl_basic_map *bmap2 = NULL;
8568 struct isl_ctx *ctx;
8569 struct isl_vec *obj;
8570 unsigned total;
8571 unsigned nparam;
8572 unsigned dim1, dim2;
8573 enum isl_lp_result res;
8575 if (!bset1 || !bset2)
8576 return isl_lp_error;
8578 nparam = isl_basic_set_n_param(bset1);
8579 dim1 = isl_basic_set_n_dim(bset1);
8580 dim2 = isl_basic_set_n_dim(bset2);
8581 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8582 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8583 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8584 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8585 if (!bmap1 || !bmap2)
8586 goto error;
8587 bmap1 = isl_basic_map_cow(bmap1);
8588 bmap1 = isl_basic_map_extend(bmap1, nparam,
8589 pos, (dim1 - pos) + (dim2 - pos),
8590 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8591 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8592 if (!bmap1)
8593 goto error2;
8594 total = isl_basic_map_total_dim(bmap1);
8595 ctx = bmap1->ctx;
8596 obj = isl_vec_alloc(ctx, 1 + total);
8597 if (!obj)
8598 goto error2;
8599 isl_seq_clr(obj->block.data, 1 + total);
8600 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8601 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8602 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8603 opt, NULL, NULL);
8604 isl_basic_map_free(bmap1);
8605 isl_vec_free(obj);
8606 return res;
8607 error:
8608 isl_basic_map_free(bmap2);
8609 error2:
8610 isl_basic_map_free(bmap1);
8611 return isl_lp_error;
8614 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8615 * for any common value of the parameters and dimensions preceding pos
8616 * in both basic sets, the values of dimension pos in bset1 are
8617 * smaller or larger than those in bset2.
8619 * Returns
8620 * 1 if bset1 follows bset2
8621 * -1 if bset1 precedes bset2
8622 * 0 if bset1 and bset2 are incomparable
8623 * -2 if some error occurred.
8625 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8626 struct isl_basic_set *bset2, int pos)
8628 isl_int opt;
8629 enum isl_lp_result res;
8630 int cmp;
8632 isl_int_init(opt);
8634 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8636 if (res == isl_lp_empty)
8637 cmp = 0;
8638 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8639 res == isl_lp_unbounded)
8640 cmp = 1;
8641 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8642 cmp = -1;
8643 else
8644 cmp = -2;
8646 isl_int_clear(opt);
8647 return cmp;
8650 /* Given two basic sets bset1 and bset2, check whether
8651 * for any common value of the parameters and dimensions preceding pos
8652 * there is a value of dimension pos in bset1 that is larger
8653 * than a value of the same dimension in bset2.
8655 * Return
8656 * 1 if there exists such a pair
8657 * 0 if there is no such pair, but there is a pair of equal values
8658 * -1 otherwise
8659 * -2 if some error occurred.
8661 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8662 __isl_keep isl_basic_set *bset2, int pos)
8664 isl_int opt;
8665 enum isl_lp_result res;
8666 int cmp;
8668 isl_int_init(opt);
8670 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8672 if (res == isl_lp_empty)
8673 cmp = -1;
8674 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8675 res == isl_lp_unbounded)
8676 cmp = 1;
8677 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8678 cmp = -1;
8679 else if (res == isl_lp_ok)
8680 cmp = 0;
8681 else
8682 cmp = -2;
8684 isl_int_clear(opt);
8685 return cmp;
8688 /* Given two sets set1 and set2, check whether
8689 * for any common value of the parameters and dimensions preceding pos
8690 * there is a value of dimension pos in set1 that is larger
8691 * than a value of the same dimension in set2.
8693 * Return
8694 * 1 if there exists such a pair
8695 * 0 if there is no such pair, but there is a pair of equal values
8696 * -1 otherwise
8697 * -2 if some error occurred.
8699 int isl_set_follows_at(__isl_keep isl_set *set1,
8700 __isl_keep isl_set *set2, int pos)
8702 int i, j;
8703 int follows = -1;
8705 if (!set1 || !set2)
8706 return -2;
8708 for (i = 0; i < set1->n; ++i)
8709 for (j = 0; j < set2->n; ++j) {
8710 int f;
8711 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8712 if (f == 1 || f == -2)
8713 return f;
8714 if (f > follows)
8715 follows = f;
8718 return follows;
8721 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8722 unsigned pos, isl_int *val)
8724 int i;
8725 int d;
8726 unsigned total;
8728 if (!bmap)
8729 return -1;
8730 total = isl_basic_map_total_dim(bmap);
8731 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8732 for (; d+1 > pos; --d)
8733 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8734 break;
8735 if (d != pos)
8736 continue;
8737 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8738 return 0;
8739 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8740 return 0;
8741 if (!isl_int_is_one(bmap->eq[i][1+d]))
8742 return 0;
8743 if (val)
8744 isl_int_neg(*val, bmap->eq[i][0]);
8745 return 1;
8747 return 0;
8750 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8751 unsigned pos, isl_int *val)
8753 int i;
8754 isl_int v;
8755 isl_int tmp;
8756 int fixed;
8758 if (!map)
8759 return -1;
8760 if (map->n == 0)
8761 return 0;
8762 if (map->n == 1)
8763 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8764 isl_int_init(v);
8765 isl_int_init(tmp);
8766 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8767 for (i = 1; fixed == 1 && i < map->n; ++i) {
8768 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8769 if (fixed == 1 && isl_int_ne(tmp, v))
8770 fixed = 0;
8772 if (val)
8773 isl_int_set(*val, v);
8774 isl_int_clear(tmp);
8775 isl_int_clear(v);
8776 return fixed;
8779 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8780 unsigned pos, isl_int *val)
8782 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8783 pos, val);
8786 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8787 isl_int *val)
8789 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8792 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8793 enum isl_dim_type type, unsigned pos, isl_int *val)
8795 if (pos >= isl_basic_map_dim(bmap, type))
8796 return -1;
8797 return isl_basic_map_plain_has_fixed_var(bmap,
8798 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8801 /* If "bmap" obviously lies on a hyperplane where the given dimension
8802 * has a fixed value, then return that value.
8803 * Otherwise return NaN.
8805 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8806 __isl_keep isl_basic_map *bmap,
8807 enum isl_dim_type type, unsigned pos)
8809 isl_ctx *ctx;
8810 isl_val *v;
8811 int fixed;
8813 if (!bmap)
8814 return NULL;
8815 ctx = isl_basic_map_get_ctx(bmap);
8816 v = isl_val_alloc(ctx);
8817 if (!v)
8818 return NULL;
8819 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8820 if (fixed < 0)
8821 return isl_val_free(v);
8822 if (fixed) {
8823 isl_int_set_si(v->d, 1);
8824 return v;
8826 isl_val_free(v);
8827 return isl_val_nan(ctx);
8830 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8831 enum isl_dim_type type, unsigned pos, isl_int *val)
8833 if (pos >= isl_map_dim(map, type))
8834 return -1;
8835 return isl_map_plain_has_fixed_var(map,
8836 map_offset(map, type) - 1 + pos, val);
8839 /* If "map" obviously lies on a hyperplane where the given dimension
8840 * has a fixed value, then return that value.
8841 * Otherwise return NaN.
8843 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8844 enum isl_dim_type type, unsigned pos)
8846 isl_ctx *ctx;
8847 isl_val *v;
8848 int fixed;
8850 if (!map)
8851 return NULL;
8852 ctx = isl_map_get_ctx(map);
8853 v = isl_val_alloc(ctx);
8854 if (!v)
8855 return NULL;
8856 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8857 if (fixed < 0)
8858 return isl_val_free(v);
8859 if (fixed) {
8860 isl_int_set_si(v->d, 1);
8861 return v;
8863 isl_val_free(v);
8864 return isl_val_nan(ctx);
8867 /* If "set" obviously lies on a hyperplane where the given dimension
8868 * has a fixed value, then return that value.
8869 * Otherwise return NaN.
8871 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8872 enum isl_dim_type type, unsigned pos)
8874 return isl_map_plain_get_val_if_fixed(set, type, pos);
8877 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8878 enum isl_dim_type type, unsigned pos, isl_int *val)
8880 return isl_map_plain_is_fixed(set, type, pos, val);
8883 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8884 enum isl_dim_type type, unsigned pos, isl_int *val)
8886 return isl_map_plain_is_fixed(map, type, pos, val);
8889 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8890 * then return this fixed value in *val.
8892 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8893 unsigned dim, isl_int *val)
8895 return isl_basic_set_plain_has_fixed_var(bset,
8896 isl_basic_set_n_param(bset) + dim, val);
8899 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8900 * then return this fixed value in *val.
8902 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8903 unsigned dim, isl_int *val)
8905 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8908 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8909 unsigned dim, isl_int *val)
8911 return isl_set_plain_dim_is_fixed(set, dim, val);
8914 /* Check if input variable in has fixed value and if so and if val is not NULL,
8915 * then return this fixed value in *val.
8917 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8918 unsigned in, isl_int *val)
8920 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8923 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8924 * and if val is not NULL, then return this lower bound in *val.
8926 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8927 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8929 int i, i_eq = -1, i_ineq = -1;
8930 isl_int *c;
8931 unsigned total;
8932 unsigned nparam;
8934 if (!bset)
8935 return -1;
8936 total = isl_basic_set_total_dim(bset);
8937 nparam = isl_basic_set_n_param(bset);
8938 for (i = 0; i < bset->n_eq; ++i) {
8939 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8940 continue;
8941 if (i_eq != -1)
8942 return 0;
8943 i_eq = i;
8945 for (i = 0; i < bset->n_ineq; ++i) {
8946 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8947 continue;
8948 if (i_eq != -1 || i_ineq != -1)
8949 return 0;
8950 i_ineq = i;
8952 if (i_eq == -1 && i_ineq == -1)
8953 return 0;
8954 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8955 /* The coefficient should always be one due to normalization. */
8956 if (!isl_int_is_one(c[1+nparam+dim]))
8957 return 0;
8958 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8959 return 0;
8960 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8961 total - nparam - dim - 1) != -1)
8962 return 0;
8963 if (val)
8964 isl_int_neg(*val, c[0]);
8965 return 1;
8968 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8969 unsigned dim, isl_int *val)
8971 int i;
8972 isl_int v;
8973 isl_int tmp;
8974 int fixed;
8976 if (!set)
8977 return -1;
8978 if (set->n == 0)
8979 return 0;
8980 if (set->n == 1)
8981 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8982 dim, val);
8983 isl_int_init(v);
8984 isl_int_init(tmp);
8985 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8986 dim, &v);
8987 for (i = 1; fixed == 1 && i < set->n; ++i) {
8988 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8989 dim, &tmp);
8990 if (fixed == 1 && isl_int_ne(tmp, v))
8991 fixed = 0;
8993 if (val)
8994 isl_int_set(*val, v);
8995 isl_int_clear(tmp);
8996 isl_int_clear(v);
8997 return fixed;
9000 /* uset_gist depends on constraints without existentially quantified
9001 * variables sorting first.
9003 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9005 isl_int **c1 = (isl_int **) p1;
9006 isl_int **c2 = (isl_int **) p2;
9007 int l1, l2;
9008 unsigned size = *(unsigned *) arg;
9010 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9011 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9013 if (l1 != l2)
9014 return l1 - l2;
9016 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9019 static struct isl_basic_map *isl_basic_map_sort_constraints(
9020 struct isl_basic_map *bmap)
9022 unsigned total;
9024 if (!bmap)
9025 return NULL;
9026 if (bmap->n_ineq == 0)
9027 return bmap;
9028 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9029 return bmap;
9030 total = isl_basic_map_total_dim(bmap);
9031 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9032 &sort_constraint_cmp, &total) < 0)
9033 return isl_basic_map_free(bmap);
9034 return bmap;
9037 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9038 __isl_take isl_basic_set *bset)
9040 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9041 (struct isl_basic_map *)bset);
9044 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9046 if (!bmap)
9047 return NULL;
9048 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9049 return bmap;
9050 bmap = isl_basic_map_remove_redundancies(bmap);
9051 bmap = isl_basic_map_sort_constraints(bmap);
9052 if (bmap)
9053 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9054 return bmap;
9057 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9059 return (struct isl_basic_set *)isl_basic_map_normalize(
9060 (struct isl_basic_map *)bset);
9063 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9064 const __isl_keep isl_basic_map *bmap2)
9066 int i, cmp;
9067 unsigned total;
9069 if (bmap1 == bmap2)
9070 return 0;
9071 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9072 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9073 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9074 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9075 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9076 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9077 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9078 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9079 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9080 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9081 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9082 return 0;
9083 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9084 return 1;
9085 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9086 return -1;
9087 if (bmap1->n_eq != bmap2->n_eq)
9088 return bmap1->n_eq - bmap2->n_eq;
9089 if (bmap1->n_ineq != bmap2->n_ineq)
9090 return bmap1->n_ineq - bmap2->n_ineq;
9091 if (bmap1->n_div != bmap2->n_div)
9092 return bmap1->n_div - bmap2->n_div;
9093 total = isl_basic_map_total_dim(bmap1);
9094 for (i = 0; i < bmap1->n_eq; ++i) {
9095 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9096 if (cmp)
9097 return cmp;
9099 for (i = 0; i < bmap1->n_ineq; ++i) {
9100 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9101 if (cmp)
9102 return cmp;
9104 for (i = 0; i < bmap1->n_div; ++i) {
9105 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9106 if (cmp)
9107 return cmp;
9109 return 0;
9112 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9113 const __isl_keep isl_basic_set *bset2)
9115 return isl_basic_map_plain_cmp(bset1, bset2);
9118 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9120 int i, cmp;
9122 if (set1 == set2)
9123 return 0;
9124 if (set1->n != set2->n)
9125 return set1->n - set2->n;
9127 for (i = 0; i < set1->n; ++i) {
9128 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9129 if (cmp)
9130 return cmp;
9133 return 0;
9136 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9137 __isl_keep isl_basic_map *bmap2)
9139 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9142 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9143 __isl_keep isl_basic_set *bset2)
9145 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9146 (isl_basic_map *)bset2);
9149 static int qsort_bmap_cmp(const void *p1, const void *p2)
9151 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9152 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9154 return isl_basic_map_plain_cmp(bmap1, bmap2);
9157 /* Sort the basic maps of "map" and remove duplicate basic maps.
9159 * While removing basic maps, we make sure that the basic maps remain
9160 * sorted because isl_map_normalize expects the basic maps of the result
9161 * to be sorted.
9163 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9165 int i, j;
9167 map = isl_map_remove_empty_parts(map);
9168 if (!map)
9169 return NULL;
9170 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9171 for (i = map->n - 1; i >= 1; --i) {
9172 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9173 continue;
9174 isl_basic_map_free(map->p[i-1]);
9175 for (j = i; j < map->n; ++j)
9176 map->p[j - 1] = map->p[j];
9177 map->n--;
9180 return map;
9183 /* Remove obvious duplicates among the basic maps of "map".
9185 * Unlike isl_map_normalize, this function does not remove redundant
9186 * constraints and only removes duplicates that have exactly the same
9187 * constraints in the input. It does sort the constraints and
9188 * the basic maps to ease the detection of duplicates.
9190 * If "map" has already been normalized or if the basic maps are
9191 * disjoint, then there can be no duplicates.
9193 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9195 int i;
9196 isl_basic_map *bmap;
9198 if (!map)
9199 return NULL;
9200 if (map->n <= 1)
9201 return map;
9202 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9203 return map;
9204 for (i = 0; i < map->n; ++i) {
9205 bmap = isl_basic_map_copy(map->p[i]);
9206 bmap = isl_basic_map_sort_constraints(bmap);
9207 if (!bmap)
9208 return isl_map_free(map);
9209 isl_basic_map_free(map->p[i]);
9210 map->p[i] = bmap;
9213 map = sort_and_remove_duplicates(map);
9214 return map;
9217 /* We normalize in place, but if anything goes wrong we need
9218 * to return NULL, so we need to make sure we don't change the
9219 * meaning of any possible other copies of map.
9221 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9223 int i;
9224 struct isl_basic_map *bmap;
9226 if (!map)
9227 return NULL;
9228 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9229 return map;
9230 for (i = 0; i < map->n; ++i) {
9231 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9232 if (!bmap)
9233 goto error;
9234 isl_basic_map_free(map->p[i]);
9235 map->p[i] = bmap;
9238 map = sort_and_remove_duplicates(map);
9239 if (map)
9240 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9241 return map;
9242 error:
9243 isl_map_free(map);
9244 return NULL;
9247 struct isl_set *isl_set_normalize(struct isl_set *set)
9249 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9252 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9254 int i;
9255 int equal;
9257 if (!map1 || !map2)
9258 return -1;
9260 if (map1 == map2)
9261 return 1;
9262 if (!isl_space_is_equal(map1->dim, map2->dim))
9263 return 0;
9265 map1 = isl_map_copy(map1);
9266 map2 = isl_map_copy(map2);
9267 map1 = isl_map_normalize(map1);
9268 map2 = isl_map_normalize(map2);
9269 if (!map1 || !map2)
9270 goto error;
9271 equal = map1->n == map2->n;
9272 for (i = 0; equal && i < map1->n; ++i) {
9273 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9274 if (equal < 0)
9275 goto error;
9277 isl_map_free(map1);
9278 isl_map_free(map2);
9279 return equal;
9280 error:
9281 isl_map_free(map1);
9282 isl_map_free(map2);
9283 return -1;
9286 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9288 return isl_map_plain_is_equal(map1, map2);
9291 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9293 return isl_map_plain_is_equal((struct isl_map *)set1,
9294 (struct isl_map *)set2);
9297 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9299 return isl_set_plain_is_equal(set1, set2);
9302 /* Return an interval that ranges from min to max (inclusive)
9304 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9305 isl_int min, isl_int max)
9307 int k;
9308 struct isl_basic_set *bset = NULL;
9310 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9311 if (!bset)
9312 goto error;
9314 k = isl_basic_set_alloc_inequality(bset);
9315 if (k < 0)
9316 goto error;
9317 isl_int_set_si(bset->ineq[k][1], 1);
9318 isl_int_neg(bset->ineq[k][0], min);
9320 k = isl_basic_set_alloc_inequality(bset);
9321 if (k < 0)
9322 goto error;
9323 isl_int_set_si(bset->ineq[k][1], -1);
9324 isl_int_set(bset->ineq[k][0], max);
9326 return bset;
9327 error:
9328 isl_basic_set_free(bset);
9329 return NULL;
9332 /* Return the basic maps in "map" as a list.
9334 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9335 __isl_keep isl_map *map)
9337 int i;
9338 isl_ctx *ctx;
9339 isl_basic_map_list *list;
9341 if (!map)
9342 return NULL;
9343 ctx = isl_map_get_ctx(map);
9344 list = isl_basic_map_list_alloc(ctx, map->n);
9346 for (i = 0; i < map->n; ++i) {
9347 isl_basic_map *bmap;
9349 bmap = isl_basic_map_copy(map->p[i]);
9350 list = isl_basic_map_list_add(list, bmap);
9353 return list;
9356 /* Return the intersection of the elements in the non-empty list "list".
9357 * All elements are assumed to live in the same space.
9359 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9360 __isl_take isl_basic_map_list *list)
9362 int i, n;
9363 isl_basic_map *bmap;
9365 if (!list)
9366 return NULL;
9367 n = isl_basic_map_list_n_basic_map(list);
9368 if (n < 1)
9369 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9370 "expecting non-empty list", goto error);
9372 bmap = isl_basic_map_list_get_basic_map(list, 0);
9373 for (i = 1; i < n; ++i) {
9374 isl_basic_map *bmap_i;
9376 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9377 bmap = isl_basic_map_intersect(bmap, bmap_i);
9380 isl_basic_map_list_free(list);
9381 return bmap;
9382 error:
9383 isl_basic_map_list_free(list);
9384 return NULL;
9387 /* Return the intersection of the elements in the non-empty list "list".
9388 * All elements are assumed to live in the same space.
9390 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9391 __isl_take isl_basic_set_list *list)
9393 return isl_basic_map_list_intersect(list);
9396 /* Return the Cartesian product of the basic sets in list (in the given order).
9398 __isl_give isl_basic_set *isl_basic_set_list_product(
9399 __isl_take struct isl_basic_set_list *list)
9401 int i;
9402 unsigned dim;
9403 unsigned nparam;
9404 unsigned extra;
9405 unsigned n_eq;
9406 unsigned n_ineq;
9407 struct isl_basic_set *product = NULL;
9409 if (!list)
9410 goto error;
9411 isl_assert(list->ctx, list->n > 0, goto error);
9412 isl_assert(list->ctx, list->p[0], goto error);
9413 nparam = isl_basic_set_n_param(list->p[0]);
9414 dim = isl_basic_set_n_dim(list->p[0]);
9415 extra = list->p[0]->n_div;
9416 n_eq = list->p[0]->n_eq;
9417 n_ineq = list->p[0]->n_ineq;
9418 for (i = 1; i < list->n; ++i) {
9419 isl_assert(list->ctx, list->p[i], goto error);
9420 isl_assert(list->ctx,
9421 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9422 dim += isl_basic_set_n_dim(list->p[i]);
9423 extra += list->p[i]->n_div;
9424 n_eq += list->p[i]->n_eq;
9425 n_ineq += list->p[i]->n_ineq;
9427 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9428 n_eq, n_ineq);
9429 if (!product)
9430 goto error;
9431 dim = 0;
9432 for (i = 0; i < list->n; ++i) {
9433 isl_basic_set_add_constraints(product,
9434 isl_basic_set_copy(list->p[i]), dim);
9435 dim += isl_basic_set_n_dim(list->p[i]);
9437 isl_basic_set_list_free(list);
9438 return product;
9439 error:
9440 isl_basic_set_free(product);
9441 isl_basic_set_list_free(list);
9442 return NULL;
9445 struct isl_basic_map *isl_basic_map_product(
9446 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9448 isl_space *dim_result = NULL;
9449 struct isl_basic_map *bmap;
9450 unsigned in1, in2, out1, out2, nparam, total, pos;
9451 struct isl_dim_map *dim_map1, *dim_map2;
9453 if (!bmap1 || !bmap2)
9454 goto error;
9456 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9457 bmap2->dim, isl_dim_param), goto error);
9458 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9459 isl_space_copy(bmap2->dim));
9461 in1 = isl_basic_map_n_in(bmap1);
9462 in2 = isl_basic_map_n_in(bmap2);
9463 out1 = isl_basic_map_n_out(bmap1);
9464 out2 = isl_basic_map_n_out(bmap2);
9465 nparam = isl_basic_map_n_param(bmap1);
9467 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9468 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9469 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9470 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9471 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9472 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9473 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9474 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9475 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9476 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9477 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9479 bmap = isl_basic_map_alloc_space(dim_result,
9480 bmap1->n_div + bmap2->n_div,
9481 bmap1->n_eq + bmap2->n_eq,
9482 bmap1->n_ineq + bmap2->n_ineq);
9483 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9484 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9485 bmap = isl_basic_map_simplify(bmap);
9486 return isl_basic_map_finalize(bmap);
9487 error:
9488 isl_basic_map_free(bmap1);
9489 isl_basic_map_free(bmap2);
9490 return NULL;
9493 __isl_give isl_basic_map *isl_basic_map_flat_product(
9494 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9496 isl_basic_map *prod;
9498 prod = isl_basic_map_product(bmap1, bmap2);
9499 prod = isl_basic_map_flatten(prod);
9500 return prod;
9503 __isl_give isl_basic_set *isl_basic_set_flat_product(
9504 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9506 return isl_basic_map_flat_range_product(bset1, bset2);
9509 __isl_give isl_basic_map *isl_basic_map_domain_product(
9510 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9512 isl_space *space_result = NULL;
9513 isl_basic_map *bmap;
9514 unsigned in1, in2, out, nparam, total, pos;
9515 struct isl_dim_map *dim_map1, *dim_map2;
9517 if (!bmap1 || !bmap2)
9518 goto error;
9520 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9521 isl_space_copy(bmap2->dim));
9523 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9524 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9525 out = isl_basic_map_dim(bmap1, isl_dim_out);
9526 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9528 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9529 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9530 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9531 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9532 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9533 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9534 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9535 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9536 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9537 isl_dim_map_div(dim_map1, bmap1, pos += out);
9538 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9540 bmap = isl_basic_map_alloc_space(space_result,
9541 bmap1->n_div + bmap2->n_div,
9542 bmap1->n_eq + bmap2->n_eq,
9543 bmap1->n_ineq + bmap2->n_ineq);
9544 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9545 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9546 bmap = isl_basic_map_simplify(bmap);
9547 return isl_basic_map_finalize(bmap);
9548 error:
9549 isl_basic_map_free(bmap1);
9550 isl_basic_map_free(bmap2);
9551 return NULL;
9554 __isl_give isl_basic_map *isl_basic_map_range_product(
9555 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9557 isl_space *dim_result = NULL;
9558 isl_basic_map *bmap;
9559 unsigned in, out1, out2, nparam, total, pos;
9560 struct isl_dim_map *dim_map1, *dim_map2;
9562 if (!bmap1 || !bmap2)
9563 goto error;
9565 if (!isl_space_match(bmap1->dim, isl_dim_param,
9566 bmap2->dim, isl_dim_param))
9567 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9568 "parameters don't match", goto error);
9570 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9571 isl_space_copy(bmap2->dim));
9573 in = isl_basic_map_dim(bmap1, isl_dim_in);
9574 out1 = isl_basic_map_n_out(bmap1);
9575 out2 = isl_basic_map_n_out(bmap2);
9576 nparam = isl_basic_map_n_param(bmap1);
9578 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9579 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9580 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9581 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9582 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9583 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9584 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9585 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9586 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9587 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9588 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9590 bmap = isl_basic_map_alloc_space(dim_result,
9591 bmap1->n_div + bmap2->n_div,
9592 bmap1->n_eq + bmap2->n_eq,
9593 bmap1->n_ineq + bmap2->n_ineq);
9594 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9595 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9596 bmap = isl_basic_map_simplify(bmap);
9597 return isl_basic_map_finalize(bmap);
9598 error:
9599 isl_basic_map_free(bmap1);
9600 isl_basic_map_free(bmap2);
9601 return NULL;
9604 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9605 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9607 isl_basic_map *prod;
9609 prod = isl_basic_map_range_product(bmap1, bmap2);
9610 prod = isl_basic_map_flatten_range(prod);
9611 return prod;
9614 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9615 * and collect the results.
9616 * The result live in the space obtained by calling "space_product"
9617 * on the spaces of "map1" and "map2".
9618 * If "remove_duplicates" is set then the result may contain duplicates
9619 * (even if the inputs do not) and so we try and remove the obvious
9620 * duplicates.
9622 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9623 __isl_take isl_map *map2,
9624 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9625 __isl_take isl_space *right),
9626 __isl_give isl_basic_map *(*basic_map_product)(
9627 __isl_take isl_basic_map *left,
9628 __isl_take isl_basic_map *right),
9629 int remove_duplicates)
9631 unsigned flags = 0;
9632 struct isl_map *result;
9633 int i, j;
9635 if (!map1 || !map2)
9636 goto error;
9638 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9639 map2->dim, isl_dim_param), goto error);
9641 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9642 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9643 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9645 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9646 isl_space_copy(map2->dim)),
9647 map1->n * map2->n, flags);
9648 if (!result)
9649 goto error;
9650 for (i = 0; i < map1->n; ++i)
9651 for (j = 0; j < map2->n; ++j) {
9652 struct isl_basic_map *part;
9653 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9654 isl_basic_map_copy(map2->p[j]));
9655 if (isl_basic_map_is_empty(part))
9656 isl_basic_map_free(part);
9657 else
9658 result = isl_map_add_basic_map(result, part);
9659 if (!result)
9660 goto error;
9662 if (remove_duplicates)
9663 result = isl_map_remove_obvious_duplicates(result);
9664 isl_map_free(map1);
9665 isl_map_free(map2);
9666 return result;
9667 error:
9668 isl_map_free(map1);
9669 isl_map_free(map2);
9670 return NULL;
9673 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9675 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9676 __isl_take isl_map *map2)
9678 return map_product(map1, map2, &isl_space_product,
9679 &isl_basic_map_product, 0);
9682 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9683 __isl_take isl_map *map2)
9685 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9688 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9690 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9691 __isl_take isl_map *map2)
9693 isl_map *prod;
9695 prod = isl_map_product(map1, map2);
9696 prod = isl_map_flatten(prod);
9697 return prod;
9700 /* Given two set A and B, construct its Cartesian product A x B.
9702 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9704 return isl_map_range_product(set1, set2);
9707 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9708 __isl_take isl_set *set2)
9710 return isl_map_flat_range_product(set1, set2);
9713 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9715 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9716 __isl_take isl_map *map2)
9718 return map_product(map1, map2, &isl_space_domain_product,
9719 &isl_basic_map_domain_product, 1);
9722 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9724 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9725 __isl_take isl_map *map2)
9727 return map_product(map1, map2, &isl_space_range_product,
9728 &isl_basic_map_range_product, 1);
9731 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9732 __isl_take isl_map *map2)
9734 return isl_map_align_params_map_map_and(map1, map2,
9735 &map_domain_product_aligned);
9738 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9739 __isl_take isl_map *map2)
9741 return isl_map_align_params_map_map_and(map1, map2,
9742 &map_range_product_aligned);
9745 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9747 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9749 isl_space *space;
9750 int total1, keep1, total2, keep2;
9752 if (!map)
9753 return NULL;
9754 if (!isl_space_domain_is_wrapping(map->dim) ||
9755 !isl_space_range_is_wrapping(map->dim))
9756 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9757 "not a product", return isl_map_free(map));
9759 space = isl_map_get_space(map);
9760 total1 = isl_space_dim(space, isl_dim_in);
9761 total2 = isl_space_dim(space, isl_dim_out);
9762 space = isl_space_factor_domain(space);
9763 keep1 = isl_space_dim(space, isl_dim_in);
9764 keep2 = isl_space_dim(space, isl_dim_out);
9765 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9766 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9767 map = isl_map_reset_space(map, space);
9769 return map;
9772 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9774 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9776 isl_space *space;
9777 int total1, keep1, total2, keep2;
9779 if (!map)
9780 return NULL;
9781 if (!isl_space_domain_is_wrapping(map->dim) ||
9782 !isl_space_range_is_wrapping(map->dim))
9783 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9784 "not a product", return isl_map_free(map));
9786 space = isl_map_get_space(map);
9787 total1 = isl_space_dim(space, isl_dim_in);
9788 total2 = isl_space_dim(space, isl_dim_out);
9789 space = isl_space_factor_range(space);
9790 keep1 = isl_space_dim(space, isl_dim_in);
9791 keep2 = isl_space_dim(space, isl_dim_out);
9792 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9793 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9794 map = isl_map_reset_space(map, space);
9796 return map;
9799 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9801 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9803 isl_space *space;
9804 int total, keep;
9806 if (!map)
9807 return NULL;
9808 if (!isl_space_domain_is_wrapping(map->dim))
9809 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9810 "domain is not a product", return isl_map_free(map));
9812 space = isl_map_get_space(map);
9813 total = isl_space_dim(space, isl_dim_in);
9814 space = isl_space_domain_factor_domain(space);
9815 keep = isl_space_dim(space, isl_dim_in);
9816 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9817 map = isl_map_reset_space(map, space);
9819 return map;
9822 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9824 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9826 isl_space *space;
9827 int total, keep;
9829 if (!map)
9830 return NULL;
9831 if (!isl_space_domain_is_wrapping(map->dim))
9832 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9833 "domain is not a product", return isl_map_free(map));
9835 space = isl_map_get_space(map);
9836 total = isl_space_dim(space, isl_dim_in);
9837 space = isl_space_domain_factor_range(space);
9838 keep = isl_space_dim(space, isl_dim_in);
9839 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9840 map = isl_map_reset_space(map, space);
9842 return map;
9845 /* Given a map A -> [B -> C], extract the map A -> B.
9847 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9849 isl_space *space;
9850 int total, keep;
9852 if (!map)
9853 return NULL;
9854 if (!isl_space_range_is_wrapping(map->dim))
9855 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9856 "range is not a product", return isl_map_free(map));
9858 space = isl_map_get_space(map);
9859 total = isl_space_dim(space, isl_dim_out);
9860 space = isl_space_range_factor_domain(space);
9861 keep = isl_space_dim(space, isl_dim_out);
9862 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9863 map = isl_map_reset_space(map, space);
9865 return map;
9868 /* Given a map A -> [B -> C], extract the map A -> C.
9870 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9872 isl_space *space;
9873 int total, keep;
9875 if (!map)
9876 return NULL;
9877 if (!isl_space_range_is_wrapping(map->dim))
9878 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9879 "range is not a product", return isl_map_free(map));
9881 space = isl_map_get_space(map);
9882 total = isl_space_dim(space, isl_dim_out);
9883 space = isl_space_range_factor_range(space);
9884 keep = isl_space_dim(space, isl_dim_out);
9885 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9886 map = isl_map_reset_space(map, space);
9888 return map;
9891 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9893 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9894 __isl_take isl_map *map2)
9896 isl_map *prod;
9898 prod = isl_map_domain_product(map1, map2);
9899 prod = isl_map_flatten_domain(prod);
9900 return prod;
9903 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9905 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9906 __isl_take isl_map *map2)
9908 isl_map *prod;
9910 prod = isl_map_range_product(map1, map2);
9911 prod = isl_map_flatten_range(prod);
9912 return prod;
9915 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9917 int i;
9918 uint32_t hash = isl_hash_init();
9919 unsigned total;
9921 if (!bmap)
9922 return 0;
9923 bmap = isl_basic_map_copy(bmap);
9924 bmap = isl_basic_map_normalize(bmap);
9925 if (!bmap)
9926 return 0;
9927 total = isl_basic_map_total_dim(bmap);
9928 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9929 for (i = 0; i < bmap->n_eq; ++i) {
9930 uint32_t c_hash;
9931 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9932 isl_hash_hash(hash, c_hash);
9934 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9935 for (i = 0; i < bmap->n_ineq; ++i) {
9936 uint32_t c_hash;
9937 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9938 isl_hash_hash(hash, c_hash);
9940 isl_hash_byte(hash, bmap->n_div & 0xFF);
9941 for (i = 0; i < bmap->n_div; ++i) {
9942 uint32_t c_hash;
9943 if (isl_int_is_zero(bmap->div[i][0]))
9944 continue;
9945 isl_hash_byte(hash, i & 0xFF);
9946 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9947 isl_hash_hash(hash, c_hash);
9949 isl_basic_map_free(bmap);
9950 return hash;
9953 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9955 return isl_basic_map_get_hash((isl_basic_map *)bset);
9958 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9960 int i;
9961 uint32_t hash;
9963 if (!map)
9964 return 0;
9965 map = isl_map_copy(map);
9966 map = isl_map_normalize(map);
9967 if (!map)
9968 return 0;
9970 hash = isl_hash_init();
9971 for (i = 0; i < map->n; ++i) {
9972 uint32_t bmap_hash;
9973 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9974 isl_hash_hash(hash, bmap_hash);
9977 isl_map_free(map);
9979 return hash;
9982 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9984 return isl_map_get_hash((isl_map *)set);
9987 /* Check if the value for dimension dim is completely determined
9988 * by the values of the other parameters and variables.
9989 * That is, check if dimension dim is involved in an equality.
9991 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9993 int i;
9994 unsigned nparam;
9996 if (!bset)
9997 return -1;
9998 nparam = isl_basic_set_n_param(bset);
9999 for (i = 0; i < bset->n_eq; ++i)
10000 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10001 return 1;
10002 return 0;
10005 /* Check if the value for dimension dim is completely determined
10006 * by the values of the other parameters and variables.
10007 * That is, check if dimension dim is involved in an equality
10008 * for each of the subsets.
10010 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10012 int i;
10014 if (!set)
10015 return -1;
10016 for (i = 0; i < set->n; ++i) {
10017 int unique;
10018 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10019 if (unique != 1)
10020 return unique;
10022 return 1;
10025 /* Return the number of basic maps in the (current) representation of "map".
10027 int isl_map_n_basic_map(__isl_keep isl_map *map)
10029 return map ? map->n : 0;
10032 int isl_set_n_basic_set(__isl_keep isl_set *set)
10034 return set ? set->n : 0;
10037 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
10038 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10040 int i;
10042 if (!map)
10043 return -1;
10045 for (i = 0; i < map->n; ++i)
10046 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10047 return -1;
10049 return 0;
10052 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
10053 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10055 int i;
10057 if (!set)
10058 return -1;
10060 for (i = 0; i < set->n; ++i)
10061 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10062 return -1;
10064 return 0;
10067 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10069 isl_space *dim;
10071 if (!bset)
10072 return NULL;
10074 bset = isl_basic_set_cow(bset);
10075 if (!bset)
10076 return NULL;
10078 dim = isl_basic_set_get_space(bset);
10079 dim = isl_space_lift(dim, bset->n_div);
10080 if (!dim)
10081 goto error;
10082 isl_space_free(bset->dim);
10083 bset->dim = dim;
10084 bset->extra -= bset->n_div;
10085 bset->n_div = 0;
10087 bset = isl_basic_set_finalize(bset);
10089 return bset;
10090 error:
10091 isl_basic_set_free(bset);
10092 return NULL;
10095 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10097 int i;
10098 isl_space *dim;
10099 unsigned n_div;
10101 set = isl_set_align_divs(set);
10103 if (!set)
10104 return NULL;
10106 set = isl_set_cow(set);
10107 if (!set)
10108 return NULL;
10110 n_div = set->p[0]->n_div;
10111 dim = isl_set_get_space(set);
10112 dim = isl_space_lift(dim, n_div);
10113 if (!dim)
10114 goto error;
10115 isl_space_free(set->dim);
10116 set->dim = dim;
10118 for (i = 0; i < set->n; ++i) {
10119 set->p[i] = isl_basic_set_lift(set->p[i]);
10120 if (!set->p[i])
10121 goto error;
10124 return set;
10125 error:
10126 isl_set_free(set);
10127 return NULL;
10130 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10132 isl_space *dim;
10133 struct isl_basic_map *bmap;
10134 unsigned n_set;
10135 unsigned n_div;
10136 unsigned n_param;
10137 unsigned total;
10138 int i, k, l;
10140 set = isl_set_align_divs(set);
10142 if (!set)
10143 return NULL;
10145 dim = isl_set_get_space(set);
10146 if (set->n == 0 || set->p[0]->n_div == 0) {
10147 isl_set_free(set);
10148 return isl_map_identity(isl_space_map_from_set(dim));
10151 n_div = set->p[0]->n_div;
10152 dim = isl_space_map_from_set(dim);
10153 n_param = isl_space_dim(dim, isl_dim_param);
10154 n_set = isl_space_dim(dim, isl_dim_in);
10155 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10156 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10157 for (i = 0; i < n_set; ++i)
10158 bmap = var_equal(bmap, i);
10160 total = n_param + n_set + n_set + n_div;
10161 for (i = 0; i < n_div; ++i) {
10162 k = isl_basic_map_alloc_inequality(bmap);
10163 if (k < 0)
10164 goto error;
10165 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10166 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10167 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10168 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10169 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10170 set->p[0]->div[i][0]);
10172 l = isl_basic_map_alloc_inequality(bmap);
10173 if (l < 0)
10174 goto error;
10175 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10176 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10177 set->p[0]->div[i][0]);
10178 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10181 isl_set_free(set);
10182 bmap = isl_basic_map_simplify(bmap);
10183 bmap = isl_basic_map_finalize(bmap);
10184 return isl_map_from_basic_map(bmap);
10185 error:
10186 isl_set_free(set);
10187 isl_basic_map_free(bmap);
10188 return NULL;
10191 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10193 unsigned dim;
10194 int size = 0;
10196 if (!bset)
10197 return -1;
10199 dim = isl_basic_set_total_dim(bset);
10200 size += bset->n_eq * (1 + dim);
10201 size += bset->n_ineq * (1 + dim);
10202 size += bset->n_div * (2 + dim);
10204 return size;
10207 int isl_set_size(__isl_keep isl_set *set)
10209 int i;
10210 int size = 0;
10212 if (!set)
10213 return -1;
10215 for (i = 0; i < set->n; ++i)
10216 size += isl_basic_set_size(set->p[i]);
10218 return size;
10221 /* Check if there is any lower bound (if lower == 0) and/or upper
10222 * bound (if upper == 0) on the specified dim.
10224 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10225 enum isl_dim_type type, unsigned pos, int lower, int upper)
10227 int i;
10229 if (!bmap)
10230 return -1;
10232 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
10234 pos += isl_basic_map_offset(bmap, type);
10236 for (i = 0; i < bmap->n_div; ++i) {
10237 if (isl_int_is_zero(bmap->div[i][0]))
10238 continue;
10239 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10240 return 1;
10243 for (i = 0; i < bmap->n_eq; ++i)
10244 if (!isl_int_is_zero(bmap->eq[i][pos]))
10245 return 1;
10247 for (i = 0; i < bmap->n_ineq; ++i) {
10248 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10249 if (sgn > 0)
10250 lower = 1;
10251 if (sgn < 0)
10252 upper = 1;
10255 return lower && upper;
10258 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10259 enum isl_dim_type type, unsigned pos)
10261 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10264 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10265 enum isl_dim_type type, unsigned pos)
10267 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10270 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10271 enum isl_dim_type type, unsigned pos)
10273 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10276 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10277 enum isl_dim_type type, unsigned pos)
10279 int i;
10281 if (!map)
10282 return -1;
10284 for (i = 0; i < map->n; ++i) {
10285 int bounded;
10286 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10287 if (bounded < 0 || !bounded)
10288 return bounded;
10291 return 1;
10294 /* Return 1 if the specified dim is involved in both an upper bound
10295 * and a lower bound.
10297 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10298 enum isl_dim_type type, unsigned pos)
10300 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10303 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10305 static int has_any_bound(__isl_keep isl_map *map,
10306 enum isl_dim_type type, unsigned pos,
10307 int (*fn)(__isl_keep isl_basic_map *bmap,
10308 enum isl_dim_type type, unsigned pos))
10310 int i;
10312 if (!map)
10313 return -1;
10315 for (i = 0; i < map->n; ++i) {
10316 int bounded;
10317 bounded = fn(map->p[i], type, pos);
10318 if (bounded < 0 || bounded)
10319 return bounded;
10322 return 0;
10325 /* Return 1 if the specified dim is involved in any lower bound.
10327 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10328 enum isl_dim_type type, unsigned pos)
10330 return has_any_bound(set, type, pos,
10331 &isl_basic_map_dim_has_lower_bound);
10334 /* Return 1 if the specified dim is involved in any upper bound.
10336 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10337 enum isl_dim_type type, unsigned pos)
10339 return has_any_bound(set, type, pos,
10340 &isl_basic_map_dim_has_upper_bound);
10343 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10345 static int has_bound(__isl_keep isl_map *map,
10346 enum isl_dim_type type, unsigned pos,
10347 int (*fn)(__isl_keep isl_basic_map *bmap,
10348 enum isl_dim_type type, unsigned pos))
10350 int i;
10352 if (!map)
10353 return -1;
10355 for (i = 0; i < map->n; ++i) {
10356 int bounded;
10357 bounded = fn(map->p[i], type, pos);
10358 if (bounded < 0 || !bounded)
10359 return bounded;
10362 return 1;
10365 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10367 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10368 enum isl_dim_type type, unsigned pos)
10370 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10373 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10375 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10376 enum isl_dim_type type, unsigned pos)
10378 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10381 /* For each of the "n" variables starting at "first", determine
10382 * the sign of the variable and put the results in the first "n"
10383 * elements of the array "signs".
10384 * Sign
10385 * 1 means that the variable is non-negative
10386 * -1 means that the variable is non-positive
10387 * 0 means the variable attains both positive and negative values.
10389 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10390 unsigned first, unsigned n, int *signs)
10392 isl_vec *bound = NULL;
10393 struct isl_tab *tab = NULL;
10394 struct isl_tab_undo *snap;
10395 int i;
10397 if (!bset || !signs)
10398 return -1;
10400 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10401 tab = isl_tab_from_basic_set(bset, 0);
10402 if (!bound || !tab)
10403 goto error;
10405 isl_seq_clr(bound->el, bound->size);
10406 isl_int_set_si(bound->el[0], -1);
10408 snap = isl_tab_snap(tab);
10409 for (i = 0; i < n; ++i) {
10410 int empty;
10412 isl_int_set_si(bound->el[1 + first + i], -1);
10413 if (isl_tab_add_ineq(tab, bound->el) < 0)
10414 goto error;
10415 empty = tab->empty;
10416 isl_int_set_si(bound->el[1 + first + i], 0);
10417 if (isl_tab_rollback(tab, snap) < 0)
10418 goto error;
10420 if (empty) {
10421 signs[i] = 1;
10422 continue;
10425 isl_int_set_si(bound->el[1 + first + i], 1);
10426 if (isl_tab_add_ineq(tab, bound->el) < 0)
10427 goto error;
10428 empty = tab->empty;
10429 isl_int_set_si(bound->el[1 + first + i], 0);
10430 if (isl_tab_rollback(tab, snap) < 0)
10431 goto error;
10433 signs[i] = empty ? -1 : 0;
10436 isl_tab_free(tab);
10437 isl_vec_free(bound);
10438 return 0;
10439 error:
10440 isl_tab_free(tab);
10441 isl_vec_free(bound);
10442 return -1;
10445 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10446 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10448 if (!bset || !signs)
10449 return -1;
10450 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10451 return -1);
10453 first += pos(bset->dim, type) - 1;
10454 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10457 /* Is it possible for the integer division "div" to depend (possibly
10458 * indirectly) on any output dimensions?
10460 * If the div is undefined, then we conservatively assume that it
10461 * may depend on them.
10462 * Otherwise, we check if it actually depends on them or on any integer
10463 * divisions that may depend on them.
10465 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10467 int i;
10468 unsigned n_out, o_out;
10469 unsigned n_div, o_div;
10471 if (isl_int_is_zero(bmap->div[div][0]))
10472 return 1;
10474 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10475 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10477 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10478 return 1;
10480 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10481 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10483 for (i = 0; i < n_div; ++i) {
10484 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10485 continue;
10486 if (div_may_involve_output(bmap, i))
10487 return 1;
10490 return 0;
10493 /* Return the index of the equality of "bmap" that defines
10494 * the output dimension "pos" in terms of earlier dimensions.
10495 * The equality may also involve integer divisions, as long
10496 * as those integer divisions are defined in terms of
10497 * parameters or input dimensions.
10498 * Return bmap->n_eq if there is no such equality.
10499 * Return -1 on error.
10501 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10502 int pos)
10504 int j, k;
10505 unsigned n_out, o_out;
10506 unsigned n_div, o_div;
10508 if (!bmap)
10509 return -1;
10511 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10512 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10513 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10514 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10516 for (j = 0; j < bmap->n_eq; ++j) {
10517 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10518 continue;
10519 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10520 n_out - (pos + 1)) != -1)
10521 continue;
10522 for (k = 0; k < n_div; ++k) {
10523 if (isl_int_is_zero(bmap->eq[j][o_div + k]))
10524 continue;
10525 if (div_may_involve_output(bmap, k))
10526 break;
10528 if (k >= n_div)
10529 return j;
10532 return bmap->n_eq;
10535 /* Check if the given basic map is obviously single-valued.
10536 * In particular, for each output dimension, check that there is
10537 * an equality that defines the output dimension in terms of
10538 * earlier dimensions.
10540 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10542 int i;
10543 unsigned n_out;
10545 if (!bmap)
10546 return -1;
10548 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10550 for (i = 0; i < n_out; ++i) {
10551 int eq;
10553 eq = isl_basic_map_output_defining_equality(bmap, i);
10554 if (eq < 0)
10555 return -1;
10556 if (eq >= bmap->n_eq)
10557 return 0;
10560 return 1;
10563 /* Check if the given basic map is single-valued.
10564 * We simply compute
10566 * M \circ M^-1
10568 * and check if the result is a subset of the identity mapping.
10570 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10572 isl_space *space;
10573 isl_basic_map *test;
10574 isl_basic_map *id;
10575 int sv;
10577 sv = isl_basic_map_plain_is_single_valued(bmap);
10578 if (sv < 0 || sv)
10579 return sv;
10581 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10582 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10584 space = isl_basic_map_get_space(bmap);
10585 space = isl_space_map_from_set(isl_space_range(space));
10586 id = isl_basic_map_identity(space);
10588 sv = isl_basic_map_is_subset(test, id);
10590 isl_basic_map_free(test);
10591 isl_basic_map_free(id);
10593 return sv;
10596 /* Check if the given map is obviously single-valued.
10598 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10600 if (!map)
10601 return -1;
10602 if (map->n == 0)
10603 return 1;
10604 if (map->n >= 2)
10605 return 0;
10607 return isl_basic_map_plain_is_single_valued(map->p[0]);
10610 /* Check if the given map is single-valued.
10611 * We simply compute
10613 * M \circ M^-1
10615 * and check if the result is a subset of the identity mapping.
10617 int isl_map_is_single_valued(__isl_keep isl_map *map)
10619 isl_space *dim;
10620 isl_map *test;
10621 isl_map *id;
10622 int sv;
10624 sv = isl_map_plain_is_single_valued(map);
10625 if (sv < 0 || sv)
10626 return sv;
10628 test = isl_map_reverse(isl_map_copy(map));
10629 test = isl_map_apply_range(test, isl_map_copy(map));
10631 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10632 id = isl_map_identity(dim);
10634 sv = isl_map_is_subset(test, id);
10636 isl_map_free(test);
10637 isl_map_free(id);
10639 return sv;
10642 int isl_map_is_injective(__isl_keep isl_map *map)
10644 int in;
10646 map = isl_map_copy(map);
10647 map = isl_map_reverse(map);
10648 in = isl_map_is_single_valued(map);
10649 isl_map_free(map);
10651 return in;
10654 /* Check if the given map is obviously injective.
10656 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10658 int in;
10660 map = isl_map_copy(map);
10661 map = isl_map_reverse(map);
10662 in = isl_map_plain_is_single_valued(map);
10663 isl_map_free(map);
10665 return in;
10668 int isl_map_is_bijective(__isl_keep isl_map *map)
10670 int sv;
10672 sv = isl_map_is_single_valued(map);
10673 if (sv < 0 || !sv)
10674 return sv;
10676 return isl_map_is_injective(map);
10679 int isl_set_is_singleton(__isl_keep isl_set *set)
10681 return isl_map_is_single_valued((isl_map *)set);
10684 int isl_map_is_translation(__isl_keep isl_map *map)
10686 int ok;
10687 isl_set *delta;
10689 delta = isl_map_deltas(isl_map_copy(map));
10690 ok = isl_set_is_singleton(delta);
10691 isl_set_free(delta);
10693 return ok;
10696 static int unique(isl_int *p, unsigned pos, unsigned len)
10698 if (isl_seq_first_non_zero(p, pos) != -1)
10699 return 0;
10700 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10701 return 0;
10702 return 1;
10705 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10707 int i, j;
10708 unsigned nvar;
10709 unsigned ovar;
10711 if (!bset)
10712 return -1;
10714 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10715 return 0;
10717 nvar = isl_basic_set_dim(bset, isl_dim_set);
10718 ovar = isl_space_offset(bset->dim, isl_dim_set);
10719 for (j = 0; j < nvar; ++j) {
10720 int lower = 0, upper = 0;
10721 for (i = 0; i < bset->n_eq; ++i) {
10722 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10723 continue;
10724 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10725 return 0;
10726 break;
10728 if (i < bset->n_eq)
10729 continue;
10730 for (i = 0; i < bset->n_ineq; ++i) {
10731 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10732 continue;
10733 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10734 return 0;
10735 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10736 lower = 1;
10737 else
10738 upper = 1;
10740 if (!lower || !upper)
10741 return 0;
10744 return 1;
10747 int isl_set_is_box(__isl_keep isl_set *set)
10749 if (!set)
10750 return -1;
10751 if (set->n != 1)
10752 return 0;
10754 return isl_basic_set_is_box(set->p[0]);
10757 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10759 if (!bset)
10760 return -1;
10762 return isl_space_is_wrapping(bset->dim);
10765 int isl_set_is_wrapping(__isl_keep isl_set *set)
10767 if (!set)
10768 return -1;
10770 return isl_space_is_wrapping(set->dim);
10773 /* Is the domain of "map" a wrapped relation?
10775 int isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10777 if (!map)
10778 return -1;
10780 return isl_space_domain_is_wrapping(map->dim);
10783 /* Is the range of "map" a wrapped relation?
10785 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10787 if (!map)
10788 return -1;
10790 return isl_space_range_is_wrapping(map->dim);
10793 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10795 bmap = isl_basic_map_cow(bmap);
10796 if (!bmap)
10797 return NULL;
10799 bmap->dim = isl_space_wrap(bmap->dim);
10800 if (!bmap->dim)
10801 goto error;
10803 bmap = isl_basic_map_finalize(bmap);
10805 return (isl_basic_set *)bmap;
10806 error:
10807 isl_basic_map_free(bmap);
10808 return NULL;
10811 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10813 int i;
10815 map = isl_map_cow(map);
10816 if (!map)
10817 return NULL;
10819 for (i = 0; i < map->n; ++i) {
10820 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10821 if (!map->p[i])
10822 goto error;
10824 map->dim = isl_space_wrap(map->dim);
10825 if (!map->dim)
10826 goto error;
10828 return (isl_set *)map;
10829 error:
10830 isl_map_free(map);
10831 return NULL;
10834 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10836 bset = isl_basic_set_cow(bset);
10837 if (!bset)
10838 return NULL;
10840 bset->dim = isl_space_unwrap(bset->dim);
10841 if (!bset->dim)
10842 goto error;
10844 bset = isl_basic_set_finalize(bset);
10846 return (isl_basic_map *)bset;
10847 error:
10848 isl_basic_set_free(bset);
10849 return NULL;
10852 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10854 int i;
10856 if (!set)
10857 return NULL;
10859 if (!isl_set_is_wrapping(set))
10860 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10861 goto error);
10863 set = isl_set_cow(set);
10864 if (!set)
10865 return NULL;
10867 for (i = 0; i < set->n; ++i) {
10868 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10869 if (!set->p[i])
10870 goto error;
10873 set->dim = isl_space_unwrap(set->dim);
10874 if (!set->dim)
10875 goto error;
10877 return (isl_map *)set;
10878 error:
10879 isl_set_free(set);
10880 return NULL;
10883 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10884 enum isl_dim_type type)
10886 if (!bmap)
10887 return NULL;
10889 if (!isl_space_is_named_or_nested(bmap->dim, type))
10890 return bmap;
10892 bmap = isl_basic_map_cow(bmap);
10893 if (!bmap)
10894 return NULL;
10896 bmap->dim = isl_space_reset(bmap->dim, type);
10897 if (!bmap->dim)
10898 goto error;
10900 bmap = isl_basic_map_finalize(bmap);
10902 return bmap;
10903 error:
10904 isl_basic_map_free(bmap);
10905 return NULL;
10908 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10909 enum isl_dim_type type)
10911 int i;
10913 if (!map)
10914 return NULL;
10916 if (!isl_space_is_named_or_nested(map->dim, type))
10917 return map;
10919 map = isl_map_cow(map);
10920 if (!map)
10921 return NULL;
10923 for (i = 0; i < map->n; ++i) {
10924 map->p[i] = isl_basic_map_reset(map->p[i], type);
10925 if (!map->p[i])
10926 goto error;
10928 map->dim = isl_space_reset(map->dim, type);
10929 if (!map->dim)
10930 goto error;
10932 return map;
10933 error:
10934 isl_map_free(map);
10935 return NULL;
10938 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10940 if (!bmap)
10941 return NULL;
10943 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10944 return bmap;
10946 bmap = isl_basic_map_cow(bmap);
10947 if (!bmap)
10948 return NULL;
10950 bmap->dim = isl_space_flatten(bmap->dim);
10951 if (!bmap->dim)
10952 goto error;
10954 bmap = isl_basic_map_finalize(bmap);
10956 return bmap;
10957 error:
10958 isl_basic_map_free(bmap);
10959 return NULL;
10962 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10964 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10967 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10968 __isl_take isl_basic_map *bmap)
10970 if (!bmap)
10971 return NULL;
10973 if (!bmap->dim->nested[0])
10974 return bmap;
10976 bmap = isl_basic_map_cow(bmap);
10977 if (!bmap)
10978 return NULL;
10980 bmap->dim = isl_space_flatten_domain(bmap->dim);
10981 if (!bmap->dim)
10982 goto error;
10984 bmap = isl_basic_map_finalize(bmap);
10986 return bmap;
10987 error:
10988 isl_basic_map_free(bmap);
10989 return NULL;
10992 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10993 __isl_take isl_basic_map *bmap)
10995 if (!bmap)
10996 return NULL;
10998 if (!bmap->dim->nested[1])
10999 return bmap;
11001 bmap = isl_basic_map_cow(bmap);
11002 if (!bmap)
11003 return NULL;
11005 bmap->dim = isl_space_flatten_range(bmap->dim);
11006 if (!bmap->dim)
11007 goto error;
11009 bmap = isl_basic_map_finalize(bmap);
11011 return bmap;
11012 error:
11013 isl_basic_map_free(bmap);
11014 return NULL;
11017 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11019 int i;
11021 if (!map)
11022 return NULL;
11024 if (!map->dim->nested[0] && !map->dim->nested[1])
11025 return map;
11027 map = isl_map_cow(map);
11028 if (!map)
11029 return NULL;
11031 for (i = 0; i < map->n; ++i) {
11032 map->p[i] = isl_basic_map_flatten(map->p[i]);
11033 if (!map->p[i])
11034 goto error;
11036 map->dim = isl_space_flatten(map->dim);
11037 if (!map->dim)
11038 goto error;
11040 return map;
11041 error:
11042 isl_map_free(map);
11043 return NULL;
11046 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11048 return (isl_set *)isl_map_flatten((isl_map *)set);
11051 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11053 isl_space *dim, *flat_dim;
11054 isl_map *map;
11056 dim = isl_set_get_space(set);
11057 flat_dim = isl_space_flatten(isl_space_copy(dim));
11058 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11059 map = isl_map_intersect_domain(map, set);
11061 return map;
11064 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11066 int i;
11068 if (!map)
11069 return NULL;
11071 if (!map->dim->nested[0])
11072 return map;
11074 map = isl_map_cow(map);
11075 if (!map)
11076 return NULL;
11078 for (i = 0; i < map->n; ++i) {
11079 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
11080 if (!map->p[i])
11081 goto error;
11083 map->dim = isl_space_flatten_domain(map->dim);
11084 if (!map->dim)
11085 goto error;
11087 return map;
11088 error:
11089 isl_map_free(map);
11090 return NULL;
11093 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11095 int i;
11097 if (!map)
11098 return NULL;
11100 if (!map->dim->nested[1])
11101 return map;
11103 map = isl_map_cow(map);
11104 if (!map)
11105 return NULL;
11107 for (i = 0; i < map->n; ++i) {
11108 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
11109 if (!map->p[i])
11110 goto error;
11112 map->dim = isl_space_flatten_range(map->dim);
11113 if (!map->dim)
11114 goto error;
11116 return map;
11117 error:
11118 isl_map_free(map);
11119 return NULL;
11122 /* Reorder the dimensions of "bmap" according to the given dim_map
11123 * and set the dimension specification to "dim".
11125 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11126 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11128 isl_basic_map *res;
11129 unsigned flags;
11131 bmap = isl_basic_map_cow(bmap);
11132 if (!bmap || !dim || !dim_map)
11133 goto error;
11135 flags = bmap->flags;
11136 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11137 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11138 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11139 res = isl_basic_map_alloc_space(dim,
11140 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11141 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11142 if (res)
11143 res->flags = flags;
11144 res = isl_basic_map_finalize(res);
11145 return res;
11146 error:
11147 free(dim_map);
11148 isl_basic_map_free(bmap);
11149 isl_space_free(dim);
11150 return NULL;
11153 /* Reorder the dimensions of "map" according to given reordering.
11155 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11156 __isl_take isl_reordering *r)
11158 int i;
11159 struct isl_dim_map *dim_map;
11161 map = isl_map_cow(map);
11162 dim_map = isl_dim_map_from_reordering(r);
11163 if (!map || !r || !dim_map)
11164 goto error;
11166 for (i = 0; i < map->n; ++i) {
11167 struct isl_dim_map *dim_map_i;
11169 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11171 map->p[i] = isl_basic_map_realign(map->p[i],
11172 isl_space_copy(r->dim), dim_map_i);
11174 if (!map->p[i])
11175 goto error;
11178 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11180 isl_reordering_free(r);
11181 free(dim_map);
11182 return map;
11183 error:
11184 free(dim_map);
11185 isl_map_free(map);
11186 isl_reordering_free(r);
11187 return NULL;
11190 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11191 __isl_take isl_reordering *r)
11193 return (isl_set *)isl_map_realign((isl_map *)set, r);
11196 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11197 __isl_take isl_space *model)
11199 isl_ctx *ctx;
11201 if (!map || !model)
11202 goto error;
11204 ctx = isl_space_get_ctx(model);
11205 if (!isl_space_has_named_params(model))
11206 isl_die(ctx, isl_error_invalid,
11207 "model has unnamed parameters", goto error);
11208 if (!isl_space_has_named_params(map->dim))
11209 isl_die(ctx, isl_error_invalid,
11210 "relation has unnamed parameters", goto error);
11211 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11212 isl_reordering *exp;
11214 model = isl_space_drop_dims(model, isl_dim_in,
11215 0, isl_space_dim(model, isl_dim_in));
11216 model = isl_space_drop_dims(model, isl_dim_out,
11217 0, isl_space_dim(model, isl_dim_out));
11218 exp = isl_parameter_alignment_reordering(map->dim, model);
11219 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11220 map = isl_map_realign(map, exp);
11223 isl_space_free(model);
11224 return map;
11225 error:
11226 isl_space_free(model);
11227 isl_map_free(map);
11228 return NULL;
11231 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11232 __isl_take isl_space *model)
11234 return isl_map_align_params(set, model);
11237 /* Align the parameters of "bmap" to those of "model", introducing
11238 * additional parameters if needed.
11240 __isl_give isl_basic_map *isl_basic_map_align_params(
11241 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11243 isl_ctx *ctx;
11245 if (!bmap || !model)
11246 goto error;
11248 ctx = isl_space_get_ctx(model);
11249 if (!isl_space_has_named_params(model))
11250 isl_die(ctx, isl_error_invalid,
11251 "model has unnamed parameters", goto error);
11252 if (!isl_space_has_named_params(bmap->dim))
11253 isl_die(ctx, isl_error_invalid,
11254 "relation has unnamed parameters", goto error);
11255 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11256 isl_reordering *exp;
11257 struct isl_dim_map *dim_map;
11259 model = isl_space_drop_dims(model, isl_dim_in,
11260 0, isl_space_dim(model, isl_dim_in));
11261 model = isl_space_drop_dims(model, isl_dim_out,
11262 0, isl_space_dim(model, isl_dim_out));
11263 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11264 exp = isl_reordering_extend_space(exp,
11265 isl_basic_map_get_space(bmap));
11266 dim_map = isl_dim_map_from_reordering(exp);
11267 bmap = isl_basic_map_realign(bmap,
11268 exp ? isl_space_copy(exp->dim) : NULL,
11269 isl_dim_map_extend(dim_map, bmap));
11270 isl_reordering_free(exp);
11271 free(dim_map);
11274 isl_space_free(model);
11275 return bmap;
11276 error:
11277 isl_space_free(model);
11278 isl_basic_map_free(bmap);
11279 return NULL;
11282 /* Align the parameters of "bset" to those of "model", introducing
11283 * additional parameters if needed.
11285 __isl_give isl_basic_set *isl_basic_set_align_params(
11286 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11288 return isl_basic_map_align_params(bset, model);
11291 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11292 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11293 enum isl_dim_type c2, enum isl_dim_type c3,
11294 enum isl_dim_type c4, enum isl_dim_type c5)
11296 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11297 struct isl_mat *mat;
11298 int i, j, k;
11299 int pos;
11301 if (!bmap)
11302 return NULL;
11303 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11304 isl_basic_map_total_dim(bmap) + 1);
11305 if (!mat)
11306 return NULL;
11307 for (i = 0; i < bmap->n_eq; ++i)
11308 for (j = 0, pos = 0; j < 5; ++j) {
11309 int off = isl_basic_map_offset(bmap, c[j]);
11310 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11311 isl_int_set(mat->row[i][pos],
11312 bmap->eq[i][off + k]);
11313 ++pos;
11317 return mat;
11320 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11321 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11322 enum isl_dim_type c2, enum isl_dim_type c3,
11323 enum isl_dim_type c4, enum isl_dim_type c5)
11325 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11326 struct isl_mat *mat;
11327 int i, j, k;
11328 int pos;
11330 if (!bmap)
11331 return NULL;
11332 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11333 isl_basic_map_total_dim(bmap) + 1);
11334 if (!mat)
11335 return NULL;
11336 for (i = 0; i < bmap->n_ineq; ++i)
11337 for (j = 0, pos = 0; j < 5; ++j) {
11338 int off = isl_basic_map_offset(bmap, c[j]);
11339 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11340 isl_int_set(mat->row[i][pos],
11341 bmap->ineq[i][off + k]);
11342 ++pos;
11346 return mat;
11349 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11350 __isl_take isl_space *dim,
11351 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11352 enum isl_dim_type c2, enum isl_dim_type c3,
11353 enum isl_dim_type c4, enum isl_dim_type c5)
11355 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11356 isl_basic_map *bmap;
11357 unsigned total;
11358 unsigned extra;
11359 int i, j, k, l;
11360 int pos;
11362 if (!dim || !eq || !ineq)
11363 goto error;
11365 if (eq->n_col != ineq->n_col)
11366 isl_die(dim->ctx, isl_error_invalid,
11367 "equalities and inequalities matrices should have "
11368 "same number of columns", goto error);
11370 total = 1 + isl_space_dim(dim, isl_dim_all);
11372 if (eq->n_col < total)
11373 isl_die(dim->ctx, isl_error_invalid,
11374 "number of columns too small", goto error);
11376 extra = eq->n_col - total;
11378 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11379 eq->n_row, ineq->n_row);
11380 if (!bmap)
11381 goto error;
11382 for (i = 0; i < extra; ++i) {
11383 k = isl_basic_map_alloc_div(bmap);
11384 if (k < 0)
11385 goto error;
11386 isl_int_set_si(bmap->div[k][0], 0);
11388 for (i = 0; i < eq->n_row; ++i) {
11389 l = isl_basic_map_alloc_equality(bmap);
11390 if (l < 0)
11391 goto error;
11392 for (j = 0, pos = 0; j < 5; ++j) {
11393 int off = isl_basic_map_offset(bmap, c[j]);
11394 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11395 isl_int_set(bmap->eq[l][off + k],
11396 eq->row[i][pos]);
11397 ++pos;
11401 for (i = 0; i < ineq->n_row; ++i) {
11402 l = isl_basic_map_alloc_inequality(bmap);
11403 if (l < 0)
11404 goto error;
11405 for (j = 0, pos = 0; j < 5; ++j) {
11406 int off = isl_basic_map_offset(bmap, c[j]);
11407 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11408 isl_int_set(bmap->ineq[l][off + k],
11409 ineq->row[i][pos]);
11410 ++pos;
11415 isl_space_free(dim);
11416 isl_mat_free(eq);
11417 isl_mat_free(ineq);
11419 bmap = isl_basic_map_simplify(bmap);
11420 return isl_basic_map_finalize(bmap);
11421 error:
11422 isl_space_free(dim);
11423 isl_mat_free(eq);
11424 isl_mat_free(ineq);
11425 return NULL;
11428 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11429 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11430 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11432 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11433 c1, c2, c3, c4, isl_dim_in);
11436 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11437 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11438 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11440 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11441 c1, c2, c3, c4, isl_dim_in);
11444 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11445 __isl_take isl_space *dim,
11446 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11447 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11449 return (isl_basic_set*)
11450 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11451 c1, c2, c3, c4, isl_dim_in);
11454 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11456 if (!bmap)
11457 return -1;
11459 return isl_space_can_zip(bmap->dim);
11462 int isl_map_can_zip(__isl_keep isl_map *map)
11464 if (!map)
11465 return -1;
11467 return isl_space_can_zip(map->dim);
11470 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11471 * (A -> C) -> (B -> D).
11473 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11475 unsigned pos;
11476 unsigned n1;
11477 unsigned n2;
11479 if (!bmap)
11480 return NULL;
11482 if (!isl_basic_map_can_zip(bmap))
11483 isl_die(bmap->ctx, isl_error_invalid,
11484 "basic map cannot be zipped", goto error);
11485 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11486 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11487 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11488 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11489 bmap = isl_basic_map_cow(bmap);
11490 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11491 if (!bmap)
11492 return NULL;
11493 bmap->dim = isl_space_zip(bmap->dim);
11494 if (!bmap->dim)
11495 goto error;
11496 return bmap;
11497 error:
11498 isl_basic_map_free(bmap);
11499 return NULL;
11502 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11503 * (A -> C) -> (B -> D).
11505 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11507 int i;
11509 if (!map)
11510 return NULL;
11512 if (!isl_map_can_zip(map))
11513 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11514 goto error);
11516 map = isl_map_cow(map);
11517 if (!map)
11518 return NULL;
11520 for (i = 0; i < map->n; ++i) {
11521 map->p[i] = isl_basic_map_zip(map->p[i]);
11522 if (!map->p[i])
11523 goto error;
11526 map->dim = isl_space_zip(map->dim);
11527 if (!map->dim)
11528 goto error;
11530 return map;
11531 error:
11532 isl_map_free(map);
11533 return NULL;
11536 /* Can we apply isl_basic_map_curry to "bmap"?
11537 * That is, does it have a nested relation in its domain?
11539 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11541 if (!bmap)
11542 return -1;
11544 return isl_space_can_curry(bmap->dim);
11547 /* Can we apply isl_map_curry to "map"?
11548 * That is, does it have a nested relation in its domain?
11550 int isl_map_can_curry(__isl_keep isl_map *map)
11552 if (!map)
11553 return -1;
11555 return isl_space_can_curry(map->dim);
11558 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11559 * A -> (B -> C).
11561 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11564 if (!bmap)
11565 return NULL;
11567 if (!isl_basic_map_can_curry(bmap))
11568 isl_die(bmap->ctx, isl_error_invalid,
11569 "basic map cannot be curried", goto error);
11570 bmap = isl_basic_map_cow(bmap);
11571 if (!bmap)
11572 return NULL;
11573 bmap->dim = isl_space_curry(bmap->dim);
11574 if (!bmap->dim)
11575 goto error;
11576 return bmap;
11577 error:
11578 isl_basic_map_free(bmap);
11579 return NULL;
11582 /* Given a map (A -> B) -> C, return the corresponding map
11583 * A -> (B -> C).
11585 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11587 int i;
11589 if (!map)
11590 return NULL;
11592 if (!isl_map_can_curry(map))
11593 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11594 goto error);
11596 map = isl_map_cow(map);
11597 if (!map)
11598 return NULL;
11600 for (i = 0; i < map->n; ++i) {
11601 map->p[i] = isl_basic_map_curry(map->p[i]);
11602 if (!map->p[i])
11603 goto error;
11606 map->dim = isl_space_curry(map->dim);
11607 if (!map->dim)
11608 goto error;
11610 return map;
11611 error:
11612 isl_map_free(map);
11613 return NULL;
11616 /* Can we apply isl_basic_map_uncurry to "bmap"?
11617 * That is, does it have a nested relation in its domain?
11619 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11621 if (!bmap)
11622 return -1;
11624 return isl_space_can_uncurry(bmap->dim);
11627 /* Can we apply isl_map_uncurry to "map"?
11628 * That is, does it have a nested relation in its domain?
11630 int isl_map_can_uncurry(__isl_keep isl_map *map)
11632 if (!map)
11633 return -1;
11635 return isl_space_can_uncurry(map->dim);
11638 /* Given a basic map A -> (B -> C), return the corresponding basic map
11639 * (A -> B) -> C.
11641 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11644 if (!bmap)
11645 return NULL;
11647 if (!isl_basic_map_can_uncurry(bmap))
11648 isl_die(bmap->ctx, isl_error_invalid,
11649 "basic map cannot be uncurried",
11650 return isl_basic_map_free(bmap));
11651 bmap = isl_basic_map_cow(bmap);
11652 if (!bmap)
11653 return NULL;
11654 bmap->dim = isl_space_uncurry(bmap->dim);
11655 if (!bmap->dim)
11656 return isl_basic_map_free(bmap);
11657 return bmap;
11660 /* Given a map A -> (B -> C), return the corresponding map
11661 * (A -> B) -> C.
11663 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11665 int i;
11667 if (!map)
11668 return NULL;
11670 if (!isl_map_can_uncurry(map))
11671 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11672 return isl_map_free(map));
11674 map = isl_map_cow(map);
11675 if (!map)
11676 return NULL;
11678 for (i = 0; i < map->n; ++i) {
11679 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11680 if (!map->p[i])
11681 return isl_map_free(map);
11684 map->dim = isl_space_uncurry(map->dim);
11685 if (!map->dim)
11686 return isl_map_free(map);
11688 return map;
11691 /* Construct a basic map mapping the domain of the affine expression
11692 * to a one-dimensional range prescribed by the affine expression.
11694 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11696 int k;
11697 int pos;
11698 isl_local_space *ls;
11699 isl_basic_map *bmap;
11701 if (!aff)
11702 return NULL;
11704 ls = isl_aff_get_local_space(aff);
11705 bmap = isl_basic_map_from_local_space(ls);
11706 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11707 k = isl_basic_map_alloc_equality(bmap);
11708 if (k < 0)
11709 goto error;
11711 pos = isl_basic_map_offset(bmap, isl_dim_out);
11712 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11713 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11714 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11715 aff->v->size - (pos + 1));
11717 isl_aff_free(aff);
11718 bmap = isl_basic_map_finalize(bmap);
11719 return bmap;
11720 error:
11721 isl_aff_free(aff);
11722 isl_basic_map_free(bmap);
11723 return NULL;
11726 /* Construct a map mapping the domain of the affine expression
11727 * to a one-dimensional range prescribed by the affine expression.
11729 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11731 isl_basic_map *bmap;
11733 bmap = isl_basic_map_from_aff(aff);
11734 return isl_map_from_basic_map(bmap);
11737 /* Construct a basic map mapping the domain the multi-affine expression
11738 * to its range, with each dimension in the range equated to the
11739 * corresponding affine expression.
11741 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11742 __isl_take isl_multi_aff *maff)
11744 int i;
11745 isl_space *space;
11746 isl_basic_map *bmap;
11748 if (!maff)
11749 return NULL;
11751 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11752 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11753 "invalid space", goto error);
11755 space = isl_space_domain(isl_multi_aff_get_space(maff));
11756 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11758 for (i = 0; i < maff->n; ++i) {
11759 isl_aff *aff;
11760 isl_basic_map *bmap_i;
11762 aff = isl_aff_copy(maff->p[i]);
11763 bmap_i = isl_basic_map_from_aff(aff);
11765 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11768 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11770 isl_multi_aff_free(maff);
11771 return bmap;
11772 error:
11773 isl_multi_aff_free(maff);
11774 return NULL;
11777 /* Construct a map mapping the domain the multi-affine expression
11778 * to its range, with each dimension in the range equated to the
11779 * corresponding affine expression.
11781 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11783 isl_basic_map *bmap;
11785 bmap = isl_basic_map_from_multi_aff(maff);
11786 return isl_map_from_basic_map(bmap);
11789 /* Construct a basic map mapping a domain in the given space to
11790 * to an n-dimensional range, with n the number of elements in the list,
11791 * where each coordinate in the range is prescribed by the
11792 * corresponding affine expression.
11793 * The domains of all affine expressions in the list are assumed to match
11794 * domain_dim.
11796 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11797 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11799 int i;
11800 isl_space *dim;
11801 isl_basic_map *bmap;
11803 if (!list)
11804 return NULL;
11806 dim = isl_space_from_domain(domain_dim);
11807 bmap = isl_basic_map_universe(dim);
11809 for (i = 0; i < list->n; ++i) {
11810 isl_aff *aff;
11811 isl_basic_map *bmap_i;
11813 aff = isl_aff_copy(list->p[i]);
11814 bmap_i = isl_basic_map_from_aff(aff);
11816 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11819 isl_aff_list_free(list);
11820 return bmap;
11823 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11824 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11826 return isl_map_equate(set, type1, pos1, type2, pos2);
11829 /* Construct a basic map where the given dimensions are equal to each other.
11831 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11832 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11834 isl_basic_map *bmap = NULL;
11835 int i;
11837 if (!space)
11838 return NULL;
11840 if (pos1 >= isl_space_dim(space, type1))
11841 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11842 "index out of bounds", goto error);
11843 if (pos2 >= isl_space_dim(space, type2))
11844 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11845 "index out of bounds", goto error);
11847 if (type1 == type2 && pos1 == pos2)
11848 return isl_basic_map_universe(space);
11850 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11851 i = isl_basic_map_alloc_equality(bmap);
11852 if (i < 0)
11853 goto error;
11854 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11855 pos1 += isl_basic_map_offset(bmap, type1);
11856 pos2 += isl_basic_map_offset(bmap, type2);
11857 isl_int_set_si(bmap->eq[i][pos1], -1);
11858 isl_int_set_si(bmap->eq[i][pos2], 1);
11859 bmap = isl_basic_map_finalize(bmap);
11860 isl_space_free(space);
11861 return bmap;
11862 error:
11863 isl_space_free(space);
11864 isl_basic_map_free(bmap);
11865 return NULL;
11868 /* Add a constraint imposing that the given two dimensions are equal.
11870 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11871 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11873 isl_basic_map *eq;
11875 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11877 bmap = isl_basic_map_intersect(bmap, eq);
11879 return bmap;
11882 /* Add a constraint imposing that the given two dimensions are equal.
11884 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11885 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11887 isl_basic_map *bmap;
11889 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11891 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11893 return map;
11896 /* Add a constraint imposing that the given two dimensions have opposite values.
11898 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11899 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11901 isl_basic_map *bmap = NULL;
11902 int i;
11904 if (!map)
11905 return NULL;
11907 if (pos1 >= isl_map_dim(map, type1))
11908 isl_die(map->ctx, isl_error_invalid,
11909 "index out of bounds", goto error);
11910 if (pos2 >= isl_map_dim(map, type2))
11911 isl_die(map->ctx, isl_error_invalid,
11912 "index out of bounds", goto error);
11914 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11915 i = isl_basic_map_alloc_equality(bmap);
11916 if (i < 0)
11917 goto error;
11918 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11919 pos1 += isl_basic_map_offset(bmap, type1);
11920 pos2 += isl_basic_map_offset(bmap, type2);
11921 isl_int_set_si(bmap->eq[i][pos1], 1);
11922 isl_int_set_si(bmap->eq[i][pos2], 1);
11923 bmap = isl_basic_map_finalize(bmap);
11925 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11927 return map;
11928 error:
11929 isl_basic_map_free(bmap);
11930 isl_map_free(map);
11931 return NULL;
11934 /* Construct a constraint imposing that the value of the first dimension is
11935 * greater than or equal to that of the second.
11937 static __isl_give isl_constraint *constraint_order_ge(
11938 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11939 enum isl_dim_type type2, int pos2)
11941 isl_constraint *c;
11943 if (!space)
11944 return NULL;
11946 c = isl_inequality_alloc(isl_local_space_from_space(space));
11948 if (pos1 >= isl_constraint_dim(c, type1))
11949 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11950 "index out of bounds", return isl_constraint_free(c));
11951 if (pos2 >= isl_constraint_dim(c, type2))
11952 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11953 "index out of bounds", return isl_constraint_free(c));
11955 if (type1 == type2 && pos1 == pos2)
11956 return c;
11958 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11959 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11961 return c;
11964 /* Add a constraint imposing that the value of the first dimension is
11965 * greater than or equal to that of the second.
11967 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11968 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11970 isl_constraint *c;
11971 isl_space *space;
11973 if (type1 == type2 && pos1 == pos2)
11974 return bmap;
11975 space = isl_basic_map_get_space(bmap);
11976 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11977 bmap = isl_basic_map_add_constraint(bmap, c);
11979 return bmap;
11982 /* Add a constraint imposing that the value of the first dimension is
11983 * greater than or equal to that of the second.
11985 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11986 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11988 isl_constraint *c;
11989 isl_space *space;
11991 if (type1 == type2 && pos1 == pos2)
11992 return map;
11993 space = isl_map_get_space(map);
11994 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11995 map = isl_map_add_constraint(map, c);
11997 return map;
12000 /* Add a constraint imposing that the value of the first dimension is
12001 * less than or equal to that of the second.
12003 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12004 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12006 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12009 /* Construct a basic map where the value of the first dimension is
12010 * greater than that of the second.
12012 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12013 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12015 isl_basic_map *bmap = NULL;
12016 int i;
12018 if (!space)
12019 return NULL;
12021 if (pos1 >= isl_space_dim(space, type1))
12022 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12023 "index out of bounds", goto error);
12024 if (pos2 >= isl_space_dim(space, type2))
12025 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12026 "index out of bounds", goto error);
12028 if (type1 == type2 && pos1 == pos2)
12029 return isl_basic_map_empty(space);
12031 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12032 i = isl_basic_map_alloc_inequality(bmap);
12033 if (i < 0)
12034 return isl_basic_map_free(bmap);
12035 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12036 pos1 += isl_basic_map_offset(bmap, type1);
12037 pos2 += isl_basic_map_offset(bmap, type2);
12038 isl_int_set_si(bmap->ineq[i][pos1], 1);
12039 isl_int_set_si(bmap->ineq[i][pos2], -1);
12040 isl_int_set_si(bmap->ineq[i][0], -1);
12041 bmap = isl_basic_map_finalize(bmap);
12043 return bmap;
12044 error:
12045 isl_space_free(space);
12046 isl_basic_map_free(bmap);
12047 return NULL;
12050 /* Add a constraint imposing that the value of the first dimension is
12051 * greater than that of the second.
12053 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12054 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12056 isl_basic_map *gt;
12058 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12060 bmap = isl_basic_map_intersect(bmap, gt);
12062 return bmap;
12065 /* Add a constraint imposing that the value of the first dimension is
12066 * greater than that of the second.
12068 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12069 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12071 isl_basic_map *bmap;
12073 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12075 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12077 return map;
12080 /* Add a constraint imposing that the value of the first dimension is
12081 * smaller than that of the second.
12083 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12084 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12086 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12089 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12090 int pos)
12092 isl_aff *div;
12093 isl_local_space *ls;
12095 if (!bmap)
12096 return NULL;
12098 if (!isl_basic_map_divs_known(bmap))
12099 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12100 "some divs are unknown", return NULL);
12102 ls = isl_basic_map_get_local_space(bmap);
12103 div = isl_local_space_get_div(ls, pos);
12104 isl_local_space_free(ls);
12106 return div;
12109 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12110 int pos)
12112 return isl_basic_map_get_div(bset, pos);
12115 /* Plug in "subs" for dimension "type", "pos" of "bset".
12117 * Let i be the dimension to replace and let "subs" be of the form
12119 * f/d
12121 * Any integer division with a non-zero coefficient for i,
12123 * floor((a i + g)/m)
12125 * is replaced by
12127 * floor((a f + d g)/(m d))
12129 * Constraints of the form
12131 * a i + g
12133 * are replaced by
12135 * a f + d g
12137 * We currently require that "subs" is an integral expression.
12138 * Handling rational expressions may require us to add stride constraints
12139 * as we do in isl_basic_set_preimage_multi_aff.
12141 __isl_give isl_basic_set *isl_basic_set_substitute(
12142 __isl_take isl_basic_set *bset,
12143 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12145 int i;
12146 isl_int v;
12147 isl_ctx *ctx;
12149 if (bset && isl_basic_set_plain_is_empty(bset))
12150 return bset;
12152 bset = isl_basic_set_cow(bset);
12153 if (!bset || !subs)
12154 goto error;
12156 ctx = isl_basic_set_get_ctx(bset);
12157 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12158 isl_die(ctx, isl_error_invalid,
12159 "spaces don't match", goto error);
12160 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12161 isl_die(ctx, isl_error_unsupported,
12162 "cannot handle divs yet", goto error);
12163 if (!isl_int_is_one(subs->v->el[0]))
12164 isl_die(ctx, isl_error_invalid,
12165 "can only substitute integer expressions", goto error);
12167 pos += isl_basic_set_offset(bset, type);
12169 isl_int_init(v);
12171 for (i = 0; i < bset->n_eq; ++i) {
12172 if (isl_int_is_zero(bset->eq[i][pos]))
12173 continue;
12174 isl_int_set(v, bset->eq[i][pos]);
12175 isl_int_set_si(bset->eq[i][pos], 0);
12176 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12177 v, subs->v->el + 1, subs->v->size - 1);
12180 for (i = 0; i < bset->n_ineq; ++i) {
12181 if (isl_int_is_zero(bset->ineq[i][pos]))
12182 continue;
12183 isl_int_set(v, bset->ineq[i][pos]);
12184 isl_int_set_si(bset->ineq[i][pos], 0);
12185 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12186 v, subs->v->el + 1, subs->v->size - 1);
12189 for (i = 0; i < bset->n_div; ++i) {
12190 if (isl_int_is_zero(bset->div[i][1 + pos]))
12191 continue;
12192 isl_int_set(v, bset->div[i][1 + pos]);
12193 isl_int_set_si(bset->div[i][1 + pos], 0);
12194 isl_seq_combine(bset->div[i] + 1,
12195 subs->v->el[0], bset->div[i] + 1,
12196 v, subs->v->el + 1, subs->v->size - 1);
12197 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12200 isl_int_clear(v);
12202 bset = isl_basic_set_simplify(bset);
12203 return isl_basic_set_finalize(bset);
12204 error:
12205 isl_basic_set_free(bset);
12206 return NULL;
12209 /* Plug in "subs" for dimension "type", "pos" of "set".
12211 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12212 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12214 int i;
12216 if (set && isl_set_plain_is_empty(set))
12217 return set;
12219 set = isl_set_cow(set);
12220 if (!set || !subs)
12221 goto error;
12223 for (i = set->n - 1; i >= 0; --i) {
12224 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12225 if (remove_if_empty(set, i) < 0)
12226 goto error;
12229 return set;
12230 error:
12231 isl_set_free(set);
12232 return NULL;
12235 /* Check if the range of "ma" is compatible with the domain or range
12236 * (depending on "type") of "bmap".
12237 * Return -1 if anything is wrong.
12239 static int check_basic_map_compatible_range_multi_aff(
12240 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12241 __isl_keep isl_multi_aff *ma)
12243 int m;
12244 isl_space *ma_space;
12246 ma_space = isl_multi_aff_get_space(ma);
12248 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12249 if (m < 0)
12250 goto error;
12251 if (!m)
12252 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12253 "parameters don't match", goto error);
12254 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12255 if (m < 0)
12256 goto error;
12257 if (!m)
12258 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12259 "spaces don't match", goto error);
12261 isl_space_free(ma_space);
12262 return m;
12263 error:
12264 isl_space_free(ma_space);
12265 return -1;
12268 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12269 * coefficients before the transformed range of dimensions,
12270 * the "n_after" coefficients after the transformed range of dimensions
12271 * and the coefficients of the other divs in "bmap".
12273 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12274 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12276 int i;
12277 int n_param;
12278 int n_set;
12279 isl_local_space *ls;
12281 if (n_div == 0)
12282 return 0;
12284 ls = isl_aff_get_domain_local_space(ma->p[0]);
12285 if (!ls)
12286 return -1;
12288 n_param = isl_local_space_dim(ls, isl_dim_param);
12289 n_set = isl_local_space_dim(ls, isl_dim_set);
12290 for (i = 0; i < n_div; ++i) {
12291 int o_bmap = 0, o_ls = 0;
12293 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12294 o_bmap += 1 + 1 + n_param;
12295 o_ls += 1 + 1 + n_param;
12296 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12297 o_bmap += n_before;
12298 isl_seq_cpy(bmap->div[i] + o_bmap,
12299 ls->div->row[i] + o_ls, n_set);
12300 o_bmap += n_set;
12301 o_ls += n_set;
12302 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12303 o_bmap += n_after;
12304 isl_seq_cpy(bmap->div[i] + o_bmap,
12305 ls->div->row[i] + o_ls, n_div);
12306 o_bmap += n_div;
12307 o_ls += n_div;
12308 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12309 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12310 goto error;
12313 isl_local_space_free(ls);
12314 return 0;
12315 error:
12316 isl_local_space_free(ls);
12317 return -1;
12320 /* How many stride constraints does "ma" enforce?
12321 * That is, how many of the affine expressions have a denominator
12322 * different from one?
12324 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12326 int i;
12327 int strides = 0;
12329 for (i = 0; i < ma->n; ++i)
12330 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12331 strides++;
12333 return strides;
12336 /* For each affine expression in ma of the form
12338 * x_i = (f_i y + h_i)/m_i
12340 * with m_i different from one, add a constraint to "bmap"
12341 * of the form
12343 * f_i y + h_i = m_i alpha_i
12345 * with alpha_i an additional existentially quantified variable.
12347 static __isl_give isl_basic_map *add_ma_strides(
12348 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12349 int n_before, int n_after)
12351 int i, k;
12352 int div;
12353 int total;
12354 int n_param;
12355 int n_in;
12356 int n_div;
12358 total = isl_basic_map_total_dim(bmap);
12359 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12360 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12361 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12362 for (i = 0; i < ma->n; ++i) {
12363 int o_bmap = 0, o_ma = 1;
12365 if (isl_int_is_one(ma->p[i]->v->el[0]))
12366 continue;
12367 div = isl_basic_map_alloc_div(bmap);
12368 k = isl_basic_map_alloc_equality(bmap);
12369 if (div < 0 || k < 0)
12370 goto error;
12371 isl_int_set_si(bmap->div[div][0], 0);
12372 isl_seq_cpy(bmap->eq[k] + o_bmap,
12373 ma->p[i]->v->el + o_ma, 1 + n_param);
12374 o_bmap += 1 + n_param;
12375 o_ma += 1 + n_param;
12376 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12377 o_bmap += n_before;
12378 isl_seq_cpy(bmap->eq[k] + o_bmap,
12379 ma->p[i]->v->el + o_ma, n_in);
12380 o_bmap += n_in;
12381 o_ma += n_in;
12382 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12383 o_bmap += n_after;
12384 isl_seq_cpy(bmap->eq[k] + o_bmap,
12385 ma->p[i]->v->el + o_ma, n_div);
12386 o_bmap += n_div;
12387 o_ma += n_div;
12388 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12389 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12390 total++;
12393 return bmap;
12394 error:
12395 isl_basic_map_free(bmap);
12396 return NULL;
12399 /* Replace the domain or range space (depending on "type) of "space" by "set".
12401 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12402 enum isl_dim_type type, __isl_take isl_space *set)
12404 if (type == isl_dim_in) {
12405 space = isl_space_range(space);
12406 space = isl_space_map_from_domain_and_range(set, space);
12407 } else {
12408 space = isl_space_domain(space);
12409 space = isl_space_map_from_domain_and_range(space, set);
12412 return space;
12415 /* Compute the preimage of the domain or range (depending on "type")
12416 * of "bmap" under the function represented by "ma".
12417 * In other words, plug in "ma" in the domain or range of "bmap".
12418 * The result is a basic map that lives in the same space as "bmap"
12419 * except that the domain or range has been replaced by
12420 * the domain space of "ma".
12422 * If bmap is represented by
12424 * A(p) + S u + B x + T v + C(divs) >= 0,
12426 * where u and x are input and output dimensions if type == isl_dim_out
12427 * while x and v are input and output dimensions if type == isl_dim_in,
12428 * and ma is represented by
12430 * x = D(p) + F(y) + G(divs')
12432 * then the result is
12434 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12436 * The divs in the input set are similarly adjusted.
12437 * In particular
12439 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12441 * becomes
12443 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12444 * B_i G(divs') + c_i(divs))/n_i)
12446 * If bmap is not a rational map and if F(y) involves any denominators
12448 * x_i = (f_i y + h_i)/m_i
12450 * then additional constraints are added to ensure that we only
12451 * map back integer points. That is we enforce
12453 * f_i y + h_i = m_i alpha_i
12455 * with alpha_i an additional existentially quantified variable.
12457 * We first copy over the divs from "ma".
12458 * Then we add the modified constraints and divs from "bmap".
12459 * Finally, we add the stride constraints, if needed.
12461 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12462 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12463 __isl_take isl_multi_aff *ma)
12465 int i, k;
12466 isl_space *space;
12467 isl_basic_map *res = NULL;
12468 int n_before, n_after, n_div_bmap, n_div_ma;
12469 isl_int f, c1, c2, g;
12470 int rational, strides;
12472 isl_int_init(f);
12473 isl_int_init(c1);
12474 isl_int_init(c2);
12475 isl_int_init(g);
12477 ma = isl_multi_aff_align_divs(ma);
12478 if (!bmap || !ma)
12479 goto error;
12480 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12481 goto error;
12483 if (type == isl_dim_in) {
12484 n_before = 0;
12485 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12486 } else {
12487 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12488 n_after = 0;
12490 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12491 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12493 space = isl_multi_aff_get_domain_space(ma);
12494 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12495 rational = isl_basic_map_is_rational(bmap);
12496 strides = rational ? 0 : multi_aff_strides(ma);
12497 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12498 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12499 if (rational)
12500 res = isl_basic_map_set_rational(res);
12502 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12503 if (isl_basic_map_alloc_div(res) < 0)
12504 goto error;
12506 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12507 goto error;
12509 for (i = 0; i < bmap->n_eq; ++i) {
12510 k = isl_basic_map_alloc_equality(res);
12511 if (k < 0)
12512 goto error;
12513 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12514 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12517 for (i = 0; i < bmap->n_ineq; ++i) {
12518 k = isl_basic_map_alloc_inequality(res);
12519 if (k < 0)
12520 goto error;
12521 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12522 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12525 for (i = 0; i < bmap->n_div; ++i) {
12526 if (isl_int_is_zero(bmap->div[i][0])) {
12527 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12528 continue;
12530 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12531 n_before, n_after, n_div_ma, n_div_bmap,
12532 f, c1, c2, g, 1);
12535 if (strides)
12536 res = add_ma_strides(res, ma, n_before, n_after);
12538 isl_int_clear(f);
12539 isl_int_clear(c1);
12540 isl_int_clear(c2);
12541 isl_int_clear(g);
12542 isl_basic_map_free(bmap);
12543 isl_multi_aff_free(ma);
12544 res = isl_basic_set_simplify(res);
12545 return isl_basic_map_finalize(res);
12546 error:
12547 isl_int_clear(f);
12548 isl_int_clear(c1);
12549 isl_int_clear(c2);
12550 isl_int_clear(g);
12551 isl_basic_map_free(bmap);
12552 isl_multi_aff_free(ma);
12553 isl_basic_map_free(res);
12554 return NULL;
12557 /* Compute the preimage of "bset" under the function represented by "ma".
12558 * In other words, plug in "ma" in "bset". The result is a basic set
12559 * that lives in the domain space of "ma".
12561 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12562 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12564 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12567 /* Compute the preimage of the domain of "bmap" under the function
12568 * represented by "ma".
12569 * In other words, plug in "ma" in the domain of "bmap".
12570 * The result is a basic map that lives in the same space as "bmap"
12571 * except that the domain has been replaced by the domain space of "ma".
12573 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12574 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12576 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12579 /* Compute the preimage of the range of "bmap" under the function
12580 * represented by "ma".
12581 * In other words, plug in "ma" in the range of "bmap".
12582 * The result is a basic map that lives in the same space as "bmap"
12583 * except that the range has been replaced by the domain space of "ma".
12585 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12586 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12588 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12591 /* Check if the range of "ma" is compatible with the domain or range
12592 * (depending on "type") of "map".
12593 * Return -1 if anything is wrong.
12595 static int check_map_compatible_range_multi_aff(
12596 __isl_keep isl_map *map, enum isl_dim_type type,
12597 __isl_keep isl_multi_aff *ma)
12599 int m;
12600 isl_space *ma_space;
12602 ma_space = isl_multi_aff_get_space(ma);
12603 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12604 isl_space_free(ma_space);
12605 if (m >= 0 && !m)
12606 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12607 "spaces don't match", return -1);
12608 return m;
12611 /* Compute the preimage of the domain or range (depending on "type")
12612 * of "map" under the function represented by "ma".
12613 * In other words, plug in "ma" in the domain or range of "map".
12614 * The result is a map that lives in the same space as "map"
12615 * except that the domain or range has been replaced by
12616 * the domain space of "ma".
12618 * The parameters are assumed to have been aligned.
12620 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12621 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12623 int i;
12624 isl_space *space;
12626 map = isl_map_cow(map);
12627 ma = isl_multi_aff_align_divs(ma);
12628 if (!map || !ma)
12629 goto error;
12630 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12631 goto error;
12633 for (i = 0; i < map->n; ++i) {
12634 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12635 isl_multi_aff_copy(ma));
12636 if (!map->p[i])
12637 goto error;
12640 space = isl_multi_aff_get_domain_space(ma);
12641 space = isl_space_set(isl_map_get_space(map), type, space);
12643 isl_space_free(map->dim);
12644 map->dim = space;
12645 if (!map->dim)
12646 goto error;
12648 isl_multi_aff_free(ma);
12649 if (map->n > 1)
12650 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12651 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12652 return map;
12653 error:
12654 isl_multi_aff_free(ma);
12655 isl_map_free(map);
12656 return NULL;
12659 /* Compute the preimage of the domain or range (depending on "type")
12660 * of "map" under the function represented by "ma".
12661 * In other words, plug in "ma" in the domain or range of "map".
12662 * The result is a map that lives in the same space as "map"
12663 * except that the domain or range has been replaced by
12664 * the domain space of "ma".
12666 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12667 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12669 if (!map || !ma)
12670 goto error;
12672 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12673 return map_preimage_multi_aff(map, type, ma);
12675 if (!isl_space_has_named_params(map->dim) ||
12676 !isl_space_has_named_params(ma->space))
12677 isl_die(map->ctx, isl_error_invalid,
12678 "unaligned unnamed parameters", goto error);
12679 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12680 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12682 return map_preimage_multi_aff(map, type, ma);
12683 error:
12684 isl_multi_aff_free(ma);
12685 return isl_map_free(map);
12688 /* Compute the preimage of "set" under the function represented by "ma".
12689 * In other words, plug in "ma" in "set". The result is a set
12690 * that lives in the domain space of "ma".
12692 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12693 __isl_take isl_multi_aff *ma)
12695 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12698 /* Compute the preimage of the domain of "map" under the function
12699 * represented by "ma".
12700 * In other words, plug in "ma" in the domain of "map".
12701 * The result is a map that lives in the same space as "map"
12702 * except that the domain has been replaced by the domain space of "ma".
12704 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12705 __isl_take isl_multi_aff *ma)
12707 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12710 /* Compute the preimage of the range of "map" under the function
12711 * represented by "ma".
12712 * In other words, plug in "ma" in the range of "map".
12713 * The result is a map that lives in the same space as "map"
12714 * except that the range has been replaced by the domain space of "ma".
12716 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12717 __isl_take isl_multi_aff *ma)
12719 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12722 /* Compute the preimage of "map" under the function represented by "pma".
12723 * In other words, plug in "pma" in the domain or range of "map".
12724 * The result is a map that lives in the same space as "map",
12725 * except that the space of type "type" has been replaced by
12726 * the domain space of "pma".
12728 * The parameters of "map" and "pma" are assumed to have been aligned.
12730 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12731 __isl_take isl_map *map, enum isl_dim_type type,
12732 __isl_take isl_pw_multi_aff *pma)
12734 int i;
12735 isl_map *res;
12737 if (!pma)
12738 goto error;
12740 if (pma->n == 0) {
12741 isl_pw_multi_aff_free(pma);
12742 res = isl_map_empty(isl_map_get_space(map));
12743 isl_map_free(map);
12744 return res;
12747 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12748 isl_multi_aff_copy(pma->p[0].maff));
12749 if (type == isl_dim_in)
12750 res = isl_map_intersect_domain(res,
12751 isl_map_copy(pma->p[0].set));
12752 else
12753 res = isl_map_intersect_range(res,
12754 isl_map_copy(pma->p[0].set));
12756 for (i = 1; i < pma->n; ++i) {
12757 isl_map *res_i;
12759 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12760 isl_multi_aff_copy(pma->p[i].maff));
12761 if (type == isl_dim_in)
12762 res_i = isl_map_intersect_domain(res_i,
12763 isl_map_copy(pma->p[i].set));
12764 else
12765 res_i = isl_map_intersect_range(res_i,
12766 isl_map_copy(pma->p[i].set));
12767 res = isl_map_union(res, res_i);
12770 isl_pw_multi_aff_free(pma);
12771 isl_map_free(map);
12772 return res;
12773 error:
12774 isl_pw_multi_aff_free(pma);
12775 isl_map_free(map);
12776 return NULL;
12779 /* Compute the preimage of "map" under the function represented by "pma".
12780 * In other words, plug in "pma" in the domain or range of "map".
12781 * The result is a map that lives in the same space as "map",
12782 * except that the space of type "type" has been replaced by
12783 * the domain space of "pma".
12785 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12786 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12788 if (!map || !pma)
12789 goto error;
12791 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12792 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12794 if (!isl_space_has_named_params(map->dim) ||
12795 !isl_space_has_named_params(pma->dim))
12796 isl_die(map->ctx, isl_error_invalid,
12797 "unaligned unnamed parameters", goto error);
12798 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12799 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12801 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12802 error:
12803 isl_pw_multi_aff_free(pma);
12804 return isl_map_free(map);
12807 /* Compute the preimage of "set" under the function represented by "pma".
12808 * In other words, plug in "pma" in "set". The result is a set
12809 * that lives in the domain space of "pma".
12811 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12812 __isl_take isl_pw_multi_aff *pma)
12814 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12817 /* Compute the preimage of the domain of "map" under the function
12818 * represented by "pma".
12819 * In other words, plug in "pma" in the domain of "map".
12820 * The result is a map that lives in the same space as "map",
12821 * except that domain space has been replaced by the domain space of "pma".
12823 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12824 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12826 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12829 /* Compute the preimage of the range of "map" under the function
12830 * represented by "pma".
12831 * In other words, plug in "pma" in the range of "map".
12832 * The result is a map that lives in the same space as "map",
12833 * except that range space has been replaced by the domain space of "pma".
12835 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12836 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12838 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12841 /* Compute the preimage of "map" under the function represented by "mpa".
12842 * In other words, plug in "mpa" in the domain or range of "map".
12843 * The result is a map that lives in the same space as "map",
12844 * except that the space of type "type" has been replaced by
12845 * the domain space of "mpa".
12847 * If the map does not involve any constraints that refer to the
12848 * dimensions of the substituted space, then the only possible
12849 * effect of "mpa" on the map is to map the space to a different space.
12850 * We create a separate isl_multi_aff to effectuate this change
12851 * in order to avoid spurious splitting of the map along the pieces
12852 * of "mpa".
12854 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12855 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12857 int n;
12858 isl_pw_multi_aff *pma;
12860 if (!map || !mpa)
12861 goto error;
12863 n = isl_map_dim(map, type);
12864 if (!isl_map_involves_dims(map, type, 0, n)) {
12865 isl_space *space;
12866 isl_multi_aff *ma;
12868 space = isl_multi_pw_aff_get_space(mpa);
12869 isl_multi_pw_aff_free(mpa);
12870 ma = isl_multi_aff_zero(space);
12871 return isl_map_preimage_multi_aff(map, type, ma);
12874 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12875 return isl_map_preimage_pw_multi_aff(map, type, pma);
12876 error:
12877 isl_map_free(map);
12878 isl_multi_pw_aff_free(mpa);
12879 return NULL;
12882 /* Compute the preimage of "map" under the function represented by "mpa".
12883 * In other words, plug in "mpa" in the domain "map".
12884 * The result is a map that lives in the same space as "map",
12885 * except that domain space has been replaced by the domain space of "mpa".
12887 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12888 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12890 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12893 /* Compute the preimage of "set" by the function represented by "mpa".
12894 * In other words, plug in "mpa" in "set".
12896 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12897 __isl_take isl_multi_pw_aff *mpa)
12899 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);