isl_basic_map_sort_constraints: skip sorting if constraints are sorted already
[isl.git] / isl_map.c
blob4c8d350251b995058f5d931ba95fda7da751935c
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_tab.h"
31 #include <isl/vec.h>
32 #include <isl_mat_private.h>
33 #include <isl_vec_private.h>
34 #include <isl_dim_map.h>
35 #include <isl_local_space_private.h>
36 #include <isl_aff_private.h>
37 #include <isl_options_private.h>
38 #include <isl_morph.h>
39 #include <isl_val_private.h>
40 #include <isl/deprecated/map_int.h>
41 #include <isl/deprecated/set_int.h>
43 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
45 switch (type) {
46 case isl_dim_param: return dim->nparam;
47 case isl_dim_in: return dim->n_in;
48 case isl_dim_out: return dim->n_out;
49 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
50 default: return 0;
54 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
56 switch (type) {
57 case isl_dim_param: return 1;
58 case isl_dim_in: return 1 + dim->nparam;
59 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
60 default: return 0;
64 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
65 enum isl_dim_type type)
67 if (!bmap)
68 return 0;
69 switch (type) {
70 case isl_dim_cst: return 1;
71 case isl_dim_param:
72 case isl_dim_in:
73 case isl_dim_out: return isl_space_dim(bmap->dim, type);
74 case isl_dim_div: return bmap->n_div;
75 case isl_dim_all: return isl_basic_map_total_dim(bmap);
76 default: return 0;
80 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
82 return map ? n(map->dim, type) : 0;
85 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
87 return set ? n(set->dim, type) : 0;
90 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
91 enum isl_dim_type type)
93 isl_space *dim = bmap->dim;
94 switch (type) {
95 case isl_dim_cst: return 0;
96 case isl_dim_param: return 1;
97 case isl_dim_in: return 1 + dim->nparam;
98 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
99 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
100 default: return 0;
104 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
105 enum isl_dim_type type)
107 return isl_basic_map_offset(bset, type);
110 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
112 return pos(map->dim, type);
115 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
116 enum isl_dim_type type)
118 return isl_basic_map_dim(bset, type);
121 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
123 return isl_basic_set_dim(bset, isl_dim_set);
126 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
128 return isl_basic_set_dim(bset, isl_dim_param);
131 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
133 if (!bset)
134 return 0;
135 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
138 unsigned isl_set_n_dim(__isl_keep isl_set *set)
140 return isl_set_dim(set, isl_dim_set);
143 unsigned isl_set_n_param(__isl_keep isl_set *set)
145 return isl_set_dim(set, isl_dim_param);
148 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
150 return bmap ? bmap->dim->n_in : 0;
153 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
155 return bmap ? bmap->dim->n_out : 0;
158 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
160 return bmap ? bmap->dim->nparam : 0;
163 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
165 return bmap ? bmap->n_div : 0;
168 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
170 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
173 unsigned isl_map_n_in(const struct isl_map *map)
175 return map ? map->dim->n_in : 0;
178 unsigned isl_map_n_out(const struct isl_map *map)
180 return map ? map->dim->n_out : 0;
183 unsigned isl_map_n_param(const struct isl_map *map)
185 return map ? map->dim->nparam : 0;
188 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
190 int m;
191 if (!map || !set)
192 return -1;
193 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
194 if (m < 0 || !m)
195 return m;
196 return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
199 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
200 struct isl_basic_set *bset)
202 int m;
203 if (!bmap || !bset)
204 return -1;
205 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
206 if (m < 0 || !m)
207 return m;
208 return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
211 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
213 int m;
214 if (!map || !set)
215 return -1;
216 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
217 if (m < 0 || !m)
218 return m;
219 return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
222 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
223 struct isl_basic_set *bset)
225 int m;
226 if (!bmap || !bset)
227 return -1;
228 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
229 if (m < 0 || !m)
230 return m;
231 return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
234 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
236 return bmap ? bmap->ctx : NULL;
239 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
241 return bset ? bset->ctx : NULL;
244 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
246 return map ? map->ctx : NULL;
249 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
251 return set ? set->ctx : NULL;
254 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
256 if (!bmap)
257 return NULL;
258 return isl_space_copy(bmap->dim);
261 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
263 if (!bset)
264 return NULL;
265 return isl_space_copy(bset->dim);
268 /* Extract the divs in "bmap" as a matrix.
270 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
272 int i;
273 isl_ctx *ctx;
274 isl_mat *div;
275 unsigned total;
276 unsigned cols;
278 if (!bmap)
279 return NULL;
281 ctx = isl_basic_map_get_ctx(bmap);
282 total = isl_space_dim(bmap->dim, isl_dim_all);
283 cols = 1 + 1 + total + bmap->n_div;
284 div = isl_mat_alloc(ctx, bmap->n_div, cols);
285 if (!div)
286 return NULL;
288 for (i = 0; i < bmap->n_div; ++i)
289 isl_seq_cpy(div->row[i], bmap->div[i], cols);
291 return div;
294 /* Extract the divs in "bset" as a matrix.
296 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
298 return isl_basic_map_get_divs(bset);
301 __isl_give isl_local_space *isl_basic_map_get_local_space(
302 __isl_keep isl_basic_map *bmap)
304 isl_mat *div;
306 if (!bmap)
307 return NULL;
309 div = isl_basic_map_get_divs(bmap);
310 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
313 __isl_give isl_local_space *isl_basic_set_get_local_space(
314 __isl_keep isl_basic_set *bset)
316 return isl_basic_map_get_local_space(bset);
319 __isl_give isl_basic_map *isl_basic_map_from_local_space(
320 __isl_take isl_local_space *ls)
322 int i;
323 int n_div;
324 isl_basic_map *bmap;
326 if (!ls)
327 return NULL;
329 n_div = isl_local_space_dim(ls, isl_dim_div);
330 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
331 n_div, 0, 2 * n_div);
333 for (i = 0; i < n_div; ++i)
334 if (isl_basic_map_alloc_div(bmap) < 0)
335 goto error;
337 for (i = 0; i < n_div; ++i) {
338 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
339 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
340 goto error;
343 isl_local_space_free(ls);
344 return bmap;
345 error:
346 isl_local_space_free(ls);
347 isl_basic_map_free(bmap);
348 return NULL;
351 __isl_give isl_basic_set *isl_basic_set_from_local_space(
352 __isl_take isl_local_space *ls)
354 return isl_basic_map_from_local_space(ls);
357 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
359 if (!map)
360 return NULL;
361 return isl_space_copy(map->dim);
364 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
366 if (!set)
367 return NULL;
368 return isl_space_copy(set->dim);
371 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
372 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
374 bmap = isl_basic_map_cow(bmap);
375 if (!bmap)
376 return NULL;
377 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
378 if (!bmap->dim)
379 goto error;
380 bmap = isl_basic_map_finalize(bmap);
381 return bmap;
382 error:
383 isl_basic_map_free(bmap);
384 return NULL;
387 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
388 __isl_take isl_basic_set *bset, const char *s)
390 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
393 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
394 enum isl_dim_type type)
396 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
399 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
400 enum isl_dim_type type, const char *s)
402 int i;
404 map = isl_map_cow(map);
405 if (!map)
406 return NULL;
408 map->dim = isl_space_set_tuple_name(map->dim, type, s);
409 if (!map->dim)
410 goto error;
412 for (i = 0; i < map->n; ++i) {
413 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
414 if (!map->p[i])
415 goto error;
418 return map;
419 error:
420 isl_map_free(map);
421 return NULL;
424 /* Replace the identifier of the tuple of type "type" by "id".
426 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
427 __isl_take isl_basic_map *bmap,
428 enum isl_dim_type type, __isl_take isl_id *id)
430 bmap = isl_basic_map_cow(bmap);
431 if (!bmap)
432 goto error;
433 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
434 if (!bmap->dim)
435 return isl_basic_map_free(bmap);
436 bmap = isl_basic_map_finalize(bmap);
437 return bmap;
438 error:
439 isl_id_free(id);
440 return NULL;
443 /* Replace the identifier of the tuple by "id".
445 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
446 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
448 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
451 /* Does the input or output tuple have a name?
453 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
455 return map ? isl_space_has_tuple_name(map->dim, type) : -1;
458 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
459 enum isl_dim_type type)
461 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
464 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
465 const char *s)
467 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
470 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
471 enum isl_dim_type type, __isl_take isl_id *id)
473 map = isl_map_cow(map);
474 if (!map)
475 goto error;
477 map->dim = isl_space_set_tuple_id(map->dim, type, id);
479 return isl_map_reset_space(map, isl_space_copy(map->dim));
480 error:
481 isl_id_free(id);
482 return NULL;
485 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
486 __isl_take isl_id *id)
488 return isl_map_set_tuple_id(set, isl_dim_set, id);
491 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
492 enum isl_dim_type type)
494 map = isl_map_cow(map);
495 if (!map)
496 return NULL;
498 map->dim = isl_space_reset_tuple_id(map->dim, type);
500 return isl_map_reset_space(map, isl_space_copy(map->dim));
503 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
505 return isl_map_reset_tuple_id(set, isl_dim_set);
508 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
510 return map ? isl_space_has_tuple_id(map->dim, type) : -1;
513 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
514 enum isl_dim_type type)
516 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
519 int isl_set_has_tuple_id(__isl_keep isl_set *set)
521 return isl_map_has_tuple_id(set, isl_dim_set);
524 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
526 return isl_map_get_tuple_id(set, isl_dim_set);
529 /* Does the set tuple have a name?
531 int isl_set_has_tuple_name(__isl_keep isl_set *set)
533 return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
537 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
539 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
542 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
544 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
547 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
548 enum isl_dim_type type, unsigned pos)
550 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
553 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
554 enum isl_dim_type type, unsigned pos)
556 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
559 /* Does the given dimension have a name?
561 int isl_map_has_dim_name(__isl_keep isl_map *map,
562 enum isl_dim_type type, unsigned pos)
564 return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
567 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
568 enum isl_dim_type type, unsigned pos)
570 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
573 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
574 enum isl_dim_type type, unsigned pos)
576 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
579 /* Does the given dimension have a name?
581 int isl_set_has_dim_name(__isl_keep isl_set *set,
582 enum isl_dim_type type, unsigned pos)
584 return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
587 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
588 __isl_take isl_basic_map *bmap,
589 enum isl_dim_type type, unsigned pos, const char *s)
591 bmap = isl_basic_map_cow(bmap);
592 if (!bmap)
593 return NULL;
594 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
595 if (!bmap->dim)
596 goto error;
597 return isl_basic_map_finalize(bmap);
598 error:
599 isl_basic_map_free(bmap);
600 return NULL;
603 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
604 enum isl_dim_type type, unsigned pos, const char *s)
606 int i;
608 map = isl_map_cow(map);
609 if (!map)
610 return NULL;
612 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
613 if (!map->dim)
614 goto error;
616 for (i = 0; i < map->n; ++i) {
617 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
618 if (!map->p[i])
619 goto error;
622 return map;
623 error:
624 isl_map_free(map);
625 return NULL;
628 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
629 __isl_take isl_basic_set *bset,
630 enum isl_dim_type type, unsigned pos, const char *s)
632 return (isl_basic_set *)isl_basic_map_set_dim_name(
633 (isl_basic_map *)bset, type, pos, s);
636 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
637 enum isl_dim_type type, unsigned pos, const char *s)
639 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
642 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
643 enum isl_dim_type type, unsigned pos)
645 return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
648 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
649 enum isl_dim_type type, unsigned pos)
651 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
654 int isl_map_has_dim_id(__isl_keep isl_map *map,
655 enum isl_dim_type type, unsigned pos)
657 return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
660 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
661 enum isl_dim_type type, unsigned pos)
663 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
666 int isl_set_has_dim_id(__isl_keep isl_set *set,
667 enum isl_dim_type type, unsigned pos)
669 return isl_map_has_dim_id(set, type, pos);
672 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
673 enum isl_dim_type type, unsigned pos)
675 return isl_map_get_dim_id(set, type, pos);
678 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
679 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
681 map = isl_map_cow(map);
682 if (!map)
683 goto error;
685 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
687 return isl_map_reset_space(map, isl_space_copy(map->dim));
688 error:
689 isl_id_free(id);
690 return NULL;
693 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
694 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
696 return isl_map_set_dim_id(set, type, pos, id);
699 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
700 __isl_keep isl_id *id)
702 if (!map)
703 return -1;
704 return isl_space_find_dim_by_id(map->dim, type, id);
707 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
708 __isl_keep isl_id *id)
710 return isl_map_find_dim_by_id(set, type, id);
713 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
714 const char *name)
716 if (!map)
717 return -1;
718 return isl_space_find_dim_by_name(map->dim, type, name);
721 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
722 const char *name)
724 return isl_map_find_dim_by_name(set, type, name);
727 /* Reset the user pointer on all identifiers of parameters and tuples
728 * of the space of "map".
730 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
732 isl_space *space;
734 space = isl_map_get_space(map);
735 space = isl_space_reset_user(space);
736 map = isl_map_reset_space(map, space);
738 return map;
741 /* Reset the user pointer on all identifiers of parameters and tuples
742 * of the space of "set".
744 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
746 return isl_map_reset_user(set);
749 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
751 if (!bmap)
752 return -1;
753 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
756 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
758 return isl_basic_map_is_rational(bset);
761 /* Does "bmap" contain any rational points?
763 * If "bmap" has an equality for each dimension, equating the dimension
764 * to an integer constant, then it has no rational points, even if it
765 * is marked as rational.
767 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
769 int has_rational = 1;
770 unsigned total;
772 if (!bmap)
773 return -1;
774 if (isl_basic_map_plain_is_empty(bmap))
775 return 0;
776 if (!isl_basic_map_is_rational(bmap))
777 return 0;
778 bmap = isl_basic_map_copy(bmap);
779 bmap = isl_basic_map_implicit_equalities(bmap);
780 if (!bmap)
781 return -1;
782 total = isl_basic_map_total_dim(bmap);
783 if (bmap->n_eq == total) {
784 int i, j;
785 for (i = 0; i < bmap->n_eq; ++i) {
786 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
787 if (j < 0)
788 break;
789 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
790 !isl_int_is_negone(bmap->eq[i][1 + j]))
791 break;
792 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
793 total - j - 1);
794 if (j >= 0)
795 break;
797 if (i == bmap->n_eq)
798 has_rational = 0;
800 isl_basic_map_free(bmap);
802 return has_rational;
805 /* Does "map" contain any rational points?
807 int isl_map_has_rational(__isl_keep isl_map *map)
809 int i;
810 int has_rational;
812 if (!map)
813 return -1;
814 for (i = 0; i < map->n; ++i) {
815 has_rational = isl_basic_map_has_rational(map->p[i]);
816 if (has_rational < 0)
817 return -1;
818 if (has_rational)
819 return 1;
821 return 0;
824 /* Does "set" contain any rational points?
826 int isl_set_has_rational(__isl_keep isl_set *set)
828 return isl_map_has_rational(set);
831 /* Is this basic set a parameter domain?
833 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
835 if (!bset)
836 return -1;
837 return isl_space_is_params(bset->dim);
840 /* Is this set a parameter domain?
842 int isl_set_is_params(__isl_keep isl_set *set)
844 if (!set)
845 return -1;
846 return isl_space_is_params(set->dim);
849 /* Is this map actually a parameter domain?
850 * Users should never call this function. Outside of isl,
851 * a map can never be a parameter domain.
853 int isl_map_is_params(__isl_keep isl_map *map)
855 if (!map)
856 return -1;
857 return isl_space_is_params(map->dim);
860 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
861 struct isl_basic_map *bmap, unsigned extra,
862 unsigned n_eq, unsigned n_ineq)
864 int i;
865 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
867 bmap->ctx = ctx;
868 isl_ctx_ref(ctx);
870 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
871 if (isl_blk_is_error(bmap->block))
872 goto error;
874 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
875 if ((n_ineq + n_eq) && !bmap->ineq)
876 goto error;
878 if (extra == 0) {
879 bmap->block2 = isl_blk_empty();
880 bmap->div = NULL;
881 } else {
882 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
883 if (isl_blk_is_error(bmap->block2))
884 goto error;
886 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
887 if (!bmap->div)
888 goto error;
891 for (i = 0; i < n_ineq + n_eq; ++i)
892 bmap->ineq[i] = bmap->block.data + i * row_size;
894 for (i = 0; i < extra; ++i)
895 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
897 bmap->ref = 1;
898 bmap->flags = 0;
899 bmap->c_size = n_eq + n_ineq;
900 bmap->eq = bmap->ineq + n_ineq;
901 bmap->extra = extra;
902 bmap->n_eq = 0;
903 bmap->n_ineq = 0;
904 bmap->n_div = 0;
905 bmap->sample = NULL;
907 return bmap;
908 error:
909 isl_basic_map_free(bmap);
910 return NULL;
913 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
914 unsigned nparam, unsigned dim, unsigned extra,
915 unsigned n_eq, unsigned n_ineq)
917 struct isl_basic_map *bmap;
918 isl_space *space;
920 space = isl_space_set_alloc(ctx, nparam, dim);
921 if (!space)
922 return NULL;
924 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
925 return (struct isl_basic_set *)bmap;
928 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
929 unsigned extra, unsigned n_eq, unsigned n_ineq)
931 struct isl_basic_map *bmap;
932 if (!dim)
933 return NULL;
934 isl_assert(dim->ctx, dim->n_in == 0, goto error);
935 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
936 return (struct isl_basic_set *)bmap;
937 error:
938 isl_space_free(dim);
939 return NULL;
942 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
943 unsigned extra, unsigned n_eq, unsigned n_ineq)
945 struct isl_basic_map *bmap;
947 if (!dim)
948 return NULL;
949 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
950 if (!bmap)
951 goto error;
952 bmap->dim = dim;
954 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
955 error:
956 isl_space_free(dim);
957 return NULL;
960 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
961 unsigned nparam, unsigned in, unsigned out, unsigned extra,
962 unsigned n_eq, unsigned n_ineq)
964 struct isl_basic_map *bmap;
965 isl_space *dim;
967 dim = isl_space_alloc(ctx, nparam, in, out);
968 if (!dim)
969 return NULL;
971 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
972 return bmap;
975 static void dup_constraints(
976 struct isl_basic_map *dst, struct isl_basic_map *src)
978 int i;
979 unsigned total = isl_basic_map_total_dim(src);
981 for (i = 0; i < src->n_eq; ++i) {
982 int j = isl_basic_map_alloc_equality(dst);
983 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
986 for (i = 0; i < src->n_ineq; ++i) {
987 int j = isl_basic_map_alloc_inequality(dst);
988 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
991 for (i = 0; i < src->n_div; ++i) {
992 int j = isl_basic_map_alloc_div(dst);
993 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
995 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
998 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1000 struct isl_basic_map *dup;
1002 if (!bmap)
1003 return NULL;
1004 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1005 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1006 if (!dup)
1007 return NULL;
1008 dup_constraints(dup, bmap);
1009 dup->flags = bmap->flags;
1010 dup->sample = isl_vec_copy(bmap->sample);
1011 return dup;
1014 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1016 struct isl_basic_map *dup;
1018 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1019 return (struct isl_basic_set *)dup;
1022 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1024 if (!bset)
1025 return NULL;
1027 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1028 bset->ref++;
1029 return bset;
1031 return isl_basic_set_dup(bset);
1034 struct isl_set *isl_set_copy(struct isl_set *set)
1036 if (!set)
1037 return NULL;
1039 set->ref++;
1040 return set;
1043 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1045 if (!bmap)
1046 return NULL;
1048 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1049 bmap->ref++;
1050 return bmap;
1052 bmap = isl_basic_map_dup(bmap);
1053 if (bmap)
1054 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1055 return bmap;
1058 struct isl_map *isl_map_copy(struct isl_map *map)
1060 if (!map)
1061 return NULL;
1063 map->ref++;
1064 return map;
1067 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1069 if (!bmap)
1070 return NULL;
1072 if (--bmap->ref > 0)
1073 return NULL;
1075 isl_ctx_deref(bmap->ctx);
1076 free(bmap->div);
1077 isl_blk_free(bmap->ctx, bmap->block2);
1078 free(bmap->ineq);
1079 isl_blk_free(bmap->ctx, bmap->block);
1080 isl_vec_free(bmap->sample);
1081 isl_space_free(bmap->dim);
1082 free(bmap);
1084 return NULL;
1087 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1089 return isl_basic_map_free((struct isl_basic_map *)bset);
1092 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1094 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1097 __isl_give isl_map *isl_map_align_params_map_map_and(
1098 __isl_take isl_map *map1, __isl_take isl_map *map2,
1099 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1100 __isl_take isl_map *map2))
1102 if (!map1 || !map2)
1103 goto error;
1104 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1105 return fn(map1, map2);
1106 if (!isl_space_has_named_params(map1->dim) ||
1107 !isl_space_has_named_params(map2->dim))
1108 isl_die(map1->ctx, isl_error_invalid,
1109 "unaligned unnamed parameters", goto error);
1110 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1111 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1112 return fn(map1, map2);
1113 error:
1114 isl_map_free(map1);
1115 isl_map_free(map2);
1116 return NULL;
1119 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1120 __isl_keep isl_map *map2,
1121 int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1123 int r;
1125 if (!map1 || !map2)
1126 return -1;
1127 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1128 return fn(map1, map2);
1129 if (!isl_space_has_named_params(map1->dim) ||
1130 !isl_space_has_named_params(map2->dim))
1131 isl_die(map1->ctx, isl_error_invalid,
1132 "unaligned unnamed parameters", return -1);
1133 map1 = isl_map_copy(map1);
1134 map2 = isl_map_copy(map2);
1135 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1136 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1137 r = fn(map1, map2);
1138 isl_map_free(map1);
1139 isl_map_free(map2);
1140 return r;
1143 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1145 struct isl_ctx *ctx;
1146 if (!bmap)
1147 return -1;
1148 ctx = bmap->ctx;
1149 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1150 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1151 return -1);
1152 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1153 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1154 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1155 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1156 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1157 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1158 isl_int *t;
1159 int j = isl_basic_map_alloc_inequality(bmap);
1160 if (j < 0)
1161 return -1;
1162 t = bmap->ineq[j];
1163 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1164 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1165 bmap->eq[-1] = t;
1166 bmap->n_eq++;
1167 bmap->n_ineq--;
1168 bmap->eq--;
1169 return 0;
1171 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1172 bmap->extra - bmap->n_div);
1173 return bmap->n_eq++;
1176 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1178 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1181 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1183 if (!bmap)
1184 return -1;
1185 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1186 bmap->n_eq -= n;
1187 return 0;
1190 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1192 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1195 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1197 isl_int *t;
1198 if (!bmap)
1199 return -1;
1200 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1202 if (pos != bmap->n_eq - 1) {
1203 t = bmap->eq[pos];
1204 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1205 bmap->eq[bmap->n_eq - 1] = t;
1207 bmap->n_eq--;
1208 return 0;
1211 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1213 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1216 /* Turn inequality "pos" of "bmap" into an equality.
1218 * In particular, we move the inequality in front of the equalities
1219 * and move the last inequality in the position of the moved inequality.
1220 * Note that isl_tab_make_equalities_explicit depends on this particular
1221 * change in the ordering of the constraints.
1223 void isl_basic_map_inequality_to_equality(
1224 struct isl_basic_map *bmap, unsigned pos)
1226 isl_int *t;
1228 t = bmap->ineq[pos];
1229 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1230 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1231 bmap->eq[-1] = t;
1232 bmap->n_eq++;
1233 bmap->n_ineq--;
1234 bmap->eq--;
1235 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1236 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1237 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1238 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1241 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1243 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1246 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1248 struct isl_ctx *ctx;
1249 if (!bmap)
1250 return -1;
1251 ctx = bmap->ctx;
1252 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1253 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1254 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1255 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1256 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1257 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1258 1 + isl_basic_map_total_dim(bmap),
1259 bmap->extra - bmap->n_div);
1260 return bmap->n_ineq++;
1263 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1265 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1268 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1270 if (!bmap)
1271 return -1;
1272 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1273 bmap->n_ineq -= n;
1274 return 0;
1277 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1279 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1282 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1284 isl_int *t;
1285 if (!bmap)
1286 return -1;
1287 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1289 if (pos != bmap->n_ineq - 1) {
1290 t = bmap->ineq[pos];
1291 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1292 bmap->ineq[bmap->n_ineq - 1] = t;
1293 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1295 bmap->n_ineq--;
1296 return 0;
1299 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1301 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1304 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1305 isl_int *eq)
1307 int k;
1309 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1310 if (!bmap)
1311 return NULL;
1312 k = isl_basic_map_alloc_equality(bmap);
1313 if (k < 0)
1314 goto error;
1315 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1316 return bmap;
1317 error:
1318 isl_basic_map_free(bmap);
1319 return NULL;
1322 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1323 isl_int *eq)
1325 return (isl_basic_set *)
1326 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1329 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1330 isl_int *ineq)
1332 int k;
1334 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1335 if (!bmap)
1336 return NULL;
1337 k = isl_basic_map_alloc_inequality(bmap);
1338 if (k < 0)
1339 goto error;
1340 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1341 return bmap;
1342 error:
1343 isl_basic_map_free(bmap);
1344 return NULL;
1347 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1348 isl_int *ineq)
1350 return (isl_basic_set *)
1351 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1354 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1356 if (!bmap)
1357 return -1;
1358 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1359 isl_seq_clr(bmap->div[bmap->n_div] +
1360 1 + 1 + isl_basic_map_total_dim(bmap),
1361 bmap->extra - bmap->n_div);
1362 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1363 return bmap->n_div++;
1366 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1368 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1371 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1373 if (!bmap)
1374 return -1;
1375 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1376 bmap->n_div -= n;
1377 return 0;
1380 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1382 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1385 /* Copy constraint from src to dst, putting the vars of src at offset
1386 * dim_off in dst and the divs of src at offset div_off in dst.
1387 * If both sets are actually map, then dim_off applies to the input
1388 * variables.
1390 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1391 struct isl_basic_map *src_map, isl_int *src,
1392 unsigned in_off, unsigned out_off, unsigned div_off)
1394 unsigned src_nparam = isl_basic_map_n_param(src_map);
1395 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1396 unsigned src_in = isl_basic_map_n_in(src_map);
1397 unsigned dst_in = isl_basic_map_n_in(dst_map);
1398 unsigned src_out = isl_basic_map_n_out(src_map);
1399 unsigned dst_out = isl_basic_map_n_out(dst_map);
1400 isl_int_set(dst[0], src[0]);
1401 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1402 if (dst_nparam > src_nparam)
1403 isl_seq_clr(dst+1+src_nparam,
1404 dst_nparam - src_nparam);
1405 isl_seq_clr(dst+1+dst_nparam, in_off);
1406 isl_seq_cpy(dst+1+dst_nparam+in_off,
1407 src+1+src_nparam,
1408 isl_min(dst_in-in_off, src_in));
1409 if (dst_in-in_off > src_in)
1410 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1411 dst_in - in_off - src_in);
1412 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1413 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1414 src+1+src_nparam+src_in,
1415 isl_min(dst_out-out_off, src_out));
1416 if (dst_out-out_off > src_out)
1417 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1418 dst_out - out_off - src_out);
1419 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1420 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1421 src+1+src_nparam+src_in+src_out,
1422 isl_min(dst_map->extra-div_off, src_map->n_div));
1423 if (dst_map->n_div-div_off > src_map->n_div)
1424 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1425 div_off+src_map->n_div,
1426 dst_map->n_div - div_off - src_map->n_div);
1429 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1430 struct isl_basic_map *src_map, isl_int *src,
1431 unsigned in_off, unsigned out_off, unsigned div_off)
1433 isl_int_set(dst[0], src[0]);
1434 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1437 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1438 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1440 int i;
1441 unsigned div_off;
1443 if (!bmap1 || !bmap2)
1444 goto error;
1446 div_off = bmap1->n_div;
1448 for (i = 0; i < bmap2->n_eq; ++i) {
1449 int i1 = isl_basic_map_alloc_equality(bmap1);
1450 if (i1 < 0)
1451 goto error;
1452 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1453 i_pos, o_pos, div_off);
1456 for (i = 0; i < bmap2->n_ineq; ++i) {
1457 int i1 = isl_basic_map_alloc_inequality(bmap1);
1458 if (i1 < 0)
1459 goto error;
1460 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1461 i_pos, o_pos, div_off);
1464 for (i = 0; i < bmap2->n_div; ++i) {
1465 int i1 = isl_basic_map_alloc_div(bmap1);
1466 if (i1 < 0)
1467 goto error;
1468 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1469 i_pos, o_pos, div_off);
1472 isl_basic_map_free(bmap2);
1474 return bmap1;
1476 error:
1477 isl_basic_map_free(bmap1);
1478 isl_basic_map_free(bmap2);
1479 return NULL;
1482 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1483 struct isl_basic_set *bset2, unsigned pos)
1485 return (struct isl_basic_set *)
1486 add_constraints((struct isl_basic_map *)bset1,
1487 (struct isl_basic_map *)bset2, 0, pos);
1490 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1491 __isl_take isl_space *dim, unsigned extra,
1492 unsigned n_eq, unsigned n_ineq)
1494 struct isl_basic_map *ext;
1495 unsigned flags;
1496 int dims_ok;
1498 if (!dim)
1499 goto error;
1501 if (!base)
1502 goto error;
1504 dims_ok = isl_space_is_equal(base->dim, dim) &&
1505 base->extra >= base->n_div + extra;
1507 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1508 room_for_ineq(base, n_ineq)) {
1509 isl_space_free(dim);
1510 return base;
1513 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1514 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1515 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1516 extra += base->extra;
1517 n_eq += base->n_eq;
1518 n_ineq += base->n_ineq;
1520 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1521 dim = NULL;
1522 if (!ext)
1523 goto error;
1525 if (dims_ok)
1526 ext->sample = isl_vec_copy(base->sample);
1527 flags = base->flags;
1528 ext = add_constraints(ext, base, 0, 0);
1529 if (ext) {
1530 ext->flags = flags;
1531 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1534 return ext;
1536 error:
1537 isl_space_free(dim);
1538 isl_basic_map_free(base);
1539 return NULL;
1542 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1543 __isl_take isl_space *dim, unsigned extra,
1544 unsigned n_eq, unsigned n_ineq)
1546 return (struct isl_basic_set *)
1547 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1548 extra, n_eq, n_ineq);
1551 struct isl_basic_map *isl_basic_map_extend_constraints(
1552 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1554 if (!base)
1555 return NULL;
1556 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1557 0, n_eq, n_ineq);
1560 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1561 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1562 unsigned n_eq, unsigned n_ineq)
1564 struct isl_basic_map *bmap;
1565 isl_space *dim;
1567 if (!base)
1568 return NULL;
1569 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1570 if (!dim)
1571 goto error;
1573 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1574 return bmap;
1575 error:
1576 isl_basic_map_free(base);
1577 return NULL;
1580 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1581 unsigned nparam, unsigned dim, unsigned extra,
1582 unsigned n_eq, unsigned n_ineq)
1584 return (struct isl_basic_set *)
1585 isl_basic_map_extend((struct isl_basic_map *)base,
1586 nparam, 0, dim, extra, n_eq, n_ineq);
1589 struct isl_basic_set *isl_basic_set_extend_constraints(
1590 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1592 return (struct isl_basic_set *)
1593 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1594 n_eq, n_ineq);
1597 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1599 return (struct isl_basic_set *)
1600 isl_basic_map_cow((struct isl_basic_map *)bset);
1603 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1605 if (!bmap)
1606 return NULL;
1608 if (bmap->ref > 1) {
1609 bmap->ref--;
1610 bmap = isl_basic_map_dup(bmap);
1612 if (bmap)
1613 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1614 return bmap;
1617 struct isl_set *isl_set_cow(struct isl_set *set)
1619 if (!set)
1620 return NULL;
1622 if (set->ref == 1)
1623 return set;
1624 set->ref--;
1625 return isl_set_dup(set);
1628 struct isl_map *isl_map_cow(struct isl_map *map)
1630 if (!map)
1631 return NULL;
1633 if (map->ref == 1)
1634 return map;
1635 map->ref--;
1636 return isl_map_dup(map);
1639 static void swap_vars(struct isl_blk blk, isl_int *a,
1640 unsigned a_len, unsigned b_len)
1642 isl_seq_cpy(blk.data, a+a_len, b_len);
1643 isl_seq_cpy(blk.data+b_len, a, a_len);
1644 isl_seq_cpy(a, blk.data, b_len+a_len);
1647 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1648 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1650 int i;
1651 struct isl_blk blk;
1653 if (!bmap)
1654 goto error;
1656 isl_assert(bmap->ctx,
1657 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1659 if (n1 == 0 || n2 == 0)
1660 return bmap;
1662 bmap = isl_basic_map_cow(bmap);
1663 if (!bmap)
1664 return NULL;
1666 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1667 if (isl_blk_is_error(blk))
1668 goto error;
1670 for (i = 0; i < bmap->n_eq; ++i)
1671 swap_vars(blk,
1672 bmap->eq[i] + pos, n1, n2);
1674 for (i = 0; i < bmap->n_ineq; ++i)
1675 swap_vars(blk,
1676 bmap->ineq[i] + pos, n1, n2);
1678 for (i = 0; i < bmap->n_div; ++i)
1679 swap_vars(blk,
1680 bmap->div[i]+1 + pos, n1, n2);
1682 isl_blk_free(bmap->ctx, blk);
1684 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1685 bmap = isl_basic_map_gauss(bmap, NULL);
1686 return isl_basic_map_finalize(bmap);
1687 error:
1688 isl_basic_map_free(bmap);
1689 return NULL;
1692 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1693 __isl_take isl_basic_set *bset, unsigned n)
1695 unsigned dim;
1696 unsigned nparam;
1698 if (!bset)
1699 return NULL;
1701 nparam = isl_basic_set_n_param(bset);
1702 dim = isl_basic_set_n_dim(bset);
1703 isl_assert(bset->ctx, n <= dim, goto error);
1705 return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1706 error:
1707 isl_basic_set_free(bset);
1708 return NULL;
1711 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1713 int i = 0;
1714 unsigned total;
1715 if (!bmap)
1716 goto error;
1717 total = isl_basic_map_total_dim(bmap);
1718 isl_basic_map_free_div(bmap, bmap->n_div);
1719 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1720 if (bmap->n_eq > 0)
1721 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1722 else {
1723 i = isl_basic_map_alloc_equality(bmap);
1724 if (i < 0)
1725 goto error;
1727 isl_int_set_si(bmap->eq[i][0], 1);
1728 isl_seq_clr(bmap->eq[i]+1, total);
1729 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1730 isl_vec_free(bmap->sample);
1731 bmap->sample = NULL;
1732 return isl_basic_map_finalize(bmap);
1733 error:
1734 isl_basic_map_free(bmap);
1735 return NULL;
1738 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1740 return (struct isl_basic_set *)
1741 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1744 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1745 * of "bmap").
1747 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1749 isl_int *t = bmap->div[a];
1750 bmap->div[a] = bmap->div[b];
1751 bmap->div[b] = t;
1754 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1755 * div definitions accordingly.
1757 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1759 int i;
1760 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1762 swap_div(bmap, a, b);
1764 for (i = 0; i < bmap->n_eq; ++i)
1765 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1767 for (i = 0; i < bmap->n_ineq; ++i)
1768 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1770 for (i = 0; i < bmap->n_div; ++i)
1771 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1772 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1775 /* Eliminate the specified n dimensions starting at first from the
1776 * constraints, without removing the dimensions from the space.
1777 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1779 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1780 enum isl_dim_type type, unsigned first, unsigned n)
1782 int i;
1784 if (!map)
1785 return NULL;
1786 if (n == 0)
1787 return map;
1789 if (first + n > isl_map_dim(map, type) || first + n < first)
1790 isl_die(map->ctx, isl_error_invalid,
1791 "index out of bounds", goto error);
1793 map = isl_map_cow(map);
1794 if (!map)
1795 return NULL;
1797 for (i = 0; i < map->n; ++i) {
1798 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1799 if (!map->p[i])
1800 goto error;
1802 return map;
1803 error:
1804 isl_map_free(map);
1805 return NULL;
1808 /* Eliminate the specified n dimensions starting at first from the
1809 * constraints, without removing the dimensions from the space.
1810 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1812 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1813 enum isl_dim_type type, unsigned first, unsigned n)
1815 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1818 /* Eliminate the specified n dimensions starting at first from the
1819 * constraints, without removing the dimensions from the space.
1820 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1822 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1823 unsigned first, unsigned n)
1825 return isl_set_eliminate(set, isl_dim_set, first, n);
1828 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1829 __isl_take isl_basic_map *bmap)
1831 if (!bmap)
1832 return NULL;
1833 bmap = isl_basic_map_eliminate_vars(bmap,
1834 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1835 if (!bmap)
1836 return NULL;
1837 bmap->n_div = 0;
1838 return isl_basic_map_finalize(bmap);
1841 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1842 __isl_take isl_basic_set *bset)
1844 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1845 (struct isl_basic_map *)bset);
1848 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1850 int i;
1852 if (!map)
1853 return NULL;
1854 if (map->n == 0)
1855 return map;
1857 map = isl_map_cow(map);
1858 if (!map)
1859 return NULL;
1861 for (i = 0; i < map->n; ++i) {
1862 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1863 if (!map->p[i])
1864 goto error;
1866 return map;
1867 error:
1868 isl_map_free(map);
1869 return NULL;
1872 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1874 return isl_map_remove_divs(set);
1877 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1878 enum isl_dim_type type, unsigned first, unsigned n)
1880 if (!bmap)
1881 return NULL;
1882 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1883 goto error);
1884 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1885 return bmap;
1886 bmap = isl_basic_map_eliminate_vars(bmap,
1887 isl_basic_map_offset(bmap, type) - 1 + first, n);
1888 if (!bmap)
1889 return bmap;
1890 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1891 return bmap;
1892 bmap = isl_basic_map_drop(bmap, type, first, n);
1893 return bmap;
1894 error:
1895 isl_basic_map_free(bmap);
1896 return NULL;
1899 /* Return true if the definition of the given div (recursively) involves
1900 * any of the given variables.
1902 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1903 unsigned first, unsigned n)
1905 int i;
1906 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1908 if (isl_int_is_zero(bmap->div[div][0]))
1909 return 0;
1910 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1911 return 1;
1913 for (i = bmap->n_div - 1; i >= 0; --i) {
1914 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1915 continue;
1916 if (div_involves_vars(bmap, i, first, n))
1917 return 1;
1920 return 0;
1923 /* Try and add a lower and/or upper bound on "div" to "bmap"
1924 * based on inequality "i".
1925 * "total" is the total number of variables (excluding the divs).
1926 * "v" is a temporary object that can be used during the calculations.
1927 * If "lb" is set, then a lower bound should be constructed.
1928 * If "ub" is set, then an upper bound should be constructed.
1930 * The calling function has already checked that the inequality does not
1931 * reference "div", but we still need to check that the inequality is
1932 * of the right form. We'll consider the case where we want to construct
1933 * a lower bound. The construction of upper bounds is similar.
1935 * Let "div" be of the form
1937 * q = floor((a + f(x))/d)
1939 * We essentially check if constraint "i" is of the form
1941 * b + f(x) >= 0
1943 * so that we can use it to derive a lower bound on "div".
1944 * However, we allow a slightly more general form
1946 * b + g(x) >= 0
1948 * with the condition that the coefficients of g(x) - f(x) are all
1949 * divisible by d.
1950 * Rewriting this constraint as
1952 * 0 >= -b - g(x)
1954 * adding a + f(x) to both sides and dividing by d, we obtain
1956 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1958 * Taking the floor on both sides, we obtain
1960 * q >= floor((a-b)/d) + (f(x)-g(x))/d
1962 * or
1964 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1966 * In the case of an upper bound, we construct the constraint
1968 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1971 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1972 __isl_take isl_basic_map *bmap, int div, int i,
1973 unsigned total, isl_int v, int lb, int ub)
1975 int j;
1977 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1978 if (lb) {
1979 isl_int_sub(v, bmap->ineq[i][1 + j],
1980 bmap->div[div][1 + 1 + j]);
1981 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1983 if (ub) {
1984 isl_int_add(v, bmap->ineq[i][1 + j],
1985 bmap->div[div][1 + 1 + j]);
1986 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1989 if (!lb && !ub)
1990 return bmap;
1992 bmap = isl_basic_map_cow(bmap);
1993 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1994 if (lb) {
1995 int k = isl_basic_map_alloc_inequality(bmap);
1996 if (k < 0)
1997 goto error;
1998 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1999 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2000 bmap->div[div][1 + j]);
2001 isl_int_cdiv_q(bmap->ineq[k][j],
2002 bmap->ineq[k][j], bmap->div[div][0]);
2004 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2006 if (ub) {
2007 int k = isl_basic_map_alloc_inequality(bmap);
2008 if (k < 0)
2009 goto error;
2010 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2011 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2012 bmap->div[div][1 + j]);
2013 isl_int_fdiv_q(bmap->ineq[k][j],
2014 bmap->ineq[k][j], bmap->div[div][0]);
2016 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2019 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2020 return bmap;
2021 error:
2022 isl_basic_map_free(bmap);
2023 return NULL;
2026 /* This function is called right before "div" is eliminated from "bmap"
2027 * using Fourier-Motzkin.
2028 * Look through the constraints of "bmap" for constraints on the argument
2029 * of the integer division and use them to construct constraints on the
2030 * integer division itself. These constraints can then be combined
2031 * during the Fourier-Motzkin elimination.
2032 * Note that it is only useful to introduce lower bounds on "div"
2033 * if "bmap" already contains upper bounds on "div" as the newly
2034 * introduce lower bounds can then be combined with the pre-existing
2035 * upper bounds. Similarly for upper bounds.
2036 * We therefore first check if "bmap" contains any lower and/or upper bounds
2037 * on "div".
2039 * It is interesting to note that the introduction of these constraints
2040 * can indeed lead to more accurate results, even when compared to
2041 * deriving constraints on the argument of "div" from constraints on "div".
2042 * Consider, for example, the set
2044 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2046 * The second constraint can be rewritten as
2048 * 2 * [(-i-2j+3)/4] + k >= 0
2050 * from which we can derive
2052 * -i - 2j + 3 >= -2k
2054 * or
2056 * i + 2j <= 3 + 2k
2058 * Combined with the first constraint, we obtain
2060 * -3 <= 3 + 2k or k >= -3
2062 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2063 * the first constraint, we obtain
2065 * [(i + 2j)/4] >= [-3/4] = -1
2067 * Combining this constraint with the second constraint, we obtain
2069 * k >= -2
2071 static __isl_give isl_basic_map *insert_bounds_on_div(
2072 __isl_take isl_basic_map *bmap, int div)
2074 int i;
2075 int check_lb, check_ub;
2076 isl_int v;
2077 unsigned total;
2079 if (!bmap)
2080 return NULL;
2082 if (isl_int_is_zero(bmap->div[div][0]))
2083 return bmap;
2085 total = isl_space_dim(bmap->dim, isl_dim_all);
2087 check_lb = 0;
2088 check_ub = 0;
2089 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2090 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2091 if (s > 0)
2092 check_ub = 1;
2093 if (s < 0)
2094 check_lb = 1;
2097 if (!check_lb && !check_ub)
2098 return bmap;
2100 isl_int_init(v);
2102 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2103 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2104 continue;
2106 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2107 check_lb, check_ub);
2110 isl_int_clear(v);
2112 return bmap;
2115 /* Remove all divs (recursively) involving any of the given dimensions
2116 * in their definitions.
2118 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2119 __isl_take isl_basic_map *bmap,
2120 enum isl_dim_type type, unsigned first, unsigned n)
2122 int i;
2124 if (!bmap)
2125 return NULL;
2126 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2127 goto error);
2128 first += isl_basic_map_offset(bmap, type);
2130 for (i = bmap->n_div - 1; i >= 0; --i) {
2131 if (!div_involves_vars(bmap, i, first, n))
2132 continue;
2133 bmap = insert_bounds_on_div(bmap, i);
2134 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2135 if (!bmap)
2136 return NULL;
2137 i = bmap->n_div;
2140 return bmap;
2141 error:
2142 isl_basic_map_free(bmap);
2143 return NULL;
2146 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2147 __isl_take isl_basic_set *bset,
2148 enum isl_dim_type type, unsigned first, unsigned n)
2150 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2153 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2154 enum isl_dim_type type, unsigned first, unsigned n)
2156 int i;
2158 if (!map)
2159 return NULL;
2160 if (map->n == 0)
2161 return map;
2163 map = isl_map_cow(map);
2164 if (!map)
2165 return NULL;
2167 for (i = 0; i < map->n; ++i) {
2168 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2169 type, first, n);
2170 if (!map->p[i])
2171 goto error;
2173 return map;
2174 error:
2175 isl_map_free(map);
2176 return NULL;
2179 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2180 enum isl_dim_type type, unsigned first, unsigned n)
2182 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2183 type, first, n);
2186 /* Does the desciption of "bmap" depend on the specified dimensions?
2187 * We also check whether the dimensions appear in any of the div definitions.
2188 * In principle there is no need for this check. If the dimensions appear
2189 * in a div definition, they also appear in the defining constraints of that
2190 * div.
2192 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2193 enum isl_dim_type type, unsigned first, unsigned n)
2195 int i;
2197 if (!bmap)
2198 return -1;
2200 if (first + n > isl_basic_map_dim(bmap, type))
2201 isl_die(bmap->ctx, isl_error_invalid,
2202 "index out of bounds", return -1);
2204 first += isl_basic_map_offset(bmap, type);
2205 for (i = 0; i < bmap->n_eq; ++i)
2206 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2207 return 1;
2208 for (i = 0; i < bmap->n_ineq; ++i)
2209 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2210 return 1;
2211 for (i = 0; i < bmap->n_div; ++i) {
2212 if (isl_int_is_zero(bmap->div[i][0]))
2213 continue;
2214 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2215 return 1;
2218 return 0;
2221 int isl_map_involves_dims(__isl_keep isl_map *map,
2222 enum isl_dim_type type, unsigned first, unsigned n)
2224 int i;
2226 if (!map)
2227 return -1;
2229 if (first + n > isl_map_dim(map, type))
2230 isl_die(map->ctx, isl_error_invalid,
2231 "index out of bounds", return -1);
2233 for (i = 0; i < map->n; ++i) {
2234 int involves = isl_basic_map_involves_dims(map->p[i],
2235 type, first, n);
2236 if (involves < 0 || involves)
2237 return involves;
2240 return 0;
2243 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2244 enum isl_dim_type type, unsigned first, unsigned n)
2246 return isl_basic_map_involves_dims(bset, type, first, n);
2249 int isl_set_involves_dims(__isl_keep isl_set *set,
2250 enum isl_dim_type type, unsigned first, unsigned n)
2252 return isl_map_involves_dims(set, type, first, n);
2255 /* Return true if the definition of the given div is unknown or depends
2256 * on unknown divs.
2258 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2260 int i;
2261 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2263 if (isl_int_is_zero(bmap->div[div][0]))
2264 return 1;
2266 for (i = bmap->n_div - 1; i >= 0; --i) {
2267 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2268 continue;
2269 if (div_is_unknown(bmap, i))
2270 return 1;
2273 return 0;
2276 /* Remove all divs that are unknown or defined in terms of unknown divs.
2278 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2279 __isl_take isl_basic_map *bmap)
2281 int i;
2283 if (!bmap)
2284 return NULL;
2286 for (i = bmap->n_div - 1; i >= 0; --i) {
2287 if (!div_is_unknown(bmap, i))
2288 continue;
2289 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2290 if (!bmap)
2291 return NULL;
2292 i = bmap->n_div;
2295 return bmap;
2298 /* Remove all divs that are unknown or defined in terms of unknown divs.
2300 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2301 __isl_take isl_basic_set *bset)
2303 return isl_basic_map_remove_unknown_divs(bset);
2306 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2308 int i;
2310 if (!map)
2311 return NULL;
2312 if (map->n == 0)
2313 return map;
2315 map = isl_map_cow(map);
2316 if (!map)
2317 return NULL;
2319 for (i = 0; i < map->n; ++i) {
2320 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2321 if (!map->p[i])
2322 goto error;
2324 return map;
2325 error:
2326 isl_map_free(map);
2327 return NULL;
2330 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2332 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2335 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2336 __isl_take isl_basic_set *bset,
2337 enum isl_dim_type type, unsigned first, unsigned n)
2339 return (isl_basic_set *)
2340 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2343 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2344 enum isl_dim_type type, unsigned first, unsigned n)
2346 int i;
2348 if (n == 0)
2349 return map;
2351 map = isl_map_cow(map);
2352 if (!map)
2353 return NULL;
2354 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2356 for (i = 0; i < map->n; ++i) {
2357 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2358 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2359 if (!map->p[i])
2360 goto error;
2362 map = isl_map_drop(map, type, first, n);
2363 return map;
2364 error:
2365 isl_map_free(map);
2366 return NULL;
2369 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2370 enum isl_dim_type type, unsigned first, unsigned n)
2372 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2375 /* Project out n inputs starting at first using Fourier-Motzkin */
2376 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2377 unsigned first, unsigned n)
2379 return isl_map_remove_dims(map, isl_dim_in, first, n);
2382 static void dump_term(struct isl_basic_map *bmap,
2383 isl_int c, int pos, FILE *out)
2385 const char *name;
2386 unsigned in = isl_basic_map_n_in(bmap);
2387 unsigned dim = in + isl_basic_map_n_out(bmap);
2388 unsigned nparam = isl_basic_map_n_param(bmap);
2389 if (!pos)
2390 isl_int_print(out, c, 0);
2391 else {
2392 if (!isl_int_is_one(c))
2393 isl_int_print(out, c, 0);
2394 if (pos < 1 + nparam) {
2395 name = isl_space_get_dim_name(bmap->dim,
2396 isl_dim_param, pos - 1);
2397 if (name)
2398 fprintf(out, "%s", name);
2399 else
2400 fprintf(out, "p%d", pos - 1);
2401 } else if (pos < 1 + nparam + in)
2402 fprintf(out, "i%d", pos - 1 - nparam);
2403 else if (pos < 1 + nparam + dim)
2404 fprintf(out, "o%d", pos - 1 - nparam - in);
2405 else
2406 fprintf(out, "e%d", pos - 1 - nparam - dim);
2410 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2411 int sign, FILE *out)
2413 int i;
2414 int first;
2415 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2416 isl_int v;
2418 isl_int_init(v);
2419 for (i = 0, first = 1; i < len; ++i) {
2420 if (isl_int_sgn(c[i]) * sign <= 0)
2421 continue;
2422 if (!first)
2423 fprintf(out, " + ");
2424 first = 0;
2425 isl_int_abs(v, c[i]);
2426 dump_term(bmap, v, i, out);
2428 isl_int_clear(v);
2429 if (first)
2430 fprintf(out, "0");
2433 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2434 const char *op, FILE *out, int indent)
2436 int i;
2438 fprintf(out, "%*s", indent, "");
2440 dump_constraint_sign(bmap, c, 1, out);
2441 fprintf(out, " %s ", op);
2442 dump_constraint_sign(bmap, c, -1, out);
2444 fprintf(out, "\n");
2446 for (i = bmap->n_div; i < bmap->extra; ++i) {
2447 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2448 continue;
2449 fprintf(out, "%*s", indent, "");
2450 fprintf(out, "ERROR: unused div coefficient not zero\n");
2451 abort();
2455 static void dump_constraints(struct isl_basic_map *bmap,
2456 isl_int **c, unsigned n,
2457 const char *op, FILE *out, int indent)
2459 int i;
2461 for (i = 0; i < n; ++i)
2462 dump_constraint(bmap, c[i], op, out, indent);
2465 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2467 int j;
2468 int first = 1;
2469 unsigned total = isl_basic_map_total_dim(bmap);
2471 for (j = 0; j < 1 + total; ++j) {
2472 if (isl_int_is_zero(exp[j]))
2473 continue;
2474 if (!first && isl_int_is_pos(exp[j]))
2475 fprintf(out, "+");
2476 dump_term(bmap, exp[j], j, out);
2477 first = 0;
2481 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2483 int i;
2485 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2486 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2488 for (i = 0; i < bmap->n_div; ++i) {
2489 fprintf(out, "%*s", indent, "");
2490 fprintf(out, "e%d = [(", i);
2491 dump_affine(bmap, bmap->div[i]+1, out);
2492 fprintf(out, ")/");
2493 isl_int_print(out, bmap->div[i][0], 0);
2494 fprintf(out, "]\n");
2498 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2499 FILE *out, int indent)
2501 if (!bset) {
2502 fprintf(out, "null basic set\n");
2503 return;
2506 fprintf(out, "%*s", indent, "");
2507 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2508 bset->ref, bset->dim->nparam, bset->dim->n_out,
2509 bset->extra, bset->flags);
2510 dump((struct isl_basic_map *)bset, out, indent);
2513 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2514 FILE *out, int indent)
2516 if (!bmap) {
2517 fprintf(out, "null basic map\n");
2518 return;
2521 fprintf(out, "%*s", indent, "");
2522 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2523 "flags: %x, n_name: %d\n",
2524 bmap->ref,
2525 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2526 bmap->extra, bmap->flags, bmap->dim->n_id);
2527 dump(bmap, out, indent);
2530 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2532 unsigned total;
2533 if (!bmap)
2534 return -1;
2535 total = isl_basic_map_total_dim(bmap);
2536 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2537 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2538 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2539 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2540 return 0;
2543 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2544 unsigned flags)
2546 struct isl_set *set;
2548 if (!dim)
2549 return NULL;
2550 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2551 isl_assert(dim->ctx, n >= 0, goto error);
2552 set = isl_alloc(dim->ctx, struct isl_set,
2553 sizeof(struct isl_set) +
2554 (n - 1) * sizeof(struct isl_basic_set *));
2555 if (!set)
2556 goto error;
2558 set->ctx = dim->ctx;
2559 isl_ctx_ref(set->ctx);
2560 set->ref = 1;
2561 set->size = n;
2562 set->n = 0;
2563 set->dim = dim;
2564 set->flags = flags;
2565 return set;
2566 error:
2567 isl_space_free(dim);
2568 return NULL;
2571 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2572 unsigned nparam, unsigned dim, int n, unsigned flags)
2574 struct isl_set *set;
2575 isl_space *dims;
2577 dims = isl_space_alloc(ctx, nparam, 0, dim);
2578 if (!dims)
2579 return NULL;
2581 set = isl_set_alloc_space(dims, n, flags);
2582 return set;
2585 /* Make sure "map" has room for at least "n" more basic maps.
2587 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2589 int i;
2590 struct isl_map *grown = NULL;
2592 if (!map)
2593 return NULL;
2594 isl_assert(map->ctx, n >= 0, goto error);
2595 if (map->n + n <= map->size)
2596 return map;
2597 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2598 if (!grown)
2599 goto error;
2600 for (i = 0; i < map->n; ++i) {
2601 grown->p[i] = isl_basic_map_copy(map->p[i]);
2602 if (!grown->p[i])
2603 goto error;
2604 grown->n++;
2606 isl_map_free(map);
2607 return grown;
2608 error:
2609 isl_map_free(grown);
2610 isl_map_free(map);
2611 return NULL;
2614 /* Make sure "set" has room for at least "n" more basic sets.
2616 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2618 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2621 struct isl_set *isl_set_dup(struct isl_set *set)
2623 int i;
2624 struct isl_set *dup;
2626 if (!set)
2627 return NULL;
2629 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2630 if (!dup)
2631 return NULL;
2632 for (i = 0; i < set->n; ++i)
2633 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2634 return dup;
2637 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2639 return isl_map_from_basic_map(bset);
2642 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2644 struct isl_map *map;
2646 if (!bmap)
2647 return NULL;
2649 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2650 return isl_map_add_basic_map(map, bmap);
2653 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2654 __isl_take isl_basic_set *bset)
2656 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2657 (struct isl_basic_map *)bset);
2660 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2662 int i;
2664 if (!set)
2665 return NULL;
2667 if (--set->ref > 0)
2668 return NULL;
2670 isl_ctx_deref(set->ctx);
2671 for (i = 0; i < set->n; ++i)
2672 isl_basic_set_free(set->p[i]);
2673 isl_space_free(set->dim);
2674 free(set);
2676 return NULL;
2679 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2681 int i;
2683 if (!set) {
2684 fprintf(out, "null set\n");
2685 return;
2688 fprintf(out, "%*s", indent, "");
2689 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2690 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2691 set->flags);
2692 for (i = 0; i < set->n; ++i) {
2693 fprintf(out, "%*s", indent, "");
2694 fprintf(out, "basic set %d:\n", i);
2695 isl_basic_set_print_internal(set->p[i], out, indent+4);
2699 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2701 int i;
2703 if (!map) {
2704 fprintf(out, "null map\n");
2705 return;
2708 fprintf(out, "%*s", indent, "");
2709 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2710 "flags: %x, n_name: %d\n",
2711 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2712 map->dim->n_out, map->flags, map->dim->n_id);
2713 for (i = 0; i < map->n; ++i) {
2714 fprintf(out, "%*s", indent, "");
2715 fprintf(out, "basic map %d:\n", i);
2716 isl_basic_map_print_internal(map->p[i], out, indent+4);
2720 struct isl_basic_map *isl_basic_map_intersect_domain(
2721 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2723 struct isl_basic_map *bmap_domain;
2725 if (!bmap || !bset)
2726 goto error;
2728 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2729 bset->dim, isl_dim_param), goto error);
2731 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2732 isl_assert(bset->ctx,
2733 isl_basic_map_compatible_domain(bmap, bset), goto error);
2735 bmap = isl_basic_map_cow(bmap);
2736 if (!bmap)
2737 goto error;
2738 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2739 bset->n_div, bset->n_eq, bset->n_ineq);
2740 bmap_domain = isl_basic_map_from_domain(bset);
2741 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2743 bmap = isl_basic_map_simplify(bmap);
2744 return isl_basic_map_finalize(bmap);
2745 error:
2746 isl_basic_map_free(bmap);
2747 isl_basic_set_free(bset);
2748 return NULL;
2751 struct isl_basic_map *isl_basic_map_intersect_range(
2752 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2754 struct isl_basic_map *bmap_range;
2756 if (!bmap || !bset)
2757 goto error;
2759 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2760 bset->dim, isl_dim_param), goto error);
2762 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2763 isl_assert(bset->ctx,
2764 isl_basic_map_compatible_range(bmap, bset), goto error);
2766 if (isl_basic_set_is_universe(bset)) {
2767 isl_basic_set_free(bset);
2768 return bmap;
2771 bmap = isl_basic_map_cow(bmap);
2772 if (!bmap)
2773 goto error;
2774 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2775 bset->n_div, bset->n_eq, bset->n_ineq);
2776 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2777 bmap = add_constraints(bmap, bmap_range, 0, 0);
2779 bmap = isl_basic_map_simplify(bmap);
2780 return isl_basic_map_finalize(bmap);
2781 error:
2782 isl_basic_map_free(bmap);
2783 isl_basic_set_free(bset);
2784 return NULL;
2787 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2789 int i;
2790 unsigned total;
2791 isl_int s;
2793 if (!bmap || !vec)
2794 return -1;
2796 total = 1 + isl_basic_map_total_dim(bmap);
2797 if (total != vec->size)
2798 return -1;
2800 isl_int_init(s);
2802 for (i = 0; i < bmap->n_eq; ++i) {
2803 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2804 if (!isl_int_is_zero(s)) {
2805 isl_int_clear(s);
2806 return 0;
2810 for (i = 0; i < bmap->n_ineq; ++i) {
2811 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2812 if (isl_int_is_neg(s)) {
2813 isl_int_clear(s);
2814 return 0;
2818 isl_int_clear(s);
2820 return 1;
2823 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2825 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2828 struct isl_basic_map *isl_basic_map_intersect(
2829 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2831 struct isl_vec *sample = NULL;
2833 if (!bmap1 || !bmap2)
2834 goto error;
2836 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2837 bmap2->dim, isl_dim_param), goto error);
2838 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2839 isl_space_dim(bmap1->dim, isl_dim_param) &&
2840 isl_space_dim(bmap2->dim, isl_dim_all) !=
2841 isl_space_dim(bmap2->dim, isl_dim_param))
2842 return isl_basic_map_intersect(bmap2, bmap1);
2844 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2845 isl_space_dim(bmap2->dim, isl_dim_param))
2846 isl_assert(bmap1->ctx,
2847 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2849 if (bmap1->sample &&
2850 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2851 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2852 sample = isl_vec_copy(bmap1->sample);
2853 else if (bmap2->sample &&
2854 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2855 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2856 sample = isl_vec_copy(bmap2->sample);
2858 bmap1 = isl_basic_map_cow(bmap1);
2859 if (!bmap1)
2860 goto error;
2861 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2862 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2863 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2865 if (!bmap1)
2866 isl_vec_free(sample);
2867 else if (sample) {
2868 isl_vec_free(bmap1->sample);
2869 bmap1->sample = sample;
2872 bmap1 = isl_basic_map_simplify(bmap1);
2873 return isl_basic_map_finalize(bmap1);
2874 error:
2875 if (sample)
2876 isl_vec_free(sample);
2877 isl_basic_map_free(bmap1);
2878 isl_basic_map_free(bmap2);
2879 return NULL;
2882 struct isl_basic_set *isl_basic_set_intersect(
2883 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2885 return (struct isl_basic_set *)
2886 isl_basic_map_intersect(
2887 (struct isl_basic_map *)bset1,
2888 (struct isl_basic_map *)bset2);
2891 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2892 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2894 return isl_basic_set_intersect(bset1, bset2);
2897 /* Special case of isl_map_intersect, where both map1 and map2
2898 * are convex, without any divs and such that either map1 or map2
2899 * contains a single constraint. This constraint is then simply
2900 * added to the other map.
2902 static __isl_give isl_map *map_intersect_add_constraint(
2903 __isl_take isl_map *map1, __isl_take isl_map *map2)
2905 isl_assert(map1->ctx, map1->n == 1, goto error);
2906 isl_assert(map2->ctx, map1->n == 1, goto error);
2907 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2908 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2910 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2911 return isl_map_intersect(map2, map1);
2913 isl_assert(map2->ctx,
2914 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2916 map1 = isl_map_cow(map1);
2917 if (!map1)
2918 goto error;
2919 if (isl_map_plain_is_empty(map1)) {
2920 isl_map_free(map2);
2921 return map1;
2923 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2924 if (map2->p[0]->n_eq == 1)
2925 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2926 else
2927 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2928 map2->p[0]->ineq[0]);
2930 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2931 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2932 if (!map1->p[0])
2933 goto error;
2935 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2936 isl_basic_map_free(map1->p[0]);
2937 map1->n = 0;
2940 isl_map_free(map2);
2942 return map1;
2943 error:
2944 isl_map_free(map1);
2945 isl_map_free(map2);
2946 return NULL;
2949 /* map2 may be either a parameter domain or a map living in the same
2950 * space as map1.
2952 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2953 __isl_take isl_map *map2)
2955 unsigned flags = 0;
2956 isl_map *result;
2957 int i, j;
2959 if (!map1 || !map2)
2960 goto error;
2962 if ((isl_map_plain_is_empty(map1) ||
2963 isl_map_plain_is_universe(map2)) &&
2964 isl_space_is_equal(map1->dim, map2->dim)) {
2965 isl_map_free(map2);
2966 return map1;
2968 if ((isl_map_plain_is_empty(map2) ||
2969 isl_map_plain_is_universe(map1)) &&
2970 isl_space_is_equal(map1->dim, map2->dim)) {
2971 isl_map_free(map1);
2972 return map2;
2975 if (map1->n == 1 && map2->n == 1 &&
2976 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2977 isl_space_is_equal(map1->dim, map2->dim) &&
2978 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2979 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2980 return map_intersect_add_constraint(map1, map2);
2982 if (isl_space_dim(map2->dim, isl_dim_all) !=
2983 isl_space_dim(map2->dim, isl_dim_param))
2984 isl_assert(map1->ctx,
2985 isl_space_is_equal(map1->dim, map2->dim), goto error);
2987 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2988 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2989 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2991 result = isl_map_alloc_space(isl_space_copy(map1->dim),
2992 map1->n * map2->n, flags);
2993 if (!result)
2994 goto error;
2995 for (i = 0; i < map1->n; ++i)
2996 for (j = 0; j < map2->n; ++j) {
2997 struct isl_basic_map *part;
2998 part = isl_basic_map_intersect(
2999 isl_basic_map_copy(map1->p[i]),
3000 isl_basic_map_copy(map2->p[j]));
3001 if (isl_basic_map_is_empty(part) < 0)
3002 part = isl_basic_map_free(part);
3003 result = isl_map_add_basic_map(result, part);
3004 if (!result)
3005 goto error;
3007 isl_map_free(map1);
3008 isl_map_free(map2);
3009 return result;
3010 error:
3011 isl_map_free(map1);
3012 isl_map_free(map2);
3013 return NULL;
3016 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3017 __isl_take isl_map *map2)
3019 if (!map1 || !map2)
3020 goto error;
3021 if (!isl_space_is_equal(map1->dim, map2->dim))
3022 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3023 "spaces don't match", goto error);
3024 return map_intersect_internal(map1, map2);
3025 error:
3026 isl_map_free(map1);
3027 isl_map_free(map2);
3028 return NULL;
3031 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3032 __isl_take isl_map *map2)
3034 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3037 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3039 return (struct isl_set *)
3040 isl_map_intersect((struct isl_map *)set1,
3041 (struct isl_map *)set2);
3044 /* map_intersect_internal accepts intersections
3045 * with parameter domains, so we can just call that function.
3047 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3048 __isl_take isl_set *params)
3050 return map_intersect_internal(map, params);
3053 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3054 __isl_take isl_map *map2)
3056 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3059 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3060 __isl_take isl_set *params)
3062 return isl_map_intersect_params(set, params);
3065 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3067 isl_space *dim;
3068 struct isl_basic_set *bset;
3069 unsigned in;
3071 if (!bmap)
3072 return NULL;
3073 bmap = isl_basic_map_cow(bmap);
3074 if (!bmap)
3075 return NULL;
3076 dim = isl_space_reverse(isl_space_copy(bmap->dim));
3077 in = isl_basic_map_n_in(bmap);
3078 bset = isl_basic_set_from_basic_map(bmap);
3079 bset = isl_basic_set_swap_vars(bset, in);
3080 return isl_basic_map_from_basic_set(bset, dim);
3083 static __isl_give isl_basic_map *basic_map_space_reset(
3084 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3086 isl_space *space;
3088 if (!bmap)
3089 return NULL;
3090 if (!isl_space_is_named_or_nested(bmap->dim, type))
3091 return bmap;
3093 space = isl_basic_map_get_space(bmap);
3094 space = isl_space_reset(space, type);
3095 bmap = isl_basic_map_reset_space(bmap, space);
3096 return bmap;
3099 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3100 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3101 unsigned pos, unsigned n)
3103 isl_space *res_dim;
3104 struct isl_basic_map *res;
3105 struct isl_dim_map *dim_map;
3106 unsigned total, off;
3107 enum isl_dim_type t;
3109 if (n == 0)
3110 return basic_map_space_reset(bmap, type);
3112 if (!bmap)
3113 return NULL;
3115 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3117 total = isl_basic_map_total_dim(bmap) + n;
3118 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3119 off = 0;
3120 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3121 if (t != type) {
3122 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3123 } else {
3124 unsigned size = isl_basic_map_dim(bmap, t);
3125 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3126 0, pos, off);
3127 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3128 pos, size - pos, off + pos + n);
3130 off += isl_space_dim(res_dim, t);
3132 isl_dim_map_div(dim_map, bmap, off);
3134 res = isl_basic_map_alloc_space(res_dim,
3135 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3136 if (isl_basic_map_is_rational(bmap))
3137 res = isl_basic_map_set_rational(res);
3138 if (isl_basic_map_plain_is_empty(bmap)) {
3139 isl_basic_map_free(bmap);
3140 free(dim_map);
3141 return isl_basic_map_set_to_empty(res);
3143 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3144 return isl_basic_map_finalize(res);
3147 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3148 __isl_take isl_basic_set *bset,
3149 enum isl_dim_type type, unsigned pos, unsigned n)
3151 return isl_basic_map_insert_dims(bset, type, pos, n);
3154 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3155 enum isl_dim_type type, unsigned n)
3157 if (!bmap)
3158 return NULL;
3159 return isl_basic_map_insert_dims(bmap, type,
3160 isl_basic_map_dim(bmap, type), n);
3163 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3164 enum isl_dim_type type, unsigned n)
3166 if (!bset)
3167 return NULL;
3168 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3169 return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3170 error:
3171 isl_basic_set_free(bset);
3172 return NULL;
3175 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3176 enum isl_dim_type type)
3178 isl_space *space;
3180 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3181 return map;
3183 space = isl_map_get_space(map);
3184 space = isl_space_reset(space, type);
3185 map = isl_map_reset_space(map, space);
3186 return map;
3189 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3190 enum isl_dim_type type, unsigned pos, unsigned n)
3192 int i;
3194 if (n == 0)
3195 return map_space_reset(map, type);
3197 map = isl_map_cow(map);
3198 if (!map)
3199 return NULL;
3201 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3202 if (!map->dim)
3203 goto error;
3205 for (i = 0; i < map->n; ++i) {
3206 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3207 if (!map->p[i])
3208 goto error;
3211 return map;
3212 error:
3213 isl_map_free(map);
3214 return NULL;
3217 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3218 enum isl_dim_type type, unsigned pos, unsigned n)
3220 return isl_map_insert_dims(set, type, pos, n);
3223 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3224 enum isl_dim_type type, unsigned n)
3226 if (!map)
3227 return NULL;
3228 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3231 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3232 enum isl_dim_type type, unsigned n)
3234 if (!set)
3235 return NULL;
3236 isl_assert(set->ctx, type != isl_dim_in, goto error);
3237 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3238 error:
3239 isl_set_free(set);
3240 return NULL;
3243 __isl_give isl_basic_map *isl_basic_map_move_dims(
3244 __isl_take isl_basic_map *bmap,
3245 enum isl_dim_type dst_type, unsigned dst_pos,
3246 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3248 struct isl_dim_map *dim_map;
3249 struct isl_basic_map *res;
3250 enum isl_dim_type t;
3251 unsigned total, off;
3253 if (!bmap)
3254 return NULL;
3255 if (n == 0)
3256 return bmap;
3258 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3259 goto error);
3261 if (dst_type == src_type && dst_pos == src_pos)
3262 return bmap;
3264 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3266 if (pos(bmap->dim, dst_type) + dst_pos ==
3267 pos(bmap->dim, src_type) + src_pos +
3268 ((src_type < dst_type) ? n : 0)) {
3269 bmap = isl_basic_map_cow(bmap);
3270 if (!bmap)
3271 return NULL;
3273 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3274 src_type, src_pos, n);
3275 if (!bmap->dim)
3276 goto error;
3278 bmap = isl_basic_map_finalize(bmap);
3280 return bmap;
3283 total = isl_basic_map_total_dim(bmap);
3284 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3286 off = 0;
3287 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3288 unsigned size = isl_space_dim(bmap->dim, t);
3289 if (t == dst_type) {
3290 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3291 0, dst_pos, off);
3292 off += dst_pos;
3293 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3294 src_pos, n, off);
3295 off += n;
3296 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3297 dst_pos, size - dst_pos, off);
3298 off += size - dst_pos;
3299 } else if (t == src_type) {
3300 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3301 0, src_pos, off);
3302 off += src_pos;
3303 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3304 src_pos + n, size - src_pos - n, off);
3305 off += size - src_pos - n;
3306 } else {
3307 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3308 off += size;
3311 isl_dim_map_div(dim_map, bmap, off);
3313 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3314 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3315 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3316 if (!bmap)
3317 goto error;
3319 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3320 src_type, src_pos, n);
3321 if (!bmap->dim)
3322 goto error;
3324 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3325 bmap = isl_basic_map_gauss(bmap, NULL);
3326 bmap = isl_basic_map_finalize(bmap);
3328 return bmap;
3329 error:
3330 isl_basic_map_free(bmap);
3331 return NULL;
3334 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3335 enum isl_dim_type dst_type, unsigned dst_pos,
3336 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3338 return (isl_basic_set *)isl_basic_map_move_dims(
3339 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3342 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3343 enum isl_dim_type dst_type, unsigned dst_pos,
3344 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3346 if (!set)
3347 return NULL;
3348 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3349 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3350 src_type, src_pos, n);
3351 error:
3352 isl_set_free(set);
3353 return NULL;
3356 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3357 enum isl_dim_type dst_type, unsigned dst_pos,
3358 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3360 int i;
3362 if (!map)
3363 return NULL;
3364 if (n == 0)
3365 return map;
3367 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3368 goto error);
3370 if (dst_type == src_type && dst_pos == src_pos)
3371 return map;
3373 isl_assert(map->ctx, dst_type != src_type, goto error);
3375 map = isl_map_cow(map);
3376 if (!map)
3377 return NULL;
3379 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3380 if (!map->dim)
3381 goto error;
3383 for (i = 0; i < map->n; ++i) {
3384 map->p[i] = isl_basic_map_move_dims(map->p[i],
3385 dst_type, dst_pos,
3386 src_type, src_pos, n);
3387 if (!map->p[i])
3388 goto error;
3391 return map;
3392 error:
3393 isl_map_free(map);
3394 return NULL;
3397 /* Move the specified dimensions to the last columns right before
3398 * the divs. Don't change the dimension specification of bmap.
3399 * That's the responsibility of the caller.
3401 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3402 enum isl_dim_type type, unsigned first, unsigned n)
3404 struct isl_dim_map *dim_map;
3405 struct isl_basic_map *res;
3406 enum isl_dim_type t;
3407 unsigned total, off;
3409 if (!bmap)
3410 return NULL;
3411 if (pos(bmap->dim, type) + first + n ==
3412 1 + isl_space_dim(bmap->dim, isl_dim_all))
3413 return bmap;
3415 total = isl_basic_map_total_dim(bmap);
3416 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3418 off = 0;
3419 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3420 unsigned size = isl_space_dim(bmap->dim, t);
3421 if (t == type) {
3422 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3423 0, first, off);
3424 off += first;
3425 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3426 first, n, total - bmap->n_div - n);
3427 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3428 first + n, size - (first + n), off);
3429 off += size - (first + n);
3430 } else {
3431 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3432 off += size;
3435 isl_dim_map_div(dim_map, bmap, off + n);
3437 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3438 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3439 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3440 return res;
3443 /* Insert "n" rows in the divs of "bmap".
3445 * The number of columns is not changed, which means that the last
3446 * dimensions of "bmap" are being reintepreted as the new divs.
3447 * The space of "bmap" is not adjusted, however, which means
3448 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3449 * from the space of "bmap" is the responsibility of the caller.
3451 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3452 int n)
3454 int i;
3455 size_t row_size;
3456 isl_int **new_div;
3457 isl_int *old;
3459 bmap = isl_basic_map_cow(bmap);
3460 if (!bmap)
3461 return NULL;
3463 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3464 old = bmap->block2.data;
3465 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3466 (bmap->extra + n) * (1 + row_size));
3467 if (!bmap->block2.data)
3468 return isl_basic_map_free(bmap);
3469 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3470 if (!new_div)
3471 return isl_basic_map_free(bmap);
3472 for (i = 0; i < n; ++i) {
3473 new_div[i] = bmap->block2.data +
3474 (bmap->extra + i) * (1 + row_size);
3475 isl_seq_clr(new_div[i], 1 + row_size);
3477 for (i = 0; i < bmap->extra; ++i)
3478 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3479 free(bmap->div);
3480 bmap->div = new_div;
3481 bmap->n_div += n;
3482 bmap->extra += n;
3484 return bmap;
3487 /* Turn the n dimensions of type type, starting at first
3488 * into existentially quantified variables.
3490 __isl_give isl_basic_map *isl_basic_map_project_out(
3491 __isl_take isl_basic_map *bmap,
3492 enum isl_dim_type type, unsigned first, unsigned n)
3494 if (n == 0)
3495 return basic_map_space_reset(bmap, type);
3497 if (!bmap)
3498 return NULL;
3500 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3501 return isl_basic_map_remove_dims(bmap, type, first, n);
3503 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3504 goto error);
3506 bmap = move_last(bmap, type, first, n);
3507 bmap = isl_basic_map_cow(bmap);
3508 bmap = insert_div_rows(bmap, n);
3509 if (!bmap)
3510 return NULL;
3512 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3513 if (!bmap->dim)
3514 goto error;
3515 bmap = isl_basic_map_simplify(bmap);
3516 bmap = isl_basic_map_drop_redundant_divs(bmap);
3517 return isl_basic_map_finalize(bmap);
3518 error:
3519 isl_basic_map_free(bmap);
3520 return NULL;
3523 /* Turn the n dimensions of type type, starting at first
3524 * into existentially quantified variables.
3526 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3527 enum isl_dim_type type, unsigned first, unsigned n)
3529 return (isl_basic_set *)isl_basic_map_project_out(
3530 (isl_basic_map *)bset, type, first, n);
3533 /* Turn the n dimensions of type type, starting at first
3534 * into existentially quantified variables.
3536 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3537 enum isl_dim_type type, unsigned first, unsigned n)
3539 int i;
3541 if (!map)
3542 return NULL;
3544 if (n == 0)
3545 return map_space_reset(map, type);
3547 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3549 map = isl_map_cow(map);
3550 if (!map)
3551 return NULL;
3553 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3554 if (!map->dim)
3555 goto error;
3557 for (i = 0; i < map->n; ++i) {
3558 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3559 if (!map->p[i])
3560 goto error;
3563 return map;
3564 error:
3565 isl_map_free(map);
3566 return NULL;
3569 /* Turn the n dimensions of type type, starting at first
3570 * into existentially quantified variables.
3572 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3573 enum isl_dim_type type, unsigned first, unsigned n)
3575 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3578 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3580 int i, j;
3582 for (i = 0; i < n; ++i) {
3583 j = isl_basic_map_alloc_div(bmap);
3584 if (j < 0)
3585 goto error;
3586 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3588 return bmap;
3589 error:
3590 isl_basic_map_free(bmap);
3591 return NULL;
3594 struct isl_basic_map *isl_basic_map_apply_range(
3595 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3597 isl_space *dim_result = NULL;
3598 struct isl_basic_map *bmap;
3599 unsigned n_in, n_out, n, nparam, total, pos;
3600 struct isl_dim_map *dim_map1, *dim_map2;
3602 if (!bmap1 || !bmap2)
3603 goto error;
3604 if (!isl_space_match(bmap1->dim, isl_dim_param,
3605 bmap2->dim, isl_dim_param))
3606 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3607 "parameters don't match", goto error);
3608 if (!isl_space_tuple_match(bmap1->dim, isl_dim_out,
3609 bmap2->dim, isl_dim_in))
3610 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3611 "spaces don't match", goto error);
3613 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3614 isl_space_copy(bmap2->dim));
3616 n_in = isl_basic_map_n_in(bmap1);
3617 n_out = isl_basic_map_n_out(bmap2);
3618 n = isl_basic_map_n_out(bmap1);
3619 nparam = isl_basic_map_n_param(bmap1);
3621 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3622 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3623 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3624 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3625 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3626 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3627 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3628 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3629 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3630 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3631 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3633 bmap = isl_basic_map_alloc_space(dim_result,
3634 bmap1->n_div + bmap2->n_div + n,
3635 bmap1->n_eq + bmap2->n_eq,
3636 bmap1->n_ineq + bmap2->n_ineq);
3637 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3638 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3639 bmap = add_divs(bmap, n);
3640 bmap = isl_basic_map_simplify(bmap);
3641 bmap = isl_basic_map_drop_redundant_divs(bmap);
3642 return isl_basic_map_finalize(bmap);
3643 error:
3644 isl_basic_map_free(bmap1);
3645 isl_basic_map_free(bmap2);
3646 return NULL;
3649 struct isl_basic_set *isl_basic_set_apply(
3650 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3652 if (!bset || !bmap)
3653 goto error;
3655 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3656 goto error);
3658 return (struct isl_basic_set *)
3659 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3660 error:
3661 isl_basic_set_free(bset);
3662 isl_basic_map_free(bmap);
3663 return NULL;
3666 struct isl_basic_map *isl_basic_map_apply_domain(
3667 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3669 if (!bmap1 || !bmap2)
3670 goto error;
3672 isl_assert(bmap1->ctx,
3673 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3674 isl_assert(bmap1->ctx,
3675 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3676 goto error);
3678 bmap1 = isl_basic_map_reverse(bmap1);
3679 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3680 return isl_basic_map_reverse(bmap1);
3681 error:
3682 isl_basic_map_free(bmap1);
3683 isl_basic_map_free(bmap2);
3684 return NULL;
3687 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3688 * A \cap B -> f(A) + f(B)
3690 struct isl_basic_map *isl_basic_map_sum(
3691 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3693 unsigned n_in, n_out, nparam, total, pos;
3694 struct isl_basic_map *bmap = NULL;
3695 struct isl_dim_map *dim_map1, *dim_map2;
3696 int i;
3698 if (!bmap1 || !bmap2)
3699 goto error;
3701 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3702 goto error);
3704 nparam = isl_basic_map_n_param(bmap1);
3705 n_in = isl_basic_map_n_in(bmap1);
3706 n_out = isl_basic_map_n_out(bmap1);
3708 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3709 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3710 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3711 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3712 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3713 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3714 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3715 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3716 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3717 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3718 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3720 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3721 bmap1->n_div + bmap2->n_div + 2 * n_out,
3722 bmap1->n_eq + bmap2->n_eq + n_out,
3723 bmap1->n_ineq + bmap2->n_ineq);
3724 for (i = 0; i < n_out; ++i) {
3725 int j = isl_basic_map_alloc_equality(bmap);
3726 if (j < 0)
3727 goto error;
3728 isl_seq_clr(bmap->eq[j], 1+total);
3729 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3730 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3731 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3733 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3734 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3735 bmap = add_divs(bmap, 2 * n_out);
3737 bmap = isl_basic_map_simplify(bmap);
3738 return isl_basic_map_finalize(bmap);
3739 error:
3740 isl_basic_map_free(bmap);
3741 isl_basic_map_free(bmap1);
3742 isl_basic_map_free(bmap2);
3743 return NULL;
3746 /* Given two maps A -> f(A) and B -> g(B), construct a map
3747 * A \cap B -> f(A) + f(B)
3749 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3751 struct isl_map *result;
3752 int i, j;
3754 if (!map1 || !map2)
3755 goto error;
3757 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3759 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3760 map1->n * map2->n, 0);
3761 if (!result)
3762 goto error;
3763 for (i = 0; i < map1->n; ++i)
3764 for (j = 0; j < map2->n; ++j) {
3765 struct isl_basic_map *part;
3766 part = isl_basic_map_sum(
3767 isl_basic_map_copy(map1->p[i]),
3768 isl_basic_map_copy(map2->p[j]));
3769 if (isl_basic_map_is_empty(part))
3770 isl_basic_map_free(part);
3771 else
3772 result = isl_map_add_basic_map(result, part);
3773 if (!result)
3774 goto error;
3776 isl_map_free(map1);
3777 isl_map_free(map2);
3778 return result;
3779 error:
3780 isl_map_free(map1);
3781 isl_map_free(map2);
3782 return NULL;
3785 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3786 __isl_take isl_set *set2)
3788 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3791 /* Given a basic map A -> f(A), construct A -> -f(A).
3793 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3795 int i, j;
3796 unsigned off, n;
3798 bmap = isl_basic_map_cow(bmap);
3799 if (!bmap)
3800 return NULL;
3802 n = isl_basic_map_dim(bmap, isl_dim_out);
3803 off = isl_basic_map_offset(bmap, isl_dim_out);
3804 for (i = 0; i < bmap->n_eq; ++i)
3805 for (j = 0; j < n; ++j)
3806 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3807 for (i = 0; i < bmap->n_ineq; ++i)
3808 for (j = 0; j < n; ++j)
3809 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3810 for (i = 0; i < bmap->n_div; ++i)
3811 for (j = 0; j < n; ++j)
3812 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3813 bmap = isl_basic_map_gauss(bmap, NULL);
3814 return isl_basic_map_finalize(bmap);
3817 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3819 return isl_basic_map_neg(bset);
3822 /* Given a map A -> f(A), construct A -> -f(A).
3824 struct isl_map *isl_map_neg(struct isl_map *map)
3826 int i;
3828 map = isl_map_cow(map);
3829 if (!map)
3830 return NULL;
3832 for (i = 0; i < map->n; ++i) {
3833 map->p[i] = isl_basic_map_neg(map->p[i]);
3834 if (!map->p[i])
3835 goto error;
3838 return map;
3839 error:
3840 isl_map_free(map);
3841 return NULL;
3844 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3846 return (isl_set *)isl_map_neg((isl_map *)set);
3849 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3850 * A -> floor(f(A)/d).
3852 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3853 isl_int d)
3855 unsigned n_in, n_out, nparam, total, pos;
3856 struct isl_basic_map *result = NULL;
3857 struct isl_dim_map *dim_map;
3858 int i;
3860 if (!bmap)
3861 return NULL;
3863 nparam = isl_basic_map_n_param(bmap);
3864 n_in = isl_basic_map_n_in(bmap);
3865 n_out = isl_basic_map_n_out(bmap);
3867 total = nparam + n_in + n_out + bmap->n_div + n_out;
3868 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3869 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3870 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3871 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3872 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3874 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3875 bmap->n_div + n_out,
3876 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3877 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3878 result = add_divs(result, n_out);
3879 for (i = 0; i < n_out; ++i) {
3880 int j;
3881 j = isl_basic_map_alloc_inequality(result);
3882 if (j < 0)
3883 goto error;
3884 isl_seq_clr(result->ineq[j], 1+total);
3885 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3886 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3887 j = isl_basic_map_alloc_inequality(result);
3888 if (j < 0)
3889 goto error;
3890 isl_seq_clr(result->ineq[j], 1+total);
3891 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3892 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3893 isl_int_sub_ui(result->ineq[j][0], d, 1);
3896 result = isl_basic_map_simplify(result);
3897 return isl_basic_map_finalize(result);
3898 error:
3899 isl_basic_map_free(result);
3900 return NULL;
3903 /* Given a map A -> f(A) and an integer d, construct a map
3904 * A -> floor(f(A)/d).
3906 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3908 int i;
3910 map = isl_map_cow(map);
3911 if (!map)
3912 return NULL;
3914 ISL_F_CLR(map, ISL_MAP_DISJOINT);
3915 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3916 for (i = 0; i < map->n; ++i) {
3917 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3918 if (!map->p[i])
3919 goto error;
3922 return map;
3923 error:
3924 isl_map_free(map);
3925 return NULL;
3928 /* Given a map A -> f(A) and an integer d, construct a map
3929 * A -> floor(f(A)/d).
3931 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
3932 __isl_take isl_val *d)
3934 if (!map || !d)
3935 goto error;
3936 if (!isl_val_is_int(d))
3937 isl_die(isl_val_get_ctx(d), isl_error_invalid,
3938 "expecting integer denominator", goto error);
3939 map = isl_map_floordiv(map, d->n);
3940 isl_val_free(d);
3941 return map;
3942 error:
3943 isl_map_free(map);
3944 isl_val_free(d);
3945 return NULL;
3948 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3950 int i;
3951 unsigned nparam;
3952 unsigned n_in;
3954 i = isl_basic_map_alloc_equality(bmap);
3955 if (i < 0)
3956 goto error;
3957 nparam = isl_basic_map_n_param(bmap);
3958 n_in = isl_basic_map_n_in(bmap);
3959 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3960 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3961 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3962 return isl_basic_map_finalize(bmap);
3963 error:
3964 isl_basic_map_free(bmap);
3965 return NULL;
3968 /* Add a constraints to "bmap" expressing i_pos < o_pos
3970 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3972 int i;
3973 unsigned nparam;
3974 unsigned n_in;
3976 i = isl_basic_map_alloc_inequality(bmap);
3977 if (i < 0)
3978 goto error;
3979 nparam = isl_basic_map_n_param(bmap);
3980 n_in = isl_basic_map_n_in(bmap);
3981 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3982 isl_int_set_si(bmap->ineq[i][0], -1);
3983 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3984 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3985 return isl_basic_map_finalize(bmap);
3986 error:
3987 isl_basic_map_free(bmap);
3988 return NULL;
3991 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3993 static __isl_give isl_basic_map *var_less_or_equal(
3994 __isl_take isl_basic_map *bmap, unsigned pos)
3996 int i;
3997 unsigned nparam;
3998 unsigned n_in;
4000 i = isl_basic_map_alloc_inequality(bmap);
4001 if (i < 0)
4002 goto error;
4003 nparam = isl_basic_map_n_param(bmap);
4004 n_in = isl_basic_map_n_in(bmap);
4005 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4006 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4007 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4008 return isl_basic_map_finalize(bmap);
4009 error:
4010 isl_basic_map_free(bmap);
4011 return NULL;
4014 /* Add a constraints to "bmap" expressing i_pos > o_pos
4016 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4018 int i;
4019 unsigned nparam;
4020 unsigned n_in;
4022 i = isl_basic_map_alloc_inequality(bmap);
4023 if (i < 0)
4024 goto error;
4025 nparam = isl_basic_map_n_param(bmap);
4026 n_in = isl_basic_map_n_in(bmap);
4027 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4028 isl_int_set_si(bmap->ineq[i][0], -1);
4029 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4030 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4031 return isl_basic_map_finalize(bmap);
4032 error:
4033 isl_basic_map_free(bmap);
4034 return NULL;
4037 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4039 static __isl_give isl_basic_map *var_more_or_equal(
4040 __isl_take isl_basic_map *bmap, unsigned pos)
4042 int i;
4043 unsigned nparam;
4044 unsigned n_in;
4046 i = isl_basic_map_alloc_inequality(bmap);
4047 if (i < 0)
4048 goto error;
4049 nparam = isl_basic_map_n_param(bmap);
4050 n_in = isl_basic_map_n_in(bmap);
4051 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4052 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4053 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4054 return isl_basic_map_finalize(bmap);
4055 error:
4056 isl_basic_map_free(bmap);
4057 return NULL;
4060 __isl_give isl_basic_map *isl_basic_map_equal(
4061 __isl_take isl_space *dim, unsigned n_equal)
4063 int i;
4064 struct isl_basic_map *bmap;
4065 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4066 if (!bmap)
4067 return NULL;
4068 for (i = 0; i < n_equal && bmap; ++i)
4069 bmap = var_equal(bmap, i);
4070 return isl_basic_map_finalize(bmap);
4073 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4075 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4076 unsigned pos)
4078 int i;
4079 struct isl_basic_map *bmap;
4080 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4081 if (!bmap)
4082 return NULL;
4083 for (i = 0; i < pos && bmap; ++i)
4084 bmap = var_equal(bmap, i);
4085 if (bmap)
4086 bmap = var_less(bmap, pos);
4087 return isl_basic_map_finalize(bmap);
4090 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4092 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4093 __isl_take isl_space *dim, unsigned pos)
4095 int i;
4096 isl_basic_map *bmap;
4098 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4099 for (i = 0; i < pos; ++i)
4100 bmap = var_equal(bmap, i);
4101 bmap = var_less_or_equal(bmap, pos);
4102 return isl_basic_map_finalize(bmap);
4105 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4107 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4108 unsigned pos)
4110 int i;
4111 struct isl_basic_map *bmap;
4112 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4113 if (!bmap)
4114 return NULL;
4115 for (i = 0; i < pos && bmap; ++i)
4116 bmap = var_equal(bmap, i);
4117 if (bmap)
4118 bmap = var_more(bmap, pos);
4119 return isl_basic_map_finalize(bmap);
4122 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4124 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4125 __isl_take isl_space *dim, unsigned pos)
4127 int i;
4128 isl_basic_map *bmap;
4130 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4131 for (i = 0; i < pos; ++i)
4132 bmap = var_equal(bmap, i);
4133 bmap = var_more_or_equal(bmap, pos);
4134 return isl_basic_map_finalize(bmap);
4137 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4138 unsigned n, int equal)
4140 struct isl_map *map;
4141 int i;
4143 if (n == 0 && equal)
4144 return isl_map_universe(dims);
4146 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4148 for (i = 0; i + 1 < n; ++i)
4149 map = isl_map_add_basic_map(map,
4150 isl_basic_map_less_at(isl_space_copy(dims), i));
4151 if (n > 0) {
4152 if (equal)
4153 map = isl_map_add_basic_map(map,
4154 isl_basic_map_less_or_equal_at(dims, n - 1));
4155 else
4156 map = isl_map_add_basic_map(map,
4157 isl_basic_map_less_at(dims, n - 1));
4158 } else
4159 isl_space_free(dims);
4161 return map;
4164 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4166 if (!dims)
4167 return NULL;
4168 return map_lex_lte_first(dims, dims->n_out, equal);
4171 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4173 return map_lex_lte_first(dim, n, 0);
4176 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4178 return map_lex_lte_first(dim, n, 1);
4181 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4183 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4186 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4188 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4191 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4192 unsigned n, int equal)
4194 struct isl_map *map;
4195 int i;
4197 if (n == 0 && equal)
4198 return isl_map_universe(dims);
4200 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4202 for (i = 0; i + 1 < n; ++i)
4203 map = isl_map_add_basic_map(map,
4204 isl_basic_map_more_at(isl_space_copy(dims), i));
4205 if (n > 0) {
4206 if (equal)
4207 map = isl_map_add_basic_map(map,
4208 isl_basic_map_more_or_equal_at(dims, n - 1));
4209 else
4210 map = isl_map_add_basic_map(map,
4211 isl_basic_map_more_at(dims, n - 1));
4212 } else
4213 isl_space_free(dims);
4215 return map;
4218 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4220 if (!dims)
4221 return NULL;
4222 return map_lex_gte_first(dims, dims->n_out, equal);
4225 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4227 return map_lex_gte_first(dim, n, 0);
4230 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4232 return map_lex_gte_first(dim, n, 1);
4235 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4237 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4240 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4242 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4245 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4246 __isl_take isl_set *set2)
4248 isl_map *map;
4249 map = isl_map_lex_le(isl_set_get_space(set1));
4250 map = isl_map_intersect_domain(map, set1);
4251 map = isl_map_intersect_range(map, set2);
4252 return map;
4255 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4256 __isl_take isl_set *set2)
4258 isl_map *map;
4259 map = isl_map_lex_lt(isl_set_get_space(set1));
4260 map = isl_map_intersect_domain(map, set1);
4261 map = isl_map_intersect_range(map, set2);
4262 return map;
4265 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4266 __isl_take isl_set *set2)
4268 isl_map *map;
4269 map = isl_map_lex_ge(isl_set_get_space(set1));
4270 map = isl_map_intersect_domain(map, set1);
4271 map = isl_map_intersect_range(map, set2);
4272 return map;
4275 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4276 __isl_take isl_set *set2)
4278 isl_map *map;
4279 map = isl_map_lex_gt(isl_set_get_space(set1));
4280 map = isl_map_intersect_domain(map, set1);
4281 map = isl_map_intersect_range(map, set2);
4282 return map;
4285 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4286 __isl_take isl_map *map2)
4288 isl_map *map;
4289 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4290 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4291 map = isl_map_apply_range(map, isl_map_reverse(map2));
4292 return map;
4295 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4296 __isl_take isl_map *map2)
4298 isl_map *map;
4299 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4300 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4301 map = isl_map_apply_range(map, isl_map_reverse(map2));
4302 return map;
4305 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4306 __isl_take isl_map *map2)
4308 isl_map *map;
4309 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4310 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4311 map = isl_map_apply_range(map, isl_map_reverse(map2));
4312 return map;
4315 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4316 __isl_take isl_map *map2)
4318 isl_map *map;
4319 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4320 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4321 map = isl_map_apply_range(map, isl_map_reverse(map2));
4322 return map;
4325 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4326 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4328 struct isl_basic_map *bmap;
4330 bset = isl_basic_set_cow(bset);
4331 if (!bset || !dim)
4332 goto error;
4334 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4335 isl_space_free(bset->dim);
4336 bmap = (struct isl_basic_map *) bset;
4337 bmap->dim = dim;
4338 return isl_basic_map_finalize(bmap);
4339 error:
4340 isl_basic_set_free(bset);
4341 isl_space_free(dim);
4342 return NULL;
4345 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4347 if (!bmap)
4348 goto error;
4349 if (bmap->dim->n_in == 0)
4350 return (struct isl_basic_set *)bmap;
4351 bmap = isl_basic_map_cow(bmap);
4352 if (!bmap)
4353 goto error;
4354 bmap->dim = isl_space_as_set_space(bmap->dim);
4355 if (!bmap->dim)
4356 goto error;
4357 bmap = isl_basic_map_finalize(bmap);
4358 return (struct isl_basic_set *)bmap;
4359 error:
4360 isl_basic_map_free(bmap);
4361 return NULL;
4364 /* For a div d = floor(f/m), add the constraint
4366 * f - m d >= 0
4368 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4369 unsigned pos, isl_int *div)
4371 int i;
4372 unsigned total = isl_basic_map_total_dim(bmap);
4374 i = isl_basic_map_alloc_inequality(bmap);
4375 if (i < 0)
4376 return -1;
4377 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4378 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4380 return 0;
4383 /* For a div d = floor(f/m), add the constraint
4385 * -(f-(n-1)) + m d >= 0
4387 static int add_lower_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_neg(bmap->ineq[i], div + 1, 1 + total);
4397 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4398 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4399 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4401 return 0;
4404 /* For a div d = floor(f/m), add the constraints
4406 * f - m d >= 0
4407 * -(f-(n-1)) + m d >= 0
4409 * Note that the second constraint is the negation of
4411 * f - m d >= n
4413 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4414 unsigned pos, isl_int *div)
4416 if (add_upper_div_constraint(bmap, pos, div) < 0)
4417 return -1;
4418 if (add_lower_div_constraint(bmap, pos, div) < 0)
4419 return -1;
4420 return 0;
4423 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4424 unsigned pos, isl_int *div)
4426 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4427 pos, div);
4430 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4432 unsigned total = isl_basic_map_total_dim(bmap);
4433 unsigned div_pos = total - bmap->n_div + div;
4435 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4436 bmap->div[div]);
4439 /* For each known div d = floor(f/m), add the constraints
4441 * f - m d >= 0
4442 * -(f-(n-1)) + m d >= 0
4444 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4445 __isl_take isl_basic_map *bmap)
4447 int i;
4448 unsigned n_div;
4450 if (!bmap)
4451 return NULL;
4452 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4453 if (n_div == 0)
4454 return bmap;
4455 bmap = isl_basic_map_cow(bmap);
4456 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
4457 if (!bmap)
4458 return NULL;
4459 for (i = 0; i < n_div; ++i) {
4460 if (isl_int_is_zero(bmap->div[i][0]))
4461 continue;
4462 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4463 return isl_basic_map_free(bmap);
4466 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4467 bmap = isl_basic_map_finalize(bmap);
4468 return bmap;
4471 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4473 * In particular, if this div is of the form d = floor(f/m),
4474 * then add the constraint
4476 * f - m d >= 0
4478 * if sign < 0 or the constraint
4480 * -(f-(n-1)) + m d >= 0
4482 * if sign > 0.
4484 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4485 unsigned div, int sign)
4487 unsigned total;
4488 unsigned div_pos;
4490 if (!bmap)
4491 return -1;
4493 total = isl_basic_map_total_dim(bmap);
4494 div_pos = total - bmap->n_div + div;
4496 if (sign < 0)
4497 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4498 else
4499 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4502 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4504 return isl_basic_map_add_div_constraints(bset, div);
4507 struct isl_basic_set *isl_basic_map_underlying_set(
4508 struct isl_basic_map *bmap)
4510 if (!bmap)
4511 goto error;
4512 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4513 bmap->n_div == 0 &&
4514 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4515 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4516 return (struct isl_basic_set *)bmap;
4517 bmap = isl_basic_map_cow(bmap);
4518 if (!bmap)
4519 goto error;
4520 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4521 if (!bmap->dim)
4522 goto error;
4523 bmap->extra -= bmap->n_div;
4524 bmap->n_div = 0;
4525 bmap = isl_basic_map_finalize(bmap);
4526 return (struct isl_basic_set *)bmap;
4527 error:
4528 isl_basic_map_free(bmap);
4529 return NULL;
4532 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4533 __isl_take isl_basic_set *bset)
4535 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4538 /* Replace each element in "list" by the result of applying
4539 * isl_basic_set_underlying_set to the element.
4541 __isl_give isl_basic_set_list *isl_basic_set_list_underlying_set(
4542 __isl_take isl_basic_set_list *list)
4544 int i, n;
4546 if (!list)
4547 return NULL;
4549 n = isl_basic_set_list_n_basic_set(list);
4550 for (i = 0; i < n; ++i) {
4551 isl_basic_set *bset;
4553 bset = isl_basic_set_list_get_basic_set(list, i);
4554 bset = isl_basic_set_underlying_set(bset);
4555 list = isl_basic_set_list_set_basic_set(list, i, bset);
4558 return list;
4561 struct isl_basic_map *isl_basic_map_overlying_set(
4562 struct isl_basic_set *bset, struct isl_basic_map *like)
4564 struct isl_basic_map *bmap;
4565 struct isl_ctx *ctx;
4566 unsigned total;
4567 int i;
4569 if (!bset || !like)
4570 goto error;
4571 ctx = bset->ctx;
4572 isl_assert(ctx, bset->n_div == 0, goto error);
4573 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4574 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4575 goto error);
4576 if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4577 isl_basic_map_free(like);
4578 return (struct isl_basic_map *)bset;
4580 bset = isl_basic_set_cow(bset);
4581 if (!bset)
4582 goto error;
4583 total = bset->dim->n_out + bset->extra;
4584 bmap = (struct isl_basic_map *)bset;
4585 isl_space_free(bmap->dim);
4586 bmap->dim = isl_space_copy(like->dim);
4587 if (!bmap->dim)
4588 goto error;
4589 bmap->n_div = like->n_div;
4590 bmap->extra += like->n_div;
4591 if (bmap->extra) {
4592 unsigned ltotal;
4593 isl_int **div;
4594 ltotal = total - bmap->extra + like->extra;
4595 if (ltotal > total)
4596 ltotal = total;
4597 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4598 bmap->extra * (1 + 1 + total));
4599 if (isl_blk_is_error(bmap->block2))
4600 goto error;
4601 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4602 if (!div)
4603 goto error;
4604 bmap->div = div;
4605 for (i = 0; i < bmap->extra; ++i)
4606 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4607 for (i = 0; i < like->n_div; ++i) {
4608 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4609 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4611 bmap = isl_basic_map_add_known_div_constraints(bmap);
4613 isl_basic_map_free(like);
4614 bmap = isl_basic_map_simplify(bmap);
4615 bmap = isl_basic_map_finalize(bmap);
4616 return bmap;
4617 error:
4618 isl_basic_map_free(like);
4619 isl_basic_set_free(bset);
4620 return NULL;
4623 struct isl_basic_set *isl_basic_set_from_underlying_set(
4624 struct isl_basic_set *bset, struct isl_basic_set *like)
4626 return (struct isl_basic_set *)
4627 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4630 struct isl_set *isl_set_from_underlying_set(
4631 struct isl_set *set, struct isl_basic_set *like)
4633 int i;
4635 if (!set || !like)
4636 goto error;
4637 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4638 goto error);
4639 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4640 isl_basic_set_free(like);
4641 return set;
4643 set = isl_set_cow(set);
4644 if (!set)
4645 goto error;
4646 for (i = 0; i < set->n; ++i) {
4647 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4648 isl_basic_set_copy(like));
4649 if (!set->p[i])
4650 goto error;
4652 isl_space_free(set->dim);
4653 set->dim = isl_space_copy(like->dim);
4654 if (!set->dim)
4655 goto error;
4656 isl_basic_set_free(like);
4657 return set;
4658 error:
4659 isl_basic_set_free(like);
4660 isl_set_free(set);
4661 return NULL;
4664 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4666 int i;
4668 map = isl_map_cow(map);
4669 if (!map)
4670 return NULL;
4671 map->dim = isl_space_cow(map->dim);
4672 if (!map->dim)
4673 goto error;
4675 for (i = 1; i < map->n; ++i)
4676 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4677 goto error);
4678 for (i = 0; i < map->n; ++i) {
4679 map->p[i] = (struct isl_basic_map *)
4680 isl_basic_map_underlying_set(map->p[i]);
4681 if (!map->p[i])
4682 goto error;
4684 if (map->n == 0)
4685 map->dim = isl_space_underlying(map->dim, 0);
4686 else {
4687 isl_space_free(map->dim);
4688 map->dim = isl_space_copy(map->p[0]->dim);
4690 if (!map->dim)
4691 goto error;
4692 return (struct isl_set *)map;
4693 error:
4694 isl_map_free(map);
4695 return NULL;
4698 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4700 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4703 __isl_give isl_basic_map *isl_basic_map_reset_space(
4704 __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4706 bmap = isl_basic_map_cow(bmap);
4707 if (!bmap || !dim)
4708 goto error;
4710 isl_space_free(bmap->dim);
4711 bmap->dim = dim;
4713 bmap = isl_basic_map_finalize(bmap);
4715 return bmap;
4716 error:
4717 isl_basic_map_free(bmap);
4718 isl_space_free(dim);
4719 return NULL;
4722 __isl_give isl_basic_set *isl_basic_set_reset_space(
4723 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4725 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4726 dim);
4729 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4730 __isl_take isl_space *dim)
4732 int i;
4734 map = isl_map_cow(map);
4735 if (!map || !dim)
4736 goto error;
4738 for (i = 0; i < map->n; ++i) {
4739 map->p[i] = isl_basic_map_reset_space(map->p[i],
4740 isl_space_copy(dim));
4741 if (!map->p[i])
4742 goto error;
4744 isl_space_free(map->dim);
4745 map->dim = dim;
4747 return map;
4748 error:
4749 isl_map_free(map);
4750 isl_space_free(dim);
4751 return NULL;
4754 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4755 __isl_take isl_space *dim)
4757 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4760 /* Compute the parameter domain of the given basic set.
4762 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4764 isl_space *space;
4765 unsigned n;
4767 if (isl_basic_set_is_params(bset))
4768 return bset;
4770 n = isl_basic_set_dim(bset, isl_dim_set);
4771 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4772 space = isl_basic_set_get_space(bset);
4773 space = isl_space_params(space);
4774 bset = isl_basic_set_reset_space(bset, space);
4775 return bset;
4778 /* Construct a zero-dimensional basic set with the given parameter domain.
4780 __isl_give isl_basic_set *isl_basic_set_from_params(
4781 __isl_take isl_basic_set *bset)
4783 isl_space *space;
4784 space = isl_basic_set_get_space(bset);
4785 space = isl_space_set_from_params(space);
4786 bset = isl_basic_set_reset_space(bset, space);
4787 return bset;
4790 /* Compute the parameter domain of the given set.
4792 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4794 isl_space *space;
4795 unsigned n;
4797 if (isl_set_is_params(set))
4798 return set;
4800 n = isl_set_dim(set, isl_dim_set);
4801 set = isl_set_project_out(set, isl_dim_set, 0, n);
4802 space = isl_set_get_space(set);
4803 space = isl_space_params(space);
4804 set = isl_set_reset_space(set, space);
4805 return set;
4808 /* Construct a zero-dimensional set with the given parameter domain.
4810 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4812 isl_space *space;
4813 space = isl_set_get_space(set);
4814 space = isl_space_set_from_params(space);
4815 set = isl_set_reset_space(set, space);
4816 return set;
4819 /* Compute the parameter domain of the given map.
4821 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4823 isl_space *space;
4824 unsigned n;
4826 n = isl_map_dim(map, isl_dim_in);
4827 map = isl_map_project_out(map, isl_dim_in, 0, n);
4828 n = isl_map_dim(map, isl_dim_out);
4829 map = isl_map_project_out(map, isl_dim_out, 0, n);
4830 space = isl_map_get_space(map);
4831 space = isl_space_params(space);
4832 map = isl_map_reset_space(map, space);
4833 return map;
4836 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4838 isl_space *dim;
4839 struct isl_basic_set *domain;
4840 unsigned n_in;
4841 unsigned n_out;
4843 if (!bmap)
4844 return NULL;
4845 dim = isl_space_domain(isl_basic_map_get_space(bmap));
4847 n_in = isl_basic_map_n_in(bmap);
4848 n_out = isl_basic_map_n_out(bmap);
4849 domain = isl_basic_set_from_basic_map(bmap);
4850 domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4852 domain = isl_basic_set_reset_space(domain, dim);
4854 return domain;
4857 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4859 if (!bmap)
4860 return -1;
4861 return isl_space_may_be_set(bmap->dim);
4864 /* Is this basic map actually a set?
4865 * Users should never call this function. Outside of isl,
4866 * the type should indicate whether something is a set or a map.
4868 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4870 if (!bmap)
4871 return -1;
4872 return isl_space_is_set(bmap->dim);
4875 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4877 if (!bmap)
4878 return NULL;
4879 if (isl_basic_map_is_set(bmap))
4880 return bmap;
4881 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4884 __isl_give isl_basic_map *isl_basic_map_domain_map(
4885 __isl_take isl_basic_map *bmap)
4887 int i, k;
4888 isl_space *dim;
4889 isl_basic_map *domain;
4890 int nparam, n_in, n_out;
4891 unsigned total;
4893 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4894 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4895 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4897 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4898 domain = isl_basic_map_universe(dim);
4900 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4901 bmap = isl_basic_map_apply_range(bmap, domain);
4902 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4904 total = isl_basic_map_total_dim(bmap);
4906 for (i = 0; i < n_in; ++i) {
4907 k = isl_basic_map_alloc_equality(bmap);
4908 if (k < 0)
4909 goto error;
4910 isl_seq_clr(bmap->eq[k], 1 + total);
4911 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4912 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4915 bmap = isl_basic_map_gauss(bmap, NULL);
4916 return isl_basic_map_finalize(bmap);
4917 error:
4918 isl_basic_map_free(bmap);
4919 return NULL;
4922 __isl_give isl_basic_map *isl_basic_map_range_map(
4923 __isl_take isl_basic_map *bmap)
4925 int i, k;
4926 isl_space *dim;
4927 isl_basic_map *range;
4928 int nparam, n_in, n_out;
4929 unsigned total;
4931 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4932 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4933 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4935 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4936 range = isl_basic_map_universe(dim);
4938 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4939 bmap = isl_basic_map_apply_range(bmap, range);
4940 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4942 total = isl_basic_map_total_dim(bmap);
4944 for (i = 0; i < n_out; ++i) {
4945 k = isl_basic_map_alloc_equality(bmap);
4946 if (k < 0)
4947 goto error;
4948 isl_seq_clr(bmap->eq[k], 1 + total);
4949 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4950 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4953 bmap = isl_basic_map_gauss(bmap, NULL);
4954 return isl_basic_map_finalize(bmap);
4955 error:
4956 isl_basic_map_free(bmap);
4957 return NULL;
4960 int isl_map_may_be_set(__isl_keep isl_map *map)
4962 if (!map)
4963 return -1;
4964 return isl_space_may_be_set(map->dim);
4967 /* Is this map actually a set?
4968 * Users should never call this function. Outside of isl,
4969 * the type should indicate whether something is a set or a map.
4971 int isl_map_is_set(__isl_keep isl_map *map)
4973 if (!map)
4974 return -1;
4975 return isl_space_is_set(map->dim);
4978 struct isl_set *isl_map_range(struct isl_map *map)
4980 int i;
4981 struct isl_set *set;
4983 if (!map)
4984 goto error;
4985 if (isl_map_is_set(map))
4986 return (isl_set *)map;
4988 map = isl_map_cow(map);
4989 if (!map)
4990 goto error;
4992 set = (struct isl_set *) map;
4993 set->dim = isl_space_range(set->dim);
4994 if (!set->dim)
4995 goto error;
4996 for (i = 0; i < map->n; ++i) {
4997 set->p[i] = isl_basic_map_range(map->p[i]);
4998 if (!set->p[i])
4999 goto error;
5001 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5002 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5003 return set;
5004 error:
5005 isl_map_free(map);
5006 return NULL;
5009 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5011 int i;
5013 map = isl_map_cow(map);
5014 if (!map)
5015 return NULL;
5017 map->dim = isl_space_domain_map(map->dim);
5018 if (!map->dim)
5019 goto error;
5020 for (i = 0; i < map->n; ++i) {
5021 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5022 if (!map->p[i])
5023 goto error;
5025 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5026 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5027 return map;
5028 error:
5029 isl_map_free(map);
5030 return NULL;
5033 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5035 int i;
5036 isl_space *range_dim;
5038 map = isl_map_cow(map);
5039 if (!map)
5040 return NULL;
5042 range_dim = isl_space_range(isl_map_get_space(map));
5043 range_dim = isl_space_from_range(range_dim);
5044 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5045 map->dim = isl_space_join(map->dim, range_dim);
5046 if (!map->dim)
5047 goto error;
5048 for (i = 0; i < map->n; ++i) {
5049 map->p[i] = isl_basic_map_range_map(map->p[i]);
5050 if (!map->p[i])
5051 goto error;
5053 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5054 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5055 return map;
5056 error:
5057 isl_map_free(map);
5058 return NULL;
5061 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5062 __isl_take isl_space *dim)
5064 int i;
5065 struct isl_map *map = NULL;
5067 set = isl_set_cow(set);
5068 if (!set || !dim)
5069 goto error;
5070 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5071 map = (struct isl_map *)set;
5072 for (i = 0; i < set->n; ++i) {
5073 map->p[i] = isl_basic_map_from_basic_set(
5074 set->p[i], isl_space_copy(dim));
5075 if (!map->p[i])
5076 goto error;
5078 isl_space_free(map->dim);
5079 map->dim = dim;
5080 return map;
5081 error:
5082 isl_space_free(dim);
5083 isl_set_free(set);
5084 return NULL;
5087 __isl_give isl_basic_map *isl_basic_map_from_domain(
5088 __isl_take isl_basic_set *bset)
5090 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5093 __isl_give isl_basic_map *isl_basic_map_from_range(
5094 __isl_take isl_basic_set *bset)
5096 isl_space *space;
5097 space = isl_basic_set_get_space(bset);
5098 space = isl_space_from_range(space);
5099 bset = isl_basic_set_reset_space(bset, space);
5100 return (isl_basic_map *)bset;
5103 /* Create a relation with the given set as range.
5104 * The domain of the created relation is a zero-dimensional
5105 * flat anonymous space.
5107 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5109 isl_space *space;
5110 space = isl_set_get_space(set);
5111 space = isl_space_from_range(space);
5112 set = isl_set_reset_space(set, space);
5113 return (struct isl_map *)set;
5116 /* Create a relation with the given set as domain.
5117 * The range of the created relation is a zero-dimensional
5118 * flat anonymous space.
5120 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5122 return isl_map_reverse(isl_map_from_range(set));
5125 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5126 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5128 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5131 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5132 __isl_take isl_set *range)
5134 return isl_map_apply_range(isl_map_reverse(domain), range);
5137 struct isl_set *isl_set_from_map(struct isl_map *map)
5139 int i;
5140 struct isl_set *set = NULL;
5142 if (!map)
5143 return NULL;
5144 map = isl_map_cow(map);
5145 if (!map)
5146 return NULL;
5147 map->dim = isl_space_as_set_space(map->dim);
5148 if (!map->dim)
5149 goto error;
5150 set = (struct isl_set *)map;
5151 for (i = 0; i < map->n; ++i) {
5152 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
5153 if (!set->p[i])
5154 goto error;
5156 return set;
5157 error:
5158 isl_map_free(map);
5159 return NULL;
5162 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5163 unsigned flags)
5165 struct isl_map *map;
5167 if (!dim)
5168 return NULL;
5169 if (n < 0)
5170 isl_die(dim->ctx, isl_error_internal,
5171 "negative number of basic maps", goto error);
5172 map = isl_alloc(dim->ctx, struct isl_map,
5173 sizeof(struct isl_map) +
5174 (n - 1) * sizeof(struct isl_basic_map *));
5175 if (!map)
5176 goto error;
5178 map->ctx = dim->ctx;
5179 isl_ctx_ref(map->ctx);
5180 map->ref = 1;
5181 map->size = n;
5182 map->n = 0;
5183 map->dim = dim;
5184 map->flags = flags;
5185 return map;
5186 error:
5187 isl_space_free(dim);
5188 return NULL;
5191 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5192 unsigned nparam, unsigned in, unsigned out, int n,
5193 unsigned flags)
5195 struct isl_map *map;
5196 isl_space *dims;
5198 dims = isl_space_alloc(ctx, nparam, in, out);
5199 if (!dims)
5200 return NULL;
5202 map = isl_map_alloc_space(dims, n, flags);
5203 return map;
5206 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5208 struct isl_basic_map *bmap;
5209 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5210 bmap = isl_basic_map_set_to_empty(bmap);
5211 return bmap;
5214 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5216 struct isl_basic_set *bset;
5217 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5218 bset = isl_basic_set_set_to_empty(bset);
5219 return bset;
5222 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5224 struct isl_basic_map *bmap;
5225 if (!model)
5226 return NULL;
5227 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5228 bmap = isl_basic_map_set_to_empty(bmap);
5229 return bmap;
5232 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5234 struct isl_basic_map *bmap;
5235 if (!model)
5236 return NULL;
5237 bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5238 bmap = isl_basic_map_set_to_empty(bmap);
5239 return bmap;
5242 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5244 struct isl_basic_set *bset;
5245 if (!model)
5246 return NULL;
5247 bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5248 bset = isl_basic_set_set_to_empty(bset);
5249 return bset;
5252 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5254 struct isl_basic_map *bmap;
5255 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5256 bmap = isl_basic_map_finalize(bmap);
5257 return bmap;
5260 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5262 struct isl_basic_set *bset;
5263 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5264 bset = isl_basic_set_finalize(bset);
5265 return bset;
5268 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5270 int i;
5271 unsigned total = isl_space_dim(dim, isl_dim_all);
5272 isl_basic_map *bmap;
5274 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5275 for (i = 0; i < total; ++i) {
5276 int k = isl_basic_map_alloc_inequality(bmap);
5277 if (k < 0)
5278 goto error;
5279 isl_seq_clr(bmap->ineq[k], 1 + total);
5280 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5282 return bmap;
5283 error:
5284 isl_basic_map_free(bmap);
5285 return NULL;
5288 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5290 return isl_basic_map_nat_universe(dim);
5293 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5295 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5298 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5300 return isl_map_nat_universe(dim);
5303 __isl_give isl_basic_map *isl_basic_map_universe_like(
5304 __isl_keep isl_basic_map *model)
5306 if (!model)
5307 return NULL;
5308 return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5311 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5313 if (!model)
5314 return NULL;
5315 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5318 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5319 __isl_keep isl_set *model)
5321 if (!model)
5322 return NULL;
5323 return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5326 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5328 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5331 struct isl_map *isl_map_empty_like(struct isl_map *model)
5333 if (!model)
5334 return NULL;
5335 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5338 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5340 if (!model)
5341 return NULL;
5342 return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5345 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5347 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5350 struct isl_set *isl_set_empty_like(struct isl_set *model)
5352 if (!model)
5353 return NULL;
5354 return isl_set_empty(isl_space_copy(model->dim));
5357 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5359 struct isl_map *map;
5360 if (!dim)
5361 return NULL;
5362 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5363 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5364 return map;
5367 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5369 struct isl_set *set;
5370 if (!dim)
5371 return NULL;
5372 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5373 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5374 return set;
5377 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5379 if (!model)
5380 return NULL;
5381 return isl_set_universe(isl_space_copy(model->dim));
5384 struct isl_map *isl_map_dup(struct isl_map *map)
5386 int i;
5387 struct isl_map *dup;
5389 if (!map)
5390 return NULL;
5391 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5392 for (i = 0; i < map->n; ++i)
5393 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5394 return dup;
5397 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5398 __isl_take isl_basic_map *bmap)
5400 if (!bmap || !map)
5401 goto error;
5402 if (isl_basic_map_plain_is_empty(bmap)) {
5403 isl_basic_map_free(bmap);
5404 return map;
5406 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5407 isl_assert(map->ctx, map->n < map->size, goto error);
5408 map->p[map->n] = bmap;
5409 map->n++;
5410 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5411 return map;
5412 error:
5413 if (map)
5414 isl_map_free(map);
5415 if (bmap)
5416 isl_basic_map_free(bmap);
5417 return NULL;
5420 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5422 int i;
5424 if (!map)
5425 return NULL;
5427 if (--map->ref > 0)
5428 return NULL;
5430 isl_ctx_deref(map->ctx);
5431 for (i = 0; i < map->n; ++i)
5432 isl_basic_map_free(map->p[i]);
5433 isl_space_free(map->dim);
5434 free(map);
5436 return NULL;
5439 struct isl_map *isl_map_extend(struct isl_map *base,
5440 unsigned nparam, unsigned n_in, unsigned n_out)
5442 int i;
5444 base = isl_map_cow(base);
5445 if (!base)
5446 return NULL;
5448 base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5449 if (!base->dim)
5450 goto error;
5451 for (i = 0; i < base->n; ++i) {
5452 base->p[i] = isl_basic_map_extend_space(base->p[i],
5453 isl_space_copy(base->dim), 0, 0, 0);
5454 if (!base->p[i])
5455 goto error;
5457 return base;
5458 error:
5459 isl_map_free(base);
5460 return NULL;
5463 struct isl_set *isl_set_extend(struct isl_set *base,
5464 unsigned nparam, unsigned dim)
5466 return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5467 nparam, 0, dim);
5470 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5471 struct isl_basic_map *bmap, unsigned pos, int value)
5473 int j;
5475 bmap = isl_basic_map_cow(bmap);
5476 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5477 j = isl_basic_map_alloc_equality(bmap);
5478 if (j < 0)
5479 goto error;
5480 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5481 isl_int_set_si(bmap->eq[j][pos], -1);
5482 isl_int_set_si(bmap->eq[j][0], value);
5483 bmap = isl_basic_map_simplify(bmap);
5484 return isl_basic_map_finalize(bmap);
5485 error:
5486 isl_basic_map_free(bmap);
5487 return NULL;
5490 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5491 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5493 int j;
5495 bmap = isl_basic_map_cow(bmap);
5496 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5497 j = isl_basic_map_alloc_equality(bmap);
5498 if (j < 0)
5499 goto error;
5500 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5501 isl_int_set_si(bmap->eq[j][pos], -1);
5502 isl_int_set(bmap->eq[j][0], value);
5503 bmap = isl_basic_map_simplify(bmap);
5504 return isl_basic_map_finalize(bmap);
5505 error:
5506 isl_basic_map_free(bmap);
5507 return NULL;
5510 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5511 enum isl_dim_type type, unsigned pos, int value)
5513 if (!bmap)
5514 return NULL;
5515 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5516 return isl_basic_map_fix_pos_si(bmap,
5517 isl_basic_map_offset(bmap, type) + pos, value);
5518 error:
5519 isl_basic_map_free(bmap);
5520 return NULL;
5523 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5524 enum isl_dim_type type, unsigned pos, isl_int value)
5526 if (!bmap)
5527 return NULL;
5528 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5529 return isl_basic_map_fix_pos(bmap,
5530 isl_basic_map_offset(bmap, type) + pos, value);
5531 error:
5532 isl_basic_map_free(bmap);
5533 return NULL;
5536 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5537 * to be equal to "v".
5539 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5540 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5542 if (!bmap || !v)
5543 goto error;
5544 if (!isl_val_is_int(v))
5545 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5546 "expecting integer value", goto error);
5547 if (pos >= isl_basic_map_dim(bmap, type))
5548 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5549 "index out of bounds", goto error);
5550 pos += isl_basic_map_offset(bmap, type);
5551 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5552 isl_val_free(v);
5553 return bmap;
5554 error:
5555 isl_basic_map_free(bmap);
5556 isl_val_free(v);
5557 return NULL;
5560 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5561 * to be equal to "v".
5563 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5564 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5566 return isl_basic_map_fix_val(bset, type, pos, v);
5569 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5570 enum isl_dim_type type, unsigned pos, int value)
5572 return (struct isl_basic_set *)
5573 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5574 type, pos, value);
5577 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5578 enum isl_dim_type type, unsigned pos, isl_int value)
5580 return (struct isl_basic_set *)
5581 isl_basic_map_fix((struct isl_basic_map *)bset,
5582 type, pos, value);
5585 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5586 unsigned input, int value)
5588 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5591 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5592 unsigned dim, int value)
5594 return (struct isl_basic_set *)
5595 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5596 isl_dim_set, dim, value);
5599 static int remove_if_empty(__isl_keep isl_map *map, int i)
5601 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5603 if (empty < 0)
5604 return -1;
5605 if (!empty)
5606 return 0;
5608 isl_basic_map_free(map->p[i]);
5609 if (i != map->n - 1) {
5610 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5611 map->p[i] = map->p[map->n - 1];
5613 map->n--;
5615 return 0;
5618 /* Perform "fn" on each basic map of "map", where we may not be holding
5619 * the only reference to "map".
5620 * In particular, "fn" should be a semantics preserving operation
5621 * that we want to apply to all copies of "map". We therefore need
5622 * to be careful not to modify "map" in a way that breaks "map"
5623 * in case anything goes wrong.
5625 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5626 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5628 struct isl_basic_map *bmap;
5629 int i;
5631 if (!map)
5632 return NULL;
5634 for (i = map->n - 1; i >= 0; --i) {
5635 bmap = isl_basic_map_copy(map->p[i]);
5636 bmap = fn(bmap);
5637 if (!bmap)
5638 goto error;
5639 isl_basic_map_free(map->p[i]);
5640 map->p[i] = bmap;
5641 if (remove_if_empty(map, i) < 0)
5642 goto error;
5645 return map;
5646 error:
5647 isl_map_free(map);
5648 return NULL;
5651 struct isl_map *isl_map_fix_si(struct isl_map *map,
5652 enum isl_dim_type type, unsigned pos, int value)
5654 int i;
5656 map = isl_map_cow(map);
5657 if (!map)
5658 return NULL;
5660 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5661 for (i = map->n - 1; i >= 0; --i) {
5662 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5663 if (remove_if_empty(map, i) < 0)
5664 goto error;
5666 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5667 return map;
5668 error:
5669 isl_map_free(map);
5670 return NULL;
5673 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5674 enum isl_dim_type type, unsigned pos, int value)
5676 return (struct isl_set *)
5677 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5680 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5681 enum isl_dim_type type, unsigned pos, isl_int value)
5683 int i;
5685 map = isl_map_cow(map);
5686 if (!map)
5687 return NULL;
5689 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5690 for (i = 0; i < map->n; ++i) {
5691 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5692 if (!map->p[i])
5693 goto error;
5695 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5696 return map;
5697 error:
5698 isl_map_free(map);
5699 return NULL;
5702 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5703 enum isl_dim_type type, unsigned pos, isl_int value)
5705 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5708 /* Fix the value of the variable at position "pos" of type "type" of "map"
5709 * to be equal to "v".
5711 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5712 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5714 int i;
5716 map = isl_map_cow(map);
5717 if (!map || !v)
5718 goto error;
5720 if (!isl_val_is_int(v))
5721 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5722 "expecting integer value", goto error);
5723 if (pos >= isl_map_dim(map, type))
5724 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5725 "index out of bounds", goto error);
5726 for (i = map->n - 1; i >= 0; --i) {
5727 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5728 isl_val_copy(v));
5729 if (remove_if_empty(map, i) < 0)
5730 goto error;
5732 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5733 isl_val_free(v);
5734 return map;
5735 error:
5736 isl_map_free(map);
5737 isl_val_free(v);
5738 return NULL;
5741 /* Fix the value of the variable at position "pos" of type "type" of "set"
5742 * to be equal to "v".
5744 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5745 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5747 return isl_map_fix_val(set, type, pos, v);
5750 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5751 unsigned input, int value)
5753 return isl_map_fix_si(map, isl_dim_in, input, value);
5756 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5758 return (struct isl_set *)
5759 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5762 static __isl_give isl_basic_map *basic_map_bound_si(
5763 __isl_take isl_basic_map *bmap,
5764 enum isl_dim_type type, unsigned pos, int value, int upper)
5766 int j;
5768 if (!bmap)
5769 return NULL;
5770 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5771 pos += isl_basic_map_offset(bmap, type);
5772 bmap = isl_basic_map_cow(bmap);
5773 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5774 j = isl_basic_map_alloc_inequality(bmap);
5775 if (j < 0)
5776 goto error;
5777 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5778 if (upper) {
5779 isl_int_set_si(bmap->ineq[j][pos], -1);
5780 isl_int_set_si(bmap->ineq[j][0], value);
5781 } else {
5782 isl_int_set_si(bmap->ineq[j][pos], 1);
5783 isl_int_set_si(bmap->ineq[j][0], -value);
5785 bmap = isl_basic_map_simplify(bmap);
5786 return isl_basic_map_finalize(bmap);
5787 error:
5788 isl_basic_map_free(bmap);
5789 return NULL;
5792 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5793 __isl_take isl_basic_map *bmap,
5794 enum isl_dim_type type, unsigned pos, int value)
5796 return basic_map_bound_si(bmap, type, pos, value, 0);
5799 /* Constrain the values of the given dimension to be no greater than "value".
5801 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5802 __isl_take isl_basic_map *bmap,
5803 enum isl_dim_type type, unsigned pos, int value)
5805 return basic_map_bound_si(bmap, type, pos, value, 1);
5808 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5809 unsigned dim, isl_int value)
5811 int j;
5813 bset = isl_basic_set_cow(bset);
5814 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5815 j = isl_basic_set_alloc_inequality(bset);
5816 if (j < 0)
5817 goto error;
5818 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5819 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5820 isl_int_neg(bset->ineq[j][0], value);
5821 bset = isl_basic_set_simplify(bset);
5822 return isl_basic_set_finalize(bset);
5823 error:
5824 isl_basic_set_free(bset);
5825 return NULL;
5828 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5829 enum isl_dim_type type, unsigned pos, int value, int upper)
5831 int i;
5833 map = isl_map_cow(map);
5834 if (!map)
5835 return NULL;
5837 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5838 for (i = 0; i < map->n; ++i) {
5839 map->p[i] = basic_map_bound_si(map->p[i],
5840 type, pos, value, upper);
5841 if (!map->p[i])
5842 goto error;
5844 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5845 return map;
5846 error:
5847 isl_map_free(map);
5848 return NULL;
5851 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5852 enum isl_dim_type type, unsigned pos, int value)
5854 return map_bound_si(map, type, pos, value, 0);
5857 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5858 enum isl_dim_type type, unsigned pos, int value)
5860 return map_bound_si(map, type, pos, value, 1);
5863 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5864 enum isl_dim_type type, unsigned pos, int value)
5866 return (struct isl_set *)
5867 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5870 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5871 enum isl_dim_type type, unsigned pos, int value)
5873 return isl_map_upper_bound_si(set, type, pos, value);
5876 /* Bound the given variable of "bmap" from below (or above is "upper"
5877 * is set) to "value".
5879 static __isl_give isl_basic_map *basic_map_bound(
5880 __isl_take isl_basic_map *bmap,
5881 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5883 int j;
5885 if (!bmap)
5886 return NULL;
5887 if (pos >= isl_basic_map_dim(bmap, type))
5888 isl_die(bmap->ctx, isl_error_invalid,
5889 "index out of bounds", goto error);
5890 pos += isl_basic_map_offset(bmap, type);
5891 bmap = isl_basic_map_cow(bmap);
5892 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5893 j = isl_basic_map_alloc_inequality(bmap);
5894 if (j < 0)
5895 goto error;
5896 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5897 if (upper) {
5898 isl_int_set_si(bmap->ineq[j][pos], -1);
5899 isl_int_set(bmap->ineq[j][0], value);
5900 } else {
5901 isl_int_set_si(bmap->ineq[j][pos], 1);
5902 isl_int_neg(bmap->ineq[j][0], value);
5904 bmap = isl_basic_map_simplify(bmap);
5905 return isl_basic_map_finalize(bmap);
5906 error:
5907 isl_basic_map_free(bmap);
5908 return NULL;
5911 /* Bound the given variable of "map" from below (or above is "upper"
5912 * is set) to "value".
5914 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5915 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5917 int i;
5919 map = isl_map_cow(map);
5920 if (!map)
5921 return NULL;
5923 if (pos >= isl_map_dim(map, type))
5924 isl_die(map->ctx, isl_error_invalid,
5925 "index out of bounds", goto error);
5926 for (i = map->n - 1; i >= 0; --i) {
5927 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5928 if (remove_if_empty(map, i) < 0)
5929 goto error;
5931 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5932 return map;
5933 error:
5934 isl_map_free(map);
5935 return NULL;
5938 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5939 enum isl_dim_type type, unsigned pos, isl_int value)
5941 return map_bound(map, type, pos, value, 0);
5944 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5945 enum isl_dim_type type, unsigned pos, isl_int value)
5947 return map_bound(map, type, pos, value, 1);
5950 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5951 enum isl_dim_type type, unsigned pos, isl_int value)
5953 return isl_map_lower_bound(set, type, pos, value);
5956 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5957 enum isl_dim_type type, unsigned pos, isl_int value)
5959 return isl_map_upper_bound(set, type, pos, value);
5962 /* Force the values of the variable at position "pos" of type "type" of "set"
5963 * to be no smaller than "value".
5965 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5966 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5968 if (!value)
5969 goto error;
5970 if (!isl_val_is_int(value))
5971 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5972 "expecting integer value", goto error);
5973 set = isl_set_lower_bound(set, type, pos, value->n);
5974 isl_val_free(value);
5975 return set;
5976 error:
5977 isl_val_free(value);
5978 isl_set_free(set);
5979 return NULL;
5982 /* Force the values of the variable at position "pos" of type "type" of "set"
5983 * to be no greater than "value".
5985 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5986 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5988 if (!value)
5989 goto error;
5990 if (!isl_val_is_int(value))
5991 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5992 "expecting integer value", goto error);
5993 set = isl_set_upper_bound(set, type, pos, value->n);
5994 isl_val_free(value);
5995 return set;
5996 error:
5997 isl_val_free(value);
5998 isl_set_free(set);
5999 return NULL;
6002 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6003 isl_int value)
6005 int i;
6007 set = isl_set_cow(set);
6008 if (!set)
6009 return NULL;
6011 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6012 for (i = 0; i < set->n; ++i) {
6013 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6014 if (!set->p[i])
6015 goto error;
6017 return set;
6018 error:
6019 isl_set_free(set);
6020 return NULL;
6023 struct isl_map *isl_map_reverse(struct isl_map *map)
6025 int i;
6027 map = isl_map_cow(map);
6028 if (!map)
6029 return NULL;
6031 map->dim = isl_space_reverse(map->dim);
6032 if (!map->dim)
6033 goto error;
6034 for (i = 0; i < map->n; ++i) {
6035 map->p[i] = isl_basic_map_reverse(map->p[i]);
6036 if (!map->p[i])
6037 goto error;
6039 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6040 return map;
6041 error:
6042 isl_map_free(map);
6043 return NULL;
6046 static struct isl_map *isl_basic_map_partial_lexopt(
6047 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6048 struct isl_set **empty, int max)
6050 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6053 struct isl_map *isl_basic_map_partial_lexmax(
6054 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6055 struct isl_set **empty)
6057 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6060 struct isl_map *isl_basic_map_partial_lexmin(
6061 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6062 struct isl_set **empty)
6064 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6067 struct isl_set *isl_basic_set_partial_lexmin(
6068 struct isl_basic_set *bset, struct isl_basic_set *dom,
6069 struct isl_set **empty)
6071 return (struct isl_set *)
6072 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6073 dom, empty);
6076 struct isl_set *isl_basic_set_partial_lexmax(
6077 struct isl_basic_set *bset, struct isl_basic_set *dom,
6078 struct isl_set **empty)
6080 return (struct isl_set *)
6081 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6082 dom, empty);
6085 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6086 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6087 __isl_give isl_set **empty)
6089 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6092 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6093 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6094 __isl_give isl_set **empty)
6096 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6099 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6100 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6101 __isl_give isl_set **empty)
6103 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6106 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6107 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6108 __isl_give isl_set **empty)
6110 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6113 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6114 __isl_take isl_basic_map *bmap, int max)
6116 isl_basic_set *dom = NULL;
6117 isl_space *dom_space;
6119 if (!bmap)
6120 goto error;
6121 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6122 dom = isl_basic_set_universe(dom_space);
6123 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6124 error:
6125 isl_basic_map_free(bmap);
6126 return NULL;
6129 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6130 __isl_take isl_basic_map *bmap)
6132 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6135 #undef TYPE
6136 #define TYPE isl_pw_multi_aff
6137 #undef SUFFIX
6138 #define SUFFIX _pw_multi_aff
6139 #undef EMPTY
6140 #define EMPTY isl_pw_multi_aff_empty
6141 #undef ADD
6142 #define ADD isl_pw_multi_aff_union_add
6143 #include "isl_map_lexopt_templ.c"
6145 /* Given a map "map", compute the lexicographically minimal
6146 * (or maximal) image element for each domain element in dom,
6147 * in the form of an isl_pw_multi_aff.
6148 * Set *empty to those elements in dom that do not have an image element.
6150 * We first compute the lexicographically minimal or maximal element
6151 * in the first basic map. This results in a partial solution "res"
6152 * and a subset "todo" of dom that still need to be handled.
6153 * We then consider each of the remaining maps in "map" and successively
6154 * update both "res" and "todo".
6156 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6157 __isl_take isl_map *map, __isl_take isl_set *dom,
6158 __isl_give isl_set **empty, int max)
6160 int i;
6161 isl_pw_multi_aff *res;
6162 isl_set *todo;
6164 if (!map || !dom)
6165 goto error;
6167 if (isl_map_plain_is_empty(map)) {
6168 if (empty)
6169 *empty = dom;
6170 else
6171 isl_set_free(dom);
6172 return isl_pw_multi_aff_from_map(map);
6175 res = basic_map_partial_lexopt_pw_multi_aff(
6176 isl_basic_map_copy(map->p[0]),
6177 isl_set_copy(dom), &todo, max);
6179 for (i = 1; i < map->n; ++i) {
6180 isl_pw_multi_aff *res_i;
6181 isl_set *todo_i;
6183 res_i = basic_map_partial_lexopt_pw_multi_aff(
6184 isl_basic_map_copy(map->p[i]),
6185 isl_set_copy(dom), &todo_i, max);
6187 if (max)
6188 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6189 else
6190 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6192 todo = isl_set_intersect(todo, todo_i);
6195 isl_set_free(dom);
6196 isl_map_free(map);
6198 if (empty)
6199 *empty = todo;
6200 else
6201 isl_set_free(todo);
6203 return res;
6204 error:
6205 if (empty)
6206 *empty = NULL;
6207 isl_set_free(dom);
6208 isl_map_free(map);
6209 return NULL;
6212 #undef TYPE
6213 #define TYPE isl_map
6214 #undef SUFFIX
6215 #define SUFFIX
6216 #undef EMPTY
6217 #define EMPTY isl_map_empty
6218 #undef ADD
6219 #define ADD isl_map_union_disjoint
6220 #include "isl_map_lexopt_templ.c"
6222 /* Given a map "map", compute the lexicographically minimal
6223 * (or maximal) image element for each domain element in dom.
6224 * Set *empty to those elements in dom that do not have an image element.
6226 * We first compute the lexicographically minimal or maximal element
6227 * in the first basic map. This results in a partial solution "res"
6228 * and a subset "todo" of dom that still need to be handled.
6229 * We then consider each of the remaining maps in "map" and successively
6230 * update both "res" and "todo".
6232 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6233 * Assume we are computing the lexicographical maximum.
6234 * We first compute the lexicographically maximal element in basic map i.
6235 * This results in a partial solution res_i and a subset todo_i.
6236 * Then we combine these results with those obtain for the first k basic maps
6237 * to obtain a result that is valid for the first k+1 basic maps.
6238 * In particular, the set where there is no solution is the set where
6239 * there is no solution for the first k basic maps and also no solution
6240 * for the ith basic map, i.e.,
6242 * todo^i = todo^k * todo_i
6244 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6245 * solutions, arbitrarily breaking ties in favor of res^k.
6246 * That is, when res^k(a) >= res_i(a), we pick res^k and
6247 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6248 * the lexicographic order.)
6249 * In practice, we compute
6251 * res^k * (res_i . "<=")
6253 * and
6255 * res_i * (res^k . "<")
6257 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6258 * where only one of res^k and res_i provides a solution and we simply pick
6259 * that one, i.e.,
6261 * res^k * todo_i
6262 * and
6263 * res_i * todo^k
6265 * Note that we only compute these intersections when dom(res^k) intersects
6266 * dom(res_i). Otherwise, the only effect of these intersections is to
6267 * potentially break up res^k and res_i into smaller pieces.
6268 * We want to avoid such splintering as much as possible.
6269 * In fact, an earlier implementation of this function would look for
6270 * better results in the domain of res^k and for extra results in todo^k,
6271 * but this would always result in a splintering according to todo^k,
6272 * even when the domain of basic map i is disjoint from the domains of
6273 * the previous basic maps.
6275 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6276 __isl_take isl_map *map, __isl_take isl_set *dom,
6277 __isl_give isl_set **empty, int max)
6279 int i;
6280 struct isl_map *res;
6281 struct isl_set *todo;
6283 if (!map || !dom)
6284 goto error;
6286 if (isl_map_plain_is_empty(map)) {
6287 if (empty)
6288 *empty = dom;
6289 else
6290 isl_set_free(dom);
6291 return map;
6294 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6295 isl_set_copy(dom), &todo, max);
6297 for (i = 1; i < map->n; ++i) {
6298 isl_map *lt, *le;
6299 isl_map *res_i;
6300 isl_set *todo_i;
6301 isl_space *dim = isl_space_range(isl_map_get_space(res));
6303 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6304 isl_set_copy(dom), &todo_i, max);
6306 if (max) {
6307 lt = isl_map_lex_lt(isl_space_copy(dim));
6308 le = isl_map_lex_le(dim);
6309 } else {
6310 lt = isl_map_lex_gt(isl_space_copy(dim));
6311 le = isl_map_lex_ge(dim);
6313 lt = isl_map_apply_range(isl_map_copy(res), lt);
6314 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6315 le = isl_map_apply_range(isl_map_copy(res_i), le);
6316 le = isl_map_intersect(le, isl_map_copy(res));
6318 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6319 res = isl_map_intersect_domain(res,
6320 isl_set_copy(todo_i));
6321 res_i = isl_map_intersect_domain(res_i,
6322 isl_set_copy(todo));
6325 res = isl_map_union_disjoint(res, res_i);
6326 res = isl_map_union_disjoint(res, lt);
6327 res = isl_map_union_disjoint(res, le);
6329 todo = isl_set_intersect(todo, todo_i);
6332 isl_set_free(dom);
6333 isl_map_free(map);
6335 if (empty)
6336 *empty = todo;
6337 else
6338 isl_set_free(todo);
6340 return res;
6341 error:
6342 if (empty)
6343 *empty = NULL;
6344 isl_set_free(dom);
6345 isl_map_free(map);
6346 return NULL;
6349 __isl_give isl_map *isl_map_partial_lexmax(
6350 __isl_take isl_map *map, __isl_take isl_set *dom,
6351 __isl_give isl_set **empty)
6353 return isl_map_partial_lexopt(map, dom, empty, 1);
6356 __isl_give isl_map *isl_map_partial_lexmin(
6357 __isl_take isl_map *map, __isl_take isl_set *dom,
6358 __isl_give isl_set **empty)
6360 return isl_map_partial_lexopt(map, dom, empty, 0);
6363 __isl_give isl_set *isl_set_partial_lexmin(
6364 __isl_take isl_set *set, __isl_take isl_set *dom,
6365 __isl_give isl_set **empty)
6367 return (struct isl_set *)
6368 isl_map_partial_lexmin((struct isl_map *)set,
6369 dom, empty);
6372 __isl_give isl_set *isl_set_partial_lexmax(
6373 __isl_take isl_set *set, __isl_take isl_set *dom,
6374 __isl_give isl_set **empty)
6376 return (struct isl_set *)
6377 isl_map_partial_lexmax((struct isl_map *)set,
6378 dom, empty);
6381 /* Compute the lexicographic minimum (or maximum if "max" is set)
6382 * of "bmap" over its domain.
6384 * Since we are not interested in the part of the domain space where
6385 * there is no solution, we initialize the domain to those constraints
6386 * of "bmap" that only involve the parameters and the input dimensions.
6387 * This relieves the parametric programming engine from detecting those
6388 * inequalities and transferring them to the context. More importantly,
6389 * it ensures that those inequalities are transferred first and not
6390 * intermixed with inequalities that actually split the domain.
6392 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6394 int n_div;
6395 int n_out;
6396 isl_basic_map *copy;
6397 isl_basic_set *dom;
6399 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6400 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6401 copy = isl_basic_map_copy(bmap);
6402 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6403 isl_dim_div, 0, n_div);
6404 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6405 isl_dim_out, 0, n_out);
6406 dom = isl_basic_map_domain(copy);
6407 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6410 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6412 return isl_basic_map_lexopt(bmap, 0);
6415 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6417 return isl_basic_map_lexopt(bmap, 1);
6420 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6422 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6425 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6427 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6430 /* Extract the first and only affine expression from list
6431 * and then add it to *pwaff with the given dom.
6432 * This domain is known to be disjoint from other domains
6433 * because of the way isl_basic_map_foreach_lexmax works.
6435 static int update_dim_opt(__isl_take isl_basic_set *dom,
6436 __isl_take isl_aff_list *list, void *user)
6438 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6439 isl_aff *aff;
6440 isl_pw_aff **pwaff = user;
6441 isl_pw_aff *pwaff_i;
6443 if (!list)
6444 goto error;
6445 if (isl_aff_list_n_aff(list) != 1)
6446 isl_die(ctx, isl_error_internal,
6447 "expecting single element list", goto error);
6449 aff = isl_aff_list_get_aff(list, 0);
6450 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6452 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6454 isl_aff_list_free(list);
6456 return 0;
6457 error:
6458 isl_basic_set_free(dom);
6459 isl_aff_list_free(list);
6460 return -1;
6463 /* Given a basic map with one output dimension, compute the minimum or
6464 * maximum of that dimension as an isl_pw_aff.
6466 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6467 * call update_dim_opt on each leaf of the result.
6469 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6470 int max)
6472 isl_space *dim = isl_basic_map_get_space(bmap);
6473 isl_pw_aff *pwaff;
6474 int r;
6476 dim = isl_space_from_domain(isl_space_domain(dim));
6477 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6478 pwaff = isl_pw_aff_empty(dim);
6480 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6481 if (r < 0)
6482 return isl_pw_aff_free(pwaff);
6484 return pwaff;
6487 /* Compute the minimum or maximum of the given output dimension
6488 * as a function of the parameters and the input dimensions,
6489 * but independently of the other output dimensions.
6491 * We first project out the other output dimension and then compute
6492 * the "lexicographic" maximum in each basic map, combining the results
6493 * using isl_pw_aff_union_max.
6495 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6496 int max)
6498 int i;
6499 isl_pw_aff *pwaff;
6500 unsigned n_out;
6502 n_out = isl_map_dim(map, isl_dim_out);
6503 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6504 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6505 if (!map)
6506 return NULL;
6508 if (map->n == 0) {
6509 isl_space *dim = isl_map_get_space(map);
6510 isl_map_free(map);
6511 return isl_pw_aff_empty(dim);
6514 pwaff = basic_map_dim_opt(map->p[0], max);
6515 for (i = 1; i < map->n; ++i) {
6516 isl_pw_aff *pwaff_i;
6518 pwaff_i = basic_map_dim_opt(map->p[i], max);
6519 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6522 isl_map_free(map);
6524 return pwaff;
6527 /* Compute the maximum of the given output dimension as a function of the
6528 * parameters and input dimensions, but independently of
6529 * the other output dimensions.
6531 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6533 return map_dim_opt(map, pos, 1);
6536 /* Compute the minimum or maximum of the given set dimension
6537 * as a function of the parameters,
6538 * but independently of the other set dimensions.
6540 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6541 int max)
6543 return map_dim_opt(set, pos, max);
6546 /* Compute the maximum of the given set dimension as a function of the
6547 * parameters, but independently of the other set dimensions.
6549 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6551 return set_dim_opt(set, pos, 1);
6554 /* Compute the minimum of the given set dimension as a function of the
6555 * parameters, but independently of the other set dimensions.
6557 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6559 return set_dim_opt(set, pos, 0);
6562 /* Apply a preimage specified by "mat" on the parameters of "bset".
6563 * bset is assumed to have only parameters and divs.
6565 static struct isl_basic_set *basic_set_parameter_preimage(
6566 struct isl_basic_set *bset, struct isl_mat *mat)
6568 unsigned nparam;
6570 if (!bset || !mat)
6571 goto error;
6573 bset->dim = isl_space_cow(bset->dim);
6574 if (!bset->dim)
6575 goto error;
6577 nparam = isl_basic_set_dim(bset, isl_dim_param);
6579 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6581 bset->dim->nparam = 0;
6582 bset->dim->n_out = nparam;
6583 bset = isl_basic_set_preimage(bset, mat);
6584 if (bset) {
6585 bset->dim->nparam = bset->dim->n_out;
6586 bset->dim->n_out = 0;
6588 return bset;
6589 error:
6590 isl_mat_free(mat);
6591 isl_basic_set_free(bset);
6592 return NULL;
6595 /* Apply a preimage specified by "mat" on the parameters of "set".
6596 * set is assumed to have only parameters and divs.
6598 static struct isl_set *set_parameter_preimage(
6599 struct isl_set *set, struct isl_mat *mat)
6601 isl_space *dim = NULL;
6602 unsigned nparam;
6604 if (!set || !mat)
6605 goto error;
6607 dim = isl_space_copy(set->dim);
6608 dim = isl_space_cow(dim);
6609 if (!dim)
6610 goto error;
6612 nparam = isl_set_dim(set, isl_dim_param);
6614 isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6616 dim->nparam = 0;
6617 dim->n_out = nparam;
6618 isl_set_reset_space(set, dim);
6619 set = isl_set_preimage(set, mat);
6620 if (!set)
6621 goto error2;
6622 dim = isl_space_copy(set->dim);
6623 dim = isl_space_cow(dim);
6624 if (!dim)
6625 goto error2;
6626 dim->nparam = dim->n_out;
6627 dim->n_out = 0;
6628 isl_set_reset_space(set, dim);
6629 return set;
6630 error:
6631 isl_space_free(dim);
6632 isl_mat_free(mat);
6633 error2:
6634 isl_set_free(set);
6635 return NULL;
6638 /* Intersect the basic set "bset" with the affine space specified by the
6639 * equalities in "eq".
6641 static struct isl_basic_set *basic_set_append_equalities(
6642 struct isl_basic_set *bset, struct isl_mat *eq)
6644 int i, k;
6645 unsigned len;
6647 if (!bset || !eq)
6648 goto error;
6650 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6651 eq->n_row, 0);
6652 if (!bset)
6653 goto error;
6655 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6656 for (i = 0; i < eq->n_row; ++i) {
6657 k = isl_basic_set_alloc_equality(bset);
6658 if (k < 0)
6659 goto error;
6660 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6661 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6663 isl_mat_free(eq);
6665 bset = isl_basic_set_gauss(bset, NULL);
6666 bset = isl_basic_set_finalize(bset);
6668 return bset;
6669 error:
6670 isl_mat_free(eq);
6671 isl_basic_set_free(bset);
6672 return NULL;
6675 /* Intersect the set "set" with the affine space specified by the
6676 * equalities in "eq".
6678 static struct isl_set *set_append_equalities(struct isl_set *set,
6679 struct isl_mat *eq)
6681 int i;
6683 if (!set || !eq)
6684 goto error;
6686 for (i = 0; i < set->n; ++i) {
6687 set->p[i] = basic_set_append_equalities(set->p[i],
6688 isl_mat_copy(eq));
6689 if (!set->p[i])
6690 goto error;
6692 isl_mat_free(eq);
6693 return set;
6694 error:
6695 isl_mat_free(eq);
6696 isl_set_free(set);
6697 return NULL;
6700 /* Given a basic set "bset" that only involves parameters and existentially
6701 * quantified variables, return the index of the first equality
6702 * that only involves parameters. If there is no such equality then
6703 * return bset->n_eq.
6705 * This function assumes that isl_basic_set_gauss has been called on "bset".
6707 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6709 int i, j;
6710 unsigned nparam, n_div;
6712 if (!bset)
6713 return -1;
6715 nparam = isl_basic_set_dim(bset, isl_dim_param);
6716 n_div = isl_basic_set_dim(bset, isl_dim_div);
6718 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6719 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6720 ++i;
6723 return i;
6726 /* Compute an explicit representation for the existentially quantified
6727 * variables in "bset" by computing the "minimal value" of the set
6728 * variables. Since there are no set variables, the computation of
6729 * the minimal value essentially computes an explicit representation
6730 * of the non-empty part(s) of "bset".
6732 * The input only involves parameters and existentially quantified variables.
6733 * All equalities among parameters have been removed.
6735 * Since the existentially quantified variables in the result are in general
6736 * going to be different from those in the input, we first replace
6737 * them by the minimal number of variables based on their equalities.
6738 * This should simplify the parametric integer programming.
6740 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6742 isl_morph *morph1, *morph2;
6743 isl_set *set;
6744 unsigned n;
6746 if (!bset)
6747 return NULL;
6748 if (bset->n_eq == 0)
6749 return isl_basic_set_lexmin(bset);
6751 morph1 = isl_basic_set_parameter_compression(bset);
6752 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6753 bset = isl_basic_set_lift(bset);
6754 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6755 bset = isl_morph_basic_set(morph2, bset);
6756 n = isl_basic_set_dim(bset, isl_dim_set);
6757 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6759 set = isl_basic_set_lexmin(bset);
6761 set = isl_morph_set(isl_morph_inverse(morph1), set);
6763 return set;
6766 /* Project the given basic set onto its parameter domain, possibly introducing
6767 * new, explicit, existential variables in the constraints.
6768 * The input has parameters and (possibly implicit) existential variables.
6769 * The output has the same parameters, but only
6770 * explicit existentially quantified variables.
6772 * The actual projection is performed by pip, but pip doesn't seem
6773 * to like equalities very much, so we first remove the equalities
6774 * among the parameters by performing a variable compression on
6775 * the parameters. Afterward, an inverse transformation is performed
6776 * and the equalities among the parameters are inserted back in.
6778 * The variable compression on the parameters may uncover additional
6779 * equalities that were only implicit before. We therefore check
6780 * if there are any new parameter equalities in the result and
6781 * if so recurse. The removal of parameter equalities is required
6782 * for the parameter compression performed by base_compute_divs.
6784 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6786 int i;
6787 struct isl_mat *eq;
6788 struct isl_mat *T, *T2;
6789 struct isl_set *set;
6790 unsigned nparam;
6792 bset = isl_basic_set_cow(bset);
6793 if (!bset)
6794 return NULL;
6796 if (bset->n_eq == 0)
6797 return base_compute_divs(bset);
6799 bset = isl_basic_set_gauss(bset, NULL);
6800 if (!bset)
6801 return NULL;
6802 if (isl_basic_set_plain_is_empty(bset))
6803 return isl_set_from_basic_set(bset);
6805 i = first_parameter_equality(bset);
6806 if (i == bset->n_eq)
6807 return base_compute_divs(bset);
6809 nparam = isl_basic_set_dim(bset, isl_dim_param);
6810 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6811 0, 1 + nparam);
6812 eq = isl_mat_cow(eq);
6813 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6814 if (T && T->n_col == 0) {
6815 isl_mat_free(T);
6816 isl_mat_free(T2);
6817 isl_mat_free(eq);
6818 bset = isl_basic_set_set_to_empty(bset);
6819 return isl_set_from_basic_set(bset);
6821 bset = basic_set_parameter_preimage(bset, T);
6823 i = first_parameter_equality(bset);
6824 if (!bset)
6825 set = NULL;
6826 else if (i == bset->n_eq)
6827 set = base_compute_divs(bset);
6828 else
6829 set = parameter_compute_divs(bset);
6830 set = set_parameter_preimage(set, T2);
6831 set = set_append_equalities(set, eq);
6832 return set;
6835 /* Insert the divs from "ls" before those of "bmap".
6837 * The number of columns is not changed, which means that the last
6838 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6839 * The caller is responsible for removing the same number of dimensions
6840 * from the space of "bmap".
6842 static __isl_give isl_basic_map *insert_divs_from_local_space(
6843 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6845 int i;
6846 int n_div;
6847 int old_n_div;
6849 n_div = isl_local_space_dim(ls, isl_dim_div);
6850 if (n_div == 0)
6851 return bmap;
6853 old_n_div = bmap->n_div;
6854 bmap = insert_div_rows(bmap, n_div);
6855 if (!bmap)
6856 return NULL;
6858 for (i = 0; i < n_div; ++i) {
6859 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6860 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6863 return bmap;
6866 /* Replace the space of "bmap" by the space and divs of "ls".
6868 * If "ls" has any divs, then we simplify the result since we may
6869 * have discovered some additional equalities that could simplify
6870 * the div expressions.
6872 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6873 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6875 int n_div;
6877 bmap = isl_basic_map_cow(bmap);
6878 if (!bmap || !ls)
6879 goto error;
6881 n_div = isl_local_space_dim(ls, isl_dim_div);
6882 bmap = insert_divs_from_local_space(bmap, ls);
6883 if (!bmap)
6884 goto error;
6886 isl_space_free(bmap->dim);
6887 bmap->dim = isl_local_space_get_space(ls);
6888 if (!bmap->dim)
6889 goto error;
6891 isl_local_space_free(ls);
6892 if (n_div > 0)
6893 bmap = isl_basic_map_simplify(bmap);
6894 bmap = isl_basic_map_finalize(bmap);
6895 return bmap;
6896 error:
6897 isl_basic_map_free(bmap);
6898 isl_local_space_free(ls);
6899 return NULL;
6902 /* Replace the space of "map" by the space and divs of "ls".
6904 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6905 __isl_take isl_local_space *ls)
6907 int i;
6909 map = isl_map_cow(map);
6910 if (!map || !ls)
6911 goto error;
6913 for (i = 0; i < map->n; ++i) {
6914 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6915 isl_local_space_copy(ls));
6916 if (!map->p[i])
6917 goto error;
6919 isl_space_free(map->dim);
6920 map->dim = isl_local_space_get_space(ls);
6921 if (!map->dim)
6922 goto error;
6924 isl_local_space_free(ls);
6925 return map;
6926 error:
6927 isl_local_space_free(ls);
6928 isl_map_free(map);
6929 return NULL;
6932 /* Compute an explicit representation for the existentially
6933 * quantified variables for which do not know any explicit representation yet.
6935 * We first sort the existentially quantified variables so that the
6936 * existentially quantified variables for which we already have an explicit
6937 * representation are placed before those for which we do not.
6938 * The input dimensions, the output dimensions and the existentially
6939 * quantified variables for which we already have an explicit
6940 * representation are then turned into parameters.
6941 * compute_divs returns a map with the same parameters and
6942 * no input or output dimensions and the dimension specification
6943 * is reset to that of the input, including the existentially quantified
6944 * variables for which we already had an explicit representation.
6946 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6948 struct isl_basic_set *bset;
6949 struct isl_set *set;
6950 struct isl_map *map;
6951 isl_space *dim;
6952 isl_local_space *ls;
6953 unsigned nparam;
6954 unsigned n_in;
6955 unsigned n_out;
6956 unsigned n_known;
6957 int i;
6959 bmap = isl_basic_map_sort_divs(bmap);
6960 bmap = isl_basic_map_cow(bmap);
6961 if (!bmap)
6962 return NULL;
6964 for (n_known = 0; n_known < bmap->n_div; ++n_known)
6965 if (isl_int_is_zero(bmap->div[n_known][0]))
6966 break;
6968 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6969 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6970 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6971 dim = isl_space_set_alloc(bmap->ctx,
6972 nparam + n_in + n_out + n_known, 0);
6973 if (!dim)
6974 goto error;
6976 ls = isl_basic_map_get_local_space(bmap);
6977 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6978 n_known, bmap->n_div - n_known);
6979 if (n_known > 0) {
6980 for (i = n_known; i < bmap->n_div; ++i)
6981 swap_div(bmap, i - n_known, i);
6982 bmap->n_div -= n_known;
6983 bmap->extra -= n_known;
6985 bmap = isl_basic_map_reset_space(bmap, dim);
6986 bset = (struct isl_basic_set *)bmap;
6988 set = parameter_compute_divs(bset);
6989 map = (struct isl_map *)set;
6990 map = replace_space_by_local_space(map, ls);
6992 return map;
6993 error:
6994 isl_basic_map_free(bmap);
6995 return NULL;
6998 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7000 int i;
7001 unsigned off;
7003 if (!bmap)
7004 return -1;
7006 off = isl_space_dim(bmap->dim, isl_dim_all);
7007 for (i = 0; i < bmap->n_div; ++i) {
7008 if (isl_int_is_zero(bmap->div[i][0]))
7009 return 0;
7010 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
7011 return -1);
7013 return 1;
7016 static int map_divs_known(__isl_keep isl_map *map)
7018 int i;
7020 if (!map)
7021 return -1;
7023 for (i = 0; i < map->n; ++i) {
7024 int known = isl_basic_map_divs_known(map->p[i]);
7025 if (known <= 0)
7026 return known;
7029 return 1;
7032 /* If bmap contains any unknown divs, then compute explicit
7033 * expressions for them. However, this computation may be
7034 * quite expensive, so first try to remove divs that aren't
7035 * strictly needed.
7037 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7039 int known;
7040 struct isl_map *map;
7042 known = isl_basic_map_divs_known(bmap);
7043 if (known < 0)
7044 goto error;
7045 if (known)
7046 return isl_map_from_basic_map(bmap);
7048 bmap = isl_basic_map_drop_redundant_divs(bmap);
7050 known = isl_basic_map_divs_known(bmap);
7051 if (known < 0)
7052 goto error;
7053 if (known)
7054 return isl_map_from_basic_map(bmap);
7056 map = compute_divs(bmap);
7057 return map;
7058 error:
7059 isl_basic_map_free(bmap);
7060 return NULL;
7063 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7065 int i;
7066 int known;
7067 struct isl_map *res;
7069 if (!map)
7070 return NULL;
7071 if (map->n == 0)
7072 return map;
7074 known = map_divs_known(map);
7075 if (known < 0) {
7076 isl_map_free(map);
7077 return NULL;
7079 if (known)
7080 return map;
7082 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7083 for (i = 1 ; i < map->n; ++i) {
7084 struct isl_map *r2;
7085 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7086 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7087 res = isl_map_union_disjoint(res, r2);
7088 else
7089 res = isl_map_union(res, r2);
7091 isl_map_free(map);
7093 return res;
7096 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7098 return (struct isl_set *)
7099 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7102 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7104 return (struct isl_set *)
7105 isl_map_compute_divs((struct isl_map *)set);
7108 struct isl_set *isl_map_domain(struct isl_map *map)
7110 int i;
7111 struct isl_set *set;
7113 if (!map)
7114 goto error;
7116 map = isl_map_cow(map);
7117 if (!map)
7118 return NULL;
7120 set = (struct isl_set *)map;
7121 set->dim = isl_space_domain(set->dim);
7122 if (!set->dim)
7123 goto error;
7124 for (i = 0; i < map->n; ++i) {
7125 set->p[i] = isl_basic_map_domain(map->p[i]);
7126 if (!set->p[i])
7127 goto error;
7129 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7130 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7131 return set;
7132 error:
7133 isl_map_free(map);
7134 return NULL;
7137 /* Return the union of "map1" and "map2", where we assume for now that
7138 * "map1" and "map2" are disjoint. Note that the basic maps inside
7139 * "map1" or "map2" may not be disjoint from each other.
7140 * Also note that this function is also called from isl_map_union,
7141 * which takes care of handling the situation where "map1" and "map2"
7142 * may not be disjoint.
7144 * If one of the inputs is empty, we can simply return the other input.
7145 * Similarly, if one of the inputs is universal, then it is equal to the union.
7147 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7148 __isl_take isl_map *map2)
7150 int i;
7151 unsigned flags = 0;
7152 struct isl_map *map = NULL;
7153 int is_universe;
7155 if (!map1 || !map2)
7156 goto error;
7158 if (!isl_space_is_equal(map1->dim, map2->dim))
7159 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7160 "spaces don't match", goto error);
7162 if (map1->n == 0) {
7163 isl_map_free(map1);
7164 return map2;
7166 if (map2->n == 0) {
7167 isl_map_free(map2);
7168 return map1;
7171 is_universe = isl_map_plain_is_universe(map1);
7172 if (is_universe < 0)
7173 goto error;
7174 if (is_universe) {
7175 isl_map_free(map2);
7176 return map1;
7179 is_universe = isl_map_plain_is_universe(map2);
7180 if (is_universe < 0)
7181 goto error;
7182 if (is_universe) {
7183 isl_map_free(map1);
7184 return map2;
7187 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7188 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7189 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7191 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7192 map1->n + map2->n, flags);
7193 if (!map)
7194 goto error;
7195 for (i = 0; i < map1->n; ++i) {
7196 map = isl_map_add_basic_map(map,
7197 isl_basic_map_copy(map1->p[i]));
7198 if (!map)
7199 goto error;
7201 for (i = 0; i < map2->n; ++i) {
7202 map = isl_map_add_basic_map(map,
7203 isl_basic_map_copy(map2->p[i]));
7204 if (!map)
7205 goto error;
7207 isl_map_free(map1);
7208 isl_map_free(map2);
7209 return map;
7210 error:
7211 isl_map_free(map);
7212 isl_map_free(map1);
7213 isl_map_free(map2);
7214 return NULL;
7217 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7218 * guaranteed to be disjoint by the caller.
7220 * Note that this functions is called from within isl_map_make_disjoint,
7221 * so we have to be careful not to touch the constraints of the inputs
7222 * in any way.
7224 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7225 __isl_take isl_map *map2)
7227 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7230 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7231 * not be disjoint. The parameters are assumed to have been aligned.
7233 * We currently simply call map_union_disjoint, the internal operation
7234 * of which does not really depend on the inputs being disjoint.
7235 * If the result contains more than one basic map, then we clear
7236 * the disjoint flag since the result may contain basic maps from
7237 * both inputs and these are not guaranteed to be disjoint.
7239 * As a special case, if "map1" and "map2" are obviously equal,
7240 * then we simply return "map1".
7242 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7243 __isl_take isl_map *map2)
7245 int equal;
7247 if (!map1 || !map2)
7248 goto error;
7250 equal = isl_map_plain_is_equal(map1, map2);
7251 if (equal < 0)
7252 goto error;
7253 if (equal) {
7254 isl_map_free(map2);
7255 return map1;
7258 map1 = map_union_disjoint(map1, map2);
7259 if (!map1)
7260 return NULL;
7261 if (map1->n > 1)
7262 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7263 return map1;
7264 error:
7265 isl_map_free(map1);
7266 isl_map_free(map2);
7267 return NULL;
7270 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7271 * not be disjoint.
7273 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7274 __isl_take isl_map *map2)
7276 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7279 struct isl_set *isl_set_union_disjoint(
7280 struct isl_set *set1, struct isl_set *set2)
7282 return (struct isl_set *)
7283 isl_map_union_disjoint(
7284 (struct isl_map *)set1, (struct isl_map *)set2);
7287 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7289 return (struct isl_set *)
7290 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7293 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7294 * the results.
7296 * "map" and "set" are assumed to be compatible and non-NULL.
7298 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7299 __isl_take isl_set *set,
7300 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7301 __isl_take isl_basic_set *bset))
7303 unsigned flags = 0;
7304 struct isl_map *result;
7305 int i, j;
7307 if (isl_set_plain_is_universe(set)) {
7308 isl_set_free(set);
7309 return map;
7312 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7313 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7314 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7316 result = isl_map_alloc_space(isl_space_copy(map->dim),
7317 map->n * set->n, flags);
7318 for (i = 0; result && i < map->n; ++i)
7319 for (j = 0; j < set->n; ++j) {
7320 result = isl_map_add_basic_map(result,
7321 fn(isl_basic_map_copy(map->p[i]),
7322 isl_basic_set_copy(set->p[j])));
7323 if (!result)
7324 break;
7327 isl_map_free(map);
7328 isl_set_free(set);
7329 return result;
7332 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7333 __isl_take isl_set *set)
7335 if (!map || !set)
7336 goto error;
7338 if (!isl_map_compatible_range(map, set))
7339 isl_die(set->ctx, isl_error_invalid,
7340 "incompatible spaces", goto error);
7342 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7343 error:
7344 isl_map_free(map);
7345 isl_set_free(set);
7346 return NULL;
7349 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7350 __isl_take isl_set *set)
7352 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7355 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7356 __isl_take isl_set *set)
7358 if (!map || !set)
7359 goto error;
7361 if (!isl_map_compatible_domain(map, set))
7362 isl_die(set->ctx, isl_error_invalid,
7363 "incompatible spaces", goto error);
7365 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7366 error:
7367 isl_map_free(map);
7368 isl_set_free(set);
7369 return NULL;
7372 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7373 __isl_take isl_set *set)
7375 return isl_map_align_params_map_map_and(map, set,
7376 &map_intersect_domain);
7379 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7380 __isl_take isl_map *map2)
7382 if (!map1 || !map2)
7383 goto error;
7384 map1 = isl_map_reverse(map1);
7385 map1 = isl_map_apply_range(map1, map2);
7386 return isl_map_reverse(map1);
7387 error:
7388 isl_map_free(map1);
7389 isl_map_free(map2);
7390 return NULL;
7393 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7394 __isl_take isl_map *map2)
7396 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7399 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7400 __isl_take isl_map *map2)
7402 isl_space *dim_result;
7403 struct isl_map *result;
7404 int i, j;
7406 if (!map1 || !map2)
7407 goto error;
7409 dim_result = isl_space_join(isl_space_copy(map1->dim),
7410 isl_space_copy(map2->dim));
7412 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7413 if (!result)
7414 goto error;
7415 for (i = 0; i < map1->n; ++i)
7416 for (j = 0; j < map2->n; ++j) {
7417 result = isl_map_add_basic_map(result,
7418 isl_basic_map_apply_range(
7419 isl_basic_map_copy(map1->p[i]),
7420 isl_basic_map_copy(map2->p[j])));
7421 if (!result)
7422 goto error;
7424 isl_map_free(map1);
7425 isl_map_free(map2);
7426 if (result && result->n <= 1)
7427 ISL_F_SET(result, ISL_MAP_DISJOINT);
7428 return result;
7429 error:
7430 isl_map_free(map1);
7431 isl_map_free(map2);
7432 return NULL;
7435 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7436 __isl_take isl_map *map2)
7438 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7442 * returns range - domain
7444 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7446 isl_space *dims, *target_dim;
7447 struct isl_basic_set *bset;
7448 unsigned dim;
7449 unsigned nparam;
7450 int i;
7452 if (!bmap)
7453 goto error;
7454 isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7455 bmap->dim, isl_dim_out),
7456 goto error);
7457 target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7458 dim = isl_basic_map_n_in(bmap);
7459 nparam = isl_basic_map_n_param(bmap);
7460 bset = isl_basic_set_from_basic_map(bmap);
7461 bset = isl_basic_set_cow(bset);
7462 dims = isl_basic_set_get_space(bset);
7463 dims = isl_space_add_dims(dims, isl_dim_set, dim);
7464 bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7465 bset = isl_basic_set_swap_vars(bset, 2*dim);
7466 for (i = 0; i < dim; ++i) {
7467 int j = isl_basic_map_alloc_equality(
7468 (struct isl_basic_map *)bset);
7469 if (j < 0) {
7470 bset = isl_basic_set_free(bset);
7471 break;
7473 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7474 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7475 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7476 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7478 bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7479 bset = isl_basic_set_reset_space(bset, target_dim);
7480 return bset;
7481 error:
7482 isl_basic_map_free(bmap);
7483 return NULL;
7487 * returns range - domain
7489 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7491 int i;
7492 isl_space *dim;
7493 struct isl_set *result;
7495 if (!map)
7496 return NULL;
7498 isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7499 map->dim, isl_dim_out),
7500 goto error);
7501 dim = isl_map_get_space(map);
7502 dim = isl_space_domain(dim);
7503 result = isl_set_alloc_space(dim, map->n, 0);
7504 if (!result)
7505 goto error;
7506 for (i = 0; i < map->n; ++i)
7507 result = isl_set_add_basic_set(result,
7508 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7509 isl_map_free(map);
7510 return result;
7511 error:
7512 isl_map_free(map);
7513 return NULL;
7517 * returns [domain -> range] -> range - domain
7519 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7520 __isl_take isl_basic_map *bmap)
7522 int i, k;
7523 isl_space *dim;
7524 isl_basic_map *domain;
7525 int nparam, n;
7526 unsigned total;
7528 if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7529 isl_die(bmap->ctx, isl_error_invalid,
7530 "domain and range don't match", goto error);
7532 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7533 n = isl_basic_map_dim(bmap, isl_dim_in);
7535 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7536 domain = isl_basic_map_universe(dim);
7538 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7539 bmap = isl_basic_map_apply_range(bmap, domain);
7540 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7542 total = isl_basic_map_total_dim(bmap);
7544 for (i = 0; i < n; ++i) {
7545 k = isl_basic_map_alloc_equality(bmap);
7546 if (k < 0)
7547 goto error;
7548 isl_seq_clr(bmap->eq[k], 1 + total);
7549 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7550 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7551 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7554 bmap = isl_basic_map_gauss(bmap, NULL);
7555 return isl_basic_map_finalize(bmap);
7556 error:
7557 isl_basic_map_free(bmap);
7558 return NULL;
7562 * returns [domain -> range] -> range - domain
7564 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7566 int i;
7567 isl_space *domain_dim;
7569 if (!map)
7570 return NULL;
7572 if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7573 isl_die(map->ctx, isl_error_invalid,
7574 "domain and range don't match", goto error);
7576 map = isl_map_cow(map);
7577 if (!map)
7578 return NULL;
7580 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7581 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7582 map->dim = isl_space_join(map->dim, domain_dim);
7583 if (!map->dim)
7584 goto error;
7585 for (i = 0; i < map->n; ++i) {
7586 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7587 if (!map->p[i])
7588 goto error;
7590 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7591 return map;
7592 error:
7593 isl_map_free(map);
7594 return NULL;
7597 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7599 struct isl_basic_map *bmap;
7600 unsigned nparam;
7601 unsigned dim;
7602 int i;
7604 if (!dims)
7605 return NULL;
7607 nparam = dims->nparam;
7608 dim = dims->n_out;
7609 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7610 if (!bmap)
7611 goto error;
7613 for (i = 0; i < dim; ++i) {
7614 int j = isl_basic_map_alloc_equality(bmap);
7615 if (j < 0)
7616 goto error;
7617 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7618 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7619 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7621 return isl_basic_map_finalize(bmap);
7622 error:
7623 isl_basic_map_free(bmap);
7624 return NULL;
7627 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7629 if (!dim)
7630 return NULL;
7631 if (dim->n_in != dim->n_out)
7632 isl_die(dim->ctx, isl_error_invalid,
7633 "number of input and output dimensions needs to be "
7634 "the same", goto error);
7635 return basic_map_identity(dim);
7636 error:
7637 isl_space_free(dim);
7638 return NULL;
7641 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7643 if (!model || !model->dim)
7644 return NULL;
7645 return isl_basic_map_identity(isl_space_copy(model->dim));
7648 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7650 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7653 struct isl_map *isl_map_identity_like(struct isl_map *model)
7655 if (!model || !model->dim)
7656 return NULL;
7657 return isl_map_identity(isl_space_copy(model->dim));
7660 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7662 if (!model || !model->dim)
7663 return NULL;
7664 return isl_map_identity(isl_space_copy(model->dim));
7667 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7669 isl_space *dim = isl_set_get_space(set);
7670 isl_map *id;
7671 id = isl_map_identity(isl_space_map_from_set(dim));
7672 return isl_map_intersect_range(id, set);
7675 /* Construct a basic set with all set dimensions having only non-negative
7676 * values.
7678 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7679 __isl_take isl_space *space)
7681 int i;
7682 unsigned nparam;
7683 unsigned dim;
7684 struct isl_basic_set *bset;
7686 if (!space)
7687 return NULL;
7688 nparam = space->nparam;
7689 dim = space->n_out;
7690 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7691 if (!bset)
7692 return NULL;
7693 for (i = 0; i < dim; ++i) {
7694 int k = isl_basic_set_alloc_inequality(bset);
7695 if (k < 0)
7696 goto error;
7697 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7698 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7700 return bset;
7701 error:
7702 isl_basic_set_free(bset);
7703 return NULL;
7706 /* Construct the half-space x_pos >= 0.
7708 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7709 int pos)
7711 int k;
7712 isl_basic_set *nonneg;
7714 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7715 k = isl_basic_set_alloc_inequality(nonneg);
7716 if (k < 0)
7717 goto error;
7718 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7719 isl_int_set_si(nonneg->ineq[k][pos], 1);
7721 return isl_basic_set_finalize(nonneg);
7722 error:
7723 isl_basic_set_free(nonneg);
7724 return NULL;
7727 /* Construct the half-space x_pos <= -1.
7729 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7731 int k;
7732 isl_basic_set *neg;
7734 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7735 k = isl_basic_set_alloc_inequality(neg);
7736 if (k < 0)
7737 goto error;
7738 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7739 isl_int_set_si(neg->ineq[k][0], -1);
7740 isl_int_set_si(neg->ineq[k][pos], -1);
7742 return isl_basic_set_finalize(neg);
7743 error:
7744 isl_basic_set_free(neg);
7745 return NULL;
7748 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7749 enum isl_dim_type type, unsigned first, unsigned n)
7751 int i;
7752 isl_basic_set *nonneg;
7753 isl_basic_set *neg;
7755 if (!set)
7756 return NULL;
7757 if (n == 0)
7758 return set;
7760 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7762 for (i = 0; i < n; ++i) {
7763 nonneg = nonneg_halfspace(isl_set_get_space(set),
7764 pos(set->dim, type) + first + i);
7765 neg = neg_halfspace(isl_set_get_space(set),
7766 pos(set->dim, type) + first + i);
7768 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7771 return set;
7772 error:
7773 isl_set_free(set);
7774 return NULL;
7777 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7778 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7779 void *user)
7781 isl_set *half;
7783 if (!set)
7784 return -1;
7785 if (isl_set_plain_is_empty(set)) {
7786 isl_set_free(set);
7787 return 0;
7789 if (first == len)
7790 return fn(set, signs, user);
7792 signs[first] = 1;
7793 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7794 1 + first));
7795 half = isl_set_intersect(half, isl_set_copy(set));
7796 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7797 goto error;
7799 signs[first] = -1;
7800 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7801 1 + first));
7802 half = isl_set_intersect(half, set);
7803 return foreach_orthant(half, signs, first + 1, len, fn, user);
7804 error:
7805 isl_set_free(set);
7806 return -1;
7809 /* Call "fn" on the intersections of "set" with each of the orthants
7810 * (except for obviously empty intersections). The orthant is identified
7811 * by the signs array, with each entry having value 1 or -1 according
7812 * to the sign of the corresponding variable.
7814 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7815 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7816 void *user)
7818 unsigned nparam;
7819 unsigned nvar;
7820 int *signs;
7821 int r;
7823 if (!set)
7824 return -1;
7825 if (isl_set_plain_is_empty(set))
7826 return 0;
7828 nparam = isl_set_dim(set, isl_dim_param);
7829 nvar = isl_set_dim(set, isl_dim_set);
7831 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7833 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7834 fn, user);
7836 free(signs);
7838 return r;
7841 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7843 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7846 int isl_basic_map_is_subset(
7847 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7849 int is_subset;
7850 struct isl_map *map1;
7851 struct isl_map *map2;
7853 if (!bmap1 || !bmap2)
7854 return -1;
7856 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7857 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7859 is_subset = isl_map_is_subset(map1, map2);
7861 isl_map_free(map1);
7862 isl_map_free(map2);
7864 return is_subset;
7867 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7868 __isl_keep isl_basic_set *bset2)
7870 return isl_basic_map_is_subset(bset1, bset2);
7873 int isl_basic_map_is_equal(
7874 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7876 int is_subset;
7878 if (!bmap1 || !bmap2)
7879 return -1;
7880 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7881 if (is_subset != 1)
7882 return is_subset;
7883 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7884 return is_subset;
7887 int isl_basic_set_is_equal(
7888 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7890 return isl_basic_map_is_equal(
7891 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7894 int isl_map_is_empty(struct isl_map *map)
7896 int i;
7897 int is_empty;
7899 if (!map)
7900 return -1;
7901 for (i = 0; i < map->n; ++i) {
7902 is_empty = isl_basic_map_is_empty(map->p[i]);
7903 if (is_empty < 0)
7904 return -1;
7905 if (!is_empty)
7906 return 0;
7908 return 1;
7911 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7913 return map ? map->n == 0 : -1;
7916 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7918 return isl_map_plain_is_empty(map);
7921 int isl_set_plain_is_empty(struct isl_set *set)
7923 return set ? set->n == 0 : -1;
7926 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7928 return isl_set_plain_is_empty(set);
7931 int isl_set_is_empty(struct isl_set *set)
7933 return isl_map_is_empty((struct isl_map *)set);
7936 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7938 if (!map1 || !map2)
7939 return -1;
7941 return isl_space_is_equal(map1->dim, map2->dim);
7944 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7946 if (!set1 || !set2)
7947 return -1;
7949 return isl_space_is_equal(set1->dim, set2->dim);
7952 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7954 int is_subset;
7956 if (!map1 || !map2)
7957 return -1;
7958 is_subset = isl_map_is_subset(map1, map2);
7959 if (is_subset != 1)
7960 return is_subset;
7961 is_subset = isl_map_is_subset(map2, map1);
7962 return is_subset;
7965 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7967 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7970 int isl_basic_map_is_strict_subset(
7971 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7973 int is_subset;
7975 if (!bmap1 || !bmap2)
7976 return -1;
7977 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7978 if (is_subset != 1)
7979 return is_subset;
7980 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7981 if (is_subset == -1)
7982 return is_subset;
7983 return !is_subset;
7986 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7988 int is_subset;
7990 if (!map1 || !map2)
7991 return -1;
7992 is_subset = isl_map_is_subset(map1, map2);
7993 if (is_subset != 1)
7994 return is_subset;
7995 is_subset = isl_map_is_subset(map2, map1);
7996 if (is_subset == -1)
7997 return is_subset;
7998 return !is_subset;
8001 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8003 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
8006 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
8008 if (!bmap)
8009 return -1;
8010 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8013 int isl_basic_set_is_universe(struct isl_basic_set *bset)
8015 if (!bset)
8016 return -1;
8017 return bset->n_eq == 0 && bset->n_ineq == 0;
8020 int isl_map_plain_is_universe(__isl_keep isl_map *map)
8022 int i;
8024 if (!map)
8025 return -1;
8027 for (i = 0; i < map->n; ++i) {
8028 int r = isl_basic_map_is_universe(map->p[i]);
8029 if (r < 0 || r)
8030 return r;
8033 return 0;
8036 int isl_set_plain_is_universe(__isl_keep isl_set *set)
8038 return isl_map_plain_is_universe((isl_map *) set);
8041 int isl_set_fast_is_universe(__isl_keep isl_set *set)
8043 return isl_set_plain_is_universe(set);
8046 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
8048 struct isl_basic_set *bset = NULL;
8049 struct isl_vec *sample = NULL;
8050 int empty;
8051 unsigned total;
8053 if (!bmap)
8054 return -1;
8056 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8057 return 1;
8059 if (isl_basic_map_is_universe(bmap))
8060 return 0;
8062 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8063 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8064 copy = isl_basic_map_remove_redundancies(copy);
8065 empty = isl_basic_map_plain_is_empty(copy);
8066 isl_basic_map_free(copy);
8067 return empty;
8070 total = 1 + isl_basic_map_total_dim(bmap);
8071 if (bmap->sample && bmap->sample->size == total) {
8072 int contains = isl_basic_map_contains(bmap, bmap->sample);
8073 if (contains < 0)
8074 return -1;
8075 if (contains)
8076 return 0;
8078 isl_vec_free(bmap->sample);
8079 bmap->sample = NULL;
8080 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8081 if (!bset)
8082 return -1;
8083 sample = isl_basic_set_sample_vec(bset);
8084 if (!sample)
8085 return -1;
8086 empty = sample->size == 0;
8087 isl_vec_free(bmap->sample);
8088 bmap->sample = sample;
8089 if (empty)
8090 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8092 return empty;
8095 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8097 if (!bmap)
8098 return -1;
8099 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8102 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
8104 return isl_basic_map_plain_is_empty(bmap);
8107 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8109 if (!bset)
8110 return -1;
8111 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8114 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
8116 return isl_basic_set_plain_is_empty(bset);
8119 int isl_basic_set_is_empty(struct isl_basic_set *bset)
8121 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8124 struct isl_map *isl_basic_map_union(
8125 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8127 struct isl_map *map;
8128 if (!bmap1 || !bmap2)
8129 goto error;
8131 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8133 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8134 if (!map)
8135 goto error;
8136 map = isl_map_add_basic_map(map, bmap1);
8137 map = isl_map_add_basic_map(map, bmap2);
8138 return map;
8139 error:
8140 isl_basic_map_free(bmap1);
8141 isl_basic_map_free(bmap2);
8142 return NULL;
8145 struct isl_set *isl_basic_set_union(
8146 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8148 return (struct isl_set *)isl_basic_map_union(
8149 (struct isl_basic_map *)bset1,
8150 (struct isl_basic_map *)bset2);
8153 /* Order divs such that any div only depends on previous divs */
8154 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8156 int i;
8157 unsigned off;
8159 if (!bmap)
8160 return NULL;
8162 off = isl_space_dim(bmap->dim, isl_dim_all);
8164 for (i = 0; i < bmap->n_div; ++i) {
8165 int pos;
8166 if (isl_int_is_zero(bmap->div[i][0]))
8167 continue;
8168 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8169 bmap->n_div-i);
8170 if (pos == -1)
8171 continue;
8172 isl_basic_map_swap_div(bmap, i, i + pos);
8173 --i;
8175 return bmap;
8178 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8180 return (struct isl_basic_set *)
8181 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8184 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8186 int i;
8188 if (!map)
8189 return 0;
8191 for (i = 0; i < map->n; ++i) {
8192 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8193 if (!map->p[i])
8194 goto error;
8197 return map;
8198 error:
8199 isl_map_free(map);
8200 return NULL;
8203 /* Apply the expansion computed by isl_merge_divs.
8204 * The expansion itself is given by "exp" while the resulting
8205 * list of divs is given by "div".
8207 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8208 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8210 int i, j;
8211 int n_div;
8213 bset = isl_basic_set_cow(bset);
8214 if (!bset || !div)
8215 goto error;
8217 if (div->n_row < bset->n_div)
8218 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8219 "not an expansion", goto error);
8221 n_div = bset->n_div;
8222 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8223 div->n_row - n_div, 0,
8224 2 * (div->n_row - n_div));
8226 for (i = n_div; i < div->n_row; ++i)
8227 if (isl_basic_set_alloc_div(bset) < 0)
8228 goto error;
8230 j = n_div - 1;
8231 for (i = div->n_row - 1; i >= 0; --i) {
8232 if (j >= 0 && exp[j] == i) {
8233 if (i != j)
8234 isl_basic_map_swap_div(bset, i, j);
8235 j--;
8236 } else {
8237 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8238 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8239 goto error;
8243 isl_mat_free(div);
8244 return bset;
8245 error:
8246 isl_basic_set_free(bset);
8247 isl_mat_free(div);
8248 return NULL;
8251 /* Look for a div in dst that corresponds to the div "div" in src.
8252 * The divs before "div" in src and dst are assumed to be the same.
8254 * Returns -1 if no corresponding div was found and the position
8255 * of the corresponding div in dst otherwise.
8257 static int find_div(struct isl_basic_map *dst,
8258 struct isl_basic_map *src, unsigned div)
8260 int i;
8262 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8264 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8265 for (i = div; i < dst->n_div; ++i)
8266 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8267 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8268 dst->n_div - div) == -1)
8269 return i;
8270 return -1;
8273 /* Align the divs of "dst" to those of "src", adding divs from "src"
8274 * if needed. That is, make sure that the first src->n_div divs
8275 * of the result are equal to those of src.
8277 * The result is not finalized as by design it will have redundant
8278 * divs if any divs from "src" were copied.
8280 __isl_give isl_basic_map *isl_basic_map_align_divs(
8281 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8283 int i;
8284 int known, extended;
8285 unsigned total;
8287 if (!dst || !src)
8288 return isl_basic_map_free(dst);
8290 if (src->n_div == 0)
8291 return dst;
8293 known = isl_basic_map_divs_known(src);
8294 if (known < 0)
8295 return isl_basic_map_free(dst);
8296 if (!known)
8297 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8298 "some src divs are unknown",
8299 return isl_basic_map_free(dst));
8301 src = isl_basic_map_order_divs(src);
8303 extended = 0;
8304 total = isl_space_dim(src->dim, isl_dim_all);
8305 for (i = 0; i < src->n_div; ++i) {
8306 int j = find_div(dst, src, i);
8307 if (j < 0) {
8308 if (!extended) {
8309 int extra = src->n_div - i;
8310 dst = isl_basic_map_cow(dst);
8311 if (!dst)
8312 return NULL;
8313 dst = isl_basic_map_extend_space(dst,
8314 isl_space_copy(dst->dim),
8315 extra, 0, 2 * extra);
8316 extended = 1;
8318 j = isl_basic_map_alloc_div(dst);
8319 if (j < 0)
8320 return isl_basic_map_free(dst);
8321 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8322 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8323 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8324 return isl_basic_map_free(dst);
8326 if (j != i)
8327 isl_basic_map_swap_div(dst, i, j);
8329 return dst;
8332 struct isl_basic_set *isl_basic_set_align_divs(
8333 struct isl_basic_set *dst, struct isl_basic_set *src)
8335 return (struct isl_basic_set *)isl_basic_map_align_divs(
8336 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8339 struct isl_map *isl_map_align_divs(struct isl_map *map)
8341 int i;
8343 if (!map)
8344 return NULL;
8345 if (map->n == 0)
8346 return map;
8347 map = isl_map_compute_divs(map);
8348 map = isl_map_cow(map);
8349 if (!map)
8350 return NULL;
8352 for (i = 1; i < map->n; ++i)
8353 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8354 for (i = 1; i < map->n; ++i) {
8355 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8356 if (!map->p[i])
8357 return isl_map_free(map);
8360 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8361 return map;
8364 struct isl_set *isl_set_align_divs(struct isl_set *set)
8366 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8369 /* Align the divs of the basic sets in "set" to those
8370 * of the basic sets in "list", as well as to the other basic sets in "set".
8371 * The elements in "list" are assumed to have known divs.
8373 __isl_give isl_set *isl_set_align_divs_to_basic_set_list(
8374 __isl_take isl_set *set, __isl_keep isl_basic_set_list *list)
8376 int i, n;
8378 set = isl_set_compute_divs(set);
8379 set = isl_set_cow(set);
8380 if (!set || !list)
8381 return isl_set_free(set);
8382 if (set->n == 0)
8383 return set;
8385 n = isl_basic_set_list_n_basic_set(list);
8386 for (i = 0; i < n; ++i) {
8387 isl_basic_set *bset;
8389 bset = isl_basic_set_list_get_basic_set(list, i);
8390 set->p[0] = isl_basic_set_align_divs(set->p[0], bset);
8391 isl_basic_set_free(bset);
8393 if (!set->p[0])
8394 return isl_set_free(set);
8396 return isl_set_align_divs(set);
8399 /* Align the divs of each element of "list" to those of "bset".
8400 * Both "bset" and the elements of "list" are assumed to have known divs.
8402 __isl_give isl_basic_set_list *isl_basic_set_list_align_divs_to_basic_set(
8403 __isl_take isl_basic_set_list *list, __isl_keep isl_basic_set *bset)
8405 int i, n;
8407 if (!list || !bset)
8408 return isl_basic_set_list_free(list);
8410 n = isl_basic_set_list_n_basic_set(list);
8411 for (i = 0; i < n; ++i) {
8412 isl_basic_set *bset_i;
8414 bset_i = isl_basic_set_list_get_basic_set(list, i);
8415 bset_i = isl_basic_set_align_divs(bset_i, bset);
8416 list = isl_basic_set_list_set_basic_set(list, i, bset_i);
8419 return list;
8422 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8423 __isl_take isl_map *map)
8425 if (!set || !map)
8426 goto error;
8427 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8428 map = isl_map_intersect_domain(map, set);
8429 set = isl_map_range(map);
8430 return set;
8431 error:
8432 isl_set_free(set);
8433 isl_map_free(map);
8434 return NULL;
8437 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8438 __isl_take isl_map *map)
8440 return isl_map_align_params_map_map_and(set, map, &set_apply);
8443 /* There is no need to cow as removing empty parts doesn't change
8444 * the meaning of the set.
8446 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8448 int i;
8450 if (!map)
8451 return NULL;
8453 for (i = map->n - 1; i >= 0; --i)
8454 remove_if_empty(map, i);
8456 return map;
8459 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8461 return (struct isl_set *)
8462 isl_map_remove_empty_parts((struct isl_map *)set);
8465 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8467 struct isl_basic_map *bmap;
8468 if (!map || map->n == 0)
8469 return NULL;
8470 bmap = map->p[map->n-1];
8471 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8472 return isl_basic_map_copy(bmap);
8475 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8477 return (struct isl_basic_set *)
8478 isl_map_copy_basic_map((struct isl_map *)set);
8481 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8482 __isl_keep isl_basic_map *bmap)
8484 int i;
8486 if (!map || !bmap)
8487 goto error;
8488 for (i = map->n-1; i >= 0; --i) {
8489 if (map->p[i] != bmap)
8490 continue;
8491 map = isl_map_cow(map);
8492 if (!map)
8493 goto error;
8494 isl_basic_map_free(map->p[i]);
8495 if (i != map->n-1) {
8496 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8497 map->p[i] = map->p[map->n-1];
8499 map->n--;
8500 return map;
8502 return map;
8503 error:
8504 isl_map_free(map);
8505 return NULL;
8508 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8509 struct isl_basic_set *bset)
8511 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8512 (struct isl_basic_map *)bset);
8515 /* Given two basic sets bset1 and bset2, compute the maximal difference
8516 * between the values of dimension pos in bset1 and those in bset2
8517 * for any common value of the parameters and dimensions preceding pos.
8519 static enum isl_lp_result basic_set_maximal_difference_at(
8520 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8521 int pos, isl_int *opt)
8523 isl_space *dims;
8524 struct isl_basic_map *bmap1 = NULL;
8525 struct isl_basic_map *bmap2 = NULL;
8526 struct isl_ctx *ctx;
8527 struct isl_vec *obj;
8528 unsigned total;
8529 unsigned nparam;
8530 unsigned dim1, dim2;
8531 enum isl_lp_result res;
8533 if (!bset1 || !bset2)
8534 return isl_lp_error;
8536 nparam = isl_basic_set_n_param(bset1);
8537 dim1 = isl_basic_set_n_dim(bset1);
8538 dim2 = isl_basic_set_n_dim(bset2);
8539 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8540 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8541 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8542 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8543 if (!bmap1 || !bmap2)
8544 goto error;
8545 bmap1 = isl_basic_map_cow(bmap1);
8546 bmap1 = isl_basic_map_extend(bmap1, nparam,
8547 pos, (dim1 - pos) + (dim2 - pos),
8548 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8549 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8550 if (!bmap1)
8551 goto error2;
8552 total = isl_basic_map_total_dim(bmap1);
8553 ctx = bmap1->ctx;
8554 obj = isl_vec_alloc(ctx, 1 + total);
8555 if (!obj)
8556 goto error2;
8557 isl_seq_clr(obj->block.data, 1 + total);
8558 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8559 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8560 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8561 opt, NULL, NULL);
8562 isl_basic_map_free(bmap1);
8563 isl_vec_free(obj);
8564 return res;
8565 error:
8566 isl_basic_map_free(bmap2);
8567 error2:
8568 isl_basic_map_free(bmap1);
8569 return isl_lp_error;
8572 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8573 * for any common value of the parameters and dimensions preceding pos
8574 * in both basic sets, the values of dimension pos in bset1 are
8575 * smaller or larger than those in bset2.
8577 * Returns
8578 * 1 if bset1 follows bset2
8579 * -1 if bset1 precedes bset2
8580 * 0 if bset1 and bset2 are incomparable
8581 * -2 if some error occurred.
8583 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8584 struct isl_basic_set *bset2, int pos)
8586 isl_int opt;
8587 enum isl_lp_result res;
8588 int cmp;
8590 isl_int_init(opt);
8592 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8594 if (res == isl_lp_empty)
8595 cmp = 0;
8596 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8597 res == isl_lp_unbounded)
8598 cmp = 1;
8599 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8600 cmp = -1;
8601 else
8602 cmp = -2;
8604 isl_int_clear(opt);
8605 return cmp;
8608 /* Given two basic sets bset1 and bset2, check whether
8609 * for any common value of the parameters and dimensions preceding pos
8610 * there is a value of dimension pos in bset1 that is larger
8611 * than a value of the same dimension in bset2.
8613 * Return
8614 * 1 if there exists such a pair
8615 * 0 if there is no such pair, but there is a pair of equal values
8616 * -1 otherwise
8617 * -2 if some error occurred.
8619 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8620 __isl_keep isl_basic_set *bset2, int pos)
8622 isl_int opt;
8623 enum isl_lp_result res;
8624 int cmp;
8626 isl_int_init(opt);
8628 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8630 if (res == isl_lp_empty)
8631 cmp = -1;
8632 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8633 res == isl_lp_unbounded)
8634 cmp = 1;
8635 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8636 cmp = -1;
8637 else if (res == isl_lp_ok)
8638 cmp = 0;
8639 else
8640 cmp = -2;
8642 isl_int_clear(opt);
8643 return cmp;
8646 /* Given two sets set1 and set2, check whether
8647 * for any common value of the parameters and dimensions preceding pos
8648 * there is a value of dimension pos in set1 that is larger
8649 * than a value of the same dimension in set2.
8651 * Return
8652 * 1 if there exists such a pair
8653 * 0 if there is no such pair, but there is a pair of equal values
8654 * -1 otherwise
8655 * -2 if some error occurred.
8657 int isl_set_follows_at(__isl_keep isl_set *set1,
8658 __isl_keep isl_set *set2, int pos)
8660 int i, j;
8661 int follows = -1;
8663 if (!set1 || !set2)
8664 return -2;
8666 for (i = 0; i < set1->n; ++i)
8667 for (j = 0; j < set2->n; ++j) {
8668 int f;
8669 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8670 if (f == 1 || f == -2)
8671 return f;
8672 if (f > follows)
8673 follows = f;
8676 return follows;
8679 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8680 unsigned pos, isl_int *val)
8682 int i;
8683 int d;
8684 unsigned total;
8686 if (!bmap)
8687 return -1;
8688 total = isl_basic_map_total_dim(bmap);
8689 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8690 for (; d+1 > pos; --d)
8691 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8692 break;
8693 if (d != pos)
8694 continue;
8695 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8696 return 0;
8697 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8698 return 0;
8699 if (!isl_int_is_one(bmap->eq[i][1+d]))
8700 return 0;
8701 if (val)
8702 isl_int_neg(*val, bmap->eq[i][0]);
8703 return 1;
8705 return 0;
8708 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8709 unsigned pos, isl_int *val)
8711 int i;
8712 isl_int v;
8713 isl_int tmp;
8714 int fixed;
8716 if (!map)
8717 return -1;
8718 if (map->n == 0)
8719 return 0;
8720 if (map->n == 1)
8721 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8722 isl_int_init(v);
8723 isl_int_init(tmp);
8724 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8725 for (i = 1; fixed == 1 && i < map->n; ++i) {
8726 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8727 if (fixed == 1 && isl_int_ne(tmp, v))
8728 fixed = 0;
8730 if (val)
8731 isl_int_set(*val, v);
8732 isl_int_clear(tmp);
8733 isl_int_clear(v);
8734 return fixed;
8737 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8738 unsigned pos, isl_int *val)
8740 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8741 pos, val);
8744 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8745 isl_int *val)
8747 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8750 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8751 enum isl_dim_type type, unsigned pos, isl_int *val)
8753 if (pos >= isl_basic_map_dim(bmap, type))
8754 return -1;
8755 return isl_basic_map_plain_has_fixed_var(bmap,
8756 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8759 /* If "bmap" obviously lies on a hyperplane where the given dimension
8760 * has a fixed value, then return that value.
8761 * Otherwise return NaN.
8763 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8764 __isl_keep isl_basic_map *bmap,
8765 enum isl_dim_type type, unsigned pos)
8767 isl_ctx *ctx;
8768 isl_val *v;
8769 int fixed;
8771 if (!bmap)
8772 return NULL;
8773 ctx = isl_basic_map_get_ctx(bmap);
8774 v = isl_val_alloc(ctx);
8775 if (!v)
8776 return NULL;
8777 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8778 if (fixed < 0)
8779 return isl_val_free(v);
8780 if (fixed) {
8781 isl_int_set_si(v->d, 1);
8782 return v;
8784 isl_val_free(v);
8785 return isl_val_nan(ctx);
8788 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8789 enum isl_dim_type type, unsigned pos, isl_int *val)
8791 if (pos >= isl_map_dim(map, type))
8792 return -1;
8793 return isl_map_plain_has_fixed_var(map,
8794 map_offset(map, type) - 1 + pos, val);
8797 /* If "map" obviously lies on a hyperplane where the given dimension
8798 * has a fixed value, then return that value.
8799 * Otherwise return NaN.
8801 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8802 enum isl_dim_type type, unsigned pos)
8804 isl_ctx *ctx;
8805 isl_val *v;
8806 int fixed;
8808 if (!map)
8809 return NULL;
8810 ctx = isl_map_get_ctx(map);
8811 v = isl_val_alloc(ctx);
8812 if (!v)
8813 return NULL;
8814 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8815 if (fixed < 0)
8816 return isl_val_free(v);
8817 if (fixed) {
8818 isl_int_set_si(v->d, 1);
8819 return v;
8821 isl_val_free(v);
8822 return isl_val_nan(ctx);
8825 /* If "set" obviously lies on a hyperplane where the given dimension
8826 * has a fixed value, then return that value.
8827 * Otherwise return NaN.
8829 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8830 enum isl_dim_type type, unsigned pos)
8832 return isl_map_plain_get_val_if_fixed(set, type, pos);
8835 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8836 enum isl_dim_type type, unsigned pos, isl_int *val)
8838 return isl_map_plain_is_fixed(set, type, pos, val);
8841 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8842 enum isl_dim_type type, unsigned pos, isl_int *val)
8844 return isl_map_plain_is_fixed(map, type, pos, val);
8847 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8848 * then return this fixed value in *val.
8850 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8851 unsigned dim, isl_int *val)
8853 return isl_basic_set_plain_has_fixed_var(bset,
8854 isl_basic_set_n_param(bset) + dim, val);
8857 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8858 * then return this fixed value in *val.
8860 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8861 unsigned dim, isl_int *val)
8863 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8866 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8867 unsigned dim, isl_int *val)
8869 return isl_set_plain_dim_is_fixed(set, dim, val);
8872 /* Check if input variable in has fixed value and if so and if val is not NULL,
8873 * then return this fixed value in *val.
8875 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8876 unsigned in, isl_int *val)
8878 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8881 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8882 * and if val is not NULL, then return this lower bound in *val.
8884 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8885 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8887 int i, i_eq = -1, i_ineq = -1;
8888 isl_int *c;
8889 unsigned total;
8890 unsigned nparam;
8892 if (!bset)
8893 return -1;
8894 total = isl_basic_set_total_dim(bset);
8895 nparam = isl_basic_set_n_param(bset);
8896 for (i = 0; i < bset->n_eq; ++i) {
8897 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8898 continue;
8899 if (i_eq != -1)
8900 return 0;
8901 i_eq = i;
8903 for (i = 0; i < bset->n_ineq; ++i) {
8904 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8905 continue;
8906 if (i_eq != -1 || i_ineq != -1)
8907 return 0;
8908 i_ineq = i;
8910 if (i_eq == -1 && i_ineq == -1)
8911 return 0;
8912 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8913 /* The coefficient should always be one due to normalization. */
8914 if (!isl_int_is_one(c[1+nparam+dim]))
8915 return 0;
8916 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8917 return 0;
8918 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8919 total - nparam - dim - 1) != -1)
8920 return 0;
8921 if (val)
8922 isl_int_neg(*val, c[0]);
8923 return 1;
8926 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8927 unsigned dim, isl_int *val)
8929 int i;
8930 isl_int v;
8931 isl_int tmp;
8932 int fixed;
8934 if (!set)
8935 return -1;
8936 if (set->n == 0)
8937 return 0;
8938 if (set->n == 1)
8939 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8940 dim, val);
8941 isl_int_init(v);
8942 isl_int_init(tmp);
8943 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8944 dim, &v);
8945 for (i = 1; fixed == 1 && i < set->n; ++i) {
8946 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8947 dim, &tmp);
8948 if (fixed == 1 && isl_int_ne(tmp, v))
8949 fixed = 0;
8951 if (val)
8952 isl_int_set(*val, v);
8953 isl_int_clear(tmp);
8954 isl_int_clear(v);
8955 return fixed;
8958 struct constraint {
8959 unsigned size;
8960 isl_int *c;
8963 /* uset_gist depends on constraints without existentially quantified
8964 * variables sorting first.
8966 static int qsort_constraint_cmp(const void *p1, const void *p2)
8968 const struct constraint *c1 = (const struct constraint *)p1;
8969 const struct constraint *c2 = (const struct constraint *)p2;
8970 int l1, l2;
8971 unsigned size = isl_min(c1->size, c2->size);
8973 l1 = isl_seq_last_non_zero(c1->c + 1, size);
8974 l2 = isl_seq_last_non_zero(c2->c + 1, size);
8976 if (l1 != l2)
8977 return l1 - l2;
8979 return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8982 static struct isl_basic_map *isl_basic_map_sort_constraints(
8983 struct isl_basic_map *bmap)
8985 int i;
8986 struct constraint *c;
8987 unsigned total;
8989 if (!bmap)
8990 return NULL;
8991 if (bmap->n_ineq == 0)
8992 return bmap;
8993 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8994 return bmap;
8995 total = isl_basic_map_total_dim(bmap);
8996 c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8997 if (!c)
8998 goto error;
8999 for (i = 0; i < bmap->n_ineq; ++i) {
9000 c[i].size = total;
9001 c[i].c = bmap->ineq[i];
9003 qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
9004 for (i = 0; i < bmap->n_ineq; ++i)
9005 bmap->ineq[i] = c[i].c;
9006 free(c);
9007 return bmap;
9008 error:
9009 isl_basic_map_free(bmap);
9010 return NULL;
9013 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9014 __isl_take isl_basic_set *bset)
9016 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9017 (struct isl_basic_map *)bset);
9020 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9022 if (!bmap)
9023 return NULL;
9024 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9025 return bmap;
9026 bmap = isl_basic_map_remove_redundancies(bmap);
9027 bmap = isl_basic_map_sort_constraints(bmap);
9028 if (bmap)
9029 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9030 return bmap;
9033 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9035 return (struct isl_basic_set *)isl_basic_map_normalize(
9036 (struct isl_basic_map *)bset);
9039 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9040 const __isl_keep isl_basic_map *bmap2)
9042 int i, cmp;
9043 unsigned total;
9045 if (bmap1 == bmap2)
9046 return 0;
9047 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9048 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9049 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9050 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9051 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9052 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9053 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9054 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9055 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9056 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9057 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9058 return 0;
9059 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9060 return 1;
9061 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9062 return -1;
9063 if (bmap1->n_eq != bmap2->n_eq)
9064 return bmap1->n_eq - bmap2->n_eq;
9065 if (bmap1->n_ineq != bmap2->n_ineq)
9066 return bmap1->n_ineq - bmap2->n_ineq;
9067 if (bmap1->n_div != bmap2->n_div)
9068 return bmap1->n_div - bmap2->n_div;
9069 total = isl_basic_map_total_dim(bmap1);
9070 for (i = 0; i < bmap1->n_eq; ++i) {
9071 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9072 if (cmp)
9073 return cmp;
9075 for (i = 0; i < bmap1->n_ineq; ++i) {
9076 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9077 if (cmp)
9078 return cmp;
9080 for (i = 0; i < bmap1->n_div; ++i) {
9081 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9082 if (cmp)
9083 return cmp;
9085 return 0;
9088 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9089 const __isl_keep isl_basic_set *bset2)
9091 return isl_basic_map_plain_cmp(bset1, bset2);
9094 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9096 int i, cmp;
9098 if (set1 == set2)
9099 return 0;
9100 if (set1->n != set2->n)
9101 return set1->n - set2->n;
9103 for (i = 0; i < set1->n; ++i) {
9104 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9105 if (cmp)
9106 return cmp;
9109 return 0;
9112 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9113 __isl_keep isl_basic_map *bmap2)
9115 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9118 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9119 __isl_keep isl_basic_set *bset2)
9121 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9122 (isl_basic_map *)bset2);
9125 static int qsort_bmap_cmp(const void *p1, const void *p2)
9127 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9128 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9130 return isl_basic_map_plain_cmp(bmap1, bmap2);
9133 /* We normalize in place, but if anything goes wrong we need
9134 * to return NULL, so we need to make sure we don't change the
9135 * meaning of any possible other copies of map.
9137 struct isl_map *isl_map_normalize(struct isl_map *map)
9139 int i, j;
9140 struct isl_basic_map *bmap;
9142 if (!map)
9143 return NULL;
9144 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9145 return map;
9146 for (i = 0; i < map->n; ++i) {
9147 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9148 if (!bmap)
9149 goto error;
9150 isl_basic_map_free(map->p[i]);
9151 map->p[i] = bmap;
9153 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9154 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9155 map = isl_map_remove_empty_parts(map);
9156 if (!map)
9157 return NULL;
9158 for (i = map->n - 1; i >= 1; --i) {
9159 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
9160 continue;
9161 isl_basic_map_free(map->p[i-1]);
9162 for (j = i; j < map->n; ++j)
9163 map->p[j-1] = map->p[j];
9164 map->n--;
9166 return map;
9167 error:
9168 isl_map_free(map);
9169 return NULL;
9172 struct isl_set *isl_set_normalize(struct isl_set *set)
9174 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9177 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9179 int i;
9180 int equal;
9182 if (!map1 || !map2)
9183 return -1;
9185 if (map1 == map2)
9186 return 1;
9187 if (!isl_space_is_equal(map1->dim, map2->dim))
9188 return 0;
9190 map1 = isl_map_copy(map1);
9191 map2 = isl_map_copy(map2);
9192 map1 = isl_map_normalize(map1);
9193 map2 = isl_map_normalize(map2);
9194 if (!map1 || !map2)
9195 goto error;
9196 equal = map1->n == map2->n;
9197 for (i = 0; equal && i < map1->n; ++i) {
9198 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9199 if (equal < 0)
9200 goto error;
9202 isl_map_free(map1);
9203 isl_map_free(map2);
9204 return equal;
9205 error:
9206 isl_map_free(map1);
9207 isl_map_free(map2);
9208 return -1;
9211 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
9213 return isl_map_plain_is_equal(map1, map2);
9216 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9218 return isl_map_plain_is_equal((struct isl_map *)set1,
9219 (struct isl_map *)set2);
9222 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9224 return isl_set_plain_is_equal(set1, set2);
9227 /* Return an interval that ranges from min to max (inclusive)
9229 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9230 isl_int min, isl_int max)
9232 int k;
9233 struct isl_basic_set *bset = NULL;
9235 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9236 if (!bset)
9237 goto error;
9239 k = isl_basic_set_alloc_inequality(bset);
9240 if (k < 0)
9241 goto error;
9242 isl_int_set_si(bset->ineq[k][1], 1);
9243 isl_int_neg(bset->ineq[k][0], min);
9245 k = isl_basic_set_alloc_inequality(bset);
9246 if (k < 0)
9247 goto error;
9248 isl_int_set_si(bset->ineq[k][1], -1);
9249 isl_int_set(bset->ineq[k][0], max);
9251 return bset;
9252 error:
9253 isl_basic_set_free(bset);
9254 return NULL;
9257 /* Return the basic sets in "set" as a list.
9259 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9260 __isl_keep isl_set *set)
9262 int i;
9263 isl_ctx *ctx;
9264 isl_basic_set_list *list;
9266 if (!set)
9267 return NULL;
9268 ctx = isl_set_get_ctx(set);
9269 list = isl_basic_set_list_alloc(ctx, set->n);
9271 for (i = 0; i < set->n; ++i) {
9272 isl_basic_set *bset;
9274 bset = isl_basic_set_copy(set->p[i]);
9275 list = isl_basic_set_list_add(list, bset);
9278 return list;
9281 /* Return the intersection of the elements in the non-empty list "list".
9282 * All elements are assumed to live in the same space.
9284 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9285 __isl_take struct isl_basic_set_list *list)
9287 int i, n;
9288 isl_basic_set *bset;
9290 if (!list)
9291 return NULL;
9292 n = isl_basic_set_list_n_basic_set(list);
9293 if (n < 1)
9294 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9295 "expecting non-empty list", goto error);
9297 bset = isl_basic_set_list_get_basic_set(list, 0);
9298 for (i = 1; i < n; ++i) {
9299 isl_basic_set *bset_i;
9301 bset_i = isl_basic_set_list_get_basic_set(list, i);
9302 bset = isl_basic_set_intersect(bset, bset_i);
9305 isl_basic_set_list_free(list);
9306 return bset;
9307 error:
9308 isl_basic_set_list_free(list);
9309 return NULL;
9312 /* Return the Cartesian product of the basic sets in list (in the given order).
9314 __isl_give isl_basic_set *isl_basic_set_list_product(
9315 __isl_take struct isl_basic_set_list *list)
9317 int i;
9318 unsigned dim;
9319 unsigned nparam;
9320 unsigned extra;
9321 unsigned n_eq;
9322 unsigned n_ineq;
9323 struct isl_basic_set *product = NULL;
9325 if (!list)
9326 goto error;
9327 isl_assert(list->ctx, list->n > 0, goto error);
9328 isl_assert(list->ctx, list->p[0], goto error);
9329 nparam = isl_basic_set_n_param(list->p[0]);
9330 dim = isl_basic_set_n_dim(list->p[0]);
9331 extra = list->p[0]->n_div;
9332 n_eq = list->p[0]->n_eq;
9333 n_ineq = list->p[0]->n_ineq;
9334 for (i = 1; i < list->n; ++i) {
9335 isl_assert(list->ctx, list->p[i], goto error);
9336 isl_assert(list->ctx,
9337 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9338 dim += isl_basic_set_n_dim(list->p[i]);
9339 extra += list->p[i]->n_div;
9340 n_eq += list->p[i]->n_eq;
9341 n_ineq += list->p[i]->n_ineq;
9343 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9344 n_eq, n_ineq);
9345 if (!product)
9346 goto error;
9347 dim = 0;
9348 for (i = 0; i < list->n; ++i) {
9349 isl_basic_set_add_constraints(product,
9350 isl_basic_set_copy(list->p[i]), dim);
9351 dim += isl_basic_set_n_dim(list->p[i]);
9353 isl_basic_set_list_free(list);
9354 return product;
9355 error:
9356 isl_basic_set_free(product);
9357 isl_basic_set_list_free(list);
9358 return NULL;
9361 struct isl_basic_map *isl_basic_map_product(
9362 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9364 isl_space *dim_result = NULL;
9365 struct isl_basic_map *bmap;
9366 unsigned in1, in2, out1, out2, nparam, total, pos;
9367 struct isl_dim_map *dim_map1, *dim_map2;
9369 if (!bmap1 || !bmap2)
9370 goto error;
9372 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9373 bmap2->dim, isl_dim_param), goto error);
9374 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9375 isl_space_copy(bmap2->dim));
9377 in1 = isl_basic_map_n_in(bmap1);
9378 in2 = isl_basic_map_n_in(bmap2);
9379 out1 = isl_basic_map_n_out(bmap1);
9380 out2 = isl_basic_map_n_out(bmap2);
9381 nparam = isl_basic_map_n_param(bmap1);
9383 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9384 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9385 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9386 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9387 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9388 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9389 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9390 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9391 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9392 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9393 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9395 bmap = isl_basic_map_alloc_space(dim_result,
9396 bmap1->n_div + bmap2->n_div,
9397 bmap1->n_eq + bmap2->n_eq,
9398 bmap1->n_ineq + bmap2->n_ineq);
9399 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9400 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9401 bmap = isl_basic_map_simplify(bmap);
9402 return isl_basic_map_finalize(bmap);
9403 error:
9404 isl_basic_map_free(bmap1);
9405 isl_basic_map_free(bmap2);
9406 return NULL;
9409 __isl_give isl_basic_map *isl_basic_map_flat_product(
9410 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9412 isl_basic_map *prod;
9414 prod = isl_basic_map_product(bmap1, bmap2);
9415 prod = isl_basic_map_flatten(prod);
9416 return prod;
9419 __isl_give isl_basic_set *isl_basic_set_flat_product(
9420 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9422 return isl_basic_map_flat_range_product(bset1, bset2);
9425 __isl_give isl_basic_map *isl_basic_map_domain_product(
9426 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9428 isl_space *space_result = NULL;
9429 isl_basic_map *bmap;
9430 unsigned in1, in2, out, nparam, total, pos;
9431 struct isl_dim_map *dim_map1, *dim_map2;
9433 if (!bmap1 || !bmap2)
9434 goto error;
9436 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9437 isl_space_copy(bmap2->dim));
9439 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9440 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9441 out = isl_basic_map_dim(bmap1, isl_dim_out);
9442 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9444 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9445 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9446 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9447 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9448 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9449 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9450 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9451 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9452 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9453 isl_dim_map_div(dim_map1, bmap1, pos += out);
9454 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9456 bmap = isl_basic_map_alloc_space(space_result,
9457 bmap1->n_div + bmap2->n_div,
9458 bmap1->n_eq + bmap2->n_eq,
9459 bmap1->n_ineq + bmap2->n_ineq);
9460 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9461 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9462 bmap = isl_basic_map_simplify(bmap);
9463 return isl_basic_map_finalize(bmap);
9464 error:
9465 isl_basic_map_free(bmap1);
9466 isl_basic_map_free(bmap2);
9467 return NULL;
9470 __isl_give isl_basic_map *isl_basic_map_range_product(
9471 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9473 isl_space *dim_result = NULL;
9474 isl_basic_map *bmap;
9475 unsigned in, out1, out2, nparam, total, pos;
9476 struct isl_dim_map *dim_map1, *dim_map2;
9478 if (!bmap1 || !bmap2)
9479 goto error;
9481 if (!isl_space_match(bmap1->dim, isl_dim_param,
9482 bmap2->dim, isl_dim_param))
9483 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9484 "parameters don't match", goto error);
9486 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9487 isl_space_copy(bmap2->dim));
9489 in = isl_basic_map_dim(bmap1, isl_dim_in);
9490 out1 = isl_basic_map_n_out(bmap1);
9491 out2 = isl_basic_map_n_out(bmap2);
9492 nparam = isl_basic_map_n_param(bmap1);
9494 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9495 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9496 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9497 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9498 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9499 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9500 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9501 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9502 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9503 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9504 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9506 bmap = isl_basic_map_alloc_space(dim_result,
9507 bmap1->n_div + bmap2->n_div,
9508 bmap1->n_eq + bmap2->n_eq,
9509 bmap1->n_ineq + bmap2->n_ineq);
9510 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9511 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9512 bmap = isl_basic_map_simplify(bmap);
9513 return isl_basic_map_finalize(bmap);
9514 error:
9515 isl_basic_map_free(bmap1);
9516 isl_basic_map_free(bmap2);
9517 return NULL;
9520 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9521 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9523 isl_basic_map *prod;
9525 prod = isl_basic_map_range_product(bmap1, bmap2);
9526 prod = isl_basic_map_flatten_range(prod);
9527 return prod;
9530 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9531 __isl_take isl_map *map2,
9532 __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9533 __isl_take isl_space *right),
9534 __isl_give isl_basic_map *(*basic_map_product)(
9535 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9537 unsigned flags = 0;
9538 struct isl_map *result;
9539 int i, j;
9541 if (!map1 || !map2)
9542 goto error;
9544 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9545 map2->dim, isl_dim_param), goto error);
9547 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9548 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9549 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9551 result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9552 isl_space_copy(map2->dim)),
9553 map1->n * map2->n, flags);
9554 if (!result)
9555 goto error;
9556 for (i = 0; i < map1->n; ++i)
9557 for (j = 0; j < map2->n; ++j) {
9558 struct isl_basic_map *part;
9559 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9560 isl_basic_map_copy(map2->p[j]));
9561 if (isl_basic_map_is_empty(part))
9562 isl_basic_map_free(part);
9563 else
9564 result = isl_map_add_basic_map(result, part);
9565 if (!result)
9566 goto error;
9568 isl_map_free(map1);
9569 isl_map_free(map2);
9570 return result;
9571 error:
9572 isl_map_free(map1);
9573 isl_map_free(map2);
9574 return NULL;
9577 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9579 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9580 __isl_take isl_map *map2)
9582 return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9585 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9586 __isl_take isl_map *map2)
9588 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9591 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9593 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9594 __isl_take isl_map *map2)
9596 isl_map *prod;
9598 prod = isl_map_product(map1, map2);
9599 prod = isl_map_flatten(prod);
9600 return prod;
9603 /* Given two set A and B, construct its Cartesian product A x B.
9605 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9607 return isl_map_range_product(set1, set2);
9610 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9611 __isl_take isl_set *set2)
9613 return isl_map_flat_range_product(set1, set2);
9616 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9618 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9619 __isl_take isl_map *map2)
9621 return map_product(map1, map2, &isl_space_domain_product,
9622 &isl_basic_map_domain_product);
9625 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9627 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9628 __isl_take isl_map *map2)
9630 return map_product(map1, map2, &isl_space_range_product,
9631 &isl_basic_map_range_product);
9634 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9635 __isl_take isl_map *map2)
9637 return isl_map_align_params_map_map_and(map1, map2,
9638 &map_domain_product_aligned);
9641 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9642 __isl_take isl_map *map2)
9644 return isl_map_align_params_map_map_and(map1, map2,
9645 &map_range_product_aligned);
9648 /* Given a map A -> [B -> C], extract the map A -> B.
9650 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9652 isl_space *space;
9653 int total, keep;
9655 if (!map)
9656 return NULL;
9657 if (!isl_space_range_is_wrapping(map->dim))
9658 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9659 "range is not a product", return isl_map_free(map));
9661 space = isl_map_get_space(map);
9662 total = isl_space_dim(space, isl_dim_out);
9663 space = isl_space_range_factor_domain(space);
9664 keep = isl_space_dim(space, isl_dim_out);
9665 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9666 map = isl_map_reset_space(map, space);
9668 return map;
9671 /* Given a map A -> [B -> C], extract the map A -> C.
9673 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9675 isl_space *space;
9676 int total, keep;
9678 if (!map)
9679 return NULL;
9680 if (!isl_space_range_is_wrapping(map->dim))
9681 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9682 "range is not a product", return isl_map_free(map));
9684 space = isl_map_get_space(map);
9685 total = isl_space_dim(space, isl_dim_out);
9686 space = isl_space_range_factor_range(space);
9687 keep = isl_space_dim(space, isl_dim_out);
9688 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9689 map = isl_map_reset_space(map, space);
9691 return map;
9694 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9696 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9697 __isl_take isl_map *map2)
9699 isl_map *prod;
9701 prod = isl_map_domain_product(map1, map2);
9702 prod = isl_map_flatten_domain(prod);
9703 return prod;
9706 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9708 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9709 __isl_take isl_map *map2)
9711 isl_map *prod;
9713 prod = isl_map_range_product(map1, map2);
9714 prod = isl_map_flatten_range(prod);
9715 return prod;
9718 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9720 int i;
9721 uint32_t hash = isl_hash_init();
9722 unsigned total;
9724 if (!bmap)
9725 return 0;
9726 bmap = isl_basic_map_copy(bmap);
9727 bmap = isl_basic_map_normalize(bmap);
9728 if (!bmap)
9729 return 0;
9730 total = isl_basic_map_total_dim(bmap);
9731 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9732 for (i = 0; i < bmap->n_eq; ++i) {
9733 uint32_t c_hash;
9734 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9735 isl_hash_hash(hash, c_hash);
9737 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9738 for (i = 0; i < bmap->n_ineq; ++i) {
9739 uint32_t c_hash;
9740 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9741 isl_hash_hash(hash, c_hash);
9743 isl_hash_byte(hash, bmap->n_div & 0xFF);
9744 for (i = 0; i < bmap->n_div; ++i) {
9745 uint32_t c_hash;
9746 if (isl_int_is_zero(bmap->div[i][0]))
9747 continue;
9748 isl_hash_byte(hash, i & 0xFF);
9749 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9750 isl_hash_hash(hash, c_hash);
9752 isl_basic_map_free(bmap);
9753 return hash;
9756 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9758 return isl_basic_map_get_hash((isl_basic_map *)bset);
9761 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9763 int i;
9764 uint32_t hash;
9766 if (!map)
9767 return 0;
9768 map = isl_map_copy(map);
9769 map = isl_map_normalize(map);
9770 if (!map)
9771 return 0;
9773 hash = isl_hash_init();
9774 for (i = 0; i < map->n; ++i) {
9775 uint32_t bmap_hash;
9776 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9777 isl_hash_hash(hash, bmap_hash);
9780 isl_map_free(map);
9782 return hash;
9785 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9787 return isl_map_get_hash((isl_map *)set);
9790 /* Check if the value for dimension dim is completely determined
9791 * by the values of the other parameters and variables.
9792 * That is, check if dimension dim is involved in an equality.
9794 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9796 int i;
9797 unsigned nparam;
9799 if (!bset)
9800 return -1;
9801 nparam = isl_basic_set_n_param(bset);
9802 for (i = 0; i < bset->n_eq; ++i)
9803 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9804 return 1;
9805 return 0;
9808 /* Check if the value for dimension dim is completely determined
9809 * by the values of the other parameters and variables.
9810 * That is, check if dimension dim is involved in an equality
9811 * for each of the subsets.
9813 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9815 int i;
9817 if (!set)
9818 return -1;
9819 for (i = 0; i < set->n; ++i) {
9820 int unique;
9821 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9822 if (unique != 1)
9823 return unique;
9825 return 1;
9828 int isl_set_n_basic_set(__isl_keep isl_set *set)
9830 return set ? set->n : 0;
9833 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9834 int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9836 int i;
9838 if (!map)
9839 return -1;
9841 for (i = 0; i < map->n; ++i)
9842 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9843 return -1;
9845 return 0;
9848 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9849 int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9851 int i;
9853 if (!set)
9854 return -1;
9856 for (i = 0; i < set->n; ++i)
9857 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9858 return -1;
9860 return 0;
9863 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9865 isl_space *dim;
9867 if (!bset)
9868 return NULL;
9870 bset = isl_basic_set_cow(bset);
9871 if (!bset)
9872 return NULL;
9874 dim = isl_basic_set_get_space(bset);
9875 dim = isl_space_lift(dim, bset->n_div);
9876 if (!dim)
9877 goto error;
9878 isl_space_free(bset->dim);
9879 bset->dim = dim;
9880 bset->extra -= bset->n_div;
9881 bset->n_div = 0;
9883 bset = isl_basic_set_finalize(bset);
9885 return bset;
9886 error:
9887 isl_basic_set_free(bset);
9888 return NULL;
9891 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9893 int i;
9894 isl_space *dim;
9895 unsigned n_div;
9897 set = isl_set_align_divs(set);
9899 if (!set)
9900 return NULL;
9902 set = isl_set_cow(set);
9903 if (!set)
9904 return NULL;
9906 n_div = set->p[0]->n_div;
9907 dim = isl_set_get_space(set);
9908 dim = isl_space_lift(dim, n_div);
9909 if (!dim)
9910 goto error;
9911 isl_space_free(set->dim);
9912 set->dim = dim;
9914 for (i = 0; i < set->n; ++i) {
9915 set->p[i] = isl_basic_set_lift(set->p[i]);
9916 if (!set->p[i])
9917 goto error;
9920 return set;
9921 error:
9922 isl_set_free(set);
9923 return NULL;
9926 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9928 isl_space *dim;
9929 struct isl_basic_map *bmap;
9930 unsigned n_set;
9931 unsigned n_div;
9932 unsigned n_param;
9933 unsigned total;
9934 int i, k, l;
9936 set = isl_set_align_divs(set);
9938 if (!set)
9939 return NULL;
9941 dim = isl_set_get_space(set);
9942 if (set->n == 0 || set->p[0]->n_div == 0) {
9943 isl_set_free(set);
9944 return isl_map_identity(isl_space_map_from_set(dim));
9947 n_div = set->p[0]->n_div;
9948 dim = isl_space_map_from_set(dim);
9949 n_param = isl_space_dim(dim, isl_dim_param);
9950 n_set = isl_space_dim(dim, isl_dim_in);
9951 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9952 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9953 for (i = 0; i < n_set; ++i)
9954 bmap = var_equal(bmap, i);
9956 total = n_param + n_set + n_set + n_div;
9957 for (i = 0; i < n_div; ++i) {
9958 k = isl_basic_map_alloc_inequality(bmap);
9959 if (k < 0)
9960 goto error;
9961 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9962 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9963 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9964 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9965 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9966 set->p[0]->div[i][0]);
9968 l = isl_basic_map_alloc_inequality(bmap);
9969 if (l < 0)
9970 goto error;
9971 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9972 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9973 set->p[0]->div[i][0]);
9974 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9977 isl_set_free(set);
9978 bmap = isl_basic_map_simplify(bmap);
9979 bmap = isl_basic_map_finalize(bmap);
9980 return isl_map_from_basic_map(bmap);
9981 error:
9982 isl_set_free(set);
9983 isl_basic_map_free(bmap);
9984 return NULL;
9987 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9989 unsigned dim;
9990 int size = 0;
9992 if (!bset)
9993 return -1;
9995 dim = isl_basic_set_total_dim(bset);
9996 size += bset->n_eq * (1 + dim);
9997 size += bset->n_ineq * (1 + dim);
9998 size += bset->n_div * (2 + dim);
10000 return size;
10003 int isl_set_size(__isl_keep isl_set *set)
10005 int i;
10006 int size = 0;
10008 if (!set)
10009 return -1;
10011 for (i = 0; i < set->n; ++i)
10012 size += isl_basic_set_size(set->p[i]);
10014 return size;
10017 /* Check if there is any lower bound (if lower == 0) and/or upper
10018 * bound (if upper == 0) on the specified dim.
10020 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10021 enum isl_dim_type type, unsigned pos, int lower, int upper)
10023 int i;
10025 if (!bmap)
10026 return -1;
10028 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
10030 pos += isl_basic_map_offset(bmap, type);
10032 for (i = 0; i < bmap->n_div; ++i) {
10033 if (isl_int_is_zero(bmap->div[i][0]))
10034 continue;
10035 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10036 return 1;
10039 for (i = 0; i < bmap->n_eq; ++i)
10040 if (!isl_int_is_zero(bmap->eq[i][pos]))
10041 return 1;
10043 for (i = 0; i < bmap->n_ineq; ++i) {
10044 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10045 if (sgn > 0)
10046 lower = 1;
10047 if (sgn < 0)
10048 upper = 1;
10051 return lower && upper;
10054 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10055 enum isl_dim_type type, unsigned pos)
10057 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10060 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10061 enum isl_dim_type type, unsigned pos)
10063 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10066 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10067 enum isl_dim_type type, unsigned pos)
10069 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10072 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10073 enum isl_dim_type type, unsigned pos)
10075 int i;
10077 if (!map)
10078 return -1;
10080 for (i = 0; i < map->n; ++i) {
10081 int bounded;
10082 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10083 if (bounded < 0 || !bounded)
10084 return bounded;
10087 return 1;
10090 /* Return 1 if the specified dim is involved in both an upper bound
10091 * and a lower bound.
10093 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10094 enum isl_dim_type type, unsigned pos)
10096 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10099 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10101 static int has_any_bound(__isl_keep isl_map *map,
10102 enum isl_dim_type type, unsigned pos,
10103 int (*fn)(__isl_keep isl_basic_map *bmap,
10104 enum isl_dim_type type, unsigned pos))
10106 int i;
10108 if (!map)
10109 return -1;
10111 for (i = 0; i < map->n; ++i) {
10112 int bounded;
10113 bounded = fn(map->p[i], type, pos);
10114 if (bounded < 0 || bounded)
10115 return bounded;
10118 return 0;
10121 /* Return 1 if the specified dim is involved in any lower bound.
10123 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10124 enum isl_dim_type type, unsigned pos)
10126 return has_any_bound(set, type, pos,
10127 &isl_basic_map_dim_has_lower_bound);
10130 /* Return 1 if the specified dim is involved in any upper bound.
10132 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10133 enum isl_dim_type type, unsigned pos)
10135 return has_any_bound(set, type, pos,
10136 &isl_basic_map_dim_has_upper_bound);
10139 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10141 static int has_bound(__isl_keep isl_map *map,
10142 enum isl_dim_type type, unsigned pos,
10143 int (*fn)(__isl_keep isl_basic_map *bmap,
10144 enum isl_dim_type type, unsigned pos))
10146 int i;
10148 if (!map)
10149 return -1;
10151 for (i = 0; i < map->n; ++i) {
10152 int bounded;
10153 bounded = fn(map->p[i], type, pos);
10154 if (bounded < 0 || !bounded)
10155 return bounded;
10158 return 1;
10161 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10163 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10164 enum isl_dim_type type, unsigned pos)
10166 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10169 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10171 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10172 enum isl_dim_type type, unsigned pos)
10174 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10177 /* For each of the "n" variables starting at "first", determine
10178 * the sign of the variable and put the results in the first "n"
10179 * elements of the array "signs".
10180 * Sign
10181 * 1 means that the variable is non-negative
10182 * -1 means that the variable is non-positive
10183 * 0 means the variable attains both positive and negative values.
10185 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10186 unsigned first, unsigned n, int *signs)
10188 isl_vec *bound = NULL;
10189 struct isl_tab *tab = NULL;
10190 struct isl_tab_undo *snap;
10191 int i;
10193 if (!bset || !signs)
10194 return -1;
10196 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10197 tab = isl_tab_from_basic_set(bset, 0);
10198 if (!bound || !tab)
10199 goto error;
10201 isl_seq_clr(bound->el, bound->size);
10202 isl_int_set_si(bound->el[0], -1);
10204 snap = isl_tab_snap(tab);
10205 for (i = 0; i < n; ++i) {
10206 int empty;
10208 isl_int_set_si(bound->el[1 + first + i], -1);
10209 if (isl_tab_add_ineq(tab, bound->el) < 0)
10210 goto error;
10211 empty = tab->empty;
10212 isl_int_set_si(bound->el[1 + first + i], 0);
10213 if (isl_tab_rollback(tab, snap) < 0)
10214 goto error;
10216 if (empty) {
10217 signs[i] = 1;
10218 continue;
10221 isl_int_set_si(bound->el[1 + first + i], 1);
10222 if (isl_tab_add_ineq(tab, bound->el) < 0)
10223 goto error;
10224 empty = tab->empty;
10225 isl_int_set_si(bound->el[1 + first + i], 0);
10226 if (isl_tab_rollback(tab, snap) < 0)
10227 goto error;
10229 signs[i] = empty ? -1 : 0;
10232 isl_tab_free(tab);
10233 isl_vec_free(bound);
10234 return 0;
10235 error:
10236 isl_tab_free(tab);
10237 isl_vec_free(bound);
10238 return -1;
10241 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10242 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10244 if (!bset || !signs)
10245 return -1;
10246 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10247 return -1);
10249 first += pos(bset->dim, type) - 1;
10250 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10253 /* Check if the given basic map is obviously single-valued.
10254 * In particular, for each output dimension, check that there is
10255 * an equality that defines the output dimension in terms of
10256 * earlier dimensions.
10258 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10260 int i, j;
10261 unsigned total;
10262 unsigned n_out;
10263 unsigned o_out;
10265 if (!bmap)
10266 return -1;
10268 total = 1 + isl_basic_map_total_dim(bmap);
10269 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10270 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10272 for (i = 0; i < n_out; ++i) {
10273 for (j = 0; j < bmap->n_eq; ++j) {
10274 if (isl_int_is_zero(bmap->eq[j][o_out + i]))
10275 continue;
10276 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
10277 total - (o_out + i + 1)) == -1)
10278 break;
10280 if (j >= bmap->n_eq)
10281 return 0;
10284 return 1;
10287 /* Check if the given basic map is single-valued.
10288 * We simply compute
10290 * M \circ M^-1
10292 * and check if the result is a subset of the identity mapping.
10294 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10296 isl_space *space;
10297 isl_basic_map *test;
10298 isl_basic_map *id;
10299 int sv;
10301 sv = isl_basic_map_plain_is_single_valued(bmap);
10302 if (sv < 0 || sv)
10303 return sv;
10305 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10306 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10308 space = isl_basic_map_get_space(bmap);
10309 space = isl_space_map_from_set(isl_space_range(space));
10310 id = isl_basic_map_identity(space);
10312 sv = isl_basic_map_is_subset(test, id);
10314 isl_basic_map_free(test);
10315 isl_basic_map_free(id);
10317 return sv;
10320 /* Check if the given map is obviously single-valued.
10322 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10324 if (!map)
10325 return -1;
10326 if (map->n == 0)
10327 return 1;
10328 if (map->n >= 2)
10329 return 0;
10331 return isl_basic_map_plain_is_single_valued(map->p[0]);
10334 /* Check if the given map is single-valued.
10335 * We simply compute
10337 * M \circ M^-1
10339 * and check if the result is a subset of the identity mapping.
10341 int isl_map_is_single_valued(__isl_keep isl_map *map)
10343 isl_space *dim;
10344 isl_map *test;
10345 isl_map *id;
10346 int sv;
10348 sv = isl_map_plain_is_single_valued(map);
10349 if (sv < 0 || sv)
10350 return sv;
10352 test = isl_map_reverse(isl_map_copy(map));
10353 test = isl_map_apply_range(test, isl_map_copy(map));
10355 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10356 id = isl_map_identity(dim);
10358 sv = isl_map_is_subset(test, id);
10360 isl_map_free(test);
10361 isl_map_free(id);
10363 return sv;
10366 int isl_map_is_injective(__isl_keep isl_map *map)
10368 int in;
10370 map = isl_map_copy(map);
10371 map = isl_map_reverse(map);
10372 in = isl_map_is_single_valued(map);
10373 isl_map_free(map);
10375 return in;
10378 /* Check if the given map is obviously injective.
10380 int isl_map_plain_is_injective(__isl_keep isl_map *map)
10382 int in;
10384 map = isl_map_copy(map);
10385 map = isl_map_reverse(map);
10386 in = isl_map_plain_is_single_valued(map);
10387 isl_map_free(map);
10389 return in;
10392 int isl_map_is_bijective(__isl_keep isl_map *map)
10394 int sv;
10396 sv = isl_map_is_single_valued(map);
10397 if (sv < 0 || !sv)
10398 return sv;
10400 return isl_map_is_injective(map);
10403 int isl_set_is_singleton(__isl_keep isl_set *set)
10405 return isl_map_is_single_valued((isl_map *)set);
10408 int isl_map_is_translation(__isl_keep isl_map *map)
10410 int ok;
10411 isl_set *delta;
10413 delta = isl_map_deltas(isl_map_copy(map));
10414 ok = isl_set_is_singleton(delta);
10415 isl_set_free(delta);
10417 return ok;
10420 static int unique(isl_int *p, unsigned pos, unsigned len)
10422 if (isl_seq_first_non_zero(p, pos) != -1)
10423 return 0;
10424 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10425 return 0;
10426 return 1;
10429 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10431 int i, j;
10432 unsigned nvar;
10433 unsigned ovar;
10435 if (!bset)
10436 return -1;
10438 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10439 return 0;
10441 nvar = isl_basic_set_dim(bset, isl_dim_set);
10442 ovar = isl_space_offset(bset->dim, isl_dim_set);
10443 for (j = 0; j < nvar; ++j) {
10444 int lower = 0, upper = 0;
10445 for (i = 0; i < bset->n_eq; ++i) {
10446 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10447 continue;
10448 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10449 return 0;
10450 break;
10452 if (i < bset->n_eq)
10453 continue;
10454 for (i = 0; i < bset->n_ineq; ++i) {
10455 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10456 continue;
10457 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10458 return 0;
10459 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10460 lower = 1;
10461 else
10462 upper = 1;
10464 if (!lower || !upper)
10465 return 0;
10468 return 1;
10471 int isl_set_is_box(__isl_keep isl_set *set)
10473 if (!set)
10474 return -1;
10475 if (set->n != 1)
10476 return 0;
10478 return isl_basic_set_is_box(set->p[0]);
10481 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10483 if (!bset)
10484 return -1;
10486 return isl_space_is_wrapping(bset->dim);
10489 int isl_set_is_wrapping(__isl_keep isl_set *set)
10491 if (!set)
10492 return -1;
10494 return isl_space_is_wrapping(set->dim);
10497 /* Is the domain of "map" a wrapped relation?
10499 int isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10501 if (!map)
10502 return -1;
10504 return isl_space_domain_is_wrapping(map->dim);
10507 /* Is the range of "map" a wrapped relation?
10509 int isl_map_range_is_wrapping(__isl_keep isl_map *map)
10511 if (!map)
10512 return -1;
10514 return isl_space_range_is_wrapping(map->dim);
10517 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10519 bmap = isl_basic_map_cow(bmap);
10520 if (!bmap)
10521 return NULL;
10523 bmap->dim = isl_space_wrap(bmap->dim);
10524 if (!bmap->dim)
10525 goto error;
10527 bmap = isl_basic_map_finalize(bmap);
10529 return (isl_basic_set *)bmap;
10530 error:
10531 isl_basic_map_free(bmap);
10532 return NULL;
10535 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10537 int i;
10539 map = isl_map_cow(map);
10540 if (!map)
10541 return NULL;
10543 for (i = 0; i < map->n; ++i) {
10544 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10545 if (!map->p[i])
10546 goto error;
10548 map->dim = isl_space_wrap(map->dim);
10549 if (!map->dim)
10550 goto error;
10552 return (isl_set *)map;
10553 error:
10554 isl_map_free(map);
10555 return NULL;
10558 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10560 bset = isl_basic_set_cow(bset);
10561 if (!bset)
10562 return NULL;
10564 bset->dim = isl_space_unwrap(bset->dim);
10565 if (!bset->dim)
10566 goto error;
10568 bset = isl_basic_set_finalize(bset);
10570 return (isl_basic_map *)bset;
10571 error:
10572 isl_basic_set_free(bset);
10573 return NULL;
10576 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10578 int i;
10580 if (!set)
10581 return NULL;
10583 if (!isl_set_is_wrapping(set))
10584 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10585 goto error);
10587 set = isl_set_cow(set);
10588 if (!set)
10589 return NULL;
10591 for (i = 0; i < set->n; ++i) {
10592 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10593 if (!set->p[i])
10594 goto error;
10597 set->dim = isl_space_unwrap(set->dim);
10598 if (!set->dim)
10599 goto error;
10601 return (isl_map *)set;
10602 error:
10603 isl_set_free(set);
10604 return NULL;
10607 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10608 enum isl_dim_type type)
10610 if (!bmap)
10611 return NULL;
10613 if (!isl_space_is_named_or_nested(bmap->dim, type))
10614 return bmap;
10616 bmap = isl_basic_map_cow(bmap);
10617 if (!bmap)
10618 return NULL;
10620 bmap->dim = isl_space_reset(bmap->dim, type);
10621 if (!bmap->dim)
10622 goto error;
10624 bmap = isl_basic_map_finalize(bmap);
10626 return bmap;
10627 error:
10628 isl_basic_map_free(bmap);
10629 return NULL;
10632 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10633 enum isl_dim_type type)
10635 int i;
10637 if (!map)
10638 return NULL;
10640 if (!isl_space_is_named_or_nested(map->dim, type))
10641 return map;
10643 map = isl_map_cow(map);
10644 if (!map)
10645 return NULL;
10647 for (i = 0; i < map->n; ++i) {
10648 map->p[i] = isl_basic_map_reset(map->p[i], type);
10649 if (!map->p[i])
10650 goto error;
10652 map->dim = isl_space_reset(map->dim, type);
10653 if (!map->dim)
10654 goto error;
10656 return map;
10657 error:
10658 isl_map_free(map);
10659 return NULL;
10662 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10664 if (!bmap)
10665 return NULL;
10667 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10668 return bmap;
10670 bmap = isl_basic_map_cow(bmap);
10671 if (!bmap)
10672 return NULL;
10674 bmap->dim = isl_space_flatten(bmap->dim);
10675 if (!bmap->dim)
10676 goto error;
10678 bmap = isl_basic_map_finalize(bmap);
10680 return bmap;
10681 error:
10682 isl_basic_map_free(bmap);
10683 return NULL;
10686 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10688 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10691 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10692 __isl_take isl_basic_map *bmap)
10694 if (!bmap)
10695 return NULL;
10697 if (!bmap->dim->nested[0])
10698 return bmap;
10700 bmap = isl_basic_map_cow(bmap);
10701 if (!bmap)
10702 return NULL;
10704 bmap->dim = isl_space_flatten_domain(bmap->dim);
10705 if (!bmap->dim)
10706 goto error;
10708 bmap = isl_basic_map_finalize(bmap);
10710 return bmap;
10711 error:
10712 isl_basic_map_free(bmap);
10713 return NULL;
10716 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10717 __isl_take isl_basic_map *bmap)
10719 if (!bmap)
10720 return NULL;
10722 if (!bmap->dim->nested[1])
10723 return bmap;
10725 bmap = isl_basic_map_cow(bmap);
10726 if (!bmap)
10727 return NULL;
10729 bmap->dim = isl_space_flatten_range(bmap->dim);
10730 if (!bmap->dim)
10731 goto error;
10733 bmap = isl_basic_map_finalize(bmap);
10735 return bmap;
10736 error:
10737 isl_basic_map_free(bmap);
10738 return NULL;
10741 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10743 int i;
10745 if (!map)
10746 return NULL;
10748 if (!map->dim->nested[0] && !map->dim->nested[1])
10749 return map;
10751 map = isl_map_cow(map);
10752 if (!map)
10753 return NULL;
10755 for (i = 0; i < map->n; ++i) {
10756 map->p[i] = isl_basic_map_flatten(map->p[i]);
10757 if (!map->p[i])
10758 goto error;
10760 map->dim = isl_space_flatten(map->dim);
10761 if (!map->dim)
10762 goto error;
10764 return map;
10765 error:
10766 isl_map_free(map);
10767 return NULL;
10770 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10772 return (isl_set *)isl_map_flatten((isl_map *)set);
10775 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10777 isl_space *dim, *flat_dim;
10778 isl_map *map;
10780 dim = isl_set_get_space(set);
10781 flat_dim = isl_space_flatten(isl_space_copy(dim));
10782 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10783 map = isl_map_intersect_domain(map, set);
10785 return map;
10788 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10790 int i;
10792 if (!map)
10793 return NULL;
10795 if (!map->dim->nested[0])
10796 return map;
10798 map = isl_map_cow(map);
10799 if (!map)
10800 return NULL;
10802 for (i = 0; i < map->n; ++i) {
10803 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10804 if (!map->p[i])
10805 goto error;
10807 map->dim = isl_space_flatten_domain(map->dim);
10808 if (!map->dim)
10809 goto error;
10811 return map;
10812 error:
10813 isl_map_free(map);
10814 return NULL;
10817 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10819 int i;
10821 if (!map)
10822 return NULL;
10824 if (!map->dim->nested[1])
10825 return map;
10827 map = isl_map_cow(map);
10828 if (!map)
10829 return NULL;
10831 for (i = 0; i < map->n; ++i) {
10832 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10833 if (!map->p[i])
10834 goto error;
10836 map->dim = isl_space_flatten_range(map->dim);
10837 if (!map->dim)
10838 goto error;
10840 return map;
10841 error:
10842 isl_map_free(map);
10843 return NULL;
10846 /* Reorder the dimensions of "bmap" according to the given dim_map
10847 * and set the dimension specification to "dim".
10849 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10850 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10852 isl_basic_map *res;
10853 unsigned flags;
10855 bmap = isl_basic_map_cow(bmap);
10856 if (!bmap || !dim || !dim_map)
10857 goto error;
10859 flags = bmap->flags;
10860 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10861 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10862 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10863 res = isl_basic_map_alloc_space(dim,
10864 bmap->n_div, bmap->n_eq, bmap->n_ineq);
10865 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10866 if (res)
10867 res->flags = flags;
10868 res = isl_basic_map_finalize(res);
10869 return res;
10870 error:
10871 free(dim_map);
10872 isl_basic_map_free(bmap);
10873 isl_space_free(dim);
10874 return NULL;
10877 /* Reorder the dimensions of "map" according to given reordering.
10879 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10880 __isl_take isl_reordering *r)
10882 int i;
10883 struct isl_dim_map *dim_map;
10885 map = isl_map_cow(map);
10886 dim_map = isl_dim_map_from_reordering(r);
10887 if (!map || !r || !dim_map)
10888 goto error;
10890 for (i = 0; i < map->n; ++i) {
10891 struct isl_dim_map *dim_map_i;
10893 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10895 map->p[i] = isl_basic_map_realign(map->p[i],
10896 isl_space_copy(r->dim), dim_map_i);
10898 if (!map->p[i])
10899 goto error;
10902 map = isl_map_reset_space(map, isl_space_copy(r->dim));
10904 isl_reordering_free(r);
10905 free(dim_map);
10906 return map;
10907 error:
10908 free(dim_map);
10909 isl_map_free(map);
10910 isl_reordering_free(r);
10911 return NULL;
10914 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10915 __isl_take isl_reordering *r)
10917 return (isl_set *)isl_map_realign((isl_map *)set, r);
10920 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10921 __isl_take isl_space *model)
10923 isl_ctx *ctx;
10925 if (!map || !model)
10926 goto error;
10928 ctx = isl_space_get_ctx(model);
10929 if (!isl_space_has_named_params(model))
10930 isl_die(ctx, isl_error_invalid,
10931 "model has unnamed parameters", goto error);
10932 if (!isl_space_has_named_params(map->dim))
10933 isl_die(ctx, isl_error_invalid,
10934 "relation has unnamed parameters", goto error);
10935 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10936 isl_reordering *exp;
10938 model = isl_space_drop_dims(model, isl_dim_in,
10939 0, isl_space_dim(model, isl_dim_in));
10940 model = isl_space_drop_dims(model, isl_dim_out,
10941 0, isl_space_dim(model, isl_dim_out));
10942 exp = isl_parameter_alignment_reordering(map->dim, model);
10943 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10944 map = isl_map_realign(map, exp);
10947 isl_space_free(model);
10948 return map;
10949 error:
10950 isl_space_free(model);
10951 isl_map_free(map);
10952 return NULL;
10955 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10956 __isl_take isl_space *model)
10958 return isl_map_align_params(set, model);
10961 /* Align the parameters of "bmap" to those of "model", introducing
10962 * additional parameters if needed.
10964 __isl_give isl_basic_map *isl_basic_map_align_params(
10965 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10967 isl_ctx *ctx;
10969 if (!bmap || !model)
10970 goto error;
10972 ctx = isl_space_get_ctx(model);
10973 if (!isl_space_has_named_params(model))
10974 isl_die(ctx, isl_error_invalid,
10975 "model has unnamed parameters", goto error);
10976 if (!isl_space_has_named_params(bmap->dim))
10977 isl_die(ctx, isl_error_invalid,
10978 "relation has unnamed parameters", goto error);
10979 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10980 isl_reordering *exp;
10981 struct isl_dim_map *dim_map;
10983 model = isl_space_drop_dims(model, isl_dim_in,
10984 0, isl_space_dim(model, isl_dim_in));
10985 model = isl_space_drop_dims(model, isl_dim_out,
10986 0, isl_space_dim(model, isl_dim_out));
10987 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10988 exp = isl_reordering_extend_space(exp,
10989 isl_basic_map_get_space(bmap));
10990 dim_map = isl_dim_map_from_reordering(exp);
10991 bmap = isl_basic_map_realign(bmap,
10992 exp ? isl_space_copy(exp->dim) : NULL,
10993 isl_dim_map_extend(dim_map, bmap));
10994 isl_reordering_free(exp);
10995 free(dim_map);
10998 isl_space_free(model);
10999 return bmap;
11000 error:
11001 isl_space_free(model);
11002 isl_basic_map_free(bmap);
11003 return NULL;
11006 /* Align the parameters of "bset" to those of "model", introducing
11007 * additional parameters if needed.
11009 __isl_give isl_basic_set *isl_basic_set_align_params(
11010 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11012 return isl_basic_map_align_params(bset, model);
11015 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11016 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11017 enum isl_dim_type c2, enum isl_dim_type c3,
11018 enum isl_dim_type c4, enum isl_dim_type c5)
11020 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11021 struct isl_mat *mat;
11022 int i, j, k;
11023 int pos;
11025 if (!bmap)
11026 return NULL;
11027 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11028 isl_basic_map_total_dim(bmap) + 1);
11029 if (!mat)
11030 return NULL;
11031 for (i = 0; i < bmap->n_eq; ++i)
11032 for (j = 0, pos = 0; j < 5; ++j) {
11033 int off = isl_basic_map_offset(bmap, c[j]);
11034 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11035 isl_int_set(mat->row[i][pos],
11036 bmap->eq[i][off + k]);
11037 ++pos;
11041 return mat;
11044 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11045 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11046 enum isl_dim_type c2, enum isl_dim_type c3,
11047 enum isl_dim_type c4, enum isl_dim_type c5)
11049 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11050 struct isl_mat *mat;
11051 int i, j, k;
11052 int pos;
11054 if (!bmap)
11055 return NULL;
11056 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11057 isl_basic_map_total_dim(bmap) + 1);
11058 if (!mat)
11059 return NULL;
11060 for (i = 0; i < bmap->n_ineq; ++i)
11061 for (j = 0, pos = 0; j < 5; ++j) {
11062 int off = isl_basic_map_offset(bmap, c[j]);
11063 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11064 isl_int_set(mat->row[i][pos],
11065 bmap->ineq[i][off + k]);
11066 ++pos;
11070 return mat;
11073 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11074 __isl_take isl_space *dim,
11075 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11076 enum isl_dim_type c2, enum isl_dim_type c3,
11077 enum isl_dim_type c4, enum isl_dim_type c5)
11079 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11080 isl_basic_map *bmap;
11081 unsigned total;
11082 unsigned extra;
11083 int i, j, k, l;
11084 int pos;
11086 if (!dim || !eq || !ineq)
11087 goto error;
11089 if (eq->n_col != ineq->n_col)
11090 isl_die(dim->ctx, isl_error_invalid,
11091 "equalities and inequalities matrices should have "
11092 "same number of columns", goto error);
11094 total = 1 + isl_space_dim(dim, isl_dim_all);
11096 if (eq->n_col < total)
11097 isl_die(dim->ctx, isl_error_invalid,
11098 "number of columns too small", goto error);
11100 extra = eq->n_col - total;
11102 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11103 eq->n_row, ineq->n_row);
11104 if (!bmap)
11105 goto error;
11106 for (i = 0; i < extra; ++i) {
11107 k = isl_basic_map_alloc_div(bmap);
11108 if (k < 0)
11109 goto error;
11110 isl_int_set_si(bmap->div[k][0], 0);
11112 for (i = 0; i < eq->n_row; ++i) {
11113 l = isl_basic_map_alloc_equality(bmap);
11114 if (l < 0)
11115 goto error;
11116 for (j = 0, pos = 0; j < 5; ++j) {
11117 int off = isl_basic_map_offset(bmap, c[j]);
11118 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11119 isl_int_set(bmap->eq[l][off + k],
11120 eq->row[i][pos]);
11121 ++pos;
11125 for (i = 0; i < ineq->n_row; ++i) {
11126 l = isl_basic_map_alloc_inequality(bmap);
11127 if (l < 0)
11128 goto error;
11129 for (j = 0, pos = 0; j < 5; ++j) {
11130 int off = isl_basic_map_offset(bmap, c[j]);
11131 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11132 isl_int_set(bmap->ineq[l][off + k],
11133 ineq->row[i][pos]);
11134 ++pos;
11139 isl_space_free(dim);
11140 isl_mat_free(eq);
11141 isl_mat_free(ineq);
11143 bmap = isl_basic_map_simplify(bmap);
11144 return isl_basic_map_finalize(bmap);
11145 error:
11146 isl_space_free(dim);
11147 isl_mat_free(eq);
11148 isl_mat_free(ineq);
11149 return NULL;
11152 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11153 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11154 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11156 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11157 c1, c2, c3, c4, isl_dim_in);
11160 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11161 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11162 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11164 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11165 c1, c2, c3, c4, isl_dim_in);
11168 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11169 __isl_take isl_space *dim,
11170 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11171 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11173 return (isl_basic_set*)
11174 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11175 c1, c2, c3, c4, isl_dim_in);
11178 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11180 if (!bmap)
11181 return -1;
11183 return isl_space_can_zip(bmap->dim);
11186 int isl_map_can_zip(__isl_keep isl_map *map)
11188 if (!map)
11189 return -1;
11191 return isl_space_can_zip(map->dim);
11194 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11195 * (A -> C) -> (B -> D).
11197 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11199 unsigned pos;
11200 unsigned n1;
11201 unsigned n2;
11203 if (!bmap)
11204 return NULL;
11206 if (!isl_basic_map_can_zip(bmap))
11207 isl_die(bmap->ctx, isl_error_invalid,
11208 "basic map cannot be zipped", goto error);
11209 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11210 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11211 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11212 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11213 bmap = isl_basic_map_cow(bmap);
11214 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11215 if (!bmap)
11216 return NULL;
11217 bmap->dim = isl_space_zip(bmap->dim);
11218 if (!bmap->dim)
11219 goto error;
11220 return bmap;
11221 error:
11222 isl_basic_map_free(bmap);
11223 return NULL;
11226 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11227 * (A -> C) -> (B -> D).
11229 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11231 int i;
11233 if (!map)
11234 return NULL;
11236 if (!isl_map_can_zip(map))
11237 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11238 goto error);
11240 map = isl_map_cow(map);
11241 if (!map)
11242 return NULL;
11244 for (i = 0; i < map->n; ++i) {
11245 map->p[i] = isl_basic_map_zip(map->p[i]);
11246 if (!map->p[i])
11247 goto error;
11250 map->dim = isl_space_zip(map->dim);
11251 if (!map->dim)
11252 goto error;
11254 return map;
11255 error:
11256 isl_map_free(map);
11257 return NULL;
11260 /* Can we apply isl_basic_map_curry to "bmap"?
11261 * That is, does it have a nested relation in its domain?
11263 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11265 if (!bmap)
11266 return -1;
11268 return isl_space_can_curry(bmap->dim);
11271 /* Can we apply isl_map_curry to "map"?
11272 * That is, does it have a nested relation in its domain?
11274 int isl_map_can_curry(__isl_keep isl_map *map)
11276 if (!map)
11277 return -1;
11279 return isl_space_can_curry(map->dim);
11282 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11283 * A -> (B -> C).
11285 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11288 if (!bmap)
11289 return NULL;
11291 if (!isl_basic_map_can_curry(bmap))
11292 isl_die(bmap->ctx, isl_error_invalid,
11293 "basic map cannot be curried", goto error);
11294 bmap = isl_basic_map_cow(bmap);
11295 if (!bmap)
11296 return NULL;
11297 bmap->dim = isl_space_curry(bmap->dim);
11298 if (!bmap->dim)
11299 goto error;
11300 return bmap;
11301 error:
11302 isl_basic_map_free(bmap);
11303 return NULL;
11306 /* Given a map (A -> B) -> C, return the corresponding map
11307 * A -> (B -> C).
11309 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11311 int i;
11313 if (!map)
11314 return NULL;
11316 if (!isl_map_can_curry(map))
11317 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
11318 goto error);
11320 map = isl_map_cow(map);
11321 if (!map)
11322 return NULL;
11324 for (i = 0; i < map->n; ++i) {
11325 map->p[i] = isl_basic_map_curry(map->p[i]);
11326 if (!map->p[i])
11327 goto error;
11330 map->dim = isl_space_curry(map->dim);
11331 if (!map->dim)
11332 goto error;
11334 return map;
11335 error:
11336 isl_map_free(map);
11337 return NULL;
11340 /* Can we apply isl_basic_map_uncurry to "bmap"?
11341 * That is, does it have a nested relation in its domain?
11343 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11345 if (!bmap)
11346 return -1;
11348 return isl_space_can_uncurry(bmap->dim);
11351 /* Can we apply isl_map_uncurry to "map"?
11352 * That is, does it have a nested relation in its domain?
11354 int isl_map_can_uncurry(__isl_keep isl_map *map)
11356 if (!map)
11357 return -1;
11359 return isl_space_can_uncurry(map->dim);
11362 /* Given a basic map A -> (B -> C), return the corresponding basic map
11363 * (A -> B) -> C.
11365 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11368 if (!bmap)
11369 return NULL;
11371 if (!isl_basic_map_can_uncurry(bmap))
11372 isl_die(bmap->ctx, isl_error_invalid,
11373 "basic map cannot be uncurried",
11374 return isl_basic_map_free(bmap));
11375 bmap = isl_basic_map_cow(bmap);
11376 if (!bmap)
11377 return NULL;
11378 bmap->dim = isl_space_uncurry(bmap->dim);
11379 if (!bmap->dim)
11380 return isl_basic_map_free(bmap);
11381 return bmap;
11384 /* Given a map A -> (B -> C), return the corresponding map
11385 * (A -> B) -> C.
11387 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11389 int i;
11391 if (!map)
11392 return NULL;
11394 if (!isl_map_can_uncurry(map))
11395 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
11396 return isl_map_free(map));
11398 map = isl_map_cow(map);
11399 if (!map)
11400 return NULL;
11402 for (i = 0; i < map->n; ++i) {
11403 map->p[i] = isl_basic_map_uncurry(map->p[i]);
11404 if (!map->p[i])
11405 return isl_map_free(map);
11408 map->dim = isl_space_uncurry(map->dim);
11409 if (!map->dim)
11410 return isl_map_free(map);
11412 return map;
11415 /* Construct a basic map mapping the domain of the affine expression
11416 * to a one-dimensional range prescribed by the affine expression.
11418 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11420 int k;
11421 int pos;
11422 isl_local_space *ls;
11423 isl_basic_map *bmap;
11425 if (!aff)
11426 return NULL;
11428 ls = isl_aff_get_local_space(aff);
11429 bmap = isl_basic_map_from_local_space(ls);
11430 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11431 k = isl_basic_map_alloc_equality(bmap);
11432 if (k < 0)
11433 goto error;
11435 pos = isl_basic_map_offset(bmap, isl_dim_out);
11436 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11437 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11438 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11439 aff->v->size - (pos + 1));
11441 isl_aff_free(aff);
11442 bmap = isl_basic_map_finalize(bmap);
11443 return bmap;
11444 error:
11445 isl_aff_free(aff);
11446 isl_basic_map_free(bmap);
11447 return NULL;
11450 /* Construct a map mapping the domain of the affine expression
11451 * to a one-dimensional range prescribed by the affine expression.
11453 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11455 isl_basic_map *bmap;
11457 bmap = isl_basic_map_from_aff(aff);
11458 return isl_map_from_basic_map(bmap);
11461 /* Construct a basic map mapping the domain the multi-affine expression
11462 * to its range, with each dimension in the range equated to the
11463 * corresponding affine expression.
11465 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11466 __isl_take isl_multi_aff *maff)
11468 int i;
11469 isl_space *space;
11470 isl_basic_map *bmap;
11472 if (!maff)
11473 return NULL;
11475 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11476 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11477 "invalid space", goto error);
11479 space = isl_space_domain(isl_multi_aff_get_space(maff));
11480 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11482 for (i = 0; i < maff->n; ++i) {
11483 isl_aff *aff;
11484 isl_basic_map *bmap_i;
11486 aff = isl_aff_copy(maff->p[i]);
11487 bmap_i = isl_basic_map_from_aff(aff);
11489 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11492 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11494 isl_multi_aff_free(maff);
11495 return bmap;
11496 error:
11497 isl_multi_aff_free(maff);
11498 return NULL;
11501 /* Construct a map mapping the domain the multi-affine expression
11502 * to its range, with each dimension in the range equated to the
11503 * corresponding affine expression.
11505 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11507 isl_basic_map *bmap;
11509 bmap = isl_basic_map_from_multi_aff(maff);
11510 return isl_map_from_basic_map(bmap);
11513 /* Construct a basic map mapping a domain in the given space to
11514 * to an n-dimensional range, with n the number of elements in the list,
11515 * where each coordinate in the range is prescribed by the
11516 * corresponding affine expression.
11517 * The domains of all affine expressions in the list are assumed to match
11518 * domain_dim.
11520 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11521 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11523 int i;
11524 isl_space *dim;
11525 isl_basic_map *bmap;
11527 if (!list)
11528 return NULL;
11530 dim = isl_space_from_domain(domain_dim);
11531 bmap = isl_basic_map_universe(dim);
11533 for (i = 0; i < list->n; ++i) {
11534 isl_aff *aff;
11535 isl_basic_map *bmap_i;
11537 aff = isl_aff_copy(list->p[i]);
11538 bmap_i = isl_basic_map_from_aff(aff);
11540 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11543 isl_aff_list_free(list);
11544 return bmap;
11547 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11548 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11550 return isl_map_equate(set, type1, pos1, type2, pos2);
11553 /* Construct a basic map where the given dimensions are equal to each other.
11555 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11556 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11558 isl_basic_map *bmap = NULL;
11559 int i;
11561 if (!space)
11562 return NULL;
11564 if (pos1 >= isl_space_dim(space, type1))
11565 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11566 "index out of bounds", goto error);
11567 if (pos2 >= isl_space_dim(space, type2))
11568 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11569 "index out of bounds", goto error);
11571 if (type1 == type2 && pos1 == pos2)
11572 return isl_basic_map_universe(space);
11574 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11575 i = isl_basic_map_alloc_equality(bmap);
11576 if (i < 0)
11577 goto error;
11578 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11579 pos1 += isl_basic_map_offset(bmap, type1);
11580 pos2 += isl_basic_map_offset(bmap, type2);
11581 isl_int_set_si(bmap->eq[i][pos1], -1);
11582 isl_int_set_si(bmap->eq[i][pos2], 1);
11583 bmap = isl_basic_map_finalize(bmap);
11584 isl_space_free(space);
11585 return bmap;
11586 error:
11587 isl_space_free(space);
11588 isl_basic_map_free(bmap);
11589 return NULL;
11592 /* Add a constraint imposing that the given two dimensions are equal.
11594 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11595 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11597 isl_basic_map *eq;
11599 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11601 bmap = isl_basic_map_intersect(bmap, eq);
11603 return bmap;
11606 /* Add a constraint imposing that the given two dimensions are equal.
11608 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11609 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11611 isl_basic_map *bmap;
11613 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11615 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11617 return map;
11620 /* Add a constraint imposing that the given two dimensions have opposite values.
11622 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11623 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11625 isl_basic_map *bmap = NULL;
11626 int i;
11628 if (!map)
11629 return NULL;
11631 if (pos1 >= isl_map_dim(map, type1))
11632 isl_die(map->ctx, isl_error_invalid,
11633 "index out of bounds", goto error);
11634 if (pos2 >= isl_map_dim(map, type2))
11635 isl_die(map->ctx, isl_error_invalid,
11636 "index out of bounds", goto error);
11638 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11639 i = isl_basic_map_alloc_equality(bmap);
11640 if (i < 0)
11641 goto error;
11642 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11643 pos1 += isl_basic_map_offset(bmap, type1);
11644 pos2 += isl_basic_map_offset(bmap, type2);
11645 isl_int_set_si(bmap->eq[i][pos1], 1);
11646 isl_int_set_si(bmap->eq[i][pos2], 1);
11647 bmap = isl_basic_map_finalize(bmap);
11649 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11651 return map;
11652 error:
11653 isl_basic_map_free(bmap);
11654 isl_map_free(map);
11655 return NULL;
11658 /* Construct a constraint imposing that the value of the first dimension is
11659 * greater than or equal to that of the second.
11661 static __isl_give isl_constraint *constraint_order_ge(
11662 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11663 enum isl_dim_type type2, int pos2)
11665 isl_constraint *c;
11667 if (!space)
11668 return NULL;
11670 c = isl_inequality_alloc(isl_local_space_from_space(space));
11672 if (pos1 >= isl_constraint_dim(c, type1))
11673 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11674 "index out of bounds", return isl_constraint_free(c));
11675 if (pos2 >= isl_constraint_dim(c, type2))
11676 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11677 "index out of bounds", return isl_constraint_free(c));
11679 if (type1 == type2 && pos1 == pos2)
11680 return c;
11682 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11683 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11685 return c;
11688 /* Add a constraint imposing that the value of the first dimension is
11689 * greater than or equal to that of the second.
11691 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11692 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11694 isl_constraint *c;
11695 isl_space *space;
11697 if (type1 == type2 && pos1 == pos2)
11698 return bmap;
11699 space = isl_basic_map_get_space(bmap);
11700 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11701 bmap = isl_basic_map_add_constraint(bmap, c);
11703 return bmap;
11706 /* Add a constraint imposing that the value of the first dimension is
11707 * greater than or equal to that of the second.
11709 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11710 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11712 isl_constraint *c;
11713 isl_space *space;
11715 if (type1 == type2 && pos1 == pos2)
11716 return map;
11717 space = isl_map_get_space(map);
11718 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11719 map = isl_map_add_constraint(map, c);
11721 return map;
11724 /* Add a constraint imposing that the value of the first dimension is
11725 * less than or equal to that of the second.
11727 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11728 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11730 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11733 /* Construct a basic map where the value of the first dimension is
11734 * greater than that of the second.
11736 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11737 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11739 isl_basic_map *bmap = NULL;
11740 int i;
11742 if (!space)
11743 return NULL;
11745 if (pos1 >= isl_space_dim(space, type1))
11746 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11747 "index out of bounds", goto error);
11748 if (pos2 >= isl_space_dim(space, type2))
11749 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11750 "index out of bounds", goto error);
11752 if (type1 == type2 && pos1 == pos2)
11753 return isl_basic_map_empty(space);
11755 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11756 i = isl_basic_map_alloc_inequality(bmap);
11757 if (i < 0)
11758 return isl_basic_map_free(bmap);
11759 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11760 pos1 += isl_basic_map_offset(bmap, type1);
11761 pos2 += isl_basic_map_offset(bmap, type2);
11762 isl_int_set_si(bmap->ineq[i][pos1], 1);
11763 isl_int_set_si(bmap->ineq[i][pos2], -1);
11764 isl_int_set_si(bmap->ineq[i][0], -1);
11765 bmap = isl_basic_map_finalize(bmap);
11767 return bmap;
11768 error:
11769 isl_space_free(space);
11770 isl_basic_map_free(bmap);
11771 return NULL;
11774 /* Add a constraint imposing that the value of the first dimension is
11775 * greater than that of the second.
11777 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11778 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11780 isl_basic_map *gt;
11782 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11784 bmap = isl_basic_map_intersect(bmap, gt);
11786 return bmap;
11789 /* Add a constraint imposing that the value of the first dimension is
11790 * greater than that of the second.
11792 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11793 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11795 isl_basic_map *bmap;
11797 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11799 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11801 return map;
11804 /* Add a constraint imposing that the value of the first dimension is
11805 * smaller than that of the second.
11807 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11808 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11810 return isl_map_order_gt(map, type2, pos2, type1, pos1);
11813 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11814 int pos)
11816 isl_aff *div;
11817 isl_local_space *ls;
11819 if (!bmap)
11820 return NULL;
11822 if (!isl_basic_map_divs_known(bmap))
11823 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11824 "some divs are unknown", return NULL);
11826 ls = isl_basic_map_get_local_space(bmap);
11827 div = isl_local_space_get_div(ls, pos);
11828 isl_local_space_free(ls);
11830 return div;
11833 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11834 int pos)
11836 return isl_basic_map_get_div(bset, pos);
11839 /* Plug in "subs" for dimension "type", "pos" of "bset".
11841 * Let i be the dimension to replace and let "subs" be of the form
11843 * f/d
11845 * Any integer division with a non-zero coefficient for i,
11847 * floor((a i + g)/m)
11849 * is replaced by
11851 * floor((a f + d g)/(m d))
11853 * Constraints of the form
11855 * a i + g
11857 * are replaced by
11859 * a f + d g
11861 * We currently require that "subs" is an integral expression.
11862 * Handling rational expressions may require us to add stride constraints
11863 * as we do in isl_basic_set_preimage_multi_aff.
11865 __isl_give isl_basic_set *isl_basic_set_substitute(
11866 __isl_take isl_basic_set *bset,
11867 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11869 int i;
11870 isl_int v;
11871 isl_ctx *ctx;
11873 if (bset && isl_basic_set_plain_is_empty(bset))
11874 return bset;
11876 bset = isl_basic_set_cow(bset);
11877 if (!bset || !subs)
11878 goto error;
11880 ctx = isl_basic_set_get_ctx(bset);
11881 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11882 isl_die(ctx, isl_error_invalid,
11883 "spaces don't match", goto error);
11884 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11885 isl_die(ctx, isl_error_unsupported,
11886 "cannot handle divs yet", goto error);
11887 if (!isl_int_is_one(subs->v->el[0]))
11888 isl_die(ctx, isl_error_invalid,
11889 "can only substitute integer expressions", goto error);
11891 pos += isl_basic_set_offset(bset, type);
11893 isl_int_init(v);
11895 for (i = 0; i < bset->n_eq; ++i) {
11896 if (isl_int_is_zero(bset->eq[i][pos]))
11897 continue;
11898 isl_int_set(v, bset->eq[i][pos]);
11899 isl_int_set_si(bset->eq[i][pos], 0);
11900 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11901 v, subs->v->el + 1, subs->v->size - 1);
11904 for (i = 0; i < bset->n_ineq; ++i) {
11905 if (isl_int_is_zero(bset->ineq[i][pos]))
11906 continue;
11907 isl_int_set(v, bset->ineq[i][pos]);
11908 isl_int_set_si(bset->ineq[i][pos], 0);
11909 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11910 v, subs->v->el + 1, subs->v->size - 1);
11913 for (i = 0; i < bset->n_div; ++i) {
11914 if (isl_int_is_zero(bset->div[i][1 + pos]))
11915 continue;
11916 isl_int_set(v, bset->div[i][1 + pos]);
11917 isl_int_set_si(bset->div[i][1 + pos], 0);
11918 isl_seq_combine(bset->div[i] + 1,
11919 subs->v->el[0], bset->div[i] + 1,
11920 v, subs->v->el + 1, subs->v->size - 1);
11921 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11924 isl_int_clear(v);
11926 bset = isl_basic_set_simplify(bset);
11927 return isl_basic_set_finalize(bset);
11928 error:
11929 isl_basic_set_free(bset);
11930 return NULL;
11933 /* Plug in "subs" for dimension "type", "pos" of "set".
11935 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11936 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11938 int i;
11940 if (set && isl_set_plain_is_empty(set))
11941 return set;
11943 set = isl_set_cow(set);
11944 if (!set || !subs)
11945 goto error;
11947 for (i = set->n - 1; i >= 0; --i) {
11948 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11949 if (remove_if_empty(set, i) < 0)
11950 goto error;
11953 return set;
11954 error:
11955 isl_set_free(set);
11956 return NULL;
11959 /* Check if the range of "ma" is compatible with the domain or range
11960 * (depending on "type") of "bmap".
11961 * Return -1 if anything is wrong.
11963 static int check_basic_map_compatible_range_multi_aff(
11964 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11965 __isl_keep isl_multi_aff *ma)
11967 int m;
11968 isl_space *ma_space;
11970 ma_space = isl_multi_aff_get_space(ma);
11972 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
11973 if (m < 0)
11974 goto error;
11975 if (!m)
11976 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11977 "parameters don't match", goto error);
11978 m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11979 if (m < 0)
11980 goto error;
11981 if (!m)
11982 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11983 "spaces don't match", goto error);
11985 isl_space_free(ma_space);
11986 return m;
11987 error:
11988 isl_space_free(ma_space);
11989 return -1;
11992 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11993 * coefficients before the transformed range of dimensions,
11994 * the "n_after" coefficients after the transformed range of dimensions
11995 * and the coefficients of the other divs in "bmap".
11997 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11998 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12000 int i;
12001 int n_param;
12002 int n_set;
12003 isl_local_space *ls;
12005 if (n_div == 0)
12006 return 0;
12008 ls = isl_aff_get_domain_local_space(ma->p[0]);
12009 if (!ls)
12010 return -1;
12012 n_param = isl_local_space_dim(ls, isl_dim_param);
12013 n_set = isl_local_space_dim(ls, isl_dim_set);
12014 for (i = 0; i < n_div; ++i) {
12015 int o_bmap = 0, o_ls = 0;
12017 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12018 o_bmap += 1 + 1 + n_param;
12019 o_ls += 1 + 1 + n_param;
12020 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12021 o_bmap += n_before;
12022 isl_seq_cpy(bmap->div[i] + o_bmap,
12023 ls->div->row[i] + o_ls, n_set);
12024 o_bmap += n_set;
12025 o_ls += n_set;
12026 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12027 o_bmap += n_after;
12028 isl_seq_cpy(bmap->div[i] + o_bmap,
12029 ls->div->row[i] + o_ls, n_div);
12030 o_bmap += n_div;
12031 o_ls += n_div;
12032 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12033 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12034 goto error;
12037 isl_local_space_free(ls);
12038 return 0;
12039 error:
12040 isl_local_space_free(ls);
12041 return -1;
12044 /* How many stride constraints does "ma" enforce?
12045 * That is, how many of the affine expressions have a denominator
12046 * different from one?
12048 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12050 int i;
12051 int strides = 0;
12053 for (i = 0; i < ma->n; ++i)
12054 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12055 strides++;
12057 return strides;
12060 /* For each affine expression in ma of the form
12062 * x_i = (f_i y + h_i)/m_i
12064 * with m_i different from one, add a constraint to "bmap"
12065 * of the form
12067 * f_i y + h_i = m_i alpha_i
12069 * with alpha_i an additional existentially quantified variable.
12071 static __isl_give isl_basic_map *add_ma_strides(
12072 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12073 int n_before, int n_after)
12075 int i, k;
12076 int div;
12077 int total;
12078 int n_param;
12079 int n_in;
12080 int n_div;
12082 total = isl_basic_map_total_dim(bmap);
12083 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12084 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12085 n_div = isl_multi_aff_dim(ma, isl_dim_div);
12086 for (i = 0; i < ma->n; ++i) {
12087 int o_bmap = 0, o_ma = 1;
12089 if (isl_int_is_one(ma->p[i]->v->el[0]))
12090 continue;
12091 div = isl_basic_map_alloc_div(bmap);
12092 k = isl_basic_map_alloc_equality(bmap);
12093 if (div < 0 || k < 0)
12094 goto error;
12095 isl_int_set_si(bmap->div[div][0], 0);
12096 isl_seq_cpy(bmap->eq[k] + o_bmap,
12097 ma->p[i]->v->el + o_ma, 1 + n_param);
12098 o_bmap += 1 + n_param;
12099 o_ma += 1 + n_param;
12100 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12101 o_bmap += n_before;
12102 isl_seq_cpy(bmap->eq[k] + o_bmap,
12103 ma->p[i]->v->el + o_ma, n_in);
12104 o_bmap += n_in;
12105 o_ma += n_in;
12106 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12107 o_bmap += n_after;
12108 isl_seq_cpy(bmap->eq[k] + o_bmap,
12109 ma->p[i]->v->el + o_ma, n_div);
12110 o_bmap += n_div;
12111 o_ma += n_div;
12112 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12113 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12114 total++;
12117 return bmap;
12118 error:
12119 isl_basic_map_free(bmap);
12120 return NULL;
12123 /* Replace the domain or range space (depending on "type) of "space" by "set".
12125 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12126 enum isl_dim_type type, __isl_take isl_space *set)
12128 if (type == isl_dim_in) {
12129 space = isl_space_range(space);
12130 space = isl_space_map_from_domain_and_range(set, space);
12131 } else {
12132 space = isl_space_domain(space);
12133 space = isl_space_map_from_domain_and_range(space, set);
12136 return space;
12139 /* Compute the preimage of the domain or range (depending on "type")
12140 * of "bmap" under the function represented by "ma".
12141 * In other words, plug in "ma" in the domain or range of "bmap".
12142 * The result is a basic map that lives in the same space as "bmap"
12143 * except that the domain or range has been replaced by
12144 * the domain space of "ma".
12146 * If bmap is represented by
12148 * A(p) + S u + B x + T v + C(divs) >= 0,
12150 * where u and x are input and output dimensions if type == isl_dim_out
12151 * while x and v are input and output dimensions if type == isl_dim_in,
12152 * and ma is represented by
12154 * x = D(p) + F(y) + G(divs')
12156 * then the result is
12158 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12160 * The divs in the input set are similarly adjusted.
12161 * In particular
12163 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12165 * becomes
12167 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12168 * B_i G(divs') + c_i(divs))/n_i)
12170 * If bmap is not a rational map and if F(y) involves any denominators
12172 * x_i = (f_i y + h_i)/m_i
12174 * then additional constraints are added to ensure that we only
12175 * map back integer points. That is we enforce
12177 * f_i y + h_i = m_i alpha_i
12179 * with alpha_i an additional existentially quantified variable.
12181 * We first copy over the divs from "ma".
12182 * Then we add the modified constraints and divs from "bmap".
12183 * Finally, we add the stride constraints, if needed.
12185 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12186 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12187 __isl_take isl_multi_aff *ma)
12189 int i, k;
12190 isl_space *space;
12191 isl_basic_map *res = NULL;
12192 int n_before, n_after, n_div_bmap, n_div_ma;
12193 isl_int f, c1, c2, g;
12194 int rational, strides;
12196 isl_int_init(f);
12197 isl_int_init(c1);
12198 isl_int_init(c2);
12199 isl_int_init(g);
12201 ma = isl_multi_aff_align_divs(ma);
12202 if (!bmap || !ma)
12203 goto error;
12204 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12205 goto error;
12207 if (type == isl_dim_in) {
12208 n_before = 0;
12209 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12210 } else {
12211 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12212 n_after = 0;
12214 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12215 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12217 space = isl_multi_aff_get_domain_space(ma);
12218 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12219 rational = isl_basic_map_is_rational(bmap);
12220 strides = rational ? 0 : multi_aff_strides(ma);
12221 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12222 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12223 if (rational)
12224 res = isl_basic_map_set_rational(res);
12226 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12227 if (isl_basic_map_alloc_div(res) < 0)
12228 goto error;
12230 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12231 goto error;
12233 for (i = 0; i < bmap->n_eq; ++i) {
12234 k = isl_basic_map_alloc_equality(res);
12235 if (k < 0)
12236 goto error;
12237 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12238 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12241 for (i = 0; i < bmap->n_ineq; ++i) {
12242 k = isl_basic_map_alloc_inequality(res);
12243 if (k < 0)
12244 goto error;
12245 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12246 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12249 for (i = 0; i < bmap->n_div; ++i) {
12250 if (isl_int_is_zero(bmap->div[i][0])) {
12251 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12252 continue;
12254 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12255 n_before, n_after, n_div_ma, n_div_bmap,
12256 f, c1, c2, g, 1);
12259 if (strides)
12260 res = add_ma_strides(res, ma, n_before, n_after);
12262 isl_int_clear(f);
12263 isl_int_clear(c1);
12264 isl_int_clear(c2);
12265 isl_int_clear(g);
12266 isl_basic_map_free(bmap);
12267 isl_multi_aff_free(ma);
12268 res = isl_basic_set_simplify(res);
12269 return isl_basic_map_finalize(res);
12270 error:
12271 isl_int_clear(f);
12272 isl_int_clear(c1);
12273 isl_int_clear(c2);
12274 isl_int_clear(g);
12275 isl_basic_map_free(bmap);
12276 isl_multi_aff_free(ma);
12277 isl_basic_map_free(res);
12278 return NULL;
12281 /* Compute the preimage of "bset" under the function represented by "ma".
12282 * In other words, plug in "ma" in "bset". The result is a basic set
12283 * that lives in the domain space of "ma".
12285 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12286 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12288 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12291 /* Compute the preimage of the domain of "bmap" under the function
12292 * represented by "ma".
12293 * In other words, plug in "ma" in the domain of "bmap".
12294 * The result is a basic map that lives in the same space as "bmap"
12295 * except that the domain has been replaced by the domain space of "ma".
12297 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12298 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12300 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12303 /* Compute the preimage of the range of "bmap" under the function
12304 * represented by "ma".
12305 * In other words, plug in "ma" in the range of "bmap".
12306 * The result is a basic map that lives in the same space as "bmap"
12307 * except that the range has been replaced by the domain space of "ma".
12309 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12310 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12312 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12315 /* Check if the range of "ma" is compatible with the domain or range
12316 * (depending on "type") of "map".
12317 * Return -1 if anything is wrong.
12319 static int check_map_compatible_range_multi_aff(
12320 __isl_keep isl_map *map, enum isl_dim_type type,
12321 __isl_keep isl_multi_aff *ma)
12323 int m;
12324 isl_space *ma_space;
12326 ma_space = isl_multi_aff_get_space(ma);
12327 m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
12328 isl_space_free(ma_space);
12329 if (m >= 0 && !m)
12330 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12331 "spaces don't match", return -1);
12332 return m;
12335 /* Compute the preimage of the domain or range (depending on "type")
12336 * of "map" under the function represented by "ma".
12337 * In other words, plug in "ma" in the domain or range of "map".
12338 * The result is a map that lives in the same space as "map"
12339 * except that the domain or range has been replaced by
12340 * the domain space of "ma".
12342 * The parameters are assumed to have been aligned.
12344 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12345 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12347 int i;
12348 isl_space *space;
12350 map = isl_map_cow(map);
12351 ma = isl_multi_aff_align_divs(ma);
12352 if (!map || !ma)
12353 goto error;
12354 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12355 goto error;
12357 for (i = 0; i < map->n; ++i) {
12358 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12359 isl_multi_aff_copy(ma));
12360 if (!map->p[i])
12361 goto error;
12364 space = isl_multi_aff_get_domain_space(ma);
12365 space = isl_space_set(isl_map_get_space(map), type, space);
12367 isl_space_free(map->dim);
12368 map->dim = space;
12369 if (!map->dim)
12370 goto error;
12372 isl_multi_aff_free(ma);
12373 if (map->n > 1)
12374 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12375 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12376 return map;
12377 error:
12378 isl_multi_aff_free(ma);
12379 isl_map_free(map);
12380 return NULL;
12383 /* Compute the preimage of the domain or range (depending on "type")
12384 * of "map" under the function represented by "ma".
12385 * In other words, plug in "ma" in the domain or range of "map".
12386 * The result is a map that lives in the same space as "map"
12387 * except that the domain or range has been replaced by
12388 * the domain space of "ma".
12390 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12391 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12393 if (!map || !ma)
12394 goto error;
12396 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12397 return map_preimage_multi_aff(map, type, ma);
12399 if (!isl_space_has_named_params(map->dim) ||
12400 !isl_space_has_named_params(ma->space))
12401 isl_die(map->ctx, isl_error_invalid,
12402 "unaligned unnamed parameters", goto error);
12403 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12404 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12406 return map_preimage_multi_aff(map, type, ma);
12407 error:
12408 isl_multi_aff_free(ma);
12409 return isl_map_free(map);
12412 /* Compute the preimage of "set" under the function represented by "ma".
12413 * In other words, plug in "ma" in "set". The result is a set
12414 * that lives in the domain space of "ma".
12416 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12417 __isl_take isl_multi_aff *ma)
12419 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12422 /* Compute the preimage of the domain of "map" under the function
12423 * represented by "ma".
12424 * In other words, plug in "ma" in the domain of "map".
12425 * The result is a map that lives in the same space as "map"
12426 * except that the domain has been replaced by the domain space of "ma".
12428 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12429 __isl_take isl_multi_aff *ma)
12431 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12434 /* Compute the preimage of the range of "map" under the function
12435 * represented by "ma".
12436 * In other words, plug in "ma" in the range of "map".
12437 * The result is a map that lives in the same space as "map"
12438 * except that the range has been replaced by the domain space of "ma".
12440 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12441 __isl_take isl_multi_aff *ma)
12443 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12446 /* Compute the preimage of "map" under the function represented by "pma".
12447 * In other words, plug in "pma" in the domain or range of "map".
12448 * The result is a map that lives in the same space as "map",
12449 * except that the space of type "type" has been replaced by
12450 * the domain space of "pma".
12452 * The parameters of "map" and "pma" are assumed to have been aligned.
12454 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12455 __isl_take isl_map *map, enum isl_dim_type type,
12456 __isl_take isl_pw_multi_aff *pma)
12458 int i;
12459 isl_map *res;
12461 if (!pma)
12462 goto error;
12464 if (pma->n == 0) {
12465 isl_pw_multi_aff_free(pma);
12466 res = isl_map_empty(isl_map_get_space(map));
12467 isl_map_free(map);
12468 return res;
12471 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12472 isl_multi_aff_copy(pma->p[0].maff));
12473 if (type == isl_dim_in)
12474 res = isl_map_intersect_domain(res,
12475 isl_map_copy(pma->p[0].set));
12476 else
12477 res = isl_map_intersect_range(res,
12478 isl_map_copy(pma->p[0].set));
12480 for (i = 1; i < pma->n; ++i) {
12481 isl_map *res_i;
12483 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12484 isl_multi_aff_copy(pma->p[i].maff));
12485 if (type == isl_dim_in)
12486 res_i = isl_map_intersect_domain(res_i,
12487 isl_map_copy(pma->p[i].set));
12488 else
12489 res_i = isl_map_intersect_range(res_i,
12490 isl_map_copy(pma->p[i].set));
12491 res = isl_map_union(res, res_i);
12494 isl_pw_multi_aff_free(pma);
12495 isl_map_free(map);
12496 return res;
12497 error:
12498 isl_pw_multi_aff_free(pma);
12499 isl_map_free(map);
12500 return NULL;
12503 /* Compute the preimage of "map" under the function represented by "pma".
12504 * In other words, plug in "pma" in the domain or range of "map".
12505 * The result is a map that lives in the same space as "map",
12506 * except that the space of type "type" has been replaced by
12507 * the domain space of "pma".
12509 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12510 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12512 if (!map || !pma)
12513 goto error;
12515 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12516 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12518 if (!isl_space_has_named_params(map->dim) ||
12519 !isl_space_has_named_params(pma->dim))
12520 isl_die(map->ctx, isl_error_invalid,
12521 "unaligned unnamed parameters", goto error);
12522 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12523 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12525 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12526 error:
12527 isl_pw_multi_aff_free(pma);
12528 return isl_map_free(map);
12531 /* Compute the preimage of "set" under the function represented by "pma".
12532 * In other words, plug in "pma" in "set". The result is a set
12533 * that lives in the domain space of "pma".
12535 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12536 __isl_take isl_pw_multi_aff *pma)
12538 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12541 /* Compute the preimage of the domain of "map" under the function
12542 * represented by "pma".
12543 * In other words, plug in "pma" in the domain of "map".
12544 * The result is a map that lives in the same space as "map",
12545 * except that domain space has been replaced by the domain space of "pma".
12547 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12548 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12550 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12553 /* Compute the preimage of the range of "map" under the function
12554 * represented by "pma".
12555 * In other words, plug in "pma" in the range of "map".
12556 * The result is a map that lives in the same space as "map",
12557 * except that range space has been replaced by the domain space of "pma".
12559 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12560 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12562 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12565 /* Compute the preimage of "map" under the function represented by "mpa".
12566 * In other words, plug in "mpa" in the domain or range of "map".
12567 * The result is a map that lives in the same space as "map",
12568 * except that the space of type "type" has been replaced by
12569 * the domain space of "mpa".
12571 * If the map does not involve any constraints that refer to the
12572 * dimensions of the substituted space, then the only possible
12573 * effect of "mpa" on the map is to map the space to a different space.
12574 * We create a separate isl_multi_aff to effectuate this change
12575 * in order to avoid spurious splitting of the map along the pieces
12576 * of "mpa".
12578 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12579 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12581 int n;
12582 isl_pw_multi_aff *pma;
12584 if (!map || !mpa)
12585 goto error;
12587 n = isl_map_dim(map, type);
12588 if (!isl_map_involves_dims(map, type, 0, n)) {
12589 isl_space *space;
12590 isl_multi_aff *ma;
12592 space = isl_multi_pw_aff_get_space(mpa);
12593 isl_multi_pw_aff_free(mpa);
12594 ma = isl_multi_aff_zero(space);
12595 return isl_map_preimage_multi_aff(map, type, ma);
12598 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12599 return isl_map_preimage_pw_multi_aff(map, type, pma);
12600 error:
12601 isl_map_free(map);
12602 isl_multi_pw_aff_free(mpa);
12603 return NULL;
12606 /* Compute the preimage of "map" under the function represented by "mpa".
12607 * In other words, plug in "mpa" in the domain "map".
12608 * The result is a map that lives in the same space as "map",
12609 * except that domain space has been replaced by the domain space of "mpa".
12611 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12612 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12614 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12617 /* Compute the preimage of "set" by the function represented by "mpa".
12618 * In other words, plug in "mpa" in "set".
12620 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12621 __isl_take isl_multi_pw_aff *mpa)
12623 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);