isl_map.c: extract out shared isl_map_check_named_params
[isl.git] / isl_map.c
blobfe63bdb59bdbc4172602f199418fd0c58d8d3286
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
6 * Copyright 2016 INRIA Paris
7 * Copyright 2016 Sven Verdoolaege
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, K.U.Leuven, Departement
12 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
14 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Centre de Recherche Inria de Paris, 2 rue Simone Iff - Voie DQ12,
19 * CS 42112, 75589 Paris Cedex 12, France
22 #include <string.h>
23 #include <isl_ctx_private.h>
24 #include <isl_map_private.h>
25 #include <isl_blk.h>
26 #include <isl/constraint.h>
27 #include "isl_space_private.h"
28 #include "isl_equalities.h"
29 #include <isl_lp_private.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl/map.h>
33 #include <isl_reordering.h>
34 #include "isl_sample.h"
35 #include <isl_sort.h>
36 #include "isl_tab.h"
37 #include <isl/vec.h>
38 #include <isl_mat_private.h>
39 #include <isl_vec_private.h>
40 #include <isl_dim_map.h>
41 #include <isl_local_space_private.h>
42 #include <isl_aff_private.h>
43 #include <isl_options_private.h>
44 #include <isl_morph.h>
45 #include <isl_val_private.h>
46 #include <isl/deprecated/map_int.h>
47 #include <isl/deprecated/set_int.h>
49 #include <bset_to_bmap.c>
50 #include <bset_from_bmap.c>
51 #include <set_to_map.c>
52 #include <set_from_map.c>
54 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
56 switch (type) {
57 case isl_dim_param: return dim->nparam;
58 case isl_dim_in: return dim->n_in;
59 case isl_dim_out: return dim->n_out;
60 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
61 default: return 0;
65 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
67 switch (type) {
68 case isl_dim_param: return 1;
69 case isl_dim_in: return 1 + dim->nparam;
70 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
71 default: return 0;
75 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
76 enum isl_dim_type type)
78 if (!bmap)
79 return 0;
80 switch (type) {
81 case isl_dim_cst: return 1;
82 case isl_dim_param:
83 case isl_dim_in:
84 case isl_dim_out: return isl_space_dim(bmap->dim, type);
85 case isl_dim_div: return bmap->n_div;
86 case isl_dim_all: return isl_basic_map_total_dim(bmap);
87 default: return 0;
91 /* Return the space of "map".
93 __isl_keep isl_space *isl_map_peek_space(__isl_keep const isl_map *map)
95 return map ? map->dim : NULL;
98 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
100 return map ? n(map->dim, type) : 0;
103 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
105 return set ? n(set->dim, type) : 0;
108 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
109 enum isl_dim_type type)
111 isl_space *space;
113 if (!bmap)
114 return 0;
116 space = bmap->dim;
117 switch (type) {
118 case isl_dim_cst: return 0;
119 case isl_dim_param: return 1;
120 case isl_dim_in: return 1 + space->nparam;
121 case isl_dim_out: return 1 + space->nparam + space->n_in;
122 case isl_dim_div: return 1 + space->nparam + space->n_in +
123 space->n_out;
124 default: return 0;
128 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
129 enum isl_dim_type type)
131 return isl_basic_map_offset(bset, type);
134 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
136 return pos(map->dim, type);
139 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
140 enum isl_dim_type type)
142 return isl_basic_map_dim(bset, type);
145 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
147 return isl_basic_set_dim(bset, isl_dim_set);
150 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
152 return isl_basic_set_dim(bset, isl_dim_param);
155 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
157 if (!bset)
158 return 0;
159 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
162 unsigned isl_set_n_dim(__isl_keep isl_set *set)
164 return isl_set_dim(set, isl_dim_set);
167 unsigned isl_set_n_param(__isl_keep isl_set *set)
169 return isl_set_dim(set, isl_dim_param);
172 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
174 return bmap ? bmap->dim->n_in : 0;
177 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
179 return bmap ? bmap->dim->n_out : 0;
182 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
184 return bmap ? bmap->dim->nparam : 0;
187 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
189 return bmap ? bmap->n_div : 0;
192 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
194 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
197 unsigned isl_map_n_in(const struct isl_map *map)
199 return map ? map->dim->n_in : 0;
202 unsigned isl_map_n_out(const struct isl_map *map)
204 return map ? map->dim->n_out : 0;
207 unsigned isl_map_n_param(const struct isl_map *map)
209 return map ? map->dim->nparam : 0;
212 isl_bool isl_map_compatible_domain(__isl_keep isl_map *map,
213 __isl_keep isl_set *set)
215 isl_bool m;
216 if (!map || !set)
217 return isl_bool_error;
218 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
219 if (m < 0 || !m)
220 return m;
221 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
222 set->dim, isl_dim_set);
225 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
226 __isl_keep isl_basic_set *bset)
228 isl_bool m;
229 if (!bmap || !bset)
230 return isl_bool_error;
231 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
232 if (m < 0 || !m)
233 return m;
234 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
235 bset->dim, isl_dim_set);
238 isl_bool isl_map_compatible_range(__isl_keep isl_map *map,
239 __isl_keep isl_set *set)
241 isl_bool m;
242 if (!map || !set)
243 return isl_bool_error;
244 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
245 if (m < 0 || !m)
246 return m;
247 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
248 set->dim, isl_dim_set);
251 isl_bool isl_basic_map_compatible_range(__isl_keep isl_basic_map *bmap,
252 __isl_keep isl_basic_set *bset)
254 isl_bool m;
255 if (!bmap || !bset)
256 return isl_bool_error;
257 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
258 if (m < 0 || !m)
259 return m;
260 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
261 bset->dim, isl_dim_set);
264 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
266 return bmap ? bmap->ctx : NULL;
269 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
271 return bset ? bset->ctx : NULL;
274 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
276 return map ? map->ctx : NULL;
279 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
281 return set ? set->ctx : NULL;
284 /* Return the space of "bmap".
286 __isl_keep isl_space *isl_basic_map_peek_space(
287 __isl_keep const isl_basic_map *bmap)
289 return bmap ? bmap->dim : NULL;
292 /* Return the space of "bset".
294 __isl_keep isl_space *isl_basic_set_peek_space(__isl_keep isl_basic_set *bset)
296 return isl_basic_map_peek_space(bset_to_bmap(bset));
299 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
301 return isl_space_copy(isl_basic_map_peek_space(bmap));
304 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
306 return isl_basic_map_get_space(bset_to_bmap(bset));
309 /* Extract the divs in "bmap" as a matrix.
311 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
313 int i;
314 isl_ctx *ctx;
315 isl_mat *div;
316 unsigned total;
317 unsigned cols;
319 if (!bmap)
320 return NULL;
322 ctx = isl_basic_map_get_ctx(bmap);
323 total = isl_space_dim(bmap->dim, isl_dim_all);
324 cols = 1 + 1 + total + bmap->n_div;
325 div = isl_mat_alloc(ctx, bmap->n_div, cols);
326 if (!div)
327 return NULL;
329 for (i = 0; i < bmap->n_div; ++i)
330 isl_seq_cpy(div->row[i], bmap->div[i], cols);
332 return div;
335 /* Extract the divs in "bset" as a matrix.
337 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
339 return isl_basic_map_get_divs(bset);
342 __isl_give isl_local_space *isl_basic_map_get_local_space(
343 __isl_keep isl_basic_map *bmap)
345 isl_mat *div;
347 if (!bmap)
348 return NULL;
350 div = isl_basic_map_get_divs(bmap);
351 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
354 __isl_give isl_local_space *isl_basic_set_get_local_space(
355 __isl_keep isl_basic_set *bset)
357 return isl_basic_map_get_local_space(bset);
360 /* For each known div d = floor(f/m), add the constraints
362 * f - m d >= 0
363 * -(f-(m-1)) + m d >= 0
365 * Do not finalize the result.
367 static __isl_give isl_basic_map *add_known_div_constraints(
368 __isl_take isl_basic_map *bmap)
370 int i;
371 unsigned n_div;
373 if (!bmap)
374 return NULL;
375 n_div = isl_basic_map_dim(bmap, isl_dim_div);
376 if (n_div == 0)
377 return bmap;
378 bmap = isl_basic_map_cow(bmap);
379 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
380 if (!bmap)
381 return NULL;
382 for (i = 0; i < n_div; ++i) {
383 if (isl_int_is_zero(bmap->div[i][0]))
384 continue;
385 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
386 return isl_basic_map_free(bmap);
389 return bmap;
392 __isl_give isl_basic_map *isl_basic_map_from_local_space(
393 __isl_take isl_local_space *ls)
395 int i;
396 int n_div;
397 isl_basic_map *bmap;
399 if (!ls)
400 return NULL;
402 n_div = isl_local_space_dim(ls, isl_dim_div);
403 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
404 n_div, 0, 2 * n_div);
406 for (i = 0; i < n_div; ++i)
407 if (isl_basic_map_alloc_div(bmap) < 0)
408 goto error;
410 for (i = 0; i < n_div; ++i)
411 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
412 bmap = add_known_div_constraints(bmap);
414 isl_local_space_free(ls);
415 return bmap;
416 error:
417 isl_local_space_free(ls);
418 isl_basic_map_free(bmap);
419 return NULL;
422 __isl_give isl_basic_set *isl_basic_set_from_local_space(
423 __isl_take isl_local_space *ls)
425 return isl_basic_map_from_local_space(ls);
428 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
430 return isl_space_copy(isl_map_peek_space(map));
433 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
435 if (!set)
436 return NULL;
437 return isl_space_copy(set->dim);
440 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
441 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
443 bmap = isl_basic_map_cow(bmap);
444 if (!bmap)
445 return NULL;
446 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
447 if (!bmap->dim)
448 goto error;
449 bmap = isl_basic_map_finalize(bmap);
450 return bmap;
451 error:
452 isl_basic_map_free(bmap);
453 return NULL;
456 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
457 __isl_take isl_basic_set *bset, const char *s)
459 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
462 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
463 enum isl_dim_type type)
465 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
468 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
469 enum isl_dim_type type, const char *s)
471 int i;
473 map = isl_map_cow(map);
474 if (!map)
475 return NULL;
477 map->dim = isl_space_set_tuple_name(map->dim, type, s);
478 if (!map->dim)
479 goto error;
481 for (i = 0; i < map->n; ++i) {
482 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
483 if (!map->p[i])
484 goto error;
487 return map;
488 error:
489 isl_map_free(map);
490 return NULL;
493 /* Replace the identifier of the tuple of type "type" by "id".
495 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
496 __isl_take isl_basic_map *bmap,
497 enum isl_dim_type type, __isl_take isl_id *id)
499 bmap = isl_basic_map_cow(bmap);
500 if (!bmap)
501 goto error;
502 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
503 if (!bmap->dim)
504 return isl_basic_map_free(bmap);
505 bmap = isl_basic_map_finalize(bmap);
506 return bmap;
507 error:
508 isl_id_free(id);
509 return NULL;
512 /* Replace the identifier of the tuple by "id".
514 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
515 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
517 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
520 /* Does the input or output tuple have a name?
522 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
524 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
527 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
528 enum isl_dim_type type)
530 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
533 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
534 const char *s)
536 return set_from_map(isl_map_set_tuple_name(set_to_map(set),
537 isl_dim_set, s));
540 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
541 enum isl_dim_type type, __isl_take isl_id *id)
543 map = isl_map_cow(map);
544 if (!map)
545 goto error;
547 map->dim = isl_space_set_tuple_id(map->dim, type, id);
549 return isl_map_reset_space(map, isl_space_copy(map->dim));
550 error:
551 isl_id_free(id);
552 return NULL;
555 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
556 __isl_take isl_id *id)
558 return isl_map_set_tuple_id(set, isl_dim_set, id);
561 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
562 enum isl_dim_type type)
564 map = isl_map_cow(map);
565 if (!map)
566 return NULL;
568 map->dim = isl_space_reset_tuple_id(map->dim, type);
570 return isl_map_reset_space(map, isl_space_copy(map->dim));
573 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
575 return isl_map_reset_tuple_id(set, isl_dim_set);
578 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
580 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
583 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
584 enum isl_dim_type type)
586 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
589 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
591 return isl_map_has_tuple_id(set, isl_dim_set);
594 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
596 return isl_map_get_tuple_id(set, isl_dim_set);
599 /* Does the set tuple have a name?
601 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
603 if (!set)
604 return isl_bool_error;
605 return isl_space_has_tuple_name(set->dim, isl_dim_set);
609 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
611 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
614 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
616 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
619 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
620 enum isl_dim_type type, unsigned pos)
622 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
625 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
626 enum isl_dim_type type, unsigned pos)
628 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
631 /* Does the given dimension have a name?
633 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
634 enum isl_dim_type type, unsigned pos)
636 if (!map)
637 return isl_bool_error;
638 return isl_space_has_dim_name(map->dim, type, pos);
641 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
642 enum isl_dim_type type, unsigned pos)
644 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
647 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
648 enum isl_dim_type type, unsigned pos)
650 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
653 /* Does the given dimension have a name?
655 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
656 enum isl_dim_type type, unsigned pos)
658 if (!set)
659 return isl_bool_error;
660 return isl_space_has_dim_name(set->dim, type, pos);
663 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
664 __isl_take isl_basic_map *bmap,
665 enum isl_dim_type type, unsigned pos, const char *s)
667 bmap = isl_basic_map_cow(bmap);
668 if (!bmap)
669 return NULL;
670 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
671 if (!bmap->dim)
672 goto error;
673 return isl_basic_map_finalize(bmap);
674 error:
675 isl_basic_map_free(bmap);
676 return NULL;
679 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
680 enum isl_dim_type type, unsigned pos, const char *s)
682 int i;
684 map = isl_map_cow(map);
685 if (!map)
686 return NULL;
688 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
689 if (!map->dim)
690 goto error;
692 for (i = 0; i < map->n; ++i) {
693 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
694 if (!map->p[i])
695 goto error;
698 return map;
699 error:
700 isl_map_free(map);
701 return NULL;
704 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
705 __isl_take isl_basic_set *bset,
706 enum isl_dim_type type, unsigned pos, const char *s)
708 return bset_from_bmap(isl_basic_map_set_dim_name(bset_to_bmap(bset),
709 type, pos, s));
712 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
713 enum isl_dim_type type, unsigned pos, const char *s)
715 return set_from_map(isl_map_set_dim_name(set_to_map(set),
716 type, pos, s));
719 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
720 enum isl_dim_type type, unsigned pos)
722 if (!bmap)
723 return isl_bool_error;
724 return isl_space_has_dim_id(bmap->dim, type, pos);
727 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
728 enum isl_dim_type type, unsigned pos)
730 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
733 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
734 enum isl_dim_type type, unsigned pos)
736 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
739 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
740 enum isl_dim_type type, unsigned pos)
742 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
745 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
746 enum isl_dim_type type, unsigned pos)
748 return isl_map_has_dim_id(set, type, pos);
751 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
752 enum isl_dim_type type, unsigned pos)
754 return isl_map_get_dim_id(set, type, pos);
757 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
758 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
760 map = isl_map_cow(map);
761 if (!map)
762 goto error;
764 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
766 return isl_map_reset_space(map, isl_space_copy(map->dim));
767 error:
768 isl_id_free(id);
769 return NULL;
772 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
773 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
775 return isl_map_set_dim_id(set, type, pos, id);
778 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
779 __isl_keep isl_id *id)
781 if (!map)
782 return -1;
783 return isl_space_find_dim_by_id(map->dim, type, id);
786 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
787 __isl_keep isl_id *id)
789 return isl_map_find_dim_by_id(set, type, id);
792 /* Return the position of the dimension of the given type and name
793 * in "bmap".
794 * Return -1 if no such dimension can be found.
796 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
797 enum isl_dim_type type, const char *name)
799 if (!bmap)
800 return -1;
801 return isl_space_find_dim_by_name(bmap->dim, type, name);
804 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
805 const char *name)
807 if (!map)
808 return -1;
809 return isl_space_find_dim_by_name(map->dim, type, name);
812 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
813 const char *name)
815 return isl_map_find_dim_by_name(set, type, name);
818 /* Reset the user pointer on all identifiers of parameters and tuples
819 * of the space of "map".
821 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
823 isl_space *space;
825 space = isl_map_get_space(map);
826 space = isl_space_reset_user(space);
827 map = isl_map_reset_space(map, space);
829 return map;
832 /* Reset the user pointer on all identifiers of parameters and tuples
833 * of the space of "set".
835 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
837 return isl_map_reset_user(set);
840 isl_bool isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
842 if (!bmap)
843 return isl_bool_error;
844 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
847 /* Has "map" been marked as a rational map?
848 * In particular, have all basic maps in "map" been marked this way?
849 * An empty map is not considered to be rational.
850 * Maps where only some of the basic maps are marked rational
851 * are not allowed.
853 isl_bool isl_map_is_rational(__isl_keep isl_map *map)
855 int i;
856 isl_bool rational;
858 if (!map)
859 return isl_bool_error;
860 if (map->n == 0)
861 return isl_bool_false;
862 rational = isl_basic_map_is_rational(map->p[0]);
863 if (rational < 0)
864 return rational;
865 for (i = 1; i < map->n; ++i) {
866 isl_bool rational_i;
868 rational_i = isl_basic_map_is_rational(map->p[i]);
869 if (rational_i < 0)
870 return rational_i;
871 if (rational != rational_i)
872 isl_die(isl_map_get_ctx(map), isl_error_unsupported,
873 "mixed rational and integer basic maps "
874 "not supported", return isl_bool_error);
877 return rational;
880 /* Has "set" been marked as a rational set?
881 * In particular, have all basic set in "set" been marked this way?
882 * An empty set is not considered to be rational.
883 * Sets where only some of the basic sets are marked rational
884 * are not allowed.
886 isl_bool isl_set_is_rational(__isl_keep isl_set *set)
888 return isl_map_is_rational(set);
891 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
893 return isl_basic_map_is_rational(bset);
896 /* Does "bmap" contain any rational points?
898 * If "bmap" has an equality for each dimension, equating the dimension
899 * to an integer constant, then it has no rational points, even if it
900 * is marked as rational.
902 isl_bool isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
904 isl_bool has_rational = isl_bool_true;
905 unsigned total;
907 if (!bmap)
908 return isl_bool_error;
909 if (isl_basic_map_plain_is_empty(bmap))
910 return isl_bool_false;
911 if (!isl_basic_map_is_rational(bmap))
912 return isl_bool_false;
913 bmap = isl_basic_map_copy(bmap);
914 bmap = isl_basic_map_implicit_equalities(bmap);
915 if (!bmap)
916 return isl_bool_error;
917 total = isl_basic_map_total_dim(bmap);
918 if (bmap->n_eq == total) {
919 int i, j;
920 for (i = 0; i < bmap->n_eq; ++i) {
921 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
922 if (j < 0)
923 break;
924 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
925 !isl_int_is_negone(bmap->eq[i][1 + j]))
926 break;
927 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
928 total - j - 1);
929 if (j >= 0)
930 break;
932 if (i == bmap->n_eq)
933 has_rational = isl_bool_false;
935 isl_basic_map_free(bmap);
937 return has_rational;
940 /* Does "map" contain any rational points?
942 isl_bool isl_map_has_rational(__isl_keep isl_map *map)
944 int i;
945 isl_bool has_rational;
947 if (!map)
948 return isl_bool_error;
949 for (i = 0; i < map->n; ++i) {
950 has_rational = isl_basic_map_has_rational(map->p[i]);
951 if (has_rational < 0 || has_rational)
952 return has_rational;
954 return isl_bool_false;
957 /* Does "set" contain any rational points?
959 isl_bool isl_set_has_rational(__isl_keep isl_set *set)
961 return isl_map_has_rational(set);
964 /* Is this basic set a parameter domain?
966 isl_bool isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
968 if (!bset)
969 return isl_bool_error;
970 return isl_space_is_params(bset->dim);
973 /* Is this set a parameter domain?
975 isl_bool isl_set_is_params(__isl_keep isl_set *set)
977 if (!set)
978 return isl_bool_error;
979 return isl_space_is_params(set->dim);
982 /* Is this map actually a parameter domain?
983 * Users should never call this function. Outside of isl,
984 * a map can never be a parameter domain.
986 isl_bool isl_map_is_params(__isl_keep isl_map *map)
988 if (!map)
989 return isl_bool_error;
990 return isl_space_is_params(map->dim);
993 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
994 struct isl_basic_map *bmap, unsigned extra,
995 unsigned n_eq, unsigned n_ineq)
997 int i;
998 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
1000 bmap->ctx = ctx;
1001 isl_ctx_ref(ctx);
1003 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
1004 if (isl_blk_is_error(bmap->block))
1005 goto error;
1007 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
1008 if ((n_ineq + n_eq) && !bmap->ineq)
1009 goto error;
1011 if (extra == 0) {
1012 bmap->block2 = isl_blk_empty();
1013 bmap->div = NULL;
1014 } else {
1015 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
1016 if (isl_blk_is_error(bmap->block2))
1017 goto error;
1019 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
1020 if (!bmap->div)
1021 goto error;
1024 for (i = 0; i < n_ineq + n_eq; ++i)
1025 bmap->ineq[i] = bmap->block.data + i * row_size;
1027 for (i = 0; i < extra; ++i)
1028 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
1030 bmap->ref = 1;
1031 bmap->flags = 0;
1032 bmap->c_size = n_eq + n_ineq;
1033 bmap->eq = bmap->ineq + n_ineq;
1034 bmap->extra = extra;
1035 bmap->n_eq = 0;
1036 bmap->n_ineq = 0;
1037 bmap->n_div = 0;
1038 bmap->sample = NULL;
1040 return bmap;
1041 error:
1042 isl_basic_map_free(bmap);
1043 return NULL;
1046 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
1047 unsigned nparam, unsigned dim, unsigned extra,
1048 unsigned n_eq, unsigned n_ineq)
1050 struct isl_basic_map *bmap;
1051 isl_space *space;
1053 space = isl_space_set_alloc(ctx, nparam, dim);
1054 if (!space)
1055 return NULL;
1057 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
1058 return bset_from_bmap(bmap);
1061 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
1062 unsigned extra, unsigned n_eq, unsigned n_ineq)
1064 struct isl_basic_map *bmap;
1065 if (!dim)
1066 return NULL;
1067 isl_assert(dim->ctx, dim->n_in == 0, goto error);
1068 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1069 return bset_from_bmap(bmap);
1070 error:
1071 isl_space_free(dim);
1072 return NULL;
1075 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1076 unsigned extra, unsigned n_eq, unsigned n_ineq)
1078 struct isl_basic_map *bmap;
1080 if (!dim)
1081 return NULL;
1082 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1083 if (!bmap)
1084 goto error;
1085 bmap->dim = dim;
1087 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1088 error:
1089 isl_space_free(dim);
1090 return NULL;
1093 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1094 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1095 unsigned n_eq, unsigned n_ineq)
1097 struct isl_basic_map *bmap;
1098 isl_space *dim;
1100 dim = isl_space_alloc(ctx, nparam, in, out);
1101 if (!dim)
1102 return NULL;
1104 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1105 return bmap;
1108 static void dup_constraints(
1109 struct isl_basic_map *dst, struct isl_basic_map *src)
1111 int i;
1112 unsigned total = isl_basic_map_total_dim(src);
1114 for (i = 0; i < src->n_eq; ++i) {
1115 int j = isl_basic_map_alloc_equality(dst);
1116 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1119 for (i = 0; i < src->n_ineq; ++i) {
1120 int j = isl_basic_map_alloc_inequality(dst);
1121 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1124 for (i = 0; i < src->n_div; ++i) {
1125 int j = isl_basic_map_alloc_div(dst);
1126 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1128 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1131 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1133 struct isl_basic_map *dup;
1135 if (!bmap)
1136 return NULL;
1137 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1138 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1139 if (!dup)
1140 return NULL;
1141 dup_constraints(dup, bmap);
1142 dup->flags = bmap->flags;
1143 dup->sample = isl_vec_copy(bmap->sample);
1144 return dup;
1147 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1149 struct isl_basic_map *dup;
1151 dup = isl_basic_map_dup(bset_to_bmap(bset));
1152 return bset_from_bmap(dup);
1155 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1157 if (!bset)
1158 return NULL;
1160 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1161 bset->ref++;
1162 return bset;
1164 return isl_basic_set_dup(bset);
1167 struct isl_set *isl_set_copy(struct isl_set *set)
1169 if (!set)
1170 return NULL;
1172 set->ref++;
1173 return set;
1176 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1178 if (!bmap)
1179 return NULL;
1181 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1182 bmap->ref++;
1183 return bmap;
1185 bmap = isl_basic_map_dup(bmap);
1186 if (bmap)
1187 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1188 return bmap;
1191 struct isl_map *isl_map_copy(struct isl_map *map)
1193 if (!map)
1194 return NULL;
1196 map->ref++;
1197 return map;
1200 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1202 if (!bmap)
1203 return NULL;
1205 if (--bmap->ref > 0)
1206 return NULL;
1208 isl_ctx_deref(bmap->ctx);
1209 free(bmap->div);
1210 isl_blk_free(bmap->ctx, bmap->block2);
1211 free(bmap->ineq);
1212 isl_blk_free(bmap->ctx, bmap->block);
1213 isl_vec_free(bmap->sample);
1214 isl_space_free(bmap->dim);
1215 free(bmap);
1217 return NULL;
1220 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1222 return isl_basic_map_free(bset_to_bmap(bset));
1225 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1227 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1230 /* Check that "map" has only named parameters, reporting an error
1231 * if it does not.
1233 isl_stat isl_map_check_named_params(__isl_keep isl_map *map)
1235 return isl_space_check_named_params(isl_map_peek_space(map));
1238 __isl_give isl_map *isl_map_align_params_map_map_and(
1239 __isl_take isl_map *map1, __isl_take isl_map *map2,
1240 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1241 __isl_take isl_map *map2))
1243 if (!map1 || !map2)
1244 goto error;
1245 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1246 return fn(map1, map2);
1247 if (isl_map_check_named_params(map1) < 0)
1248 goto error;
1249 if (isl_map_check_named_params(map2) < 0)
1250 goto error;
1251 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1252 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1253 return fn(map1, map2);
1254 error:
1255 isl_map_free(map1);
1256 isl_map_free(map2);
1257 return NULL;
1260 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1261 __isl_keep isl_map *map2,
1262 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1264 isl_bool r;
1266 if (!map1 || !map2)
1267 return isl_bool_error;
1268 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1269 return fn(map1, map2);
1270 if (isl_map_check_named_params(map1) < 0)
1271 return isl_bool_error;
1272 if (isl_map_check_named_params(map2) < 0)
1273 return isl_bool_error;
1274 map1 = isl_map_copy(map1);
1275 map2 = isl_map_copy(map2);
1276 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1277 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1278 r = fn(map1, map2);
1279 isl_map_free(map1);
1280 isl_map_free(map2);
1281 return r;
1284 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1286 struct isl_ctx *ctx;
1287 if (!bmap)
1288 return -1;
1289 ctx = bmap->ctx;
1290 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1291 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1292 return -1);
1293 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1294 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1295 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1296 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1297 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1298 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1299 isl_int *t;
1300 int j = isl_basic_map_alloc_inequality(bmap);
1301 if (j < 0)
1302 return -1;
1303 t = bmap->ineq[j];
1304 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1305 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1306 bmap->eq[-1] = t;
1307 bmap->n_eq++;
1308 bmap->n_ineq--;
1309 bmap->eq--;
1310 return 0;
1312 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1313 bmap->extra - bmap->n_div);
1314 return bmap->n_eq++;
1317 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1319 return isl_basic_map_alloc_equality(bset_to_bmap(bset));
1322 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1324 if (!bmap)
1325 return -1;
1326 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1327 bmap->n_eq -= n;
1328 return 0;
1331 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1333 return isl_basic_map_free_equality(bset_to_bmap(bset), n);
1336 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1338 isl_int *t;
1339 if (!bmap)
1340 return -1;
1341 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1343 if (pos != bmap->n_eq - 1) {
1344 t = bmap->eq[pos];
1345 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1346 bmap->eq[bmap->n_eq - 1] = t;
1348 bmap->n_eq--;
1349 return 0;
1352 /* Turn inequality "pos" of "bmap" into an equality.
1354 * In particular, we move the inequality in front of the equalities
1355 * and move the last inequality in the position of the moved inequality.
1356 * Note that isl_tab_make_equalities_explicit depends on this particular
1357 * change in the ordering of the constraints.
1359 void isl_basic_map_inequality_to_equality(
1360 struct isl_basic_map *bmap, unsigned pos)
1362 isl_int *t;
1364 t = bmap->ineq[pos];
1365 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1366 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1367 bmap->eq[-1] = t;
1368 bmap->n_eq++;
1369 bmap->n_ineq--;
1370 bmap->eq--;
1371 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1372 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1373 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1374 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1377 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1379 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1382 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1384 struct isl_ctx *ctx;
1385 if (!bmap)
1386 return -1;
1387 ctx = bmap->ctx;
1388 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1389 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1390 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1391 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1392 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1393 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1394 1 + isl_basic_map_total_dim(bmap),
1395 bmap->extra - bmap->n_div);
1396 return bmap->n_ineq++;
1399 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1401 return isl_basic_map_alloc_inequality(bset_to_bmap(bset));
1404 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1406 if (!bmap)
1407 return -1;
1408 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1409 bmap->n_ineq -= n;
1410 return 0;
1413 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1415 return isl_basic_map_free_inequality(bset_to_bmap(bset), n);
1418 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1420 isl_int *t;
1421 if (!bmap)
1422 return -1;
1423 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1425 if (pos != bmap->n_ineq - 1) {
1426 t = bmap->ineq[pos];
1427 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1428 bmap->ineq[bmap->n_ineq - 1] = t;
1429 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1431 bmap->n_ineq--;
1432 return 0;
1435 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1437 return isl_basic_map_drop_inequality(bset_to_bmap(bset), pos);
1440 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1441 isl_int *eq)
1443 int k;
1445 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1446 if (!bmap)
1447 return NULL;
1448 k = isl_basic_map_alloc_equality(bmap);
1449 if (k < 0)
1450 goto error;
1451 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1452 return bmap;
1453 error:
1454 isl_basic_map_free(bmap);
1455 return NULL;
1458 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1459 isl_int *eq)
1461 return bset_from_bmap(isl_basic_map_add_eq(bset_to_bmap(bset), eq));
1464 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1465 isl_int *ineq)
1467 int k;
1469 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1470 if (!bmap)
1471 return NULL;
1472 k = isl_basic_map_alloc_inequality(bmap);
1473 if (k < 0)
1474 goto error;
1475 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1476 return bmap;
1477 error:
1478 isl_basic_map_free(bmap);
1479 return NULL;
1482 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1483 isl_int *ineq)
1485 return bset_from_bmap(isl_basic_map_add_ineq(bset_to_bmap(bset), ineq));
1488 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1490 if (!bmap)
1491 return -1;
1492 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1493 isl_seq_clr(bmap->div[bmap->n_div] +
1494 1 + 1 + isl_basic_map_total_dim(bmap),
1495 bmap->extra - bmap->n_div);
1496 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1497 return bmap->n_div++;
1500 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1502 return isl_basic_map_alloc_div(bset_to_bmap(bset));
1505 /* Check that there are "n" dimensions of type "type" starting at "first"
1506 * in "bmap".
1508 static isl_stat isl_basic_map_check_range(__isl_keep isl_basic_map *bmap,
1509 enum isl_dim_type type, unsigned first, unsigned n)
1511 unsigned dim;
1513 if (!bmap)
1514 return isl_stat_error;
1515 dim = isl_basic_map_dim(bmap, type);
1516 if (first + n > dim || first + n < first)
1517 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1518 "position or range out of bounds",
1519 return isl_stat_error);
1520 return isl_stat_ok;
1523 /* Insert an extra integer division, prescribed by "div", to "bmap"
1524 * at (integer division) position "pos".
1526 * The integer division is first added at the end and then moved
1527 * into the right position.
1529 __isl_give isl_basic_map *isl_basic_map_insert_div(
1530 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1532 int i, k;
1534 bmap = isl_basic_map_cow(bmap);
1535 if (!bmap || !div)
1536 return isl_basic_map_free(bmap);
1538 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1539 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1540 "unexpected size", return isl_basic_map_free(bmap));
1541 if (isl_basic_map_check_range(bmap, isl_dim_div, pos, 0) < 0)
1542 return isl_basic_map_free(bmap);
1544 bmap = isl_basic_map_extend_space(bmap,
1545 isl_basic_map_get_space(bmap), 1, 0, 2);
1546 k = isl_basic_map_alloc_div(bmap);
1547 if (k < 0)
1548 return isl_basic_map_free(bmap);
1549 isl_seq_cpy(bmap->div[k], div->el, div->size);
1550 isl_int_set_si(bmap->div[k][div->size], 0);
1552 for (i = k; i > pos; --i)
1553 isl_basic_map_swap_div(bmap, i, i - 1);
1555 return bmap;
1558 isl_stat isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1560 if (!bmap)
1561 return isl_stat_error;
1562 isl_assert(bmap->ctx, n <= bmap->n_div, return isl_stat_error);
1563 bmap->n_div -= n;
1564 return isl_stat_ok;
1567 /* Copy constraint from src to dst, putting the vars of src at offset
1568 * dim_off in dst and the divs of src at offset div_off in dst.
1569 * If both sets are actually map, then dim_off applies to the input
1570 * variables.
1572 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1573 struct isl_basic_map *src_map, isl_int *src,
1574 unsigned in_off, unsigned out_off, unsigned div_off)
1576 unsigned src_nparam = isl_basic_map_n_param(src_map);
1577 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1578 unsigned src_in = isl_basic_map_n_in(src_map);
1579 unsigned dst_in = isl_basic_map_n_in(dst_map);
1580 unsigned src_out = isl_basic_map_n_out(src_map);
1581 unsigned dst_out = isl_basic_map_n_out(dst_map);
1582 isl_int_set(dst[0], src[0]);
1583 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1584 if (dst_nparam > src_nparam)
1585 isl_seq_clr(dst+1+src_nparam,
1586 dst_nparam - src_nparam);
1587 isl_seq_clr(dst+1+dst_nparam, in_off);
1588 isl_seq_cpy(dst+1+dst_nparam+in_off,
1589 src+1+src_nparam,
1590 isl_min(dst_in-in_off, src_in));
1591 if (dst_in-in_off > src_in)
1592 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1593 dst_in - in_off - src_in);
1594 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1595 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1596 src+1+src_nparam+src_in,
1597 isl_min(dst_out-out_off, src_out));
1598 if (dst_out-out_off > src_out)
1599 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1600 dst_out - out_off - src_out);
1601 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1602 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1603 src+1+src_nparam+src_in+src_out,
1604 isl_min(dst_map->extra-div_off, src_map->n_div));
1605 if (dst_map->n_div-div_off > src_map->n_div)
1606 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1607 div_off+src_map->n_div,
1608 dst_map->n_div - div_off - src_map->n_div);
1611 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1612 struct isl_basic_map *src_map, isl_int *src,
1613 unsigned in_off, unsigned out_off, unsigned div_off)
1615 isl_int_set(dst[0], src[0]);
1616 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1619 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1620 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1622 int i;
1623 unsigned div_off;
1625 if (!bmap1 || !bmap2)
1626 goto error;
1628 div_off = bmap1->n_div;
1630 for (i = 0; i < bmap2->n_eq; ++i) {
1631 int i1 = isl_basic_map_alloc_equality(bmap1);
1632 if (i1 < 0)
1633 goto error;
1634 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1635 i_pos, o_pos, div_off);
1638 for (i = 0; i < bmap2->n_ineq; ++i) {
1639 int i1 = isl_basic_map_alloc_inequality(bmap1);
1640 if (i1 < 0)
1641 goto error;
1642 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1643 i_pos, o_pos, div_off);
1646 for (i = 0; i < bmap2->n_div; ++i) {
1647 int i1 = isl_basic_map_alloc_div(bmap1);
1648 if (i1 < 0)
1649 goto error;
1650 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1651 i_pos, o_pos, div_off);
1654 isl_basic_map_free(bmap2);
1656 return bmap1;
1658 error:
1659 isl_basic_map_free(bmap1);
1660 isl_basic_map_free(bmap2);
1661 return NULL;
1664 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1665 struct isl_basic_set *bset2, unsigned pos)
1667 return bset_from_bmap(add_constraints(bset_to_bmap(bset1),
1668 bset_to_bmap(bset2), 0, pos));
1671 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1672 __isl_take isl_space *dim, unsigned extra,
1673 unsigned n_eq, unsigned n_ineq)
1675 struct isl_basic_map *ext;
1676 unsigned flags;
1677 int dims_ok;
1679 if (!dim)
1680 goto error;
1682 if (!base)
1683 goto error;
1685 dims_ok = isl_space_is_equal(base->dim, dim) &&
1686 base->extra >= base->n_div + extra;
1688 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1689 room_for_ineq(base, n_ineq)) {
1690 isl_space_free(dim);
1691 return base;
1694 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1695 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1696 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1697 extra += base->extra;
1698 n_eq += base->n_eq;
1699 n_ineq += base->n_ineq;
1701 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1702 dim = NULL;
1703 if (!ext)
1704 goto error;
1706 if (dims_ok)
1707 ext->sample = isl_vec_copy(base->sample);
1708 flags = base->flags;
1709 ext = add_constraints(ext, base, 0, 0);
1710 if (ext) {
1711 ext->flags = flags;
1712 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1715 return ext;
1717 error:
1718 isl_space_free(dim);
1719 isl_basic_map_free(base);
1720 return NULL;
1723 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1724 __isl_take isl_space *dim, unsigned extra,
1725 unsigned n_eq, unsigned n_ineq)
1727 return bset_from_bmap(isl_basic_map_extend_space(bset_to_bmap(base),
1728 dim, extra, n_eq, n_ineq));
1731 struct isl_basic_map *isl_basic_map_extend_constraints(
1732 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1734 if (!base)
1735 return NULL;
1736 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1737 0, n_eq, n_ineq);
1740 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1741 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1742 unsigned n_eq, unsigned n_ineq)
1744 struct isl_basic_map *bmap;
1745 isl_space *dim;
1747 if (!base)
1748 return NULL;
1749 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1750 if (!dim)
1751 goto error;
1753 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1754 return bmap;
1755 error:
1756 isl_basic_map_free(base);
1757 return NULL;
1760 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1761 unsigned nparam, unsigned dim, unsigned extra,
1762 unsigned n_eq, unsigned n_ineq)
1764 return bset_from_bmap(isl_basic_map_extend(bset_to_bmap(base),
1765 nparam, 0, dim, extra, n_eq, n_ineq));
1768 struct isl_basic_set *isl_basic_set_extend_constraints(
1769 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1771 isl_basic_map *bmap = bset_to_bmap(base);
1772 bmap = isl_basic_map_extend_constraints(bmap, n_eq, n_ineq);
1773 return bset_from_bmap(bmap);
1776 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1778 return bset_from_bmap(isl_basic_map_cow(bset_to_bmap(bset)));
1781 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1783 if (!bmap)
1784 return NULL;
1786 if (bmap->ref > 1) {
1787 bmap->ref--;
1788 bmap = isl_basic_map_dup(bmap);
1790 if (bmap) {
1791 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1792 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1794 return bmap;
1797 /* Clear all cached information in "map", either because it is about
1798 * to be modified or because it is being freed.
1799 * Always return the same pointer that is passed in.
1800 * This is needed for the use in isl_map_free.
1802 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1804 isl_basic_map_free(map->cached_simple_hull[0]);
1805 isl_basic_map_free(map->cached_simple_hull[1]);
1806 map->cached_simple_hull[0] = NULL;
1807 map->cached_simple_hull[1] = NULL;
1808 return map;
1811 struct isl_set *isl_set_cow(struct isl_set *set)
1813 return isl_map_cow(set);
1816 /* Return an isl_map that is equal to "map" and that has only
1817 * a single reference.
1819 * If the original input already has only one reference, then
1820 * simply return it, but clear all cached information, since
1821 * it may be rendered invalid by the operations that will be
1822 * performed on the result.
1824 * Otherwise, create a duplicate (without any cached information).
1826 struct isl_map *isl_map_cow(struct isl_map *map)
1828 if (!map)
1829 return NULL;
1831 if (map->ref == 1)
1832 return clear_caches(map);
1833 map->ref--;
1834 return isl_map_dup(map);
1837 static void swap_vars(struct isl_blk blk, isl_int *a,
1838 unsigned a_len, unsigned b_len)
1840 isl_seq_cpy(blk.data, a+a_len, b_len);
1841 isl_seq_cpy(blk.data+b_len, a, a_len);
1842 isl_seq_cpy(a, blk.data, b_len+a_len);
1845 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1846 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1848 int i;
1849 struct isl_blk blk;
1851 if (!bmap)
1852 goto error;
1854 isl_assert(bmap->ctx,
1855 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1857 if (n1 == 0 || n2 == 0)
1858 return bmap;
1860 bmap = isl_basic_map_cow(bmap);
1861 if (!bmap)
1862 return NULL;
1864 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1865 if (isl_blk_is_error(blk))
1866 goto error;
1868 for (i = 0; i < bmap->n_eq; ++i)
1869 swap_vars(blk,
1870 bmap->eq[i] + pos, n1, n2);
1872 for (i = 0; i < bmap->n_ineq; ++i)
1873 swap_vars(blk,
1874 bmap->ineq[i] + pos, n1, n2);
1876 for (i = 0; i < bmap->n_div; ++i)
1877 swap_vars(blk,
1878 bmap->div[i]+1 + pos, n1, n2);
1880 isl_blk_free(bmap->ctx, blk);
1882 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1883 bmap = isl_basic_map_gauss(bmap, NULL);
1884 return isl_basic_map_finalize(bmap);
1885 error:
1886 isl_basic_map_free(bmap);
1887 return NULL;
1890 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1892 int i = 0;
1893 unsigned total;
1894 if (!bmap)
1895 goto error;
1896 total = isl_basic_map_total_dim(bmap);
1897 if (isl_basic_map_free_div(bmap, bmap->n_div) < 0)
1898 return isl_basic_map_free(bmap);
1899 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1900 if (bmap->n_eq > 0)
1901 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1902 else {
1903 i = isl_basic_map_alloc_equality(bmap);
1904 if (i < 0)
1905 goto error;
1907 isl_int_set_si(bmap->eq[i][0], 1);
1908 isl_seq_clr(bmap->eq[i]+1, total);
1909 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1910 isl_vec_free(bmap->sample);
1911 bmap->sample = NULL;
1912 return isl_basic_map_finalize(bmap);
1913 error:
1914 isl_basic_map_free(bmap);
1915 return NULL;
1918 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1920 return bset_from_bmap(isl_basic_map_set_to_empty(bset_to_bmap(bset)));
1923 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1924 * of "bmap").
1926 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1928 isl_int *t = bmap->div[a];
1929 bmap->div[a] = bmap->div[b];
1930 bmap->div[b] = t;
1933 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1934 * div definitions accordingly.
1936 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1938 int i;
1939 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1941 swap_div(bmap, a, b);
1943 for (i = 0; i < bmap->n_eq; ++i)
1944 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1946 for (i = 0; i < bmap->n_ineq; ++i)
1947 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1949 for (i = 0; i < bmap->n_div; ++i)
1950 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1951 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1954 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1955 * div definitions accordingly.
1957 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1959 isl_basic_map_swap_div(bset, a, b);
1962 /* Eliminate the specified n dimensions starting at first from the
1963 * constraints, without removing the dimensions from the space.
1964 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1966 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1967 enum isl_dim_type type, unsigned first, unsigned n)
1969 int i;
1971 if (!map)
1972 return NULL;
1973 if (n == 0)
1974 return map;
1976 if (first + n > isl_map_dim(map, type) || first + n < first)
1977 isl_die(map->ctx, isl_error_invalid,
1978 "index out of bounds", goto error);
1980 map = isl_map_cow(map);
1981 if (!map)
1982 return NULL;
1984 for (i = 0; i < map->n; ++i) {
1985 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1986 if (!map->p[i])
1987 goto error;
1989 return map;
1990 error:
1991 isl_map_free(map);
1992 return NULL;
1995 /* Eliminate the specified n dimensions starting at first from the
1996 * constraints, without removing the dimensions from the space.
1997 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1999 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
2000 enum isl_dim_type type, unsigned first, unsigned n)
2002 return set_from_map(isl_map_eliminate(set_to_map(set), type, first, n));
2005 /* Eliminate the specified n dimensions starting at first from the
2006 * constraints, without removing the dimensions from the space.
2007 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
2009 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
2010 unsigned first, unsigned n)
2012 return isl_set_eliminate(set, isl_dim_set, first, n);
2015 __isl_give isl_basic_map *isl_basic_map_remove_divs(
2016 __isl_take isl_basic_map *bmap)
2018 if (!bmap)
2019 return NULL;
2020 bmap = isl_basic_map_eliminate_vars(bmap,
2021 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
2022 if (!bmap)
2023 return NULL;
2024 bmap->n_div = 0;
2025 return isl_basic_map_finalize(bmap);
2028 __isl_give isl_basic_set *isl_basic_set_remove_divs(
2029 __isl_take isl_basic_set *bset)
2031 return bset_from_bmap(isl_basic_map_remove_divs(bset_to_bmap(bset)));
2034 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
2036 int i;
2038 if (!map)
2039 return NULL;
2040 if (map->n == 0)
2041 return map;
2043 map = isl_map_cow(map);
2044 if (!map)
2045 return NULL;
2047 for (i = 0; i < map->n; ++i) {
2048 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
2049 if (!map->p[i])
2050 goto error;
2052 return map;
2053 error:
2054 isl_map_free(map);
2055 return NULL;
2058 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
2060 return isl_map_remove_divs(set);
2063 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
2064 enum isl_dim_type type, unsigned first, unsigned n)
2066 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2067 return isl_basic_map_free(bmap);
2068 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
2069 return bmap;
2070 bmap = isl_basic_map_eliminate_vars(bmap,
2071 isl_basic_map_offset(bmap, type) - 1 + first, n);
2072 if (!bmap)
2073 return bmap;
2074 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2075 return bmap;
2076 bmap = isl_basic_map_drop(bmap, type, first, n);
2077 return bmap;
2080 /* Return true if the definition of the given div (recursively) involves
2081 * any of the given variables.
2083 static isl_bool div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2084 unsigned first, unsigned n)
2086 int i;
2087 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2089 if (isl_int_is_zero(bmap->div[div][0]))
2090 return isl_bool_false;
2091 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2092 return isl_bool_true;
2094 for (i = bmap->n_div - 1; i >= 0; --i) {
2095 isl_bool involves;
2097 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2098 continue;
2099 involves = div_involves_vars(bmap, i, first, n);
2100 if (involves < 0 || involves)
2101 return involves;
2104 return isl_bool_false;
2107 /* Try and add a lower and/or upper bound on "div" to "bmap"
2108 * based on inequality "i".
2109 * "total" is the total number of variables (excluding the divs).
2110 * "v" is a temporary object that can be used during the calculations.
2111 * If "lb" is set, then a lower bound should be constructed.
2112 * If "ub" is set, then an upper bound should be constructed.
2114 * The calling function has already checked that the inequality does not
2115 * reference "div", but we still need to check that the inequality is
2116 * of the right form. We'll consider the case where we want to construct
2117 * a lower bound. The construction of upper bounds is similar.
2119 * Let "div" be of the form
2121 * q = floor((a + f(x))/d)
2123 * We essentially check if constraint "i" is of the form
2125 * b + f(x) >= 0
2127 * so that we can use it to derive a lower bound on "div".
2128 * However, we allow a slightly more general form
2130 * b + g(x) >= 0
2132 * with the condition that the coefficients of g(x) - f(x) are all
2133 * divisible by d.
2134 * Rewriting this constraint as
2136 * 0 >= -b - g(x)
2138 * adding a + f(x) to both sides and dividing by d, we obtain
2140 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2142 * Taking the floor on both sides, we obtain
2144 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2146 * or
2148 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2150 * In the case of an upper bound, we construct the constraint
2152 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2155 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2156 __isl_take isl_basic_map *bmap, int div, int i,
2157 unsigned total, isl_int v, int lb, int ub)
2159 int j;
2161 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2162 if (lb) {
2163 isl_int_sub(v, bmap->ineq[i][1 + j],
2164 bmap->div[div][1 + 1 + j]);
2165 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2167 if (ub) {
2168 isl_int_add(v, bmap->ineq[i][1 + j],
2169 bmap->div[div][1 + 1 + j]);
2170 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2173 if (!lb && !ub)
2174 return bmap;
2176 bmap = isl_basic_map_cow(bmap);
2177 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2178 if (lb) {
2179 int k = isl_basic_map_alloc_inequality(bmap);
2180 if (k < 0)
2181 goto error;
2182 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2183 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2184 bmap->div[div][1 + j]);
2185 isl_int_cdiv_q(bmap->ineq[k][j],
2186 bmap->ineq[k][j], bmap->div[div][0]);
2188 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2190 if (ub) {
2191 int k = isl_basic_map_alloc_inequality(bmap);
2192 if (k < 0)
2193 goto error;
2194 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2195 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2196 bmap->div[div][1 + j]);
2197 isl_int_fdiv_q(bmap->ineq[k][j],
2198 bmap->ineq[k][j], bmap->div[div][0]);
2200 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2203 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2204 return bmap;
2205 error:
2206 isl_basic_map_free(bmap);
2207 return NULL;
2210 /* This function is called right before "div" is eliminated from "bmap"
2211 * using Fourier-Motzkin.
2212 * Look through the constraints of "bmap" for constraints on the argument
2213 * of the integer division and use them to construct constraints on the
2214 * integer division itself. These constraints can then be combined
2215 * during the Fourier-Motzkin elimination.
2216 * Note that it is only useful to introduce lower bounds on "div"
2217 * if "bmap" already contains upper bounds on "div" as the newly
2218 * introduce lower bounds can then be combined with the pre-existing
2219 * upper bounds. Similarly for upper bounds.
2220 * We therefore first check if "bmap" contains any lower and/or upper bounds
2221 * on "div".
2223 * It is interesting to note that the introduction of these constraints
2224 * can indeed lead to more accurate results, even when compared to
2225 * deriving constraints on the argument of "div" from constraints on "div".
2226 * Consider, for example, the set
2228 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2230 * The second constraint can be rewritten as
2232 * 2 * [(-i-2j+3)/4] + k >= 0
2234 * from which we can derive
2236 * -i - 2j + 3 >= -2k
2238 * or
2240 * i + 2j <= 3 + 2k
2242 * Combined with the first constraint, we obtain
2244 * -3 <= 3 + 2k or k >= -3
2246 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2247 * the first constraint, we obtain
2249 * [(i + 2j)/4] >= [-3/4] = -1
2251 * Combining this constraint with the second constraint, we obtain
2253 * k >= -2
2255 static __isl_give isl_basic_map *insert_bounds_on_div(
2256 __isl_take isl_basic_map *bmap, int div)
2258 int i;
2259 int check_lb, check_ub;
2260 isl_int v;
2261 unsigned total;
2263 if (!bmap)
2264 return NULL;
2266 if (isl_int_is_zero(bmap->div[div][0]))
2267 return bmap;
2269 total = isl_space_dim(bmap->dim, isl_dim_all);
2271 check_lb = 0;
2272 check_ub = 0;
2273 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2274 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2275 if (s > 0)
2276 check_ub = 1;
2277 if (s < 0)
2278 check_lb = 1;
2281 if (!check_lb && !check_ub)
2282 return bmap;
2284 isl_int_init(v);
2286 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2287 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2288 continue;
2290 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2291 check_lb, check_ub);
2294 isl_int_clear(v);
2296 return bmap;
2299 /* Remove all divs (recursively) involving any of the given dimensions
2300 * in their definitions.
2302 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2303 __isl_take isl_basic_map *bmap,
2304 enum isl_dim_type type, unsigned first, unsigned n)
2306 int i;
2308 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2309 return isl_basic_map_free(bmap);
2310 first += isl_basic_map_offset(bmap, type);
2312 for (i = bmap->n_div - 1; i >= 0; --i) {
2313 isl_bool involves;
2315 involves = div_involves_vars(bmap, i, first, n);
2316 if (involves < 0)
2317 return isl_basic_map_free(bmap);
2318 if (!involves)
2319 continue;
2320 bmap = insert_bounds_on_div(bmap, i);
2321 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2322 if (!bmap)
2323 return NULL;
2324 i = bmap->n_div;
2327 return bmap;
2330 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2331 __isl_take isl_basic_set *bset,
2332 enum isl_dim_type type, unsigned first, unsigned n)
2334 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2337 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2338 enum isl_dim_type type, unsigned first, unsigned n)
2340 int i;
2342 if (!map)
2343 return NULL;
2344 if (map->n == 0)
2345 return map;
2347 map = isl_map_cow(map);
2348 if (!map)
2349 return NULL;
2351 for (i = 0; i < map->n; ++i) {
2352 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2353 type, first, n);
2354 if (!map->p[i])
2355 goto error;
2357 return map;
2358 error:
2359 isl_map_free(map);
2360 return NULL;
2363 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2364 enum isl_dim_type type, unsigned first, unsigned n)
2366 return set_from_map(isl_map_remove_divs_involving_dims(set_to_map(set),
2367 type, first, n));
2370 /* Does the description of "bmap" depend on the specified dimensions?
2371 * We also check whether the dimensions appear in any of the div definitions.
2372 * In principle there is no need for this check. If the dimensions appear
2373 * in a div definition, they also appear in the defining constraints of that
2374 * div.
2376 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2377 enum isl_dim_type type, unsigned first, unsigned n)
2379 int i;
2381 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2382 return isl_bool_error;
2384 first += isl_basic_map_offset(bmap, type);
2385 for (i = 0; i < bmap->n_eq; ++i)
2386 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2387 return isl_bool_true;
2388 for (i = 0; i < bmap->n_ineq; ++i)
2389 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2390 return isl_bool_true;
2391 for (i = 0; i < bmap->n_div; ++i) {
2392 if (isl_int_is_zero(bmap->div[i][0]))
2393 continue;
2394 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2395 return isl_bool_true;
2398 return isl_bool_false;
2401 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2402 enum isl_dim_type type, unsigned first, unsigned n)
2404 int i;
2406 if (!map)
2407 return isl_bool_error;
2409 if (first + n > isl_map_dim(map, type))
2410 isl_die(map->ctx, isl_error_invalid,
2411 "index out of bounds", return isl_bool_error);
2413 for (i = 0; i < map->n; ++i) {
2414 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2415 type, first, n);
2416 if (involves < 0 || involves)
2417 return involves;
2420 return isl_bool_false;
2423 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2424 enum isl_dim_type type, unsigned first, unsigned n)
2426 return isl_basic_map_involves_dims(bset, type, first, n);
2429 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2430 enum isl_dim_type type, unsigned first, unsigned n)
2432 return isl_map_involves_dims(set, type, first, n);
2435 /* Drop all constraints in bmap that involve any of the dimensions
2436 * first to first+n-1.
2438 static __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving(
2439 __isl_take isl_basic_map *bmap, unsigned first, unsigned n)
2441 int i;
2443 if (n == 0)
2444 return bmap;
2446 bmap = isl_basic_map_cow(bmap);
2448 if (!bmap)
2449 return NULL;
2451 for (i = bmap->n_eq - 1; i >= 0; --i) {
2452 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) == -1)
2453 continue;
2454 isl_basic_map_drop_equality(bmap, i);
2457 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2458 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) == -1)
2459 continue;
2460 isl_basic_map_drop_inequality(bmap, i);
2463 bmap = isl_basic_map_add_known_div_constraints(bmap);
2464 return bmap;
2467 /* Drop all constraints in bset that involve any of the dimensions
2468 * first to first+n-1.
2470 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving(
2471 __isl_take isl_basic_set *bset, unsigned first, unsigned n)
2473 return isl_basic_map_drop_constraints_involving(bset, first, n);
2476 /* Drop all constraints in bmap that do not involve any of the dimensions
2477 * first to first + n - 1 of the given type.
2479 __isl_give isl_basic_map *isl_basic_map_drop_constraints_not_involving_dims(
2480 __isl_take isl_basic_map *bmap,
2481 enum isl_dim_type type, unsigned first, unsigned n)
2483 int i;
2485 if (n == 0) {
2486 isl_space *space = isl_basic_map_get_space(bmap);
2487 isl_basic_map_free(bmap);
2488 return isl_basic_map_universe(space);
2490 bmap = isl_basic_map_cow(bmap);
2491 if (!bmap)
2492 return NULL;
2494 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2495 return isl_basic_map_free(bmap);
2497 first += isl_basic_map_offset(bmap, type) - 1;
2499 for (i = bmap->n_eq - 1; i >= 0; --i) {
2500 if (isl_seq_first_non_zero(bmap->eq[i] + 1 + first, n) != -1)
2501 continue;
2502 isl_basic_map_drop_equality(bmap, i);
2505 for (i = bmap->n_ineq - 1; i >= 0; --i) {
2506 if (isl_seq_first_non_zero(bmap->ineq[i] + 1 + first, n) != -1)
2507 continue;
2508 isl_basic_map_drop_inequality(bmap, i);
2511 bmap = isl_basic_map_add_known_div_constraints(bmap);
2512 return bmap;
2515 /* Drop all constraints in bset that do not involve any of the dimensions
2516 * first to first + n - 1 of the given type.
2518 __isl_give isl_basic_set *isl_basic_set_drop_constraints_not_involving_dims(
2519 __isl_take isl_basic_set *bset,
2520 enum isl_dim_type type, unsigned first, unsigned n)
2522 return isl_basic_map_drop_constraints_not_involving_dims(bset,
2523 type, first, n);
2526 /* Drop all constraints in bmap that involve any of the dimensions
2527 * first to first + n - 1 of the given type.
2529 __isl_give isl_basic_map *isl_basic_map_drop_constraints_involving_dims(
2530 __isl_take isl_basic_map *bmap,
2531 enum isl_dim_type type, unsigned first, unsigned n)
2533 if (!bmap)
2534 return NULL;
2535 if (n == 0)
2536 return bmap;
2538 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
2539 return isl_basic_map_free(bmap);
2541 bmap = isl_basic_map_remove_divs_involving_dims(bmap, type, first, n);
2542 first += isl_basic_map_offset(bmap, type) - 1;
2543 return isl_basic_map_drop_constraints_involving(bmap, first, n);
2546 /* Drop all constraints in bset that involve any of the dimensions
2547 * first to first + n - 1 of the given type.
2549 __isl_give isl_basic_set *isl_basic_set_drop_constraints_involving_dims(
2550 __isl_take isl_basic_set *bset,
2551 enum isl_dim_type type, unsigned first, unsigned n)
2553 return isl_basic_map_drop_constraints_involving_dims(bset,
2554 type, first, n);
2557 /* Drop constraints from "map" by applying "drop" to each basic map.
2559 static __isl_give isl_map *drop_constraints(__isl_take isl_map *map,
2560 enum isl_dim_type type, unsigned first, unsigned n,
2561 __isl_give isl_basic_map *(*drop)(__isl_take isl_basic_map *bmap,
2562 enum isl_dim_type type, unsigned first, unsigned n))
2564 int i;
2565 unsigned dim;
2567 if (!map)
2568 return NULL;
2570 dim = isl_map_dim(map, type);
2571 if (first + n > dim || first + n < first)
2572 isl_die(isl_map_get_ctx(map), isl_error_invalid,
2573 "index out of bounds", return isl_map_free(map));
2575 map = isl_map_cow(map);
2576 if (!map)
2577 return NULL;
2579 for (i = 0; i < map->n; ++i) {
2580 map->p[i] = drop(map->p[i], type, first, n);
2581 if (!map->p[i])
2582 return isl_map_free(map);
2585 if (map->n > 1)
2586 ISL_F_CLR(map, ISL_MAP_DISJOINT);
2588 return map;
2591 /* Drop all constraints in map that involve any of the dimensions
2592 * first to first + n - 1 of the given type.
2594 __isl_give isl_map *isl_map_drop_constraints_involving_dims(
2595 __isl_take isl_map *map,
2596 enum isl_dim_type type, unsigned first, unsigned n)
2598 if (n == 0)
2599 return map;
2600 return drop_constraints(map, type, first, n,
2601 &isl_basic_map_drop_constraints_involving_dims);
2604 /* Drop all constraints in "map" that do not involve any of the dimensions
2605 * first to first + n - 1 of the given type.
2607 __isl_give isl_map *isl_map_drop_constraints_not_involving_dims(
2608 __isl_take isl_map *map,
2609 enum isl_dim_type type, unsigned first, unsigned n)
2611 if (n == 0) {
2612 isl_space *space = isl_map_get_space(map);
2613 isl_map_free(map);
2614 return isl_map_universe(space);
2616 return drop_constraints(map, type, first, n,
2617 &isl_basic_map_drop_constraints_not_involving_dims);
2620 /* Drop all constraints in set that involve any of the dimensions
2621 * first to first + n - 1 of the given type.
2623 __isl_give isl_set *isl_set_drop_constraints_involving_dims(
2624 __isl_take isl_set *set,
2625 enum isl_dim_type type, unsigned first, unsigned n)
2627 return isl_map_drop_constraints_involving_dims(set, type, first, n);
2630 /* Drop all constraints in "set" that do not involve any of the dimensions
2631 * first to first + n - 1 of the given type.
2633 __isl_give isl_set *isl_set_drop_constraints_not_involving_dims(
2634 __isl_take isl_set *set,
2635 enum isl_dim_type type, unsigned first, unsigned n)
2637 return isl_map_drop_constraints_not_involving_dims(set, type, first, n);
2640 /* Does local variable "div" of "bmap" have a complete explicit representation?
2641 * Having a complete explicit representation requires not only
2642 * an explicit representation, but also that all local variables
2643 * that appear in this explicit representation in turn have
2644 * a complete explicit representation.
2646 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2648 int i;
2649 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2650 isl_bool marked;
2652 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2653 if (marked < 0 || marked)
2654 return isl_bool_not(marked);
2656 for (i = bmap->n_div - 1; i >= 0; --i) {
2657 isl_bool known;
2659 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2660 continue;
2661 known = isl_basic_map_div_is_known(bmap, i);
2662 if (known < 0 || !known)
2663 return known;
2666 return isl_bool_true;
2669 /* Remove all divs that are unknown or defined in terms of unknown divs.
2671 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2672 __isl_take isl_basic_map *bmap)
2674 int i;
2676 if (!bmap)
2677 return NULL;
2679 for (i = bmap->n_div - 1; i >= 0; --i) {
2680 if (isl_basic_map_div_is_known(bmap, i))
2681 continue;
2682 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2683 if (!bmap)
2684 return NULL;
2685 i = bmap->n_div;
2688 return bmap;
2691 /* Remove all divs that are unknown or defined in terms of unknown divs.
2693 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2694 __isl_take isl_basic_set *bset)
2696 return isl_basic_map_remove_unknown_divs(bset);
2699 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2701 int i;
2703 if (!map)
2704 return NULL;
2705 if (map->n == 0)
2706 return map;
2708 map = isl_map_cow(map);
2709 if (!map)
2710 return NULL;
2712 for (i = 0; i < map->n; ++i) {
2713 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2714 if (!map->p[i])
2715 goto error;
2717 return map;
2718 error:
2719 isl_map_free(map);
2720 return NULL;
2723 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2725 return set_from_map(isl_map_remove_unknown_divs(set_to_map(set)));
2728 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2729 __isl_take isl_basic_set *bset,
2730 enum isl_dim_type type, unsigned first, unsigned n)
2732 isl_basic_map *bmap = bset_to_bmap(bset);
2733 bmap = isl_basic_map_remove_dims(bmap, type, first, n);
2734 return bset_from_bmap(bmap);
2737 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2738 enum isl_dim_type type, unsigned first, unsigned n)
2740 int i;
2742 if (n == 0)
2743 return map;
2745 map = isl_map_cow(map);
2746 if (!map)
2747 return NULL;
2748 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2750 for (i = 0; i < map->n; ++i) {
2751 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2752 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2753 if (!map->p[i])
2754 goto error;
2756 map = isl_map_drop(map, type, first, n);
2757 return map;
2758 error:
2759 isl_map_free(map);
2760 return NULL;
2763 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2764 enum isl_dim_type type, unsigned first, unsigned n)
2766 return set_from_map(isl_map_remove_dims(set_to_map(bset),
2767 type, first, n));
2770 /* Project out n inputs starting at first using Fourier-Motzkin */
2771 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2772 unsigned first, unsigned n)
2774 return isl_map_remove_dims(map, isl_dim_in, first, n);
2777 static void dump_term(struct isl_basic_map *bmap,
2778 isl_int c, int pos, FILE *out)
2780 const char *name;
2781 unsigned in = isl_basic_map_n_in(bmap);
2782 unsigned dim = in + isl_basic_map_n_out(bmap);
2783 unsigned nparam = isl_basic_map_n_param(bmap);
2784 if (!pos)
2785 isl_int_print(out, c, 0);
2786 else {
2787 if (!isl_int_is_one(c))
2788 isl_int_print(out, c, 0);
2789 if (pos < 1 + nparam) {
2790 name = isl_space_get_dim_name(bmap->dim,
2791 isl_dim_param, pos - 1);
2792 if (name)
2793 fprintf(out, "%s", name);
2794 else
2795 fprintf(out, "p%d", pos - 1);
2796 } else if (pos < 1 + nparam + in)
2797 fprintf(out, "i%d", pos - 1 - nparam);
2798 else if (pos < 1 + nparam + dim)
2799 fprintf(out, "o%d", pos - 1 - nparam - in);
2800 else
2801 fprintf(out, "e%d", pos - 1 - nparam - dim);
2805 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2806 int sign, FILE *out)
2808 int i;
2809 int first;
2810 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2811 isl_int v;
2813 isl_int_init(v);
2814 for (i = 0, first = 1; i < len; ++i) {
2815 if (isl_int_sgn(c[i]) * sign <= 0)
2816 continue;
2817 if (!first)
2818 fprintf(out, " + ");
2819 first = 0;
2820 isl_int_abs(v, c[i]);
2821 dump_term(bmap, v, i, out);
2823 isl_int_clear(v);
2824 if (first)
2825 fprintf(out, "0");
2828 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2829 const char *op, FILE *out, int indent)
2831 int i;
2833 fprintf(out, "%*s", indent, "");
2835 dump_constraint_sign(bmap, c, 1, out);
2836 fprintf(out, " %s ", op);
2837 dump_constraint_sign(bmap, c, -1, out);
2839 fprintf(out, "\n");
2841 for (i = bmap->n_div; i < bmap->extra; ++i) {
2842 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2843 continue;
2844 fprintf(out, "%*s", indent, "");
2845 fprintf(out, "ERROR: unused div coefficient not zero\n");
2846 abort();
2850 static void dump_constraints(struct isl_basic_map *bmap,
2851 isl_int **c, unsigned n,
2852 const char *op, FILE *out, int indent)
2854 int i;
2856 for (i = 0; i < n; ++i)
2857 dump_constraint(bmap, c[i], op, out, indent);
2860 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2862 int j;
2863 int first = 1;
2864 unsigned total = isl_basic_map_total_dim(bmap);
2866 for (j = 0; j < 1 + total; ++j) {
2867 if (isl_int_is_zero(exp[j]))
2868 continue;
2869 if (!first && isl_int_is_pos(exp[j]))
2870 fprintf(out, "+");
2871 dump_term(bmap, exp[j], j, out);
2872 first = 0;
2876 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2878 int i;
2880 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2881 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2883 for (i = 0; i < bmap->n_div; ++i) {
2884 fprintf(out, "%*s", indent, "");
2885 fprintf(out, "e%d = [(", i);
2886 dump_affine(bmap, bmap->div[i]+1, out);
2887 fprintf(out, ")/");
2888 isl_int_print(out, bmap->div[i][0], 0);
2889 fprintf(out, "]\n");
2893 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2894 FILE *out, int indent)
2896 if (!bset) {
2897 fprintf(out, "null basic set\n");
2898 return;
2901 fprintf(out, "%*s", indent, "");
2902 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2903 bset->ref, bset->dim->nparam, bset->dim->n_out,
2904 bset->extra, bset->flags);
2905 dump(bset_to_bmap(bset), out, indent);
2908 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2909 FILE *out, int indent)
2911 if (!bmap) {
2912 fprintf(out, "null basic map\n");
2913 return;
2916 fprintf(out, "%*s", indent, "");
2917 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2918 "flags: %x, n_name: %d\n",
2919 bmap->ref,
2920 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2921 bmap->extra, bmap->flags, bmap->dim->n_id);
2922 dump(bmap, out, indent);
2925 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2927 unsigned total;
2928 if (!bmap)
2929 return -1;
2930 total = isl_basic_map_total_dim(bmap);
2931 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2932 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2933 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2934 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2935 return 0;
2938 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2939 unsigned flags)
2941 if (!space)
2942 return NULL;
2943 if (isl_space_dim(space, isl_dim_in) != 0)
2944 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2945 "set cannot have input dimensions", goto error);
2946 return isl_map_alloc_space(space, n, flags);
2947 error:
2948 isl_space_free(space);
2949 return NULL;
2952 /* Make sure "map" has room for at least "n" more basic maps.
2954 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2956 int i;
2957 struct isl_map *grown = NULL;
2959 if (!map)
2960 return NULL;
2961 isl_assert(map->ctx, n >= 0, goto error);
2962 if (map->n + n <= map->size)
2963 return map;
2964 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2965 if (!grown)
2966 goto error;
2967 for (i = 0; i < map->n; ++i) {
2968 grown->p[i] = isl_basic_map_copy(map->p[i]);
2969 if (!grown->p[i])
2970 goto error;
2971 grown->n++;
2973 isl_map_free(map);
2974 return grown;
2975 error:
2976 isl_map_free(grown);
2977 isl_map_free(map);
2978 return NULL;
2981 /* Make sure "set" has room for at least "n" more basic sets.
2983 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2985 return set_from_map(isl_map_grow(set_to_map(set), n));
2988 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2990 return isl_map_from_basic_map(bset);
2993 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2995 struct isl_map *map;
2997 if (!bmap)
2998 return NULL;
3000 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
3001 return isl_map_add_basic_map(map, bmap);
3004 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
3005 __isl_take isl_basic_set *bset)
3007 return set_from_map(isl_map_add_basic_map(set_to_map(set),
3008 bset_to_bmap(bset)));
3011 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
3013 return isl_map_free(set);
3016 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
3018 int i;
3020 if (!set) {
3021 fprintf(out, "null set\n");
3022 return;
3025 fprintf(out, "%*s", indent, "");
3026 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
3027 set->ref, set->n, set->dim->nparam, set->dim->n_out,
3028 set->flags);
3029 for (i = 0; i < set->n; ++i) {
3030 fprintf(out, "%*s", indent, "");
3031 fprintf(out, "basic set %d:\n", i);
3032 isl_basic_set_print_internal(set->p[i], out, indent+4);
3036 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
3038 int i;
3040 if (!map) {
3041 fprintf(out, "null map\n");
3042 return;
3045 fprintf(out, "%*s", indent, "");
3046 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
3047 "flags: %x, n_name: %d\n",
3048 map->ref, map->n, map->dim->nparam, map->dim->n_in,
3049 map->dim->n_out, map->flags, map->dim->n_id);
3050 for (i = 0; i < map->n; ++i) {
3051 fprintf(out, "%*s", indent, "");
3052 fprintf(out, "basic map %d:\n", i);
3053 isl_basic_map_print_internal(map->p[i], out, indent+4);
3057 struct isl_basic_map *isl_basic_map_intersect_domain(
3058 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3060 struct isl_basic_map *bmap_domain;
3062 if (!bmap || !bset)
3063 goto error;
3065 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3066 bset->dim, isl_dim_param), goto error);
3068 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
3069 isl_assert(bset->ctx,
3070 isl_basic_map_compatible_domain(bmap, bset), goto error);
3072 bmap = isl_basic_map_cow(bmap);
3073 if (!bmap)
3074 goto error;
3075 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3076 bset->n_div, bset->n_eq, bset->n_ineq);
3077 bmap_domain = isl_basic_map_from_domain(bset);
3078 bmap = add_constraints(bmap, bmap_domain, 0, 0);
3080 bmap = isl_basic_map_simplify(bmap);
3081 return isl_basic_map_finalize(bmap);
3082 error:
3083 isl_basic_map_free(bmap);
3084 isl_basic_set_free(bset);
3085 return NULL;
3088 /* Check that the space of "bset" is the same as that of the range of "bmap".
3090 static isl_stat isl_basic_map_check_compatible_range(
3091 __isl_keep isl_basic_map *bmap, __isl_keep isl_basic_set *bset)
3093 isl_bool ok;
3095 ok = isl_basic_map_compatible_range(bmap, bset);
3096 if (ok < 0)
3097 return isl_stat_error;
3098 if (!ok)
3099 isl_die(isl_basic_set_get_ctx(bset), isl_error_invalid,
3100 "incompatible spaces", return isl_stat_error);
3102 return isl_stat_ok;
3105 struct isl_basic_map *isl_basic_map_intersect_range(
3106 struct isl_basic_map *bmap, struct isl_basic_set *bset)
3108 struct isl_basic_map *bmap_range;
3110 if (!bmap || !bset)
3111 goto error;
3113 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
3114 bset->dim, isl_dim_param), goto error);
3116 if (isl_space_dim(bset->dim, isl_dim_set) != 0 &&
3117 isl_basic_map_check_compatible_range(bmap, bset) < 0)
3118 goto error;
3120 if (isl_basic_set_plain_is_universe(bset)) {
3121 isl_basic_set_free(bset);
3122 return bmap;
3125 bmap = isl_basic_map_cow(bmap);
3126 if (!bmap)
3127 goto error;
3128 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
3129 bset->n_div, bset->n_eq, bset->n_ineq);
3130 bmap_range = bset_to_bmap(bset);
3131 bmap = add_constraints(bmap, bmap_range, 0, 0);
3133 bmap = isl_basic_map_simplify(bmap);
3134 return isl_basic_map_finalize(bmap);
3135 error:
3136 isl_basic_map_free(bmap);
3137 isl_basic_set_free(bset);
3138 return NULL;
3141 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
3142 __isl_keep isl_vec *vec)
3144 int i;
3145 unsigned total;
3146 isl_int s;
3148 if (!bmap || !vec)
3149 return isl_bool_error;
3151 total = 1 + isl_basic_map_total_dim(bmap);
3152 if (total != vec->size)
3153 return isl_bool_false;
3155 isl_int_init(s);
3157 for (i = 0; i < bmap->n_eq; ++i) {
3158 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
3159 if (!isl_int_is_zero(s)) {
3160 isl_int_clear(s);
3161 return isl_bool_false;
3165 for (i = 0; i < bmap->n_ineq; ++i) {
3166 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
3167 if (isl_int_is_neg(s)) {
3168 isl_int_clear(s);
3169 return isl_bool_false;
3173 isl_int_clear(s);
3175 return isl_bool_true;
3178 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
3179 __isl_keep isl_vec *vec)
3181 return isl_basic_map_contains(bset_to_bmap(bset), vec);
3184 struct isl_basic_map *isl_basic_map_intersect(
3185 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3187 struct isl_vec *sample = NULL;
3189 if (!bmap1 || !bmap2)
3190 goto error;
3192 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
3193 bmap2->dim, isl_dim_param), goto error);
3194 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
3195 isl_space_dim(bmap1->dim, isl_dim_param) &&
3196 isl_space_dim(bmap2->dim, isl_dim_all) !=
3197 isl_space_dim(bmap2->dim, isl_dim_param))
3198 return isl_basic_map_intersect(bmap2, bmap1);
3200 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
3201 isl_space_dim(bmap2->dim, isl_dim_param))
3202 isl_assert(bmap1->ctx,
3203 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
3205 if (isl_basic_map_plain_is_empty(bmap1)) {
3206 isl_basic_map_free(bmap2);
3207 return bmap1;
3209 if (isl_basic_map_plain_is_empty(bmap2)) {
3210 isl_basic_map_free(bmap1);
3211 return bmap2;
3214 if (bmap1->sample &&
3215 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
3216 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
3217 sample = isl_vec_copy(bmap1->sample);
3218 else if (bmap2->sample &&
3219 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
3220 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
3221 sample = isl_vec_copy(bmap2->sample);
3223 bmap1 = isl_basic_map_cow(bmap1);
3224 if (!bmap1)
3225 goto error;
3226 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
3227 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3228 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
3230 if (!bmap1)
3231 isl_vec_free(sample);
3232 else if (sample) {
3233 isl_vec_free(bmap1->sample);
3234 bmap1->sample = sample;
3237 bmap1 = isl_basic_map_simplify(bmap1);
3238 return isl_basic_map_finalize(bmap1);
3239 error:
3240 if (sample)
3241 isl_vec_free(sample);
3242 isl_basic_map_free(bmap1);
3243 isl_basic_map_free(bmap2);
3244 return NULL;
3247 struct isl_basic_set *isl_basic_set_intersect(
3248 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3250 return bset_from_bmap(isl_basic_map_intersect(bset_to_bmap(bset1),
3251 bset_to_bmap(bset2)));
3254 __isl_give isl_basic_set *isl_basic_set_intersect_params(
3255 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3257 return isl_basic_set_intersect(bset1, bset2);
3260 /* Special case of isl_map_intersect, where both map1 and map2
3261 * are convex, without any divs and such that either map1 or map2
3262 * contains a single constraint. This constraint is then simply
3263 * added to the other map.
3265 static __isl_give isl_map *map_intersect_add_constraint(
3266 __isl_take isl_map *map1, __isl_take isl_map *map2)
3268 isl_assert(map1->ctx, map1->n == 1, goto error);
3269 isl_assert(map2->ctx, map1->n == 1, goto error);
3270 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3271 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3273 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3274 return isl_map_intersect(map2, map1);
3276 map1 = isl_map_cow(map1);
3277 if (!map1)
3278 goto error;
3279 if (isl_map_plain_is_empty(map1)) {
3280 isl_map_free(map2);
3281 return map1;
3283 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3284 if (map2->p[0]->n_eq == 1)
3285 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3286 else
3287 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3288 map2->p[0]->ineq[0]);
3290 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3291 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3292 if (!map1->p[0])
3293 goto error;
3295 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3296 isl_basic_map_free(map1->p[0]);
3297 map1->n = 0;
3300 isl_map_free(map2);
3302 return map1;
3303 error:
3304 isl_map_free(map1);
3305 isl_map_free(map2);
3306 return NULL;
3309 /* map2 may be either a parameter domain or a map living in the same
3310 * space as map1.
3312 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3313 __isl_take isl_map *map2)
3315 unsigned flags = 0;
3316 isl_map *result;
3317 int i, j;
3319 if (!map1 || !map2)
3320 goto error;
3322 if ((isl_map_plain_is_empty(map1) ||
3323 isl_map_plain_is_universe(map2)) &&
3324 isl_space_is_equal(map1->dim, map2->dim)) {
3325 isl_map_free(map2);
3326 return map1;
3328 if ((isl_map_plain_is_empty(map2) ||
3329 isl_map_plain_is_universe(map1)) &&
3330 isl_space_is_equal(map1->dim, map2->dim)) {
3331 isl_map_free(map1);
3332 return map2;
3335 if (map1->n == 1 && map2->n == 1 &&
3336 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3337 isl_space_is_equal(map1->dim, map2->dim) &&
3338 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3339 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3340 return map_intersect_add_constraint(map1, map2);
3342 if (isl_space_dim(map2->dim, isl_dim_all) !=
3343 isl_space_dim(map2->dim, isl_dim_param))
3344 isl_assert(map1->ctx,
3345 isl_space_is_equal(map1->dim, map2->dim), goto error);
3347 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3348 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3349 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3351 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3352 map1->n * map2->n, flags);
3353 if (!result)
3354 goto error;
3355 for (i = 0; i < map1->n; ++i)
3356 for (j = 0; j < map2->n; ++j) {
3357 struct isl_basic_map *part;
3358 part = isl_basic_map_intersect(
3359 isl_basic_map_copy(map1->p[i]),
3360 isl_basic_map_copy(map2->p[j]));
3361 if (isl_basic_map_is_empty(part) < 0)
3362 part = isl_basic_map_free(part);
3363 result = isl_map_add_basic_map(result, part);
3364 if (!result)
3365 goto error;
3367 isl_map_free(map1);
3368 isl_map_free(map2);
3369 return result;
3370 error:
3371 isl_map_free(map1);
3372 isl_map_free(map2);
3373 return NULL;
3376 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3377 __isl_take isl_map *map2)
3379 if (!map1 || !map2)
3380 goto error;
3381 if (!isl_space_is_equal(map1->dim, map2->dim))
3382 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3383 "spaces don't match", goto error);
3384 return map_intersect_internal(map1, map2);
3385 error:
3386 isl_map_free(map1);
3387 isl_map_free(map2);
3388 return NULL;
3391 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3392 __isl_take isl_map *map2)
3394 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3397 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3399 return set_from_map(isl_map_intersect(set_to_map(set1),
3400 set_to_map(set2)));
3403 /* map_intersect_internal accepts intersections
3404 * with parameter domains, so we can just call that function.
3406 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3407 __isl_take isl_set *params)
3409 return map_intersect_internal(map, params);
3412 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3413 __isl_take isl_map *map2)
3415 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3418 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3419 __isl_take isl_set *params)
3421 return isl_map_intersect_params(set, params);
3424 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3426 isl_space *space;
3427 unsigned pos, n1, n2;
3429 if (!bmap)
3430 return NULL;
3431 bmap = isl_basic_map_cow(bmap);
3432 if (!bmap)
3433 return NULL;
3434 space = isl_space_reverse(isl_space_copy(bmap->dim));
3435 pos = isl_basic_map_offset(bmap, isl_dim_in);
3436 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3437 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3438 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3439 return isl_basic_map_reset_space(bmap, space);
3442 static __isl_give isl_basic_map *basic_map_space_reset(
3443 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3445 isl_space *space;
3447 if (!bmap)
3448 return NULL;
3449 if (!isl_space_is_named_or_nested(bmap->dim, type))
3450 return bmap;
3452 space = isl_basic_map_get_space(bmap);
3453 space = isl_space_reset(space, type);
3454 bmap = isl_basic_map_reset_space(bmap, space);
3455 return bmap;
3458 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3459 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3460 unsigned pos, unsigned n)
3462 isl_bool rational;
3463 isl_space *res_dim;
3464 struct isl_basic_map *res;
3465 struct isl_dim_map *dim_map;
3466 unsigned total, off;
3467 enum isl_dim_type t;
3469 if (n == 0)
3470 return basic_map_space_reset(bmap, type);
3472 if (!bmap)
3473 return NULL;
3475 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3477 total = isl_basic_map_total_dim(bmap) + n;
3478 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3479 off = 0;
3480 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3481 if (t != type) {
3482 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3483 } else {
3484 unsigned size = isl_basic_map_dim(bmap, t);
3485 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3486 0, pos, off);
3487 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3488 pos, size - pos, off + pos + n);
3490 off += isl_space_dim(res_dim, t);
3492 isl_dim_map_div(dim_map, bmap, off);
3494 res = isl_basic_map_alloc_space(res_dim,
3495 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3496 rational = isl_basic_map_is_rational(bmap);
3497 if (rational < 0)
3498 res = isl_basic_map_free(res);
3499 if (rational)
3500 res = isl_basic_map_set_rational(res);
3501 if (isl_basic_map_plain_is_empty(bmap)) {
3502 isl_basic_map_free(bmap);
3503 free(dim_map);
3504 return isl_basic_map_set_to_empty(res);
3506 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3507 return isl_basic_map_finalize(res);
3510 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3511 __isl_take isl_basic_set *bset,
3512 enum isl_dim_type type, unsigned pos, unsigned n)
3514 return isl_basic_map_insert_dims(bset, type, pos, n);
3517 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3518 enum isl_dim_type type, unsigned n)
3520 if (!bmap)
3521 return NULL;
3522 return isl_basic_map_insert_dims(bmap, type,
3523 isl_basic_map_dim(bmap, type), n);
3526 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3527 enum isl_dim_type type, unsigned n)
3529 if (!bset)
3530 return NULL;
3531 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3532 return isl_basic_map_add_dims(bset, type, n);
3533 error:
3534 isl_basic_set_free(bset);
3535 return NULL;
3538 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3539 enum isl_dim_type type)
3541 isl_space *space;
3543 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3544 return map;
3546 space = isl_map_get_space(map);
3547 space = isl_space_reset(space, type);
3548 map = isl_map_reset_space(map, space);
3549 return map;
3552 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3553 enum isl_dim_type type, unsigned pos, unsigned n)
3555 int i;
3557 if (n == 0)
3558 return map_space_reset(map, type);
3560 map = isl_map_cow(map);
3561 if (!map)
3562 return NULL;
3564 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3565 if (!map->dim)
3566 goto error;
3568 for (i = 0; i < map->n; ++i) {
3569 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3570 if (!map->p[i])
3571 goto error;
3574 return map;
3575 error:
3576 isl_map_free(map);
3577 return NULL;
3580 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3581 enum isl_dim_type type, unsigned pos, unsigned n)
3583 return isl_map_insert_dims(set, type, pos, n);
3586 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3587 enum isl_dim_type type, unsigned n)
3589 if (!map)
3590 return NULL;
3591 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3594 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3595 enum isl_dim_type type, unsigned n)
3597 if (!set)
3598 return NULL;
3599 isl_assert(set->ctx, type != isl_dim_in, goto error);
3600 return set_from_map(isl_map_add_dims(set_to_map(set), type, n));
3601 error:
3602 isl_set_free(set);
3603 return NULL;
3606 __isl_give isl_basic_map *isl_basic_map_move_dims(
3607 __isl_take isl_basic_map *bmap,
3608 enum isl_dim_type dst_type, unsigned dst_pos,
3609 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3611 struct isl_dim_map *dim_map;
3612 struct isl_basic_map *res;
3613 enum isl_dim_type t;
3614 unsigned total, off;
3616 if (!bmap)
3617 return NULL;
3618 if (n == 0)
3619 return bmap;
3621 if (isl_basic_map_check_range(bmap, src_type, src_pos, n) < 0)
3622 return isl_basic_map_free(bmap);
3624 if (dst_type == src_type && dst_pos == src_pos)
3625 return bmap;
3627 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3629 if (pos(bmap->dim, dst_type) + dst_pos ==
3630 pos(bmap->dim, src_type) + src_pos +
3631 ((src_type < dst_type) ? n : 0)) {
3632 bmap = isl_basic_map_cow(bmap);
3633 if (!bmap)
3634 return NULL;
3636 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3637 src_type, src_pos, n);
3638 if (!bmap->dim)
3639 goto error;
3641 bmap = isl_basic_map_finalize(bmap);
3643 return bmap;
3646 total = isl_basic_map_total_dim(bmap);
3647 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3649 off = 0;
3650 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3651 unsigned size = isl_space_dim(bmap->dim, t);
3652 if (t == dst_type) {
3653 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3654 0, dst_pos, off);
3655 off += dst_pos;
3656 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3657 src_pos, n, off);
3658 off += n;
3659 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3660 dst_pos, size - dst_pos, off);
3661 off += size - dst_pos;
3662 } else if (t == src_type) {
3663 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3664 0, src_pos, off);
3665 off += src_pos;
3666 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3667 src_pos + n, size - src_pos - n, off);
3668 off += size - src_pos - n;
3669 } else {
3670 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3671 off += size;
3674 isl_dim_map_div(dim_map, bmap, off);
3676 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3677 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3678 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3679 if (!bmap)
3680 goto error;
3682 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3683 src_type, src_pos, n);
3684 if (!bmap->dim)
3685 goto error;
3687 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3688 bmap = isl_basic_map_gauss(bmap, NULL);
3689 bmap = isl_basic_map_finalize(bmap);
3691 return bmap;
3692 error:
3693 isl_basic_map_free(bmap);
3694 return NULL;
3697 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3698 enum isl_dim_type dst_type, unsigned dst_pos,
3699 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3701 isl_basic_map *bmap = bset_to_bmap(bset);
3702 bmap = isl_basic_map_move_dims(bmap, dst_type, dst_pos,
3703 src_type, src_pos, n);
3704 return bset_from_bmap(bmap);
3707 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3708 enum isl_dim_type dst_type, unsigned dst_pos,
3709 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3711 if (!set)
3712 return NULL;
3713 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3714 return set_from_map(isl_map_move_dims(set_to_map(set),
3715 dst_type, dst_pos, src_type, src_pos, n));
3716 error:
3717 isl_set_free(set);
3718 return NULL;
3721 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3722 enum isl_dim_type dst_type, unsigned dst_pos,
3723 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3725 int i;
3727 if (!map)
3728 return NULL;
3729 if (n == 0)
3730 return map;
3732 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3733 goto error);
3735 if (dst_type == src_type && dst_pos == src_pos)
3736 return map;
3738 isl_assert(map->ctx, dst_type != src_type, goto error);
3740 map = isl_map_cow(map);
3741 if (!map)
3742 return NULL;
3744 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3745 if (!map->dim)
3746 goto error;
3748 for (i = 0; i < map->n; ++i) {
3749 map->p[i] = isl_basic_map_move_dims(map->p[i],
3750 dst_type, dst_pos,
3751 src_type, src_pos, n);
3752 if (!map->p[i])
3753 goto error;
3756 return map;
3757 error:
3758 isl_map_free(map);
3759 return NULL;
3762 /* Move the specified dimensions to the last columns right before
3763 * the divs. Don't change the dimension specification of bmap.
3764 * That's the responsibility of the caller.
3766 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3767 enum isl_dim_type type, unsigned first, unsigned n)
3769 struct isl_dim_map *dim_map;
3770 struct isl_basic_map *res;
3771 enum isl_dim_type t;
3772 unsigned total, off;
3774 if (!bmap)
3775 return NULL;
3776 if (pos(bmap->dim, type) + first + n ==
3777 1 + isl_space_dim(bmap->dim, isl_dim_all))
3778 return bmap;
3780 total = isl_basic_map_total_dim(bmap);
3781 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3783 off = 0;
3784 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3785 unsigned size = isl_space_dim(bmap->dim, t);
3786 if (t == type) {
3787 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3788 0, first, off);
3789 off += first;
3790 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3791 first, n, total - bmap->n_div - n);
3792 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3793 first + n, size - (first + n), off);
3794 off += size - (first + n);
3795 } else {
3796 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3797 off += size;
3800 isl_dim_map_div(dim_map, bmap, off + n);
3802 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3803 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3804 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3805 return res;
3808 /* Insert "n" rows in the divs of "bmap".
3810 * The number of columns is not changed, which means that the last
3811 * dimensions of "bmap" are being reintepreted as the new divs.
3812 * The space of "bmap" is not adjusted, however, which means
3813 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3814 * from the space of "bmap" is the responsibility of the caller.
3816 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3817 int n)
3819 int i;
3820 size_t row_size;
3821 isl_int **new_div;
3822 isl_int *old;
3824 bmap = isl_basic_map_cow(bmap);
3825 if (!bmap)
3826 return NULL;
3828 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3829 old = bmap->block2.data;
3830 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3831 (bmap->extra + n) * (1 + row_size));
3832 if (!bmap->block2.data)
3833 return isl_basic_map_free(bmap);
3834 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3835 if (!new_div)
3836 return isl_basic_map_free(bmap);
3837 for (i = 0; i < n; ++i) {
3838 new_div[i] = bmap->block2.data +
3839 (bmap->extra + i) * (1 + row_size);
3840 isl_seq_clr(new_div[i], 1 + row_size);
3842 for (i = 0; i < bmap->extra; ++i)
3843 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3844 free(bmap->div);
3845 bmap->div = new_div;
3846 bmap->n_div += n;
3847 bmap->extra += n;
3849 return bmap;
3852 /* Drop constraints from "bmap" that only involve the variables
3853 * of "type" in the range [first, first + n] that are not related
3854 * to any of the variables outside that interval.
3855 * These constraints cannot influence the values for the variables
3856 * outside the interval, except in case they cause "bmap" to be empty.
3857 * Only drop the constraints if "bmap" is known to be non-empty.
3859 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3860 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3861 unsigned first, unsigned n)
3863 int i;
3864 int *groups;
3865 unsigned dim, n_div;
3866 isl_bool non_empty;
3868 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3869 if (non_empty < 0)
3870 return isl_basic_map_free(bmap);
3871 if (!non_empty)
3872 return bmap;
3874 dim = isl_basic_map_dim(bmap, isl_dim_all);
3875 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3876 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3877 if (!groups)
3878 return isl_basic_map_free(bmap);
3879 first += isl_basic_map_offset(bmap, type) - 1;
3880 for (i = 0; i < first; ++i)
3881 groups[i] = -1;
3882 for (i = first + n; i < dim - n_div; ++i)
3883 groups[i] = -1;
3885 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3887 return bmap;
3890 /* Turn the n dimensions of type type, starting at first
3891 * into existentially quantified variables.
3893 * If a subset of the projected out variables are unrelated
3894 * to any of the variables that remain, then the constraints
3895 * involving this subset are simply dropped first.
3897 __isl_give isl_basic_map *isl_basic_map_project_out(
3898 __isl_take isl_basic_map *bmap,
3899 enum isl_dim_type type, unsigned first, unsigned n)
3901 if (n == 0)
3902 return basic_map_space_reset(bmap, type);
3903 if (type == isl_dim_div)
3904 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3905 "cannot project out existentially quantified variables",
3906 return isl_basic_map_free(bmap));
3908 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3909 if (!bmap)
3910 return NULL;
3912 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3913 return isl_basic_map_remove_dims(bmap, type, first, n);
3915 if (isl_basic_map_check_range(bmap, type, first, n) < 0)
3916 return isl_basic_map_free(bmap);
3918 bmap = move_last(bmap, type, first, n);
3919 bmap = isl_basic_map_cow(bmap);
3920 bmap = insert_div_rows(bmap, n);
3921 if (!bmap)
3922 return NULL;
3924 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3925 if (!bmap->dim)
3926 goto error;
3927 bmap = isl_basic_map_simplify(bmap);
3928 bmap = isl_basic_map_drop_redundant_divs(bmap);
3929 return isl_basic_map_finalize(bmap);
3930 error:
3931 isl_basic_map_free(bmap);
3932 return NULL;
3935 /* Turn the n dimensions of type type, starting at first
3936 * into existentially quantified variables.
3938 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3939 enum isl_dim_type type, unsigned first, unsigned n)
3941 return bset_from_bmap(isl_basic_map_project_out(bset_to_bmap(bset),
3942 type, first, n));
3945 /* Turn the n dimensions of type type, starting at first
3946 * into existentially quantified variables.
3948 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3949 enum isl_dim_type type, unsigned first, unsigned n)
3951 int i;
3953 if (!map)
3954 return NULL;
3956 if (n == 0)
3957 return map_space_reset(map, type);
3959 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3961 map = isl_map_cow(map);
3962 if (!map)
3963 return NULL;
3965 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3966 if (!map->dim)
3967 goto error;
3969 for (i = 0; i < map->n; ++i) {
3970 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3971 if (!map->p[i])
3972 goto error;
3975 return map;
3976 error:
3977 isl_map_free(map);
3978 return NULL;
3981 /* Turn the n dimensions of type type, starting at first
3982 * into existentially quantified variables.
3984 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3985 enum isl_dim_type type, unsigned first, unsigned n)
3987 return set_from_map(isl_map_project_out(set_to_map(set),
3988 type, first, n));
3991 /* Return a map that projects the elements in "set" onto their
3992 * "n" set dimensions starting at "first".
3993 * "type" should be equal to isl_dim_set.
3995 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3996 enum isl_dim_type type, unsigned first, unsigned n)
3998 int i;
3999 int dim;
4000 isl_map *map;
4002 if (!set)
4003 return NULL;
4004 if (type != isl_dim_set)
4005 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4006 "only set dimensions can be projected out", goto error);
4007 dim = isl_set_dim(set, isl_dim_set);
4008 if (first + n > dim || first + n < first)
4009 isl_die(isl_set_get_ctx(set), isl_error_invalid,
4010 "index out of bounds", goto error);
4012 map = isl_map_from_domain(set);
4013 map = isl_map_add_dims(map, isl_dim_out, n);
4014 for (i = 0; i < n; ++i)
4015 map = isl_map_equate(map, isl_dim_in, first + i,
4016 isl_dim_out, i);
4017 return map;
4018 error:
4019 isl_set_free(set);
4020 return NULL;
4023 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
4025 int i, j;
4027 for (i = 0; i < n; ++i) {
4028 j = isl_basic_map_alloc_div(bmap);
4029 if (j < 0)
4030 goto error;
4031 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
4033 return bmap;
4034 error:
4035 isl_basic_map_free(bmap);
4036 return NULL;
4039 struct isl_basic_map *isl_basic_map_apply_range(
4040 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4042 isl_space *dim_result = NULL;
4043 struct isl_basic_map *bmap;
4044 unsigned n_in, n_out, n, nparam, total, pos;
4045 struct isl_dim_map *dim_map1, *dim_map2;
4047 if (!bmap1 || !bmap2)
4048 goto error;
4049 if (!isl_space_match(bmap1->dim, isl_dim_param,
4050 bmap2->dim, isl_dim_param))
4051 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4052 "parameters don't match", goto error);
4053 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
4054 bmap2->dim, isl_dim_in))
4055 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4056 "spaces don't match", goto error);
4058 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
4059 isl_space_copy(bmap2->dim));
4061 n_in = isl_basic_map_n_in(bmap1);
4062 n_out = isl_basic_map_n_out(bmap2);
4063 n = isl_basic_map_n_out(bmap1);
4064 nparam = isl_basic_map_n_param(bmap1);
4066 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
4067 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4068 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4069 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4070 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4071 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4072 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
4073 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
4074 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4075 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4076 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4078 bmap = isl_basic_map_alloc_space(dim_result,
4079 bmap1->n_div + bmap2->n_div + n,
4080 bmap1->n_eq + bmap2->n_eq,
4081 bmap1->n_ineq + bmap2->n_ineq);
4082 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4083 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4084 bmap = add_divs(bmap, n);
4085 bmap = isl_basic_map_simplify(bmap);
4086 bmap = isl_basic_map_drop_redundant_divs(bmap);
4087 return isl_basic_map_finalize(bmap);
4088 error:
4089 isl_basic_map_free(bmap1);
4090 isl_basic_map_free(bmap2);
4091 return NULL;
4094 struct isl_basic_set *isl_basic_set_apply(
4095 struct isl_basic_set *bset, struct isl_basic_map *bmap)
4097 if (!bset || !bmap)
4098 goto error;
4100 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
4101 goto error);
4103 return bset_from_bmap(isl_basic_map_apply_range(bset_to_bmap(bset),
4104 bmap));
4105 error:
4106 isl_basic_set_free(bset);
4107 isl_basic_map_free(bmap);
4108 return NULL;
4111 struct isl_basic_map *isl_basic_map_apply_domain(
4112 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4114 if (!bmap1 || !bmap2)
4115 goto error;
4117 if (!isl_space_match(bmap1->dim, isl_dim_param,
4118 bmap2->dim, isl_dim_param))
4119 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4120 "parameters don't match", goto error);
4121 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_in,
4122 bmap2->dim, isl_dim_in))
4123 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
4124 "spaces don't match", goto error);
4126 bmap1 = isl_basic_map_reverse(bmap1);
4127 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
4128 return isl_basic_map_reverse(bmap1);
4129 error:
4130 isl_basic_map_free(bmap1);
4131 isl_basic_map_free(bmap2);
4132 return NULL;
4135 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
4136 * A \cap B -> f(A) + f(B)
4138 struct isl_basic_map *isl_basic_map_sum(
4139 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4141 unsigned n_in, n_out, nparam, total, pos;
4142 struct isl_basic_map *bmap = NULL;
4143 struct isl_dim_map *dim_map1, *dim_map2;
4144 int i;
4146 if (!bmap1 || !bmap2)
4147 goto error;
4149 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
4150 goto error);
4152 nparam = isl_basic_map_n_param(bmap1);
4153 n_in = isl_basic_map_n_in(bmap1);
4154 n_out = isl_basic_map_n_out(bmap1);
4156 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
4157 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4158 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
4159 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4160 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
4161 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4162 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
4163 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
4164 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4165 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
4166 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
4168 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
4169 bmap1->n_div + bmap2->n_div + 2 * n_out,
4170 bmap1->n_eq + bmap2->n_eq + n_out,
4171 bmap1->n_ineq + bmap2->n_ineq);
4172 for (i = 0; i < n_out; ++i) {
4173 int j = isl_basic_map_alloc_equality(bmap);
4174 if (j < 0)
4175 goto error;
4176 isl_seq_clr(bmap->eq[j], 1+total);
4177 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
4178 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
4179 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
4181 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
4182 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
4183 bmap = add_divs(bmap, 2 * n_out);
4185 bmap = isl_basic_map_simplify(bmap);
4186 return isl_basic_map_finalize(bmap);
4187 error:
4188 isl_basic_map_free(bmap);
4189 isl_basic_map_free(bmap1);
4190 isl_basic_map_free(bmap2);
4191 return NULL;
4194 /* Given two maps A -> f(A) and B -> g(B), construct a map
4195 * A \cap B -> f(A) + f(B)
4197 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
4199 struct isl_map *result;
4200 int i, j;
4202 if (!map1 || !map2)
4203 goto error;
4205 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
4207 result = isl_map_alloc_space(isl_space_copy(map1->dim),
4208 map1->n * map2->n, 0);
4209 if (!result)
4210 goto error;
4211 for (i = 0; i < map1->n; ++i)
4212 for (j = 0; j < map2->n; ++j) {
4213 struct isl_basic_map *part;
4214 part = isl_basic_map_sum(
4215 isl_basic_map_copy(map1->p[i]),
4216 isl_basic_map_copy(map2->p[j]));
4217 if (isl_basic_map_is_empty(part))
4218 isl_basic_map_free(part);
4219 else
4220 result = isl_map_add_basic_map(result, part);
4221 if (!result)
4222 goto error;
4224 isl_map_free(map1);
4225 isl_map_free(map2);
4226 return result;
4227 error:
4228 isl_map_free(map1);
4229 isl_map_free(map2);
4230 return NULL;
4233 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
4234 __isl_take isl_set *set2)
4236 return set_from_map(isl_map_sum(set_to_map(set1), set_to_map(set2)));
4239 /* Given a basic map A -> f(A), construct A -> -f(A).
4241 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
4243 int i, j;
4244 unsigned off, n;
4246 bmap = isl_basic_map_cow(bmap);
4247 if (!bmap)
4248 return NULL;
4250 n = isl_basic_map_dim(bmap, isl_dim_out);
4251 off = isl_basic_map_offset(bmap, isl_dim_out);
4252 for (i = 0; i < bmap->n_eq; ++i)
4253 for (j = 0; j < n; ++j)
4254 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
4255 for (i = 0; i < bmap->n_ineq; ++i)
4256 for (j = 0; j < n; ++j)
4257 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
4258 for (i = 0; i < bmap->n_div; ++i)
4259 for (j = 0; j < n; ++j)
4260 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
4261 bmap = isl_basic_map_gauss(bmap, NULL);
4262 return isl_basic_map_finalize(bmap);
4265 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4267 return isl_basic_map_neg(bset);
4270 /* Given a map A -> f(A), construct A -> -f(A).
4272 struct isl_map *isl_map_neg(struct isl_map *map)
4274 int i;
4276 map = isl_map_cow(map);
4277 if (!map)
4278 return NULL;
4280 for (i = 0; i < map->n; ++i) {
4281 map->p[i] = isl_basic_map_neg(map->p[i]);
4282 if (!map->p[i])
4283 goto error;
4286 return map;
4287 error:
4288 isl_map_free(map);
4289 return NULL;
4292 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4294 return set_from_map(isl_map_neg(set_to_map(set)));
4297 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4298 * A -> floor(f(A)/d).
4300 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4301 isl_int d)
4303 unsigned n_in, n_out, nparam, total, pos;
4304 struct isl_basic_map *result = NULL;
4305 struct isl_dim_map *dim_map;
4306 int i;
4308 if (!bmap)
4309 return NULL;
4311 nparam = isl_basic_map_n_param(bmap);
4312 n_in = isl_basic_map_n_in(bmap);
4313 n_out = isl_basic_map_n_out(bmap);
4315 total = nparam + n_in + n_out + bmap->n_div + n_out;
4316 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4317 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4318 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4319 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4320 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4322 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4323 bmap->n_div + n_out,
4324 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4325 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4326 result = add_divs(result, n_out);
4327 for (i = 0; i < n_out; ++i) {
4328 int j;
4329 j = isl_basic_map_alloc_inequality(result);
4330 if (j < 0)
4331 goto error;
4332 isl_seq_clr(result->ineq[j], 1+total);
4333 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4334 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4335 j = isl_basic_map_alloc_inequality(result);
4336 if (j < 0)
4337 goto error;
4338 isl_seq_clr(result->ineq[j], 1+total);
4339 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4340 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4341 isl_int_sub_ui(result->ineq[j][0], d, 1);
4344 result = isl_basic_map_simplify(result);
4345 return isl_basic_map_finalize(result);
4346 error:
4347 isl_basic_map_free(result);
4348 return NULL;
4351 /* Given a map A -> f(A) and an integer d, construct a map
4352 * A -> floor(f(A)/d).
4354 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4356 int i;
4358 map = isl_map_cow(map);
4359 if (!map)
4360 return NULL;
4362 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4363 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4364 for (i = 0; i < map->n; ++i) {
4365 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4366 if (!map->p[i])
4367 goto error;
4370 return map;
4371 error:
4372 isl_map_free(map);
4373 return NULL;
4376 /* Given a map A -> f(A) and an integer d, construct a map
4377 * A -> floor(f(A)/d).
4379 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4380 __isl_take isl_val *d)
4382 if (!map || !d)
4383 goto error;
4384 if (!isl_val_is_int(d))
4385 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4386 "expecting integer denominator", goto error);
4387 map = isl_map_floordiv(map, d->n);
4388 isl_val_free(d);
4389 return map;
4390 error:
4391 isl_map_free(map);
4392 isl_val_free(d);
4393 return NULL;
4396 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4398 int i;
4399 unsigned nparam;
4400 unsigned n_in;
4402 i = isl_basic_map_alloc_equality(bmap);
4403 if (i < 0)
4404 goto error;
4405 nparam = isl_basic_map_n_param(bmap);
4406 n_in = isl_basic_map_n_in(bmap);
4407 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4408 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4409 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4410 return isl_basic_map_finalize(bmap);
4411 error:
4412 isl_basic_map_free(bmap);
4413 return NULL;
4416 /* Add a constraint to "bmap" expressing i_pos < o_pos
4418 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4420 int i;
4421 unsigned nparam;
4422 unsigned n_in;
4424 i = isl_basic_map_alloc_inequality(bmap);
4425 if (i < 0)
4426 goto error;
4427 nparam = isl_basic_map_n_param(bmap);
4428 n_in = isl_basic_map_n_in(bmap);
4429 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4430 isl_int_set_si(bmap->ineq[i][0], -1);
4431 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4432 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4433 return isl_basic_map_finalize(bmap);
4434 error:
4435 isl_basic_map_free(bmap);
4436 return NULL;
4439 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4441 static __isl_give isl_basic_map *var_less_or_equal(
4442 __isl_take isl_basic_map *bmap, unsigned pos)
4444 int i;
4445 unsigned nparam;
4446 unsigned n_in;
4448 i = isl_basic_map_alloc_inequality(bmap);
4449 if (i < 0)
4450 goto error;
4451 nparam = isl_basic_map_n_param(bmap);
4452 n_in = isl_basic_map_n_in(bmap);
4453 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4454 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4455 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4456 return isl_basic_map_finalize(bmap);
4457 error:
4458 isl_basic_map_free(bmap);
4459 return NULL;
4462 /* Add a constraint to "bmap" expressing i_pos > o_pos
4464 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4466 int i;
4467 unsigned nparam;
4468 unsigned n_in;
4470 i = isl_basic_map_alloc_inequality(bmap);
4471 if (i < 0)
4472 goto error;
4473 nparam = isl_basic_map_n_param(bmap);
4474 n_in = isl_basic_map_n_in(bmap);
4475 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4476 isl_int_set_si(bmap->ineq[i][0], -1);
4477 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4478 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4479 return isl_basic_map_finalize(bmap);
4480 error:
4481 isl_basic_map_free(bmap);
4482 return NULL;
4485 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4487 static __isl_give isl_basic_map *var_more_or_equal(
4488 __isl_take isl_basic_map *bmap, unsigned pos)
4490 int i;
4491 unsigned nparam;
4492 unsigned n_in;
4494 i = isl_basic_map_alloc_inequality(bmap);
4495 if (i < 0)
4496 goto error;
4497 nparam = isl_basic_map_n_param(bmap);
4498 n_in = isl_basic_map_n_in(bmap);
4499 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4500 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4501 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4502 return isl_basic_map_finalize(bmap);
4503 error:
4504 isl_basic_map_free(bmap);
4505 return NULL;
4508 __isl_give isl_basic_map *isl_basic_map_equal(
4509 __isl_take isl_space *dim, unsigned n_equal)
4511 int i;
4512 struct isl_basic_map *bmap;
4513 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4514 if (!bmap)
4515 return NULL;
4516 for (i = 0; i < n_equal && bmap; ++i)
4517 bmap = var_equal(bmap, i);
4518 return isl_basic_map_finalize(bmap);
4521 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4523 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4524 unsigned pos)
4526 int i;
4527 struct isl_basic_map *bmap;
4528 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4529 if (!bmap)
4530 return NULL;
4531 for (i = 0; i < pos && bmap; ++i)
4532 bmap = var_equal(bmap, i);
4533 if (bmap)
4534 bmap = var_less(bmap, pos);
4535 return isl_basic_map_finalize(bmap);
4538 /* Return a relation on "dim" expressing i_[0..pos] <<= o_[0..pos]
4540 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4541 __isl_take isl_space *dim, unsigned pos)
4543 int i;
4544 isl_basic_map *bmap;
4546 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4547 for (i = 0; i < pos; ++i)
4548 bmap = var_equal(bmap, i);
4549 bmap = var_less_or_equal(bmap, pos);
4550 return isl_basic_map_finalize(bmap);
4553 /* Return a relation on "dim" expressing i_pos > o_pos
4555 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4556 unsigned pos)
4558 int i;
4559 struct isl_basic_map *bmap;
4560 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4561 if (!bmap)
4562 return NULL;
4563 for (i = 0; i < pos && bmap; ++i)
4564 bmap = var_equal(bmap, i);
4565 if (bmap)
4566 bmap = var_more(bmap, pos);
4567 return isl_basic_map_finalize(bmap);
4570 /* Return a relation on "dim" expressing i_[0..pos] >>= o_[0..pos]
4572 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4573 __isl_take isl_space *dim, unsigned pos)
4575 int i;
4576 isl_basic_map *bmap;
4578 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4579 for (i = 0; i < pos; ++i)
4580 bmap = var_equal(bmap, i);
4581 bmap = var_more_or_equal(bmap, pos);
4582 return isl_basic_map_finalize(bmap);
4585 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4586 unsigned n, int equal)
4588 struct isl_map *map;
4589 int i;
4591 if (n == 0 && equal)
4592 return isl_map_universe(dims);
4594 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4596 for (i = 0; i + 1 < n; ++i)
4597 map = isl_map_add_basic_map(map,
4598 isl_basic_map_less_at(isl_space_copy(dims), i));
4599 if (n > 0) {
4600 if (equal)
4601 map = isl_map_add_basic_map(map,
4602 isl_basic_map_less_or_equal_at(dims, n - 1));
4603 else
4604 map = isl_map_add_basic_map(map,
4605 isl_basic_map_less_at(dims, n - 1));
4606 } else
4607 isl_space_free(dims);
4609 return map;
4612 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4614 if (!dims)
4615 return NULL;
4616 return map_lex_lte_first(dims, dims->n_out, equal);
4619 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4621 return map_lex_lte_first(dim, n, 0);
4624 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4626 return map_lex_lte_first(dim, n, 1);
4629 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4631 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4634 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4636 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4639 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4640 unsigned n, int equal)
4642 struct isl_map *map;
4643 int i;
4645 if (n == 0 && equal)
4646 return isl_map_universe(dims);
4648 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4650 for (i = 0; i + 1 < n; ++i)
4651 map = isl_map_add_basic_map(map,
4652 isl_basic_map_more_at(isl_space_copy(dims), i));
4653 if (n > 0) {
4654 if (equal)
4655 map = isl_map_add_basic_map(map,
4656 isl_basic_map_more_or_equal_at(dims, n - 1));
4657 else
4658 map = isl_map_add_basic_map(map,
4659 isl_basic_map_more_at(dims, n - 1));
4660 } else
4661 isl_space_free(dims);
4663 return map;
4666 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4668 if (!dims)
4669 return NULL;
4670 return map_lex_gte_first(dims, dims->n_out, equal);
4673 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4675 return map_lex_gte_first(dim, n, 0);
4678 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4680 return map_lex_gte_first(dim, n, 1);
4683 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4685 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4688 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4690 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4693 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4694 __isl_take isl_set *set2)
4696 isl_map *map;
4697 map = isl_map_lex_le(isl_set_get_space(set1));
4698 map = isl_map_intersect_domain(map, set1);
4699 map = isl_map_intersect_range(map, set2);
4700 return map;
4703 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4704 __isl_take isl_set *set2)
4706 isl_map *map;
4707 map = isl_map_lex_lt(isl_set_get_space(set1));
4708 map = isl_map_intersect_domain(map, set1);
4709 map = isl_map_intersect_range(map, set2);
4710 return map;
4713 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4714 __isl_take isl_set *set2)
4716 isl_map *map;
4717 map = isl_map_lex_ge(isl_set_get_space(set1));
4718 map = isl_map_intersect_domain(map, set1);
4719 map = isl_map_intersect_range(map, set2);
4720 return map;
4723 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4724 __isl_take isl_set *set2)
4726 isl_map *map;
4727 map = isl_map_lex_gt(isl_set_get_space(set1));
4728 map = isl_map_intersect_domain(map, set1);
4729 map = isl_map_intersect_range(map, set2);
4730 return map;
4733 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4734 __isl_take isl_map *map2)
4736 isl_map *map;
4737 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4738 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4739 map = isl_map_apply_range(map, isl_map_reverse(map2));
4740 return map;
4743 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4744 __isl_take isl_map *map2)
4746 isl_map *map;
4747 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4748 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4749 map = isl_map_apply_range(map, isl_map_reverse(map2));
4750 return map;
4753 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4754 __isl_take isl_map *map2)
4756 isl_map *map;
4757 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4758 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4759 map = isl_map_apply_range(map, isl_map_reverse(map2));
4760 return map;
4763 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4764 __isl_take isl_map *map2)
4766 isl_map *map;
4767 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4768 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4769 map = isl_map_apply_range(map, isl_map_reverse(map2));
4770 return map;
4773 /* For a div d = floor(f/m), add the constraint
4775 * f - m d >= 0
4777 static isl_stat add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4778 unsigned pos, isl_int *div)
4780 int i;
4781 unsigned total = isl_basic_map_total_dim(bmap);
4783 i = isl_basic_map_alloc_inequality(bmap);
4784 if (i < 0)
4785 return isl_stat_error;
4786 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4787 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4789 return isl_stat_ok;
4792 /* For a div d = floor(f/m), add the constraint
4794 * -(f-(m-1)) + m d >= 0
4796 static isl_stat add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4797 unsigned pos, isl_int *div)
4799 int i;
4800 unsigned total = isl_basic_map_total_dim(bmap);
4802 i = isl_basic_map_alloc_inequality(bmap);
4803 if (i < 0)
4804 return isl_stat_error;
4805 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4806 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4807 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4808 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4810 return isl_stat_ok;
4813 /* For a div d = floor(f/m), add the constraints
4815 * f - m d >= 0
4816 * -(f-(m-1)) + m d >= 0
4818 * Note that the second constraint is the negation of
4820 * f - m d >= m
4822 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4823 unsigned pos, isl_int *div)
4825 if (add_upper_div_constraint(bmap, pos, div) < 0)
4826 return -1;
4827 if (add_lower_div_constraint(bmap, pos, div) < 0)
4828 return -1;
4829 return 0;
4832 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4833 unsigned pos, isl_int *div)
4835 return isl_basic_map_add_div_constraints_var(bset_to_bmap(bset),
4836 pos, div);
4839 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4841 unsigned total = isl_basic_map_total_dim(bmap);
4842 unsigned div_pos = total - bmap->n_div + div;
4844 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4845 bmap->div[div]);
4848 /* For each known div d = floor(f/m), add the constraints
4850 * f - m d >= 0
4851 * -(f-(m-1)) + m d >= 0
4853 * Remove duplicate constraints in case of some these div constraints
4854 * already appear in "bmap".
4856 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4857 __isl_take isl_basic_map *bmap)
4859 unsigned n_div;
4861 if (!bmap)
4862 return NULL;
4863 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4864 if (n_div == 0)
4865 return bmap;
4867 bmap = add_known_div_constraints(bmap);
4868 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4869 bmap = isl_basic_map_finalize(bmap);
4870 return bmap;
4873 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4875 * In particular, if this div is of the form d = floor(f/m),
4876 * then add the constraint
4878 * f - m d >= 0
4880 * if sign < 0 or the constraint
4882 * -(f-(m-1)) + m d >= 0
4884 * if sign > 0.
4886 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4887 unsigned div, int sign)
4889 unsigned total;
4890 unsigned div_pos;
4892 if (!bmap)
4893 return -1;
4895 total = isl_basic_map_total_dim(bmap);
4896 div_pos = total - bmap->n_div + div;
4898 if (sign < 0)
4899 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4900 else
4901 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4904 struct isl_basic_set *isl_basic_map_underlying_set(
4905 struct isl_basic_map *bmap)
4907 if (!bmap)
4908 goto error;
4909 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4910 bmap->n_div == 0 &&
4911 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4912 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4913 return bset_from_bmap(bmap);
4914 bmap = isl_basic_map_cow(bmap);
4915 if (!bmap)
4916 goto error;
4917 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4918 if (!bmap->dim)
4919 goto error;
4920 bmap->extra -= bmap->n_div;
4921 bmap->n_div = 0;
4922 bmap = isl_basic_map_finalize(bmap);
4923 return bset_from_bmap(bmap);
4924 error:
4925 isl_basic_map_free(bmap);
4926 return NULL;
4929 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4930 __isl_take isl_basic_set *bset)
4932 return isl_basic_map_underlying_set(bset_to_bmap(bset));
4935 /* Replace each element in "list" by the result of applying
4936 * isl_basic_map_underlying_set to the element.
4938 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4939 __isl_take isl_basic_map_list *list)
4941 int i, n;
4943 if (!list)
4944 return NULL;
4946 n = isl_basic_map_list_n_basic_map(list);
4947 for (i = 0; i < n; ++i) {
4948 isl_basic_map *bmap;
4949 isl_basic_set *bset;
4951 bmap = isl_basic_map_list_get_basic_map(list, i);
4952 bset = isl_basic_set_underlying_set(bmap);
4953 list = isl_basic_set_list_set_basic_set(list, i, bset);
4956 return list;
4959 struct isl_basic_map *isl_basic_map_overlying_set(
4960 struct isl_basic_set *bset, struct isl_basic_map *like)
4962 struct isl_basic_map *bmap;
4963 struct isl_ctx *ctx;
4964 unsigned total;
4965 int i;
4967 if (!bset || !like)
4968 goto error;
4969 ctx = bset->ctx;
4970 isl_assert(ctx, bset->n_div == 0, goto error);
4971 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4972 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4973 goto error);
4974 if (like->n_div == 0) {
4975 isl_space *space = isl_basic_map_get_space(like);
4976 isl_basic_map_free(like);
4977 return isl_basic_map_reset_space(bset, space);
4979 bset = isl_basic_set_cow(bset);
4980 if (!bset)
4981 goto error;
4982 total = bset->dim->n_out + bset->extra;
4983 bmap = bset_to_bmap(bset);
4984 isl_space_free(bmap->dim);
4985 bmap->dim = isl_space_copy(like->dim);
4986 if (!bmap->dim)
4987 goto error;
4988 bmap->n_div = like->n_div;
4989 bmap->extra += like->n_div;
4990 if (bmap->extra) {
4991 unsigned ltotal;
4992 isl_int **div;
4993 ltotal = total - bmap->extra + like->extra;
4994 if (ltotal > total)
4995 ltotal = total;
4996 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4997 bmap->extra * (1 + 1 + total));
4998 if (isl_blk_is_error(bmap->block2))
4999 goto error;
5000 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
5001 if (!div)
5002 goto error;
5003 bmap->div = div;
5004 for (i = 0; i < bmap->extra; ++i)
5005 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
5006 for (i = 0; i < like->n_div; ++i) {
5007 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
5008 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
5010 bmap = isl_basic_map_add_known_div_constraints(bmap);
5012 isl_basic_map_free(like);
5013 bmap = isl_basic_map_simplify(bmap);
5014 bmap = isl_basic_map_finalize(bmap);
5015 return bmap;
5016 error:
5017 isl_basic_map_free(like);
5018 isl_basic_set_free(bset);
5019 return NULL;
5022 struct isl_basic_set *isl_basic_set_from_underlying_set(
5023 struct isl_basic_set *bset, struct isl_basic_set *like)
5025 return bset_from_bmap(isl_basic_map_overlying_set(bset,
5026 bset_to_bmap(like)));
5029 struct isl_set *isl_map_underlying_set(struct isl_map *map)
5031 int i;
5033 map = isl_map_cow(map);
5034 if (!map)
5035 return NULL;
5036 map->dim = isl_space_cow(map->dim);
5037 if (!map->dim)
5038 goto error;
5040 for (i = 1; i < map->n; ++i)
5041 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
5042 goto error);
5043 for (i = 0; i < map->n; ++i) {
5044 map->p[i] = bset_to_bmap(
5045 isl_basic_map_underlying_set(map->p[i]));
5046 if (!map->p[i])
5047 goto error;
5049 if (map->n == 0)
5050 map->dim = isl_space_underlying(map->dim, 0);
5051 else {
5052 isl_space_free(map->dim);
5053 map->dim = isl_space_copy(map->p[0]->dim);
5055 if (!map->dim)
5056 goto error;
5057 return set_from_map(map);
5058 error:
5059 isl_map_free(map);
5060 return NULL;
5063 /* Replace the space of "bmap" by "space".
5065 * If the space of "bmap" is identical to "space" (including the identifiers
5066 * of the input and output dimensions), then simply return the original input.
5068 __isl_give isl_basic_map *isl_basic_map_reset_space(
5069 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
5071 isl_bool equal;
5072 isl_space *bmap_space;
5074 bmap_space = isl_basic_map_peek_space(bmap);
5075 equal = isl_space_is_equal(bmap_space, space);
5076 if (equal >= 0 && equal)
5077 equal = isl_space_match(bmap_space, isl_dim_in,
5078 space, isl_dim_in);
5079 if (equal >= 0 && equal)
5080 equal = isl_space_match(bmap_space, isl_dim_out,
5081 space, isl_dim_out);
5082 if (equal < 0)
5083 goto error;
5084 if (equal) {
5085 isl_space_free(space);
5086 return bmap;
5088 bmap = isl_basic_map_cow(bmap);
5089 if (!bmap || !space)
5090 goto error;
5092 isl_space_free(bmap->dim);
5093 bmap->dim = space;
5095 bmap = isl_basic_map_finalize(bmap);
5097 return bmap;
5098 error:
5099 isl_basic_map_free(bmap);
5100 isl_space_free(space);
5101 return NULL;
5104 __isl_give isl_basic_set *isl_basic_set_reset_space(
5105 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
5107 return bset_from_bmap(isl_basic_map_reset_space(bset_to_bmap(bset),
5108 dim));
5111 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
5112 __isl_take isl_space *dim)
5114 int i;
5116 map = isl_map_cow(map);
5117 if (!map || !dim)
5118 goto error;
5120 for (i = 0; i < map->n; ++i) {
5121 map->p[i] = isl_basic_map_reset_space(map->p[i],
5122 isl_space_copy(dim));
5123 if (!map->p[i])
5124 goto error;
5126 isl_space_free(map->dim);
5127 map->dim = dim;
5129 return map;
5130 error:
5131 isl_map_free(map);
5132 isl_space_free(dim);
5133 return NULL;
5136 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
5137 __isl_take isl_space *dim)
5139 return set_from_map(isl_map_reset_space(set_to_map(set), dim));
5142 /* Compute the parameter domain of the given basic set.
5144 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
5146 isl_bool is_params;
5147 isl_space *space;
5148 unsigned n;
5150 is_params = isl_basic_set_is_params(bset);
5151 if (is_params < 0)
5152 return isl_basic_set_free(bset);
5153 if (is_params)
5154 return bset;
5156 n = isl_basic_set_dim(bset, isl_dim_set);
5157 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
5158 space = isl_basic_set_get_space(bset);
5159 space = isl_space_params(space);
5160 bset = isl_basic_set_reset_space(bset, space);
5161 return bset;
5164 /* Construct a zero-dimensional basic set with the given parameter domain.
5166 __isl_give isl_basic_set *isl_basic_set_from_params(
5167 __isl_take isl_basic_set *bset)
5169 isl_space *space;
5170 space = isl_basic_set_get_space(bset);
5171 space = isl_space_set_from_params(space);
5172 bset = isl_basic_set_reset_space(bset, space);
5173 return bset;
5176 /* Compute the parameter domain of the given set.
5178 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
5180 isl_space *space;
5181 unsigned n;
5183 if (isl_set_is_params(set))
5184 return set;
5186 n = isl_set_dim(set, isl_dim_set);
5187 set = isl_set_project_out(set, isl_dim_set, 0, n);
5188 space = isl_set_get_space(set);
5189 space = isl_space_params(space);
5190 set = isl_set_reset_space(set, space);
5191 return set;
5194 /* Construct a zero-dimensional set with the given parameter domain.
5196 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
5198 isl_space *space;
5199 space = isl_set_get_space(set);
5200 space = isl_space_set_from_params(space);
5201 set = isl_set_reset_space(set, space);
5202 return set;
5205 /* Compute the parameter domain of the given map.
5207 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5209 isl_space *space;
5210 unsigned n;
5212 n = isl_map_dim(map, isl_dim_in);
5213 map = isl_map_project_out(map, isl_dim_in, 0, n);
5214 n = isl_map_dim(map, isl_dim_out);
5215 map = isl_map_project_out(map, isl_dim_out, 0, n);
5216 space = isl_map_get_space(map);
5217 space = isl_space_params(space);
5218 map = isl_map_reset_space(map, space);
5219 return map;
5222 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5224 isl_space *space;
5225 unsigned n_out;
5227 if (!bmap)
5228 return NULL;
5229 space = isl_space_domain(isl_basic_map_get_space(bmap));
5231 n_out = isl_basic_map_n_out(bmap);
5232 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5234 return isl_basic_map_reset_space(bmap, space);
5237 isl_bool isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5239 if (!bmap)
5240 return isl_bool_error;
5241 return isl_space_may_be_set(bmap->dim);
5244 /* Is this basic map actually a set?
5245 * Users should never call this function. Outside of isl,
5246 * the type should indicate whether something is a set or a map.
5248 isl_bool isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5250 if (!bmap)
5251 return isl_bool_error;
5252 return isl_space_is_set(bmap->dim);
5255 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5257 isl_bool is_set;
5259 is_set = isl_basic_map_is_set(bmap);
5260 if (is_set < 0)
5261 goto error;
5262 if (is_set)
5263 return bmap;
5264 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5265 error:
5266 isl_basic_map_free(bmap);
5267 return NULL;
5270 __isl_give isl_basic_map *isl_basic_map_domain_map(
5271 __isl_take isl_basic_map *bmap)
5273 int i;
5274 isl_space *dim;
5275 isl_basic_map *domain;
5276 int nparam, n_in, n_out;
5278 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5279 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5280 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5282 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5283 domain = isl_basic_map_universe(dim);
5285 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5286 bmap = isl_basic_map_apply_range(bmap, domain);
5287 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5289 for (i = 0; i < n_in; ++i)
5290 bmap = isl_basic_map_equate(bmap, isl_dim_in, i,
5291 isl_dim_out, i);
5293 bmap = isl_basic_map_gauss(bmap, NULL);
5294 return isl_basic_map_finalize(bmap);
5297 __isl_give isl_basic_map *isl_basic_map_range_map(
5298 __isl_take isl_basic_map *bmap)
5300 int i;
5301 isl_space *dim;
5302 isl_basic_map *range;
5303 int nparam, n_in, n_out;
5305 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5306 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5307 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5309 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5310 range = isl_basic_map_universe(dim);
5312 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5313 bmap = isl_basic_map_apply_range(bmap, range);
5314 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5316 for (i = 0; i < n_out; ++i)
5317 bmap = isl_basic_map_equate(bmap, isl_dim_in, n_in + i,
5318 isl_dim_out, i);
5320 bmap = isl_basic_map_gauss(bmap, NULL);
5321 return isl_basic_map_finalize(bmap);
5324 int isl_map_may_be_set(__isl_keep isl_map *map)
5326 if (!map)
5327 return -1;
5328 return isl_space_may_be_set(map->dim);
5331 /* Is this map actually a set?
5332 * Users should never call this function. Outside of isl,
5333 * the type should indicate whether something is a set or a map.
5335 isl_bool isl_map_is_set(__isl_keep isl_map *map)
5337 if (!map)
5338 return isl_bool_error;
5339 return isl_space_is_set(map->dim);
5342 struct isl_set *isl_map_range(struct isl_map *map)
5344 int i;
5345 isl_bool is_set;
5346 struct isl_set *set;
5348 is_set = isl_map_is_set(map);
5349 if (is_set < 0)
5350 goto error;
5351 if (is_set)
5352 return set_from_map(map);
5354 map = isl_map_cow(map);
5355 if (!map)
5356 goto error;
5358 set = set_from_map(map);
5359 set->dim = isl_space_range(set->dim);
5360 if (!set->dim)
5361 goto error;
5362 for (i = 0; i < map->n; ++i) {
5363 set->p[i] = isl_basic_map_range(map->p[i]);
5364 if (!set->p[i])
5365 goto error;
5367 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5368 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5369 return set;
5370 error:
5371 isl_map_free(map);
5372 return NULL;
5375 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5377 int i;
5379 map = isl_map_cow(map);
5380 if (!map)
5381 return NULL;
5383 map->dim = isl_space_domain_map(map->dim);
5384 if (!map->dim)
5385 goto error;
5386 for (i = 0; i < map->n; ++i) {
5387 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5388 if (!map->p[i])
5389 goto error;
5391 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5392 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5393 return map;
5394 error:
5395 isl_map_free(map);
5396 return NULL;
5399 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5401 int i;
5402 isl_space *range_dim;
5404 map = isl_map_cow(map);
5405 if (!map)
5406 return NULL;
5408 range_dim = isl_space_range(isl_map_get_space(map));
5409 range_dim = isl_space_from_range(range_dim);
5410 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5411 map->dim = isl_space_join(map->dim, range_dim);
5412 if (!map->dim)
5413 goto error;
5414 for (i = 0; i < map->n; ++i) {
5415 map->p[i] = isl_basic_map_range_map(map->p[i]);
5416 if (!map->p[i])
5417 goto error;
5419 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5420 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5421 return map;
5422 error:
5423 isl_map_free(map);
5424 return NULL;
5427 /* Given a wrapped map of the form A[B -> C],
5428 * return the map A[B -> C] -> B.
5430 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5432 isl_id *id;
5433 isl_map *map;
5435 if (!set)
5436 return NULL;
5437 if (!isl_set_has_tuple_id(set))
5438 return isl_map_domain_map(isl_set_unwrap(set));
5440 id = isl_set_get_tuple_id(set);
5441 map = isl_map_domain_map(isl_set_unwrap(set));
5442 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5444 return map;
5447 __isl_give isl_basic_map *isl_basic_map_from_domain(
5448 __isl_take isl_basic_set *bset)
5450 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5453 __isl_give isl_basic_map *isl_basic_map_from_range(
5454 __isl_take isl_basic_set *bset)
5456 isl_space *space;
5457 space = isl_basic_set_get_space(bset);
5458 space = isl_space_from_range(space);
5459 bset = isl_basic_set_reset_space(bset, space);
5460 return bset_to_bmap(bset);
5463 /* Create a relation with the given set as range.
5464 * The domain of the created relation is a zero-dimensional
5465 * flat anonymous space.
5467 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5469 isl_space *space;
5470 space = isl_set_get_space(set);
5471 space = isl_space_from_range(space);
5472 set = isl_set_reset_space(set, space);
5473 return set_to_map(set);
5476 /* Create a relation with the given set as domain.
5477 * The range of the created relation is a zero-dimensional
5478 * flat anonymous space.
5480 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5482 return isl_map_reverse(isl_map_from_range(set));
5485 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5486 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5488 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5491 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5492 __isl_take isl_set *range)
5494 return isl_map_apply_range(isl_map_reverse(domain), range);
5497 /* Return a newly allocated isl_map with given space and flags and
5498 * room for "n" basic maps.
5499 * Make sure that all cached information is cleared.
5501 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5502 unsigned flags)
5504 struct isl_map *map;
5506 if (!space)
5507 return NULL;
5508 if (n < 0)
5509 isl_die(space->ctx, isl_error_internal,
5510 "negative number of basic maps", goto error);
5511 map = isl_calloc(space->ctx, struct isl_map,
5512 sizeof(struct isl_map) +
5513 (n - 1) * sizeof(struct isl_basic_map *));
5514 if (!map)
5515 goto error;
5517 map->ctx = space->ctx;
5518 isl_ctx_ref(map->ctx);
5519 map->ref = 1;
5520 map->size = n;
5521 map->n = 0;
5522 map->dim = space;
5523 map->flags = flags;
5524 return map;
5525 error:
5526 isl_space_free(space);
5527 return NULL;
5530 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5532 struct isl_basic_map *bmap;
5533 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5534 bmap = isl_basic_map_set_to_empty(bmap);
5535 return bmap;
5538 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5540 struct isl_basic_set *bset;
5541 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5542 bset = isl_basic_set_set_to_empty(bset);
5543 return bset;
5546 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5548 struct isl_basic_map *bmap;
5549 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5550 bmap = isl_basic_map_finalize(bmap);
5551 return bmap;
5554 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5556 struct isl_basic_set *bset;
5557 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5558 bset = isl_basic_set_finalize(bset);
5559 return bset;
5562 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5564 int i;
5565 unsigned total = isl_space_dim(dim, isl_dim_all);
5566 isl_basic_map *bmap;
5568 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5569 for (i = 0; i < total; ++i) {
5570 int k = isl_basic_map_alloc_inequality(bmap);
5571 if (k < 0)
5572 goto error;
5573 isl_seq_clr(bmap->ineq[k], 1 + total);
5574 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5576 return bmap;
5577 error:
5578 isl_basic_map_free(bmap);
5579 return NULL;
5582 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5584 return isl_basic_map_nat_universe(dim);
5587 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5589 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5592 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5594 return isl_map_nat_universe(dim);
5597 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5599 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5602 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5604 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5607 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5609 struct isl_map *map;
5610 if (!dim)
5611 return NULL;
5612 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5613 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5614 return map;
5617 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5619 struct isl_set *set;
5620 if (!dim)
5621 return NULL;
5622 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5623 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5624 return set;
5627 struct isl_map *isl_map_dup(struct isl_map *map)
5629 int i;
5630 struct isl_map *dup;
5632 if (!map)
5633 return NULL;
5634 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5635 for (i = 0; i < map->n; ++i)
5636 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5637 return dup;
5640 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5641 __isl_take isl_basic_map *bmap)
5643 if (!bmap || !map)
5644 goto error;
5645 if (isl_basic_map_plain_is_empty(bmap)) {
5646 isl_basic_map_free(bmap);
5647 return map;
5649 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5650 isl_assert(map->ctx, map->n < map->size, goto error);
5651 map->p[map->n] = bmap;
5652 map->n++;
5653 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5654 return map;
5655 error:
5656 if (map)
5657 isl_map_free(map);
5658 if (bmap)
5659 isl_basic_map_free(bmap);
5660 return NULL;
5663 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5665 int i;
5667 if (!map)
5668 return NULL;
5670 if (--map->ref > 0)
5671 return NULL;
5673 clear_caches(map);
5674 isl_ctx_deref(map->ctx);
5675 for (i = 0; i < map->n; ++i)
5676 isl_basic_map_free(map->p[i]);
5677 isl_space_free(map->dim);
5678 free(map);
5680 return NULL;
5683 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5684 struct isl_basic_map *bmap, unsigned pos, int value)
5686 int j;
5688 bmap = isl_basic_map_cow(bmap);
5689 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5690 j = isl_basic_map_alloc_equality(bmap);
5691 if (j < 0)
5692 goto error;
5693 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5694 isl_int_set_si(bmap->eq[j][pos], -1);
5695 isl_int_set_si(bmap->eq[j][0], value);
5696 bmap = isl_basic_map_simplify(bmap);
5697 return isl_basic_map_finalize(bmap);
5698 error:
5699 isl_basic_map_free(bmap);
5700 return NULL;
5703 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5704 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5706 int j;
5708 bmap = isl_basic_map_cow(bmap);
5709 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5710 j = isl_basic_map_alloc_equality(bmap);
5711 if (j < 0)
5712 goto error;
5713 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5714 isl_int_set_si(bmap->eq[j][pos], -1);
5715 isl_int_set(bmap->eq[j][0], value);
5716 bmap = isl_basic_map_simplify(bmap);
5717 return isl_basic_map_finalize(bmap);
5718 error:
5719 isl_basic_map_free(bmap);
5720 return NULL;
5723 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5724 enum isl_dim_type type, unsigned pos, int value)
5726 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5727 return isl_basic_map_free(bmap);
5728 return isl_basic_map_fix_pos_si(bmap,
5729 isl_basic_map_offset(bmap, type) + pos, value);
5732 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5733 enum isl_dim_type type, unsigned pos, isl_int value)
5735 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5736 return isl_basic_map_free(bmap);
5737 return isl_basic_map_fix_pos(bmap,
5738 isl_basic_map_offset(bmap, type) + pos, value);
5741 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5742 * to be equal to "v".
5744 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5745 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5747 if (!bmap || !v)
5748 goto error;
5749 if (!isl_val_is_int(v))
5750 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5751 "expecting integer value", goto error);
5752 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5753 goto error;
5754 pos += isl_basic_map_offset(bmap, type);
5755 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5756 isl_val_free(v);
5757 return bmap;
5758 error:
5759 isl_basic_map_free(bmap);
5760 isl_val_free(v);
5761 return NULL;
5764 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5765 * to be equal to "v".
5767 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5768 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5770 return isl_basic_map_fix_val(bset, type, pos, v);
5773 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5774 enum isl_dim_type type, unsigned pos, int value)
5776 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5777 type, pos, value));
5780 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5781 enum isl_dim_type type, unsigned pos, isl_int value)
5783 return bset_from_bmap(isl_basic_map_fix(bset_to_bmap(bset),
5784 type, pos, value));
5787 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5788 unsigned input, int value)
5790 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5793 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5794 unsigned dim, int value)
5796 return bset_from_bmap(isl_basic_map_fix_si(bset_to_bmap(bset),
5797 isl_dim_set, dim, value));
5800 static int remove_if_empty(__isl_keep isl_map *map, int i)
5802 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5804 if (empty < 0)
5805 return -1;
5806 if (!empty)
5807 return 0;
5809 isl_basic_map_free(map->p[i]);
5810 if (i != map->n - 1) {
5811 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5812 map->p[i] = map->p[map->n - 1];
5814 map->n--;
5816 return 0;
5819 /* Perform "fn" on each basic map of "map", where we may not be holding
5820 * the only reference to "map".
5821 * In particular, "fn" should be a semantics preserving operation
5822 * that we want to apply to all copies of "map". We therefore need
5823 * to be careful not to modify "map" in a way that breaks "map"
5824 * in case anything goes wrong.
5826 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5827 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5829 struct isl_basic_map *bmap;
5830 int i;
5832 if (!map)
5833 return NULL;
5835 for (i = map->n - 1; i >= 0; --i) {
5836 bmap = isl_basic_map_copy(map->p[i]);
5837 bmap = fn(bmap);
5838 if (!bmap)
5839 goto error;
5840 isl_basic_map_free(map->p[i]);
5841 map->p[i] = bmap;
5842 if (remove_if_empty(map, i) < 0)
5843 goto error;
5846 return map;
5847 error:
5848 isl_map_free(map);
5849 return NULL;
5852 struct isl_map *isl_map_fix_si(struct isl_map *map,
5853 enum isl_dim_type type, unsigned pos, int value)
5855 int i;
5857 map = isl_map_cow(map);
5858 if (!map)
5859 return NULL;
5861 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5862 for (i = map->n - 1; i >= 0; --i) {
5863 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5864 if (remove_if_empty(map, i) < 0)
5865 goto error;
5867 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5868 return map;
5869 error:
5870 isl_map_free(map);
5871 return NULL;
5874 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5875 enum isl_dim_type type, unsigned pos, int value)
5877 return set_from_map(isl_map_fix_si(set_to_map(set), type, pos, value));
5880 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5881 enum isl_dim_type type, unsigned pos, isl_int value)
5883 int i;
5885 map = isl_map_cow(map);
5886 if (!map)
5887 return NULL;
5889 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5890 for (i = 0; i < map->n; ++i) {
5891 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5892 if (!map->p[i])
5893 goto error;
5895 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5896 return map;
5897 error:
5898 isl_map_free(map);
5899 return NULL;
5902 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5903 enum isl_dim_type type, unsigned pos, isl_int value)
5905 return set_from_map(isl_map_fix(set_to_map(set), type, pos, value));
5908 /* Fix the value of the variable at position "pos" of type "type" of "map"
5909 * to be equal to "v".
5911 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5912 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5914 int i;
5916 map = isl_map_cow(map);
5917 if (!map || !v)
5918 goto error;
5920 if (!isl_val_is_int(v))
5921 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5922 "expecting integer value", goto error);
5923 if (pos >= isl_map_dim(map, type))
5924 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5925 "index out of bounds", goto error);
5926 for (i = map->n - 1; i >= 0; --i) {
5927 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5928 isl_val_copy(v));
5929 if (remove_if_empty(map, i) < 0)
5930 goto error;
5932 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5933 isl_val_free(v);
5934 return map;
5935 error:
5936 isl_map_free(map);
5937 isl_val_free(v);
5938 return NULL;
5941 /* Fix the value of the variable at position "pos" of type "type" of "set"
5942 * to be equal to "v".
5944 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5945 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5947 return isl_map_fix_val(set, type, pos, v);
5950 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5951 unsigned input, int value)
5953 return isl_map_fix_si(map, isl_dim_in, input, value);
5956 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5958 return set_from_map(isl_map_fix_si(set_to_map(set),
5959 isl_dim_set, dim, value));
5962 static __isl_give isl_basic_map *basic_map_bound_si(
5963 __isl_take isl_basic_map *bmap,
5964 enum isl_dim_type type, unsigned pos, int value, int upper)
5966 int j;
5968 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
5969 return isl_basic_map_free(bmap);
5970 pos += isl_basic_map_offset(bmap, type);
5971 bmap = isl_basic_map_cow(bmap);
5972 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5973 j = isl_basic_map_alloc_inequality(bmap);
5974 if (j < 0)
5975 goto error;
5976 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5977 if (upper) {
5978 isl_int_set_si(bmap->ineq[j][pos], -1);
5979 isl_int_set_si(bmap->ineq[j][0], value);
5980 } else {
5981 isl_int_set_si(bmap->ineq[j][pos], 1);
5982 isl_int_set_si(bmap->ineq[j][0], -value);
5984 bmap = isl_basic_map_simplify(bmap);
5985 return isl_basic_map_finalize(bmap);
5986 error:
5987 isl_basic_map_free(bmap);
5988 return NULL;
5991 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5992 __isl_take isl_basic_map *bmap,
5993 enum isl_dim_type type, unsigned pos, int value)
5995 return basic_map_bound_si(bmap, type, pos, value, 0);
5998 /* Constrain the values of the given dimension to be no greater than "value".
6000 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
6001 __isl_take isl_basic_map *bmap,
6002 enum isl_dim_type type, unsigned pos, int value)
6004 return basic_map_bound_si(bmap, type, pos, value, 1);
6007 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
6008 enum isl_dim_type type, unsigned pos, int value, int upper)
6010 int i;
6012 map = isl_map_cow(map);
6013 if (!map)
6014 return NULL;
6016 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
6017 for (i = 0; i < map->n; ++i) {
6018 map->p[i] = basic_map_bound_si(map->p[i],
6019 type, pos, value, upper);
6020 if (!map->p[i])
6021 goto error;
6023 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6024 return map;
6025 error:
6026 isl_map_free(map);
6027 return NULL;
6030 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
6031 enum isl_dim_type type, unsigned pos, int value)
6033 return map_bound_si(map, type, pos, value, 0);
6036 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
6037 enum isl_dim_type type, unsigned pos, int value)
6039 return map_bound_si(map, type, pos, value, 1);
6042 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
6043 enum isl_dim_type type, unsigned pos, int value)
6045 return set_from_map(isl_map_lower_bound_si(set_to_map(set),
6046 type, pos, value));
6049 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
6050 enum isl_dim_type type, unsigned pos, int value)
6052 return isl_map_upper_bound_si(set, type, pos, value);
6055 /* Bound the given variable of "bmap" from below (or above is "upper"
6056 * is set) to "value".
6058 static __isl_give isl_basic_map *basic_map_bound(
6059 __isl_take isl_basic_map *bmap,
6060 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6062 int j;
6064 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
6065 return isl_basic_map_free(bmap);
6066 pos += isl_basic_map_offset(bmap, type);
6067 bmap = isl_basic_map_cow(bmap);
6068 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
6069 j = isl_basic_map_alloc_inequality(bmap);
6070 if (j < 0)
6071 goto error;
6072 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
6073 if (upper) {
6074 isl_int_set_si(bmap->ineq[j][pos], -1);
6075 isl_int_set(bmap->ineq[j][0], value);
6076 } else {
6077 isl_int_set_si(bmap->ineq[j][pos], 1);
6078 isl_int_neg(bmap->ineq[j][0], value);
6080 bmap = isl_basic_map_simplify(bmap);
6081 return isl_basic_map_finalize(bmap);
6082 error:
6083 isl_basic_map_free(bmap);
6084 return NULL;
6087 /* Bound the given variable of "map" from below (or above is "upper"
6088 * is set) to "value".
6090 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
6091 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
6093 int i;
6095 map = isl_map_cow(map);
6096 if (!map)
6097 return NULL;
6099 if (pos >= isl_map_dim(map, type))
6100 isl_die(map->ctx, isl_error_invalid,
6101 "index out of bounds", goto error);
6102 for (i = map->n - 1; i >= 0; --i) {
6103 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
6104 if (remove_if_empty(map, i) < 0)
6105 goto error;
6107 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6108 return map;
6109 error:
6110 isl_map_free(map);
6111 return NULL;
6114 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
6115 enum isl_dim_type type, unsigned pos, isl_int value)
6117 return map_bound(map, type, pos, value, 0);
6120 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6121 enum isl_dim_type type, unsigned pos, isl_int value)
6123 return map_bound(map, type, pos, value, 1);
6126 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6127 enum isl_dim_type type, unsigned pos, isl_int value)
6129 return isl_map_lower_bound(set, type, pos, value);
6132 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6133 enum isl_dim_type type, unsigned pos, isl_int value)
6135 return isl_map_upper_bound(set, type, pos, value);
6138 /* Force the values of the variable at position "pos" of type "type" of "set"
6139 * to be no smaller than "value".
6141 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6142 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6144 if (!value)
6145 goto error;
6146 if (!isl_val_is_int(value))
6147 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6148 "expecting integer value", goto error);
6149 set = isl_set_lower_bound(set, type, pos, value->n);
6150 isl_val_free(value);
6151 return set;
6152 error:
6153 isl_val_free(value);
6154 isl_set_free(set);
6155 return NULL;
6158 /* Force the values of the variable at position "pos" of type "type" of "set"
6159 * to be no greater than "value".
6161 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6162 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6164 if (!value)
6165 goto error;
6166 if (!isl_val_is_int(value))
6167 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6168 "expecting integer value", goto error);
6169 set = isl_set_upper_bound(set, type, pos, value->n);
6170 isl_val_free(value);
6171 return set;
6172 error:
6173 isl_val_free(value);
6174 isl_set_free(set);
6175 return NULL;
6178 struct isl_map *isl_map_reverse(struct isl_map *map)
6180 int i;
6182 map = isl_map_cow(map);
6183 if (!map)
6184 return NULL;
6186 map->dim = isl_space_reverse(map->dim);
6187 if (!map->dim)
6188 goto error;
6189 for (i = 0; i < map->n; ++i) {
6190 map->p[i] = isl_basic_map_reverse(map->p[i]);
6191 if (!map->p[i])
6192 goto error;
6194 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6195 return map;
6196 error:
6197 isl_map_free(map);
6198 return NULL;
6201 #undef TYPE
6202 #define TYPE isl_pw_multi_aff
6203 #undef SUFFIX
6204 #define SUFFIX _pw_multi_aff
6205 #undef EMPTY
6206 #define EMPTY isl_pw_multi_aff_empty
6207 #undef ADD
6208 #define ADD isl_pw_multi_aff_union_add
6209 #include "isl_map_lexopt_templ.c"
6211 /* Given a map "map", compute the lexicographically minimal
6212 * (or maximal) image element for each domain element in dom,
6213 * in the form of an isl_pw_multi_aff.
6214 * If "empty" is not NULL, then set *empty to those elements in dom that
6215 * do not have an image element.
6216 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6217 * should be computed over the domain of "map". "empty" is also NULL
6218 * in this case.
6220 * We first compute the lexicographically minimal or maximal element
6221 * in the first basic map. This results in a partial solution "res"
6222 * and a subset "todo" of dom that still need to be handled.
6223 * We then consider each of the remaining maps in "map" and successively
6224 * update both "res" and "todo".
6225 * If "empty" is NULL, then the todo sets are not needed and therefore
6226 * also not computed.
6228 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6229 __isl_take isl_map *map, __isl_take isl_set *dom,
6230 __isl_give isl_set **empty, unsigned flags)
6232 int i;
6233 int full;
6234 isl_pw_multi_aff *res;
6235 isl_set *todo;
6237 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6238 if (!map || (!full && !dom))
6239 goto error;
6241 if (isl_map_plain_is_empty(map)) {
6242 if (empty)
6243 *empty = dom;
6244 else
6245 isl_set_free(dom);
6246 return isl_pw_multi_aff_from_map(map);
6249 res = basic_map_partial_lexopt_pw_multi_aff(
6250 isl_basic_map_copy(map->p[0]),
6251 isl_set_copy(dom), empty, flags);
6253 if (empty)
6254 todo = *empty;
6255 for (i = 1; i < map->n; ++i) {
6256 isl_pw_multi_aff *res_i;
6258 res_i = basic_map_partial_lexopt_pw_multi_aff(
6259 isl_basic_map_copy(map->p[i]),
6260 isl_set_copy(dom), empty, flags);
6262 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6263 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6264 else
6265 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6267 if (empty)
6268 todo = isl_set_intersect(todo, *empty);
6271 isl_set_free(dom);
6272 isl_map_free(map);
6274 if (empty)
6275 *empty = todo;
6277 return res;
6278 error:
6279 if (empty)
6280 *empty = NULL;
6281 isl_set_free(dom);
6282 isl_map_free(map);
6283 return NULL;
6286 #undef TYPE
6287 #define TYPE isl_map
6288 #undef SUFFIX
6289 #define SUFFIX
6290 #undef EMPTY
6291 #define EMPTY isl_map_empty
6292 #undef ADD
6293 #define ADD isl_map_union_disjoint
6294 #include "isl_map_lexopt_templ.c"
6296 /* Given a map "map", compute the lexicographically minimal
6297 * (or maximal) image element for each domain element in "dom",
6298 * in the form of an isl_map.
6299 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6300 * do not have an image element.
6301 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6302 * should be computed over the domain of "map". "empty" is also NULL
6303 * in this case.
6305 * If the input consists of more than one disjunct, then first
6306 * compute the desired result in the form of an isl_pw_multi_aff and
6307 * then convert that into an isl_map.
6309 * This function used to have an explicit implementation in terms
6310 * of isl_maps, but it would continually intersect the domains of
6311 * partial results with the complement of the domain of the next
6312 * partial solution, potentially leading to an explosion in the number
6313 * of disjuncts if there are several disjuncts in the input.
6314 * An even earlier implementation of this function would look for
6315 * better results in the domain of the partial result and for extra
6316 * results in the complement of this domain, which would lead to
6317 * even more splintering.
6319 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6320 __isl_take isl_map *map, __isl_take isl_set *dom,
6321 __isl_give isl_set **empty, unsigned flags)
6323 int full;
6324 struct isl_map *res;
6325 isl_pw_multi_aff *pma;
6327 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6328 if (!map || (!full && !dom))
6329 goto error;
6331 if (isl_map_plain_is_empty(map)) {
6332 if (empty)
6333 *empty = dom;
6334 else
6335 isl_set_free(dom);
6336 return map;
6339 if (map->n == 1) {
6340 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6341 dom, empty, flags);
6342 isl_map_free(map);
6343 return res;
6346 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6347 flags);
6348 return isl_map_from_pw_multi_aff(pma);
6349 error:
6350 if (empty)
6351 *empty = NULL;
6352 isl_set_free(dom);
6353 isl_map_free(map);
6354 return NULL;
6357 __isl_give isl_map *isl_map_partial_lexmax(
6358 __isl_take isl_map *map, __isl_take isl_set *dom,
6359 __isl_give isl_set **empty)
6361 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6364 __isl_give isl_map *isl_map_partial_lexmin(
6365 __isl_take isl_map *map, __isl_take isl_set *dom,
6366 __isl_give isl_set **empty)
6368 return isl_map_partial_lexopt(map, dom, empty, 0);
6371 __isl_give isl_set *isl_set_partial_lexmin(
6372 __isl_take isl_set *set, __isl_take isl_set *dom,
6373 __isl_give isl_set **empty)
6375 return set_from_map(isl_map_partial_lexmin(set_to_map(set),
6376 dom, empty));
6379 __isl_give isl_set *isl_set_partial_lexmax(
6380 __isl_take isl_set *set, __isl_take isl_set *dom,
6381 __isl_give isl_set **empty)
6383 return set_from_map(isl_map_partial_lexmax(set_to_map(set),
6384 dom, empty));
6387 /* Compute the lexicographic minimum (or maximum if "flags" includes
6388 * ISL_OPT_MAX) of "bset" over its parametric domain.
6390 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6391 unsigned flags)
6393 return isl_basic_map_lexopt(bset, flags);
6396 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6398 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6401 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6403 return set_from_map(isl_basic_map_lexmin(bset_to_bmap(bset)));
6406 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6408 return set_from_map(isl_basic_map_lexmax(bset_to_bmap(bset)));
6411 /* Compute the lexicographic minimum of "bset" over its parametric domain
6412 * for the purpose of quantifier elimination.
6413 * That is, find an explicit representation for all the existentially
6414 * quantified variables in "bset" by computing their lexicographic
6415 * minimum.
6417 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6418 __isl_take isl_basic_set *bset)
6420 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6423 /* Extract the first and only affine expression from list
6424 * and then add it to *pwaff with the given dom.
6425 * This domain is known to be disjoint from other domains
6426 * because of the way isl_basic_map_foreach_lexmax works.
6428 static isl_stat update_dim_opt(__isl_take isl_basic_set *dom,
6429 __isl_take isl_aff_list *list, void *user)
6431 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6432 isl_aff *aff;
6433 isl_pw_aff **pwaff = user;
6434 isl_pw_aff *pwaff_i;
6436 if (!list)
6437 goto error;
6438 if (isl_aff_list_n_aff(list) != 1)
6439 isl_die(ctx, isl_error_internal,
6440 "expecting single element list", goto error);
6442 aff = isl_aff_list_get_aff(list, 0);
6443 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6445 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6447 isl_aff_list_free(list);
6449 return isl_stat_ok;
6450 error:
6451 isl_basic_set_free(dom);
6452 isl_aff_list_free(list);
6453 return isl_stat_error;
6456 /* Given a basic map with one output dimension, compute the minimum or
6457 * maximum of that dimension as an isl_pw_aff.
6459 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6460 * call update_dim_opt on each leaf of the result.
6462 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6463 int max)
6465 isl_space *dim = isl_basic_map_get_space(bmap);
6466 isl_pw_aff *pwaff;
6467 isl_stat r;
6469 dim = isl_space_from_domain(isl_space_domain(dim));
6470 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6471 pwaff = isl_pw_aff_empty(dim);
6473 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6474 if (r < 0)
6475 return isl_pw_aff_free(pwaff);
6477 return pwaff;
6480 /* Compute the minimum or maximum of the given output dimension
6481 * as a function of the parameters and the input dimensions,
6482 * but independently of the other output dimensions.
6484 * We first project out the other output dimension and then compute
6485 * the "lexicographic" maximum in each basic map, combining the results
6486 * using isl_pw_aff_union_max.
6488 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6489 int max)
6491 int i;
6492 isl_pw_aff *pwaff;
6493 unsigned n_out;
6495 n_out = isl_map_dim(map, isl_dim_out);
6496 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6497 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6498 if (!map)
6499 return NULL;
6501 if (map->n == 0) {
6502 isl_space *dim = isl_map_get_space(map);
6503 isl_map_free(map);
6504 return isl_pw_aff_empty(dim);
6507 pwaff = basic_map_dim_opt(map->p[0], max);
6508 for (i = 1; i < map->n; ++i) {
6509 isl_pw_aff *pwaff_i;
6511 pwaff_i = basic_map_dim_opt(map->p[i], max);
6512 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6515 isl_map_free(map);
6517 return pwaff;
6520 /* Compute the minimum of the given output dimension as a function of the
6521 * parameters and input dimensions, but independently of
6522 * the other output dimensions.
6524 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6526 return map_dim_opt(map, pos, 0);
6529 /* Compute the maximum of the given output dimension as a function of the
6530 * parameters and input dimensions, but independently of
6531 * the other output dimensions.
6533 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6535 return map_dim_opt(map, pos, 1);
6538 /* Compute the minimum or maximum of the given set dimension
6539 * as a function of the parameters,
6540 * but independently of the other set dimensions.
6542 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6543 int max)
6545 return map_dim_opt(set, pos, max);
6548 /* Compute the maximum of the given set dimension as a function of the
6549 * parameters, but independently of the other set dimensions.
6551 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6553 return set_dim_opt(set, pos, 1);
6556 /* Compute the minimum of the given set dimension as a function of the
6557 * parameters, but independently of the other set dimensions.
6559 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6561 return set_dim_opt(set, pos, 0);
6564 /* Apply a preimage specified by "mat" on the parameters of "bset".
6565 * bset is assumed to have only parameters and divs.
6567 static struct isl_basic_set *basic_set_parameter_preimage(
6568 struct isl_basic_set *bset, struct isl_mat *mat)
6570 unsigned nparam;
6572 if (!bset || !mat)
6573 goto error;
6575 bset->dim = isl_space_cow(bset->dim);
6576 if (!bset->dim)
6577 goto error;
6579 nparam = isl_basic_set_dim(bset, isl_dim_param);
6581 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6583 bset->dim->nparam = 0;
6584 bset->dim->n_out = nparam;
6585 bset = isl_basic_set_preimage(bset, mat);
6586 if (bset) {
6587 bset->dim->nparam = bset->dim->n_out;
6588 bset->dim->n_out = 0;
6590 return bset;
6591 error:
6592 isl_mat_free(mat);
6593 isl_basic_set_free(bset);
6594 return NULL;
6597 /* Apply a preimage specified by "mat" on the parameters of "set".
6598 * set is assumed to have only parameters and divs.
6600 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6601 __isl_take isl_mat *mat)
6603 isl_space *space;
6604 unsigned nparam;
6606 if (!set || !mat)
6607 goto error;
6609 nparam = isl_set_dim(set, isl_dim_param);
6611 if (mat->n_row != 1 + nparam)
6612 isl_die(isl_set_get_ctx(set), isl_error_internal,
6613 "unexpected number of rows", goto error);
6615 space = isl_set_get_space(set);
6616 space = isl_space_move_dims(space, isl_dim_set, 0,
6617 isl_dim_param, 0, nparam);
6618 set = isl_set_reset_space(set, space);
6619 set = isl_set_preimage(set, mat);
6620 nparam = isl_set_dim(set, isl_dim_out);
6621 space = isl_set_get_space(set);
6622 space = isl_space_move_dims(space, isl_dim_param, 0,
6623 isl_dim_out, 0, nparam);
6624 set = isl_set_reset_space(set, space);
6625 return set;
6626 error:
6627 isl_mat_free(mat);
6628 isl_set_free(set);
6629 return NULL;
6632 /* Intersect the basic set "bset" with the affine space specified by the
6633 * equalities in "eq".
6635 static struct isl_basic_set *basic_set_append_equalities(
6636 struct isl_basic_set *bset, struct isl_mat *eq)
6638 int i, k;
6639 unsigned len;
6641 if (!bset || !eq)
6642 goto error;
6644 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6645 eq->n_row, 0);
6646 if (!bset)
6647 goto error;
6649 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6650 for (i = 0; i < eq->n_row; ++i) {
6651 k = isl_basic_set_alloc_equality(bset);
6652 if (k < 0)
6653 goto error;
6654 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6655 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6657 isl_mat_free(eq);
6659 bset = isl_basic_set_gauss(bset, NULL);
6660 bset = isl_basic_set_finalize(bset);
6662 return bset;
6663 error:
6664 isl_mat_free(eq);
6665 isl_basic_set_free(bset);
6666 return NULL;
6669 /* Intersect the set "set" with the affine space specified by the
6670 * equalities in "eq".
6672 static struct isl_set *set_append_equalities(struct isl_set *set,
6673 struct isl_mat *eq)
6675 int i;
6677 if (!set || !eq)
6678 goto error;
6680 for (i = 0; i < set->n; ++i) {
6681 set->p[i] = basic_set_append_equalities(set->p[i],
6682 isl_mat_copy(eq));
6683 if (!set->p[i])
6684 goto error;
6686 isl_mat_free(eq);
6687 return set;
6688 error:
6689 isl_mat_free(eq);
6690 isl_set_free(set);
6691 return NULL;
6694 /* Given a basic set "bset" that only involves parameters and existentially
6695 * quantified variables, return the index of the first equality
6696 * that only involves parameters. If there is no such equality then
6697 * return bset->n_eq.
6699 * This function assumes that isl_basic_set_gauss has been called on "bset".
6701 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6703 int i, j;
6704 unsigned nparam, n_div;
6706 if (!bset)
6707 return -1;
6709 nparam = isl_basic_set_dim(bset, isl_dim_param);
6710 n_div = isl_basic_set_dim(bset, isl_dim_div);
6712 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6713 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6714 ++i;
6717 return i;
6720 /* Compute an explicit representation for the existentially quantified
6721 * variables in "bset" by computing the "minimal value" of the set
6722 * variables. Since there are no set variables, the computation of
6723 * the minimal value essentially computes an explicit representation
6724 * of the non-empty part(s) of "bset".
6726 * The input only involves parameters and existentially quantified variables.
6727 * All equalities among parameters have been removed.
6729 * Since the existentially quantified variables in the result are in general
6730 * going to be different from those in the input, we first replace
6731 * them by the minimal number of variables based on their equalities.
6732 * This should simplify the parametric integer programming.
6734 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6736 isl_morph *morph1, *morph2;
6737 isl_set *set;
6738 unsigned n;
6740 if (!bset)
6741 return NULL;
6742 if (bset->n_eq == 0)
6743 return isl_basic_set_lexmin_compute_divs(bset);
6745 morph1 = isl_basic_set_parameter_compression(bset);
6746 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6747 bset = isl_basic_set_lift(bset);
6748 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6749 bset = isl_morph_basic_set(morph2, bset);
6750 n = isl_basic_set_dim(bset, isl_dim_set);
6751 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6753 set = isl_basic_set_lexmin_compute_divs(bset);
6755 set = isl_morph_set(isl_morph_inverse(morph1), set);
6757 return set;
6760 /* Project the given basic set onto its parameter domain, possibly introducing
6761 * new, explicit, existential variables in the constraints.
6762 * The input has parameters and (possibly implicit) existential variables.
6763 * The output has the same parameters, but only
6764 * explicit existentially quantified variables.
6766 * The actual projection is performed by pip, but pip doesn't seem
6767 * to like equalities very much, so we first remove the equalities
6768 * among the parameters by performing a variable compression on
6769 * the parameters. Afterward, an inverse transformation is performed
6770 * and the equalities among the parameters are inserted back in.
6772 * The variable compression on the parameters may uncover additional
6773 * equalities that were only implicit before. We therefore check
6774 * if there are any new parameter equalities in the result and
6775 * if so recurse. The removal of parameter equalities is required
6776 * for the parameter compression performed by base_compute_divs.
6778 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6780 int i;
6781 struct isl_mat *eq;
6782 struct isl_mat *T, *T2;
6783 struct isl_set *set;
6784 unsigned nparam;
6786 bset = isl_basic_set_cow(bset);
6787 if (!bset)
6788 return NULL;
6790 if (bset->n_eq == 0)
6791 return base_compute_divs(bset);
6793 bset = isl_basic_set_gauss(bset, NULL);
6794 if (!bset)
6795 return NULL;
6796 if (isl_basic_set_plain_is_empty(bset))
6797 return isl_set_from_basic_set(bset);
6799 i = first_parameter_equality(bset);
6800 if (i == bset->n_eq)
6801 return base_compute_divs(bset);
6803 nparam = isl_basic_set_dim(bset, isl_dim_param);
6804 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6805 0, 1 + nparam);
6806 eq = isl_mat_cow(eq);
6807 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6808 if (T && T->n_col == 0) {
6809 isl_mat_free(T);
6810 isl_mat_free(T2);
6811 isl_mat_free(eq);
6812 bset = isl_basic_set_set_to_empty(bset);
6813 return isl_set_from_basic_set(bset);
6815 bset = basic_set_parameter_preimage(bset, T);
6817 i = first_parameter_equality(bset);
6818 if (!bset)
6819 set = NULL;
6820 else if (i == bset->n_eq)
6821 set = base_compute_divs(bset);
6822 else
6823 set = parameter_compute_divs(bset);
6824 set = set_parameter_preimage(set, T2);
6825 set = set_append_equalities(set, eq);
6826 return set;
6829 /* Insert the divs from "ls" before those of "bmap".
6831 * The number of columns is not changed, which means that the last
6832 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6833 * The caller is responsible for removing the same number of dimensions
6834 * from the space of "bmap".
6836 static __isl_give isl_basic_map *insert_divs_from_local_space(
6837 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6839 int i;
6840 int n_div;
6841 int old_n_div;
6843 n_div = isl_local_space_dim(ls, isl_dim_div);
6844 if (n_div == 0)
6845 return bmap;
6847 old_n_div = bmap->n_div;
6848 bmap = insert_div_rows(bmap, n_div);
6849 if (!bmap)
6850 return NULL;
6852 for (i = 0; i < n_div; ++i) {
6853 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6854 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6857 return bmap;
6860 /* Replace the space of "bmap" by the space and divs of "ls".
6862 * If "ls" has any divs, then we simplify the result since we may
6863 * have discovered some additional equalities that could simplify
6864 * the div expressions.
6866 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6867 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6869 int n_div;
6871 bmap = isl_basic_map_cow(bmap);
6872 if (!bmap || !ls)
6873 goto error;
6875 n_div = isl_local_space_dim(ls, isl_dim_div);
6876 bmap = insert_divs_from_local_space(bmap, ls);
6877 if (!bmap)
6878 goto error;
6880 isl_space_free(bmap->dim);
6881 bmap->dim = isl_local_space_get_space(ls);
6882 if (!bmap->dim)
6883 goto error;
6885 isl_local_space_free(ls);
6886 if (n_div > 0)
6887 bmap = isl_basic_map_simplify(bmap);
6888 bmap = isl_basic_map_finalize(bmap);
6889 return bmap;
6890 error:
6891 isl_basic_map_free(bmap);
6892 isl_local_space_free(ls);
6893 return NULL;
6896 /* Replace the space of "map" by the space and divs of "ls".
6898 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6899 __isl_take isl_local_space *ls)
6901 int i;
6903 map = isl_map_cow(map);
6904 if (!map || !ls)
6905 goto error;
6907 for (i = 0; i < map->n; ++i) {
6908 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6909 isl_local_space_copy(ls));
6910 if (!map->p[i])
6911 goto error;
6913 isl_space_free(map->dim);
6914 map->dim = isl_local_space_get_space(ls);
6915 if (!map->dim)
6916 goto error;
6918 isl_local_space_free(ls);
6919 return map;
6920 error:
6921 isl_local_space_free(ls);
6922 isl_map_free(map);
6923 return NULL;
6926 /* Compute an explicit representation for the existentially
6927 * quantified variables for which do not know any explicit representation yet.
6929 * We first sort the existentially quantified variables so that the
6930 * existentially quantified variables for which we already have an explicit
6931 * representation are placed before those for which we do not.
6932 * The input dimensions, the output dimensions and the existentially
6933 * quantified variables for which we already have an explicit
6934 * representation are then turned into parameters.
6935 * compute_divs returns a map with the same parameters and
6936 * no input or output dimensions and the dimension specification
6937 * is reset to that of the input, including the existentially quantified
6938 * variables for which we already had an explicit representation.
6940 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6942 struct isl_basic_set *bset;
6943 struct isl_set *set;
6944 struct isl_map *map;
6945 isl_space *dim;
6946 isl_local_space *ls;
6947 unsigned nparam;
6948 unsigned n_in;
6949 unsigned n_out;
6950 int n_known;
6951 int i;
6953 bmap = isl_basic_map_sort_divs(bmap);
6954 bmap = isl_basic_map_cow(bmap);
6955 if (!bmap)
6956 return NULL;
6958 n_known = isl_basic_map_first_unknown_div(bmap);
6959 if (n_known < 0)
6960 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6962 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6963 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6964 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6965 dim = isl_space_set_alloc(bmap->ctx,
6966 nparam + n_in + n_out + n_known, 0);
6967 if (!dim)
6968 goto error;
6970 ls = isl_basic_map_get_local_space(bmap);
6971 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6972 n_known, bmap->n_div - n_known);
6973 if (n_known > 0) {
6974 for (i = n_known; i < bmap->n_div; ++i)
6975 swap_div(bmap, i - n_known, i);
6976 bmap->n_div -= n_known;
6977 bmap->extra -= n_known;
6979 bmap = isl_basic_map_reset_space(bmap, dim);
6980 bset = bset_from_bmap(bmap);
6982 set = parameter_compute_divs(bset);
6983 map = set_to_map(set);
6984 map = replace_space_by_local_space(map, ls);
6986 return map;
6987 error:
6988 isl_basic_map_free(bmap);
6989 return NULL;
6992 /* Remove the explicit representation of local variable "div",
6993 * if there is any.
6995 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6996 __isl_take isl_basic_map *bmap, int div)
6998 isl_bool unknown;
7000 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
7001 if (unknown < 0)
7002 return isl_basic_map_free(bmap);
7003 if (unknown)
7004 return bmap;
7006 bmap = isl_basic_map_cow(bmap);
7007 if (!bmap)
7008 return NULL;
7009 isl_int_set_si(bmap->div[div][0], 0);
7010 return bmap;
7013 /* Is local variable "div" of "bmap" marked as not having an explicit
7014 * representation?
7015 * Note that even if "div" is not marked in this way and therefore
7016 * has an explicit representation, this representation may still
7017 * depend (indirectly) on other local variables that do not
7018 * have an explicit representation.
7020 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
7021 int div)
7023 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
7024 return isl_bool_error;
7025 return isl_int_is_zero(bmap->div[div][0]);
7028 /* Return the position of the first local variable that does not
7029 * have an explicit representation.
7030 * Return the total number of local variables if they all have
7031 * an explicit representation.
7032 * Return -1 on error.
7034 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7036 int i;
7038 if (!bmap)
7039 return -1;
7041 for (i = 0; i < bmap->n_div; ++i) {
7042 if (!isl_basic_map_div_is_known(bmap, i))
7043 return i;
7045 return bmap->n_div;
7048 /* Return the position of the first local variable that does not
7049 * have an explicit representation.
7050 * Return the total number of local variables if they all have
7051 * an explicit representation.
7052 * Return -1 on error.
7054 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
7056 return isl_basic_map_first_unknown_div(bset);
7059 /* Does "bmap" have an explicit representation for all local variables?
7061 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7063 int first, n;
7065 n = isl_basic_map_dim(bmap, isl_dim_div);
7066 first = isl_basic_map_first_unknown_div(bmap);
7067 if (first < 0)
7068 return isl_bool_error;
7069 return first == n;
7072 /* Do all basic maps in "map" have an explicit representation
7073 * for all local variables?
7075 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7077 int i;
7079 if (!map)
7080 return isl_bool_error;
7082 for (i = 0; i < map->n; ++i) {
7083 int known = isl_basic_map_divs_known(map->p[i]);
7084 if (known <= 0)
7085 return known;
7088 return isl_bool_true;
7091 /* If bmap contains any unknown divs, then compute explicit
7092 * expressions for them. However, this computation may be
7093 * quite expensive, so first try to remove divs that aren't
7094 * strictly needed.
7096 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7098 int known;
7099 struct isl_map *map;
7101 known = isl_basic_map_divs_known(bmap);
7102 if (known < 0)
7103 goto error;
7104 if (known)
7105 return isl_map_from_basic_map(bmap);
7107 bmap = isl_basic_map_drop_redundant_divs(bmap);
7109 known = isl_basic_map_divs_known(bmap);
7110 if (known < 0)
7111 goto error;
7112 if (known)
7113 return isl_map_from_basic_map(bmap);
7115 map = compute_divs(bmap);
7116 return map;
7117 error:
7118 isl_basic_map_free(bmap);
7119 return NULL;
7122 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7124 int i;
7125 int known;
7126 struct isl_map *res;
7128 if (!map)
7129 return NULL;
7130 if (map->n == 0)
7131 return map;
7133 known = isl_map_divs_known(map);
7134 if (known < 0) {
7135 isl_map_free(map);
7136 return NULL;
7138 if (known)
7139 return map;
7141 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7142 for (i = 1 ; i < map->n; ++i) {
7143 struct isl_map *r2;
7144 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7145 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7146 res = isl_map_union_disjoint(res, r2);
7147 else
7148 res = isl_map_union(res, r2);
7150 isl_map_free(map);
7152 return res;
7155 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7157 return set_from_map(isl_basic_map_compute_divs(bset_to_bmap(bset)));
7160 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7162 return set_from_map(isl_map_compute_divs(set_to_map(set)));
7165 struct isl_set *isl_map_domain(struct isl_map *map)
7167 int i;
7168 struct isl_set *set;
7170 if (!map)
7171 goto error;
7173 map = isl_map_cow(map);
7174 if (!map)
7175 return NULL;
7177 set = set_from_map(map);
7178 set->dim = isl_space_domain(set->dim);
7179 if (!set->dim)
7180 goto error;
7181 for (i = 0; i < map->n; ++i) {
7182 set->p[i] = isl_basic_map_domain(map->p[i]);
7183 if (!set->p[i])
7184 goto error;
7186 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7187 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7188 return set;
7189 error:
7190 isl_map_free(map);
7191 return NULL;
7194 /* Return the union of "map1" and "map2", where we assume for now that
7195 * "map1" and "map2" are disjoint. Note that the basic maps inside
7196 * "map1" or "map2" may not be disjoint from each other.
7197 * Also note that this function is also called from isl_map_union,
7198 * which takes care of handling the situation where "map1" and "map2"
7199 * may not be disjoint.
7201 * If one of the inputs is empty, we can simply return the other input.
7202 * Similarly, if one of the inputs is universal, then it is equal to the union.
7204 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7205 __isl_take isl_map *map2)
7207 int i;
7208 unsigned flags = 0;
7209 struct isl_map *map = NULL;
7210 int is_universe;
7212 if (!map1 || !map2)
7213 goto error;
7215 if (!isl_space_is_equal(map1->dim, map2->dim))
7216 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7217 "spaces don't match", goto error);
7219 if (map1->n == 0) {
7220 isl_map_free(map1);
7221 return map2;
7223 if (map2->n == 0) {
7224 isl_map_free(map2);
7225 return map1;
7228 is_universe = isl_map_plain_is_universe(map1);
7229 if (is_universe < 0)
7230 goto error;
7231 if (is_universe) {
7232 isl_map_free(map2);
7233 return map1;
7236 is_universe = isl_map_plain_is_universe(map2);
7237 if (is_universe < 0)
7238 goto error;
7239 if (is_universe) {
7240 isl_map_free(map1);
7241 return map2;
7244 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7245 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7246 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7248 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7249 map1->n + map2->n, flags);
7250 if (!map)
7251 goto error;
7252 for (i = 0; i < map1->n; ++i) {
7253 map = isl_map_add_basic_map(map,
7254 isl_basic_map_copy(map1->p[i]));
7255 if (!map)
7256 goto error;
7258 for (i = 0; i < map2->n; ++i) {
7259 map = isl_map_add_basic_map(map,
7260 isl_basic_map_copy(map2->p[i]));
7261 if (!map)
7262 goto error;
7264 isl_map_free(map1);
7265 isl_map_free(map2);
7266 return map;
7267 error:
7268 isl_map_free(map);
7269 isl_map_free(map1);
7270 isl_map_free(map2);
7271 return NULL;
7274 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7275 * guaranteed to be disjoint by the caller.
7277 * Note that this functions is called from within isl_map_make_disjoint,
7278 * so we have to be careful not to touch the constraints of the inputs
7279 * in any way.
7281 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7282 __isl_take isl_map *map2)
7284 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7287 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7288 * not be disjoint. The parameters are assumed to have been aligned.
7290 * We currently simply call map_union_disjoint, the internal operation
7291 * of which does not really depend on the inputs being disjoint.
7292 * If the result contains more than one basic map, then we clear
7293 * the disjoint flag since the result may contain basic maps from
7294 * both inputs and these are not guaranteed to be disjoint.
7296 * As a special case, if "map1" and "map2" are obviously equal,
7297 * then we simply return "map1".
7299 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7300 __isl_take isl_map *map2)
7302 int equal;
7304 if (!map1 || !map2)
7305 goto error;
7307 equal = isl_map_plain_is_equal(map1, map2);
7308 if (equal < 0)
7309 goto error;
7310 if (equal) {
7311 isl_map_free(map2);
7312 return map1;
7315 map1 = map_union_disjoint(map1, map2);
7316 if (!map1)
7317 return NULL;
7318 if (map1->n > 1)
7319 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7320 return map1;
7321 error:
7322 isl_map_free(map1);
7323 isl_map_free(map2);
7324 return NULL;
7327 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7328 * not be disjoint.
7330 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7331 __isl_take isl_map *map2)
7333 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7336 struct isl_set *isl_set_union_disjoint(
7337 struct isl_set *set1, struct isl_set *set2)
7339 return set_from_map(isl_map_union_disjoint(set_to_map(set1),
7340 set_to_map(set2)));
7343 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7345 return set_from_map(isl_map_union(set_to_map(set1), set_to_map(set2)));
7348 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7349 * the results.
7351 * "map" and "set" are assumed to be compatible and non-NULL.
7353 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7354 __isl_take isl_set *set,
7355 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7356 __isl_take isl_basic_set *bset))
7358 unsigned flags = 0;
7359 struct isl_map *result;
7360 int i, j;
7362 if (isl_set_plain_is_universe(set)) {
7363 isl_set_free(set);
7364 return map;
7367 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7368 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7369 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7371 result = isl_map_alloc_space(isl_space_copy(map->dim),
7372 map->n * set->n, flags);
7373 for (i = 0; result && i < map->n; ++i)
7374 for (j = 0; j < set->n; ++j) {
7375 result = isl_map_add_basic_map(result,
7376 fn(isl_basic_map_copy(map->p[i]),
7377 isl_basic_set_copy(set->p[j])));
7378 if (!result)
7379 break;
7382 isl_map_free(map);
7383 isl_set_free(set);
7384 return result;
7387 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7388 __isl_take isl_set *set)
7390 isl_bool ok;
7392 ok = isl_map_compatible_range(map, set);
7393 if (ok < 0)
7394 goto error;
7395 if (!ok)
7396 isl_die(set->ctx, isl_error_invalid,
7397 "incompatible spaces", goto error);
7399 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7400 error:
7401 isl_map_free(map);
7402 isl_set_free(set);
7403 return NULL;
7406 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7407 __isl_take isl_set *set)
7409 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7412 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7413 __isl_take isl_set *set)
7415 isl_bool ok;
7417 ok = isl_map_compatible_domain(map, set);
7418 if (ok < 0)
7419 goto error;
7420 if (!ok)
7421 isl_die(set->ctx, isl_error_invalid,
7422 "incompatible spaces", goto error);
7424 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7425 error:
7426 isl_map_free(map);
7427 isl_set_free(set);
7428 return NULL;
7431 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7432 __isl_take isl_set *set)
7434 return isl_map_align_params_map_map_and(map, set,
7435 &map_intersect_domain);
7438 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7439 __isl_take isl_map *map2)
7441 if (!map1 || !map2)
7442 goto error;
7443 map1 = isl_map_reverse(map1);
7444 map1 = isl_map_apply_range(map1, map2);
7445 return isl_map_reverse(map1);
7446 error:
7447 isl_map_free(map1);
7448 isl_map_free(map2);
7449 return NULL;
7452 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7453 __isl_take isl_map *map2)
7455 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7458 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7459 __isl_take isl_map *map2)
7461 isl_space *dim_result;
7462 struct isl_map *result;
7463 int i, j;
7465 if (!map1 || !map2)
7466 goto error;
7468 dim_result = isl_space_join(isl_space_copy(map1->dim),
7469 isl_space_copy(map2->dim));
7471 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7472 if (!result)
7473 goto error;
7474 for (i = 0; i < map1->n; ++i)
7475 for (j = 0; j < map2->n; ++j) {
7476 result = isl_map_add_basic_map(result,
7477 isl_basic_map_apply_range(
7478 isl_basic_map_copy(map1->p[i]),
7479 isl_basic_map_copy(map2->p[j])));
7480 if (!result)
7481 goto error;
7483 isl_map_free(map1);
7484 isl_map_free(map2);
7485 if (result && result->n <= 1)
7486 ISL_F_SET(result, ISL_MAP_DISJOINT);
7487 return result;
7488 error:
7489 isl_map_free(map1);
7490 isl_map_free(map2);
7491 return NULL;
7494 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7495 __isl_take isl_map *map2)
7497 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7501 * returns range - domain
7503 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7505 isl_space *target_space;
7506 struct isl_basic_set *bset;
7507 unsigned dim;
7508 unsigned nparam;
7509 int i;
7511 if (!bmap)
7512 goto error;
7513 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7514 bmap->dim, isl_dim_out),
7515 goto error);
7516 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7517 dim = isl_basic_map_n_in(bmap);
7518 nparam = isl_basic_map_n_param(bmap);
7519 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7520 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7521 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7522 for (i = 0; i < dim; ++i) {
7523 int j = isl_basic_map_alloc_equality(bmap);
7524 if (j < 0) {
7525 bmap = isl_basic_map_free(bmap);
7526 break;
7528 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7529 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7530 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7531 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7533 bset = isl_basic_map_domain(bmap);
7534 bset = isl_basic_set_reset_space(bset, target_space);
7535 return bset;
7536 error:
7537 isl_basic_map_free(bmap);
7538 return NULL;
7542 * returns range - domain
7544 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7546 int i;
7547 isl_space *dim;
7548 struct isl_set *result;
7550 if (!map)
7551 return NULL;
7553 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7554 map->dim, isl_dim_out),
7555 goto error);
7556 dim = isl_map_get_space(map);
7557 dim = isl_space_domain(dim);
7558 result = isl_set_alloc_space(dim, map->n, 0);
7559 if (!result)
7560 goto error;
7561 for (i = 0; i < map->n; ++i)
7562 result = isl_set_add_basic_set(result,
7563 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7564 isl_map_free(map);
7565 return result;
7566 error:
7567 isl_map_free(map);
7568 return NULL;
7572 * returns [domain -> range] -> range - domain
7574 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7575 __isl_take isl_basic_map *bmap)
7577 int i, k;
7578 isl_space *dim;
7579 isl_basic_map *domain;
7580 int nparam, n;
7581 unsigned total;
7583 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7584 bmap->dim, isl_dim_out))
7585 isl_die(bmap->ctx, isl_error_invalid,
7586 "domain and range don't match", goto error);
7588 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7589 n = isl_basic_map_dim(bmap, isl_dim_in);
7591 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7592 domain = isl_basic_map_universe(dim);
7594 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7595 bmap = isl_basic_map_apply_range(bmap, domain);
7596 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7598 total = isl_basic_map_total_dim(bmap);
7600 for (i = 0; i < n; ++i) {
7601 k = isl_basic_map_alloc_equality(bmap);
7602 if (k < 0)
7603 goto error;
7604 isl_seq_clr(bmap->eq[k], 1 + total);
7605 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7606 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7607 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7610 bmap = isl_basic_map_gauss(bmap, NULL);
7611 return isl_basic_map_finalize(bmap);
7612 error:
7613 isl_basic_map_free(bmap);
7614 return NULL;
7618 * returns [domain -> range] -> range - domain
7620 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7622 int i;
7623 isl_space *domain_dim;
7625 if (!map)
7626 return NULL;
7628 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7629 map->dim, isl_dim_out))
7630 isl_die(map->ctx, isl_error_invalid,
7631 "domain and range don't match", goto error);
7633 map = isl_map_cow(map);
7634 if (!map)
7635 return NULL;
7637 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7638 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7639 map->dim = isl_space_join(map->dim, domain_dim);
7640 if (!map->dim)
7641 goto error;
7642 for (i = 0; i < map->n; ++i) {
7643 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7644 if (!map->p[i])
7645 goto error;
7647 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7648 return map;
7649 error:
7650 isl_map_free(map);
7651 return NULL;
7654 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7656 struct isl_basic_map *bmap;
7657 unsigned nparam;
7658 unsigned dim;
7659 int i;
7661 if (!dims)
7662 return NULL;
7664 nparam = dims->nparam;
7665 dim = dims->n_out;
7666 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7667 if (!bmap)
7668 goto error;
7670 for (i = 0; i < dim; ++i) {
7671 int j = isl_basic_map_alloc_equality(bmap);
7672 if (j < 0)
7673 goto error;
7674 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7675 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7676 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7678 return isl_basic_map_finalize(bmap);
7679 error:
7680 isl_basic_map_free(bmap);
7681 return NULL;
7684 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7686 if (!dim)
7687 return NULL;
7688 if (dim->n_in != dim->n_out)
7689 isl_die(dim->ctx, isl_error_invalid,
7690 "number of input and output dimensions needs to be "
7691 "the same", goto error);
7692 return basic_map_identity(dim);
7693 error:
7694 isl_space_free(dim);
7695 return NULL;
7698 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7700 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7703 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7705 isl_space *dim = isl_set_get_space(set);
7706 isl_map *id;
7707 id = isl_map_identity(isl_space_map_from_set(dim));
7708 return isl_map_intersect_range(id, set);
7711 /* Construct a basic set with all set dimensions having only non-negative
7712 * values.
7714 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7715 __isl_take isl_space *space)
7717 int i;
7718 unsigned nparam;
7719 unsigned dim;
7720 struct isl_basic_set *bset;
7722 if (!space)
7723 return NULL;
7724 nparam = space->nparam;
7725 dim = space->n_out;
7726 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7727 if (!bset)
7728 return NULL;
7729 for (i = 0; i < dim; ++i) {
7730 int k = isl_basic_set_alloc_inequality(bset);
7731 if (k < 0)
7732 goto error;
7733 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7734 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7736 return bset;
7737 error:
7738 isl_basic_set_free(bset);
7739 return NULL;
7742 /* Construct the half-space x_pos >= 0.
7744 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7745 int pos)
7747 int k;
7748 isl_basic_set *nonneg;
7750 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7751 k = isl_basic_set_alloc_inequality(nonneg);
7752 if (k < 0)
7753 goto error;
7754 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7755 isl_int_set_si(nonneg->ineq[k][pos], 1);
7757 return isl_basic_set_finalize(nonneg);
7758 error:
7759 isl_basic_set_free(nonneg);
7760 return NULL;
7763 /* Construct the half-space x_pos <= -1.
7765 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7767 int k;
7768 isl_basic_set *neg;
7770 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7771 k = isl_basic_set_alloc_inequality(neg);
7772 if (k < 0)
7773 goto error;
7774 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7775 isl_int_set_si(neg->ineq[k][0], -1);
7776 isl_int_set_si(neg->ineq[k][pos], -1);
7778 return isl_basic_set_finalize(neg);
7779 error:
7780 isl_basic_set_free(neg);
7781 return NULL;
7784 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7785 enum isl_dim_type type, unsigned first, unsigned n)
7787 int i;
7788 unsigned offset;
7789 isl_basic_set *nonneg;
7790 isl_basic_set *neg;
7792 if (!set)
7793 return NULL;
7794 if (n == 0)
7795 return set;
7797 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7799 offset = pos(set->dim, type);
7800 for (i = 0; i < n; ++i) {
7801 nonneg = nonneg_halfspace(isl_set_get_space(set),
7802 offset + first + i);
7803 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7805 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7808 return set;
7809 error:
7810 isl_set_free(set);
7811 return NULL;
7814 static isl_stat foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7815 int len,
7816 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7817 void *user)
7819 isl_set *half;
7821 if (!set)
7822 return isl_stat_error;
7823 if (isl_set_plain_is_empty(set)) {
7824 isl_set_free(set);
7825 return isl_stat_ok;
7827 if (first == len)
7828 return fn(set, signs, user);
7830 signs[first] = 1;
7831 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7832 1 + first));
7833 half = isl_set_intersect(half, isl_set_copy(set));
7834 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7835 goto error;
7837 signs[first] = -1;
7838 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7839 1 + first));
7840 half = isl_set_intersect(half, set);
7841 return foreach_orthant(half, signs, first + 1, len, fn, user);
7842 error:
7843 isl_set_free(set);
7844 return isl_stat_error;
7847 /* Call "fn" on the intersections of "set" with each of the orthants
7848 * (except for obviously empty intersections). The orthant is identified
7849 * by the signs array, with each entry having value 1 or -1 according
7850 * to the sign of the corresponding variable.
7852 isl_stat isl_set_foreach_orthant(__isl_keep isl_set *set,
7853 isl_stat (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7854 void *user)
7856 unsigned nparam;
7857 unsigned nvar;
7858 int *signs;
7859 isl_stat r;
7861 if (!set)
7862 return isl_stat_error;
7863 if (isl_set_plain_is_empty(set))
7864 return isl_stat_ok;
7866 nparam = isl_set_dim(set, isl_dim_param);
7867 nvar = isl_set_dim(set, isl_dim_set);
7869 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7871 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7872 fn, user);
7874 free(signs);
7876 return r;
7879 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7881 return isl_map_is_equal(set_to_map(set1), set_to_map(set2));
7884 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7885 __isl_keep isl_basic_map *bmap2)
7887 int is_subset;
7888 struct isl_map *map1;
7889 struct isl_map *map2;
7891 if (!bmap1 || !bmap2)
7892 return isl_bool_error;
7894 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7895 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7897 is_subset = isl_map_is_subset(map1, map2);
7899 isl_map_free(map1);
7900 isl_map_free(map2);
7902 return is_subset;
7905 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7906 __isl_keep isl_basic_set *bset2)
7908 return isl_basic_map_is_subset(bset1, bset2);
7911 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7912 __isl_keep isl_basic_map *bmap2)
7914 isl_bool is_subset;
7916 if (!bmap1 || !bmap2)
7917 return isl_bool_error;
7918 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7919 if (is_subset != isl_bool_true)
7920 return is_subset;
7921 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7922 return is_subset;
7925 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7926 __isl_keep isl_basic_set *bset2)
7928 return isl_basic_map_is_equal(
7929 bset_to_bmap(bset1), bset_to_bmap(bset2));
7932 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7934 int i;
7935 int is_empty;
7937 if (!map)
7938 return isl_bool_error;
7939 for (i = 0; i < map->n; ++i) {
7940 is_empty = isl_basic_map_is_empty(map->p[i]);
7941 if (is_empty < 0)
7942 return isl_bool_error;
7943 if (!is_empty)
7944 return isl_bool_false;
7946 return isl_bool_true;
7949 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7951 return map ? map->n == 0 : isl_bool_error;
7954 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7956 return set ? set->n == 0 : isl_bool_error;
7959 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7961 return isl_map_is_empty(set_to_map(set));
7964 isl_bool isl_map_has_equal_space(__isl_keep isl_map *map1,
7965 __isl_keep isl_map *map2)
7967 if (!map1 || !map2)
7968 return isl_bool_error;
7970 return isl_space_is_equal(map1->dim, map2->dim);
7973 isl_bool isl_set_has_equal_space(__isl_keep isl_set *set1,
7974 __isl_keep isl_set *set2)
7976 if (!set1 || !set2)
7977 return isl_bool_error;
7979 return isl_space_is_equal(set1->dim, set2->dim);
7982 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7984 isl_bool is_subset;
7986 if (!map1 || !map2)
7987 return isl_bool_error;
7988 is_subset = isl_map_is_subset(map1, map2);
7989 if (is_subset != isl_bool_true)
7990 return is_subset;
7991 is_subset = isl_map_is_subset(map2, map1);
7992 return is_subset;
7995 /* Is "map1" equal to "map2"?
7997 * First check if they are obviously equal.
7998 * If not, then perform a more detailed analysis.
8000 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8002 isl_bool equal;
8004 equal = isl_map_plain_is_equal(map1, map2);
8005 if (equal < 0 || equal)
8006 return equal;
8007 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
8010 isl_bool isl_basic_map_is_strict_subset(
8011 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8013 isl_bool is_subset;
8015 if (!bmap1 || !bmap2)
8016 return isl_bool_error;
8017 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
8018 if (is_subset != isl_bool_true)
8019 return is_subset;
8020 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
8021 if (is_subset == isl_bool_error)
8022 return is_subset;
8023 return !is_subset;
8026 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
8027 __isl_keep isl_map *map2)
8029 isl_bool is_subset;
8031 if (!map1 || !map2)
8032 return isl_bool_error;
8033 is_subset = isl_map_is_subset(map1, map2);
8034 if (is_subset != isl_bool_true)
8035 return is_subset;
8036 is_subset = isl_map_is_subset(map2, map1);
8037 if (is_subset == isl_bool_error)
8038 return is_subset;
8039 return !is_subset;
8042 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
8043 __isl_keep isl_set *set2)
8045 return isl_map_is_strict_subset(set_to_map(set1), set_to_map(set2));
8048 /* Is "bmap" obviously equal to the universe with the same space?
8050 * That is, does it not have any constraints?
8052 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8054 if (!bmap)
8055 return isl_bool_error;
8056 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8059 /* Is "bset" obviously equal to the universe with the same space?
8061 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8063 return isl_basic_map_plain_is_universe(bset);
8066 /* If "c" does not involve any existentially quantified variables,
8067 * then set *univ to false and abort
8069 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8071 isl_bool *univ = user;
8072 unsigned n;
8074 n = isl_constraint_dim(c, isl_dim_div);
8075 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8076 isl_constraint_free(c);
8077 if (*univ < 0 || !*univ)
8078 return isl_stat_error;
8079 return isl_stat_ok;
8082 /* Is "bmap" equal to the universe with the same space?
8084 * First check if it is obviously equal to the universe.
8085 * If not and if there are any constraints not involving
8086 * existentially quantified variables, then it is certainly
8087 * not equal to the universe.
8088 * Otherwise, check if the universe is a subset of "bmap".
8090 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8092 isl_bool univ;
8093 isl_basic_map *test;
8095 univ = isl_basic_map_plain_is_universe(bmap);
8096 if (univ < 0 || univ)
8097 return univ;
8098 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8099 return isl_bool_false;
8100 univ = isl_bool_true;
8101 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8102 univ)
8103 return isl_bool_error;
8104 if (univ < 0 || !univ)
8105 return univ;
8106 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8107 univ = isl_basic_map_is_subset(test, bmap);
8108 isl_basic_map_free(test);
8109 return univ;
8112 /* Is "bset" equal to the universe with the same space?
8114 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8116 return isl_basic_map_is_universe(bset);
8119 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8121 int i;
8123 if (!map)
8124 return isl_bool_error;
8126 for (i = 0; i < map->n; ++i) {
8127 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8128 if (r < 0 || r)
8129 return r;
8132 return isl_bool_false;
8135 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8137 return isl_map_plain_is_universe(set_to_map(set));
8140 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8142 struct isl_basic_set *bset = NULL;
8143 struct isl_vec *sample = NULL;
8144 isl_bool empty, non_empty;
8146 if (!bmap)
8147 return isl_bool_error;
8149 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8150 return isl_bool_true;
8152 if (isl_basic_map_plain_is_universe(bmap))
8153 return isl_bool_false;
8155 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8156 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8157 copy = isl_basic_map_remove_redundancies(copy);
8158 empty = isl_basic_map_plain_is_empty(copy);
8159 isl_basic_map_free(copy);
8160 return empty;
8163 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8164 if (non_empty < 0)
8165 return isl_bool_error;
8166 if (non_empty)
8167 return isl_bool_false;
8168 isl_vec_free(bmap->sample);
8169 bmap->sample = NULL;
8170 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8171 if (!bset)
8172 return isl_bool_error;
8173 sample = isl_basic_set_sample_vec(bset);
8174 if (!sample)
8175 return isl_bool_error;
8176 empty = sample->size == 0;
8177 isl_vec_free(bmap->sample);
8178 bmap->sample = sample;
8179 if (empty)
8180 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8182 return empty;
8185 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8187 if (!bmap)
8188 return isl_bool_error;
8189 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8192 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8194 if (!bset)
8195 return isl_bool_error;
8196 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8199 /* Is "bmap" known to be non-empty?
8201 * That is, is the cached sample still valid?
8203 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8205 unsigned total;
8207 if (!bmap)
8208 return isl_bool_error;
8209 if (!bmap->sample)
8210 return isl_bool_false;
8211 total = 1 + isl_basic_map_total_dim(bmap);
8212 if (bmap->sample->size != total)
8213 return isl_bool_false;
8214 return isl_basic_map_contains(bmap, bmap->sample);
8217 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8219 return isl_basic_map_is_empty(bset_to_bmap(bset));
8222 struct isl_map *isl_basic_map_union(
8223 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8225 struct isl_map *map;
8226 if (!bmap1 || !bmap2)
8227 goto error;
8229 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8231 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8232 if (!map)
8233 goto error;
8234 map = isl_map_add_basic_map(map, bmap1);
8235 map = isl_map_add_basic_map(map, bmap2);
8236 return map;
8237 error:
8238 isl_basic_map_free(bmap1);
8239 isl_basic_map_free(bmap2);
8240 return NULL;
8243 struct isl_set *isl_basic_set_union(
8244 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8246 return set_from_map(isl_basic_map_union(bset_to_bmap(bset1),
8247 bset_to_bmap(bset2)));
8250 /* Order divs such that any div only depends on previous divs */
8251 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8253 int i;
8254 unsigned off;
8256 if (!bmap)
8257 return NULL;
8259 off = isl_space_dim(bmap->dim, isl_dim_all);
8261 for (i = 0; i < bmap->n_div; ++i) {
8262 int pos;
8263 if (isl_int_is_zero(bmap->div[i][0]))
8264 continue;
8265 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8266 bmap->n_div-i);
8267 if (pos == -1)
8268 continue;
8269 if (pos == 0)
8270 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8271 "integer division depends on itself",
8272 return isl_basic_map_free(bmap));
8273 isl_basic_map_swap_div(bmap, i, i + pos);
8274 --i;
8276 return bmap;
8279 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8281 return bset_from_bmap(isl_basic_map_order_divs(bset_to_bmap(bset)));
8284 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8286 int i;
8288 if (!map)
8289 return 0;
8291 for (i = 0; i < map->n; ++i) {
8292 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8293 if (!map->p[i])
8294 goto error;
8297 return map;
8298 error:
8299 isl_map_free(map);
8300 return NULL;
8303 /* Sort the local variables of "bset".
8305 __isl_give isl_basic_set *isl_basic_set_sort_divs(
8306 __isl_take isl_basic_set *bset)
8308 return bset_from_bmap(isl_basic_map_sort_divs(bset_to_bmap(bset)));
8311 /* Apply the expansion computed by isl_merge_divs.
8312 * The expansion itself is given by "exp" while the resulting
8313 * list of divs is given by "div".
8315 * Move the integer divisions of "bmap" into the right position
8316 * according to "exp" and then introduce the additional integer
8317 * divisions, adding div constraints.
8318 * The moving should be done first to avoid moving coefficients
8319 * in the definitions of the extra integer divisions.
8321 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8322 __isl_take isl_basic_map *bmap, __isl_take isl_mat *div, int *exp)
8324 int i, j;
8325 int n_div;
8327 bmap = isl_basic_map_cow(bmap);
8328 if (!bmap || !div)
8329 goto error;
8331 if (div->n_row < bmap->n_div)
8332 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8333 "not an expansion", goto error);
8335 n_div = bmap->n_div;
8336 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8337 div->n_row - n_div, 0,
8338 2 * (div->n_row - n_div));
8340 for (i = n_div; i < div->n_row; ++i)
8341 if (isl_basic_map_alloc_div(bmap) < 0)
8342 goto error;
8344 for (j = n_div - 1; j >= 0; --j) {
8345 if (exp[j] == j)
8346 break;
8347 isl_basic_map_swap_div(bmap, j, exp[j]);
8349 j = 0;
8350 for (i = 0; i < div->n_row; ++i) {
8351 if (j < n_div && exp[j] == i) {
8352 j++;
8353 } else {
8354 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8355 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8356 continue;
8357 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8358 goto error;
8362 isl_mat_free(div);
8363 return bmap;
8364 error:
8365 isl_basic_map_free(bmap);
8366 isl_mat_free(div);
8367 return NULL;
8370 /* Apply the expansion computed by isl_merge_divs.
8371 * The expansion itself is given by "exp" while the resulting
8372 * list of divs is given by "div".
8374 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8375 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8377 return isl_basic_map_expand_divs(bset, div, exp);
8380 /* Look for a div in dst that corresponds to the div "div" in src.
8381 * The divs before "div" in src and dst are assumed to be the same.
8383 * Returns -1 if no corresponding div was found and the position
8384 * of the corresponding div in dst otherwise.
8386 static int find_div(struct isl_basic_map *dst,
8387 struct isl_basic_map *src, unsigned div)
8389 int i;
8391 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8393 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8394 for (i = div; i < dst->n_div; ++i)
8395 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8396 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8397 dst->n_div - div) == -1)
8398 return i;
8399 return -1;
8402 /* Align the divs of "dst" to those of "src", adding divs from "src"
8403 * if needed. That is, make sure that the first src->n_div divs
8404 * of the result are equal to those of src.
8406 * The result is not finalized as by design it will have redundant
8407 * divs if any divs from "src" were copied.
8409 __isl_give isl_basic_map *isl_basic_map_align_divs(
8410 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8412 int i;
8413 int known, extended;
8414 unsigned total;
8416 if (!dst || !src)
8417 return isl_basic_map_free(dst);
8419 if (src->n_div == 0)
8420 return dst;
8422 known = isl_basic_map_divs_known(src);
8423 if (known < 0)
8424 return isl_basic_map_free(dst);
8425 if (!known)
8426 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8427 "some src divs are unknown",
8428 return isl_basic_map_free(dst));
8430 src = isl_basic_map_order_divs(src);
8432 extended = 0;
8433 total = isl_space_dim(src->dim, isl_dim_all);
8434 for (i = 0; i < src->n_div; ++i) {
8435 int j = find_div(dst, src, i);
8436 if (j < 0) {
8437 if (!extended) {
8438 int extra = src->n_div - i;
8439 dst = isl_basic_map_cow(dst);
8440 if (!dst)
8441 return NULL;
8442 dst = isl_basic_map_extend_space(dst,
8443 isl_space_copy(dst->dim),
8444 extra, 0, 2 * extra);
8445 extended = 1;
8447 j = isl_basic_map_alloc_div(dst);
8448 if (j < 0)
8449 return isl_basic_map_free(dst);
8450 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8451 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8452 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8453 return isl_basic_map_free(dst);
8455 if (j != i)
8456 isl_basic_map_swap_div(dst, i, j);
8458 return dst;
8461 struct isl_map *isl_map_align_divs(struct isl_map *map)
8463 int i;
8465 if (!map)
8466 return NULL;
8467 if (map->n == 0)
8468 return map;
8469 map = isl_map_compute_divs(map);
8470 map = isl_map_cow(map);
8471 if (!map)
8472 return NULL;
8474 for (i = 1; i < map->n; ++i)
8475 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8476 for (i = 1; i < map->n; ++i) {
8477 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8478 if (!map->p[i])
8479 return isl_map_free(map);
8482 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8483 return map;
8486 struct isl_set *isl_set_align_divs(struct isl_set *set)
8488 return set_from_map(isl_map_align_divs(set_to_map(set)));
8491 /* Align the divs of the basic maps in "map" to those
8492 * of the basic maps in "list", as well as to the other basic maps in "map".
8493 * The elements in "list" are assumed to have known divs.
8495 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8496 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8498 int i, n;
8500 map = isl_map_compute_divs(map);
8501 map = isl_map_cow(map);
8502 if (!map || !list)
8503 return isl_map_free(map);
8504 if (map->n == 0)
8505 return map;
8507 n = isl_basic_map_list_n_basic_map(list);
8508 for (i = 0; i < n; ++i) {
8509 isl_basic_map *bmap;
8511 bmap = isl_basic_map_list_get_basic_map(list, i);
8512 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8513 isl_basic_map_free(bmap);
8515 if (!map->p[0])
8516 return isl_map_free(map);
8518 return isl_map_align_divs(map);
8521 /* Align the divs of each element of "list" to those of "bmap".
8522 * Both "bmap" and the elements of "list" are assumed to have known divs.
8524 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8525 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8527 int i, n;
8529 if (!list || !bmap)
8530 return isl_basic_map_list_free(list);
8532 n = isl_basic_map_list_n_basic_map(list);
8533 for (i = 0; i < n; ++i) {
8534 isl_basic_map *bmap_i;
8536 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8537 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8538 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8541 return list;
8544 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8545 __isl_take isl_map *map)
8547 isl_bool ok;
8549 ok = isl_map_compatible_domain(map, set);
8550 if (ok < 0)
8551 goto error;
8552 if (!ok)
8553 isl_die(isl_set_get_ctx(set), isl_error_invalid,
8554 "incompatible spaces", goto error);
8555 map = isl_map_intersect_domain(map, set);
8556 set = isl_map_range(map);
8557 return set;
8558 error:
8559 isl_set_free(set);
8560 isl_map_free(map);
8561 return NULL;
8564 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8565 __isl_take isl_map *map)
8567 return isl_map_align_params_map_map_and(set, map, &set_apply);
8570 /* There is no need to cow as removing empty parts doesn't change
8571 * the meaning of the set.
8573 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8575 int i;
8577 if (!map)
8578 return NULL;
8580 for (i = map->n - 1; i >= 0; --i)
8581 remove_if_empty(map, i);
8583 return map;
8586 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8588 return set_from_map(isl_map_remove_empty_parts(set_to_map(set)));
8591 /* Given two basic sets bset1 and bset2, compute the maximal difference
8592 * between the values of dimension pos in bset1 and those in bset2
8593 * for any common value of the parameters and dimensions preceding pos.
8595 static enum isl_lp_result basic_set_maximal_difference_at(
8596 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8597 int pos, isl_int *opt)
8599 isl_basic_map *bmap1;
8600 isl_basic_map *bmap2;
8601 struct isl_ctx *ctx;
8602 struct isl_vec *obj;
8603 unsigned total;
8604 unsigned nparam;
8605 unsigned dim1;
8606 enum isl_lp_result res;
8608 if (!bset1 || !bset2)
8609 return isl_lp_error;
8611 nparam = isl_basic_set_n_param(bset1);
8612 dim1 = isl_basic_set_n_dim(bset1);
8614 bmap1 = isl_basic_map_from_range(isl_basic_set_copy(bset1));
8615 bmap2 = isl_basic_map_from_range(isl_basic_set_copy(bset2));
8616 bmap1 = isl_basic_map_move_dims(bmap1, isl_dim_in, 0,
8617 isl_dim_out, 0, pos);
8618 bmap2 = isl_basic_map_move_dims(bmap2, isl_dim_in, 0,
8619 isl_dim_out, 0, pos);
8620 bmap1 = isl_basic_map_range_product(bmap1, bmap2);
8621 if (!bmap1)
8622 return isl_lp_error;
8624 total = isl_basic_map_total_dim(bmap1);
8625 ctx = bmap1->ctx;
8626 obj = isl_vec_alloc(ctx, 1 + total);
8627 if (!obj)
8628 goto error;
8629 isl_seq_clr(obj->block.data, 1 + total);
8630 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8631 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8632 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8633 opt, NULL, NULL);
8634 isl_basic_map_free(bmap1);
8635 isl_vec_free(obj);
8636 return res;
8637 error:
8638 isl_basic_map_free(bmap1);
8639 return isl_lp_error;
8642 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8643 * for any common value of the parameters and dimensions preceding pos
8644 * in both basic sets, the values of dimension pos in bset1 are
8645 * smaller or larger than those in bset2.
8647 * Returns
8648 * 1 if bset1 follows bset2
8649 * -1 if bset1 precedes bset2
8650 * 0 if bset1 and bset2 are incomparable
8651 * -2 if some error occurred.
8653 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8654 struct isl_basic_set *bset2, int pos)
8656 isl_int opt;
8657 enum isl_lp_result res;
8658 int cmp;
8660 isl_int_init(opt);
8662 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8664 if (res == isl_lp_empty)
8665 cmp = 0;
8666 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8667 res == isl_lp_unbounded)
8668 cmp = 1;
8669 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8670 cmp = -1;
8671 else
8672 cmp = -2;
8674 isl_int_clear(opt);
8675 return cmp;
8678 /* Given two basic sets bset1 and bset2, check whether
8679 * for any common value of the parameters and dimensions preceding pos
8680 * there is a value of dimension pos in bset1 that is larger
8681 * than a value of the same dimension in bset2.
8683 * Return
8684 * 1 if there exists such a pair
8685 * 0 if there is no such pair, but there is a pair of equal values
8686 * -1 otherwise
8687 * -2 if some error occurred.
8689 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8690 __isl_keep isl_basic_set *bset2, int pos)
8692 isl_int opt;
8693 enum isl_lp_result res;
8694 int cmp;
8696 isl_int_init(opt);
8698 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8700 if (res == isl_lp_empty)
8701 cmp = -1;
8702 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8703 res == isl_lp_unbounded)
8704 cmp = 1;
8705 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8706 cmp = -1;
8707 else if (res == isl_lp_ok)
8708 cmp = 0;
8709 else
8710 cmp = -2;
8712 isl_int_clear(opt);
8713 return cmp;
8716 /* Given two sets set1 and set2, check whether
8717 * for any common value of the parameters and dimensions preceding pos
8718 * there is a value of dimension pos in set1 that is larger
8719 * than a value of the same dimension in set2.
8721 * Return
8722 * 1 if there exists such a pair
8723 * 0 if there is no such pair, but there is a pair of equal values
8724 * -1 otherwise
8725 * -2 if some error occurred.
8727 int isl_set_follows_at(__isl_keep isl_set *set1,
8728 __isl_keep isl_set *set2, int pos)
8730 int i, j;
8731 int follows = -1;
8733 if (!set1 || !set2)
8734 return -2;
8736 for (i = 0; i < set1->n; ++i)
8737 for (j = 0; j < set2->n; ++j) {
8738 int f;
8739 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8740 if (f == 1 || f == -2)
8741 return f;
8742 if (f > follows)
8743 follows = f;
8746 return follows;
8749 static isl_bool isl_basic_map_plain_has_fixed_var(
8750 __isl_keep isl_basic_map *bmap, unsigned pos, isl_int *val)
8752 int i;
8753 int d;
8754 unsigned total;
8756 if (!bmap)
8757 return isl_bool_error;
8758 total = isl_basic_map_total_dim(bmap);
8759 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8760 for (; d+1 > pos; --d)
8761 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8762 break;
8763 if (d != pos)
8764 continue;
8765 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8766 return isl_bool_false;
8767 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8768 return isl_bool_false;
8769 if (!isl_int_is_one(bmap->eq[i][1+d]))
8770 return isl_bool_false;
8771 if (val)
8772 isl_int_neg(*val, bmap->eq[i][0]);
8773 return isl_bool_true;
8775 return isl_bool_false;
8778 static isl_bool isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8779 unsigned pos, isl_int *val)
8781 int i;
8782 isl_int v;
8783 isl_int tmp;
8784 isl_bool fixed;
8786 if (!map)
8787 return isl_bool_error;
8788 if (map->n == 0)
8789 return isl_bool_false;
8790 if (map->n == 1)
8791 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8792 isl_int_init(v);
8793 isl_int_init(tmp);
8794 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8795 for (i = 1; fixed == isl_bool_true && i < map->n; ++i) {
8796 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8797 if (fixed == isl_bool_true && isl_int_ne(tmp, v))
8798 fixed = isl_bool_false;
8800 if (val)
8801 isl_int_set(*val, v);
8802 isl_int_clear(tmp);
8803 isl_int_clear(v);
8804 return fixed;
8807 static isl_bool isl_basic_set_plain_has_fixed_var(
8808 __isl_keep isl_basic_set *bset, unsigned pos, isl_int *val)
8810 return isl_basic_map_plain_has_fixed_var(bset_to_bmap(bset),
8811 pos, val);
8814 isl_bool isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8815 enum isl_dim_type type, unsigned pos, isl_int *val)
8817 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
8818 return isl_bool_error;
8819 return isl_basic_map_plain_has_fixed_var(bmap,
8820 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8823 /* If "bmap" obviously lies on a hyperplane where the given dimension
8824 * has a fixed value, then return that value.
8825 * Otherwise return NaN.
8827 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8828 __isl_keep isl_basic_map *bmap,
8829 enum isl_dim_type type, unsigned pos)
8831 isl_ctx *ctx;
8832 isl_val *v;
8833 isl_bool fixed;
8835 if (!bmap)
8836 return NULL;
8837 ctx = isl_basic_map_get_ctx(bmap);
8838 v = isl_val_alloc(ctx);
8839 if (!v)
8840 return NULL;
8841 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8842 if (fixed < 0)
8843 return isl_val_free(v);
8844 if (fixed) {
8845 isl_int_set_si(v->d, 1);
8846 return v;
8848 isl_val_free(v);
8849 return isl_val_nan(ctx);
8852 isl_bool isl_map_plain_is_fixed(__isl_keep isl_map *map,
8853 enum isl_dim_type type, unsigned pos, isl_int *val)
8855 if (pos >= isl_map_dim(map, type))
8856 isl_die(isl_map_get_ctx(map), isl_error_invalid,
8857 "position out of bounds", return isl_bool_error);
8858 return isl_map_plain_has_fixed_var(map,
8859 map_offset(map, type) - 1 + pos, val);
8862 /* If "map" obviously lies on a hyperplane where the given dimension
8863 * has a fixed value, then return that value.
8864 * Otherwise return NaN.
8866 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8867 enum isl_dim_type type, unsigned pos)
8869 isl_ctx *ctx;
8870 isl_val *v;
8871 isl_bool fixed;
8873 if (!map)
8874 return NULL;
8875 ctx = isl_map_get_ctx(map);
8876 v = isl_val_alloc(ctx);
8877 if (!v)
8878 return NULL;
8879 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8880 if (fixed < 0)
8881 return isl_val_free(v);
8882 if (fixed) {
8883 isl_int_set_si(v->d, 1);
8884 return v;
8886 isl_val_free(v);
8887 return isl_val_nan(ctx);
8890 /* If "set" obviously lies on a hyperplane where the given dimension
8891 * has a fixed value, then return that value.
8892 * Otherwise return NaN.
8894 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8895 enum isl_dim_type type, unsigned pos)
8897 return isl_map_plain_get_val_if_fixed(set, type, pos);
8900 isl_bool isl_set_plain_is_fixed(__isl_keep isl_set *set,
8901 enum isl_dim_type type, unsigned pos, isl_int *val)
8903 return isl_map_plain_is_fixed(set, type, pos, val);
8906 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8907 * then return this fixed value in *val.
8909 isl_bool isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8910 unsigned dim, isl_int *val)
8912 return isl_basic_set_plain_has_fixed_var(bset,
8913 isl_basic_set_n_param(bset) + dim, val);
8916 /* Return -1 if the constraint "c1" should be sorted before "c2"
8917 * and 1 if it should be sorted after "c2".
8918 * Return 0 if the two constraints are the same (up to the constant term).
8920 * In particular, if a constraint involves later variables than another
8921 * then it is sorted after this other constraint.
8922 * uset_gist depends on constraints without existentially quantified
8923 * variables sorting first.
8925 * For constraints that have the same latest variable, those
8926 * with the same coefficient for this latest variable (first in absolute value
8927 * and then in actual value) are grouped together.
8928 * This is useful for detecting pairs of constraints that can
8929 * be chained in their printed representation.
8931 * Finally, within a group, constraints are sorted according to
8932 * their coefficients (excluding the constant term).
8934 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8936 isl_int **c1 = (isl_int **) p1;
8937 isl_int **c2 = (isl_int **) p2;
8938 int l1, l2;
8939 unsigned size = *(unsigned *) arg;
8940 int cmp;
8942 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8943 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8945 if (l1 != l2)
8946 return l1 - l2;
8948 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
8949 if (cmp != 0)
8950 return cmp;
8951 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
8952 if (cmp != 0)
8953 return -cmp;
8955 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
8958 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
8959 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
8960 * and 0 if the two constraints are the same (up to the constant term).
8962 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
8963 isl_int *c1, isl_int *c2)
8965 unsigned total;
8967 if (!bmap)
8968 return -2;
8969 total = isl_basic_map_total_dim(bmap);
8970 return sort_constraint_cmp(&c1, &c2, &total);
8973 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
8974 __isl_take isl_basic_map *bmap)
8976 unsigned total;
8978 if (!bmap)
8979 return NULL;
8980 if (bmap->n_ineq == 0)
8981 return bmap;
8982 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8983 return bmap;
8984 total = isl_basic_map_total_dim(bmap);
8985 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
8986 &sort_constraint_cmp, &total) < 0)
8987 return isl_basic_map_free(bmap);
8988 return bmap;
8991 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8992 __isl_take isl_basic_set *bset)
8994 isl_basic_map *bmap = bset_to_bmap(bset);
8995 return bset_from_bmap(isl_basic_map_sort_constraints(bmap));
8998 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9000 if (!bmap)
9001 return NULL;
9002 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9003 return bmap;
9004 bmap = isl_basic_map_remove_redundancies(bmap);
9005 bmap = isl_basic_map_sort_constraints(bmap);
9006 if (bmap)
9007 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9008 return bmap;
9010 int isl_basic_map_plain_cmp(__isl_keep isl_basic_map *bmap1,
9011 __isl_keep isl_basic_map *bmap2)
9013 int i, cmp;
9014 unsigned total;
9016 if (!bmap1 || !bmap2)
9017 return -1;
9019 if (bmap1 == bmap2)
9020 return 0;
9021 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9022 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9023 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9024 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9025 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9026 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9027 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9028 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9029 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9030 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9031 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9032 return 0;
9033 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9034 return 1;
9035 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9036 return -1;
9037 if (bmap1->n_eq != bmap2->n_eq)
9038 return bmap1->n_eq - bmap2->n_eq;
9039 if (bmap1->n_ineq != bmap2->n_ineq)
9040 return bmap1->n_ineq - bmap2->n_ineq;
9041 if (bmap1->n_div != bmap2->n_div)
9042 return bmap1->n_div - bmap2->n_div;
9043 total = isl_basic_map_total_dim(bmap1);
9044 for (i = 0; i < bmap1->n_eq; ++i) {
9045 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9046 if (cmp)
9047 return cmp;
9049 for (i = 0; i < bmap1->n_ineq; ++i) {
9050 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9051 if (cmp)
9052 return cmp;
9054 for (i = 0; i < bmap1->n_div; ++i) {
9055 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9056 if (cmp)
9057 return cmp;
9059 return 0;
9062 int isl_basic_set_plain_cmp(__isl_keep isl_basic_set *bset1,
9063 __isl_keep isl_basic_set *bset2)
9065 return isl_basic_map_plain_cmp(bset1, bset2);
9068 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9070 int i, cmp;
9072 if (set1 == set2)
9073 return 0;
9074 if (set1->n != set2->n)
9075 return set1->n - set2->n;
9077 for (i = 0; i < set1->n; ++i) {
9078 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9079 if (cmp)
9080 return cmp;
9083 return 0;
9086 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9087 __isl_keep isl_basic_map *bmap2)
9089 if (!bmap1 || !bmap2)
9090 return isl_bool_error;
9091 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9094 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9095 __isl_keep isl_basic_set *bset2)
9097 return isl_basic_map_plain_is_equal(bset_to_bmap(bset1),
9098 bset_to_bmap(bset2));
9101 static int qsort_bmap_cmp(const void *p1, const void *p2)
9103 isl_basic_map *bmap1 = *(isl_basic_map **) p1;
9104 isl_basic_map *bmap2 = *(isl_basic_map **) p2;
9106 return isl_basic_map_plain_cmp(bmap1, bmap2);
9109 /* Sort the basic maps of "map" and remove duplicate basic maps.
9111 * While removing basic maps, we make sure that the basic maps remain
9112 * sorted because isl_map_normalize expects the basic maps of the result
9113 * to be sorted.
9115 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9117 int i, j;
9119 map = isl_map_remove_empty_parts(map);
9120 if (!map)
9121 return NULL;
9122 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9123 for (i = map->n - 1; i >= 1; --i) {
9124 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9125 continue;
9126 isl_basic_map_free(map->p[i-1]);
9127 for (j = i; j < map->n; ++j)
9128 map->p[j - 1] = map->p[j];
9129 map->n--;
9132 return map;
9135 /* Remove obvious duplicates among the basic maps of "map".
9137 * Unlike isl_map_normalize, this function does not remove redundant
9138 * constraints and only removes duplicates that have exactly the same
9139 * constraints in the input. It does sort the constraints and
9140 * the basic maps to ease the detection of duplicates.
9142 * If "map" has already been normalized or if the basic maps are
9143 * disjoint, then there can be no duplicates.
9145 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9147 int i;
9148 isl_basic_map *bmap;
9150 if (!map)
9151 return NULL;
9152 if (map->n <= 1)
9153 return map;
9154 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9155 return map;
9156 for (i = 0; i < map->n; ++i) {
9157 bmap = isl_basic_map_copy(map->p[i]);
9158 bmap = isl_basic_map_sort_constraints(bmap);
9159 if (!bmap)
9160 return isl_map_free(map);
9161 isl_basic_map_free(map->p[i]);
9162 map->p[i] = bmap;
9165 map = sort_and_remove_duplicates(map);
9166 return map;
9169 /* We normalize in place, but if anything goes wrong we need
9170 * to return NULL, so we need to make sure we don't change the
9171 * meaning of any possible other copies of map.
9173 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9175 int i;
9176 struct isl_basic_map *bmap;
9178 if (!map)
9179 return NULL;
9180 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9181 return map;
9182 for (i = 0; i < map->n; ++i) {
9183 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9184 if (!bmap)
9185 goto error;
9186 isl_basic_map_free(map->p[i]);
9187 map->p[i] = bmap;
9190 map = sort_and_remove_duplicates(map);
9191 if (map)
9192 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9193 return map;
9194 error:
9195 isl_map_free(map);
9196 return NULL;
9199 struct isl_set *isl_set_normalize(struct isl_set *set)
9201 return set_from_map(isl_map_normalize(set_to_map(set)));
9204 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9205 __isl_keep isl_map *map2)
9207 int i;
9208 isl_bool equal;
9210 if (!map1 || !map2)
9211 return isl_bool_error;
9213 if (map1 == map2)
9214 return isl_bool_true;
9215 if (!isl_space_is_equal(map1->dim, map2->dim))
9216 return isl_bool_false;
9218 map1 = isl_map_copy(map1);
9219 map2 = isl_map_copy(map2);
9220 map1 = isl_map_normalize(map1);
9221 map2 = isl_map_normalize(map2);
9222 if (!map1 || !map2)
9223 goto error;
9224 equal = map1->n == map2->n;
9225 for (i = 0; equal && i < map1->n; ++i) {
9226 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9227 if (equal < 0)
9228 goto error;
9230 isl_map_free(map1);
9231 isl_map_free(map2);
9232 return equal;
9233 error:
9234 isl_map_free(map1);
9235 isl_map_free(map2);
9236 return isl_bool_error;
9239 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9240 __isl_keep isl_set *set2)
9242 return isl_map_plain_is_equal(set_to_map(set1), set_to_map(set2));
9245 /* Return the basic maps in "map" as a list.
9247 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9248 __isl_keep isl_map *map)
9250 int i;
9251 isl_ctx *ctx;
9252 isl_basic_map_list *list;
9254 if (!map)
9255 return NULL;
9256 ctx = isl_map_get_ctx(map);
9257 list = isl_basic_map_list_alloc(ctx, map->n);
9259 for (i = 0; i < map->n; ++i) {
9260 isl_basic_map *bmap;
9262 bmap = isl_basic_map_copy(map->p[i]);
9263 list = isl_basic_map_list_add(list, bmap);
9266 return list;
9269 /* Return the intersection of the elements in the non-empty list "list".
9270 * All elements are assumed to live in the same space.
9272 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9273 __isl_take isl_basic_map_list *list)
9275 int i, n;
9276 isl_basic_map *bmap;
9278 if (!list)
9279 return NULL;
9280 n = isl_basic_map_list_n_basic_map(list);
9281 if (n < 1)
9282 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9283 "expecting non-empty list", goto error);
9285 bmap = isl_basic_map_list_get_basic_map(list, 0);
9286 for (i = 1; i < n; ++i) {
9287 isl_basic_map *bmap_i;
9289 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9290 bmap = isl_basic_map_intersect(bmap, bmap_i);
9293 isl_basic_map_list_free(list);
9294 return bmap;
9295 error:
9296 isl_basic_map_list_free(list);
9297 return NULL;
9300 /* Return the intersection of the elements in the non-empty list "list".
9301 * All elements are assumed to live in the same space.
9303 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9304 __isl_take isl_basic_set_list *list)
9306 return isl_basic_map_list_intersect(list);
9309 /* Return the union of the elements of "list".
9310 * The list is required to have at least one element.
9312 __isl_give isl_set *isl_basic_set_list_union(
9313 __isl_take isl_basic_set_list *list)
9315 int i, n;
9316 isl_space *space;
9317 isl_basic_set *bset;
9318 isl_set *set;
9320 if (!list)
9321 return NULL;
9322 n = isl_basic_set_list_n_basic_set(list);
9323 if (n < 1)
9324 isl_die(isl_basic_set_list_get_ctx(list), isl_error_invalid,
9325 "expecting non-empty list", goto error);
9327 bset = isl_basic_set_list_get_basic_set(list, 0);
9328 space = isl_basic_set_get_space(bset);
9329 isl_basic_set_free(bset);
9331 set = isl_set_alloc_space(space, n, 0);
9332 for (i = 0; i < n; ++i) {
9333 bset = isl_basic_set_list_get_basic_set(list, i);
9334 set = isl_set_add_basic_set(set, bset);
9337 isl_basic_set_list_free(list);
9338 return set;
9339 error:
9340 isl_basic_set_list_free(list);
9341 return NULL;
9344 /* Return the union of the elements in the non-empty list "list".
9345 * All elements are assumed to live in the same space.
9347 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9349 int i, n;
9350 isl_set *set;
9352 if (!list)
9353 return NULL;
9354 n = isl_set_list_n_set(list);
9355 if (n < 1)
9356 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9357 "expecting non-empty list", goto error);
9359 set = isl_set_list_get_set(list, 0);
9360 for (i = 1; i < n; ++i) {
9361 isl_set *set_i;
9363 set_i = isl_set_list_get_set(list, i);
9364 set = isl_set_union(set, set_i);
9367 isl_set_list_free(list);
9368 return set;
9369 error:
9370 isl_set_list_free(list);
9371 return NULL;
9374 struct isl_basic_map *isl_basic_map_product(
9375 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9377 isl_space *dim_result = NULL;
9378 struct isl_basic_map *bmap;
9379 unsigned in1, in2, out1, out2, nparam, total, pos;
9380 struct isl_dim_map *dim_map1, *dim_map2;
9382 if (!bmap1 || !bmap2)
9383 goto error;
9385 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9386 bmap2->dim, isl_dim_param), goto error);
9387 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9388 isl_space_copy(bmap2->dim));
9390 in1 = isl_basic_map_n_in(bmap1);
9391 in2 = isl_basic_map_n_in(bmap2);
9392 out1 = isl_basic_map_n_out(bmap1);
9393 out2 = isl_basic_map_n_out(bmap2);
9394 nparam = isl_basic_map_n_param(bmap1);
9396 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9397 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9398 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9399 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9400 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9401 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9402 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9403 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9404 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9405 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9406 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9408 bmap = isl_basic_map_alloc_space(dim_result,
9409 bmap1->n_div + bmap2->n_div,
9410 bmap1->n_eq + bmap2->n_eq,
9411 bmap1->n_ineq + bmap2->n_ineq);
9412 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9413 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9414 bmap = isl_basic_map_simplify(bmap);
9415 return isl_basic_map_finalize(bmap);
9416 error:
9417 isl_basic_map_free(bmap1);
9418 isl_basic_map_free(bmap2);
9419 return NULL;
9422 __isl_give isl_basic_map *isl_basic_map_flat_product(
9423 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9425 isl_basic_map *prod;
9427 prod = isl_basic_map_product(bmap1, bmap2);
9428 prod = isl_basic_map_flatten(prod);
9429 return prod;
9432 __isl_give isl_basic_set *isl_basic_set_flat_product(
9433 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9435 return isl_basic_map_flat_range_product(bset1, bset2);
9438 __isl_give isl_basic_map *isl_basic_map_domain_product(
9439 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9441 isl_space *space_result = NULL;
9442 isl_basic_map *bmap;
9443 unsigned in1, in2, out, nparam, total, pos;
9444 struct isl_dim_map *dim_map1, *dim_map2;
9446 if (!bmap1 || !bmap2)
9447 goto error;
9449 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9450 isl_space_copy(bmap2->dim));
9452 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9453 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9454 out = isl_basic_map_dim(bmap1, isl_dim_out);
9455 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9457 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9458 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9459 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9460 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9461 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9462 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9463 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9464 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9465 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9466 isl_dim_map_div(dim_map1, bmap1, pos += out);
9467 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9469 bmap = isl_basic_map_alloc_space(space_result,
9470 bmap1->n_div + bmap2->n_div,
9471 bmap1->n_eq + bmap2->n_eq,
9472 bmap1->n_ineq + bmap2->n_ineq);
9473 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9474 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9475 bmap = isl_basic_map_simplify(bmap);
9476 return isl_basic_map_finalize(bmap);
9477 error:
9478 isl_basic_map_free(bmap1);
9479 isl_basic_map_free(bmap2);
9480 return NULL;
9483 __isl_give isl_basic_map *isl_basic_map_range_product(
9484 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9486 isl_bool rational;
9487 isl_space *dim_result = NULL;
9488 isl_basic_map *bmap;
9489 unsigned in, out1, out2, nparam, total, pos;
9490 struct isl_dim_map *dim_map1, *dim_map2;
9492 rational = isl_basic_map_is_rational(bmap1);
9493 if (rational >= 0 && rational)
9494 rational = isl_basic_map_is_rational(bmap2);
9495 if (!bmap1 || !bmap2 || rational < 0)
9496 goto error;
9498 if (!isl_space_match(bmap1->dim, isl_dim_param,
9499 bmap2->dim, isl_dim_param))
9500 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9501 "parameters don't match", goto error);
9503 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9504 isl_space_copy(bmap2->dim));
9506 in = isl_basic_map_dim(bmap1, isl_dim_in);
9507 out1 = isl_basic_map_n_out(bmap1);
9508 out2 = isl_basic_map_n_out(bmap2);
9509 nparam = isl_basic_map_n_param(bmap1);
9511 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9512 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9513 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9514 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9515 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9516 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9517 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9518 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9519 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9520 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9521 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9523 bmap = isl_basic_map_alloc_space(dim_result,
9524 bmap1->n_div + bmap2->n_div,
9525 bmap1->n_eq + bmap2->n_eq,
9526 bmap1->n_ineq + bmap2->n_ineq);
9527 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9528 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9529 if (rational)
9530 bmap = isl_basic_map_set_rational(bmap);
9531 bmap = isl_basic_map_simplify(bmap);
9532 return isl_basic_map_finalize(bmap);
9533 error:
9534 isl_basic_map_free(bmap1);
9535 isl_basic_map_free(bmap2);
9536 return NULL;
9539 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9540 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9542 isl_basic_map *prod;
9544 prod = isl_basic_map_range_product(bmap1, bmap2);
9545 prod = isl_basic_map_flatten_range(prod);
9546 return prod;
9549 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9550 * and collect the results.
9551 * The result live in the space obtained by calling "space_product"
9552 * on the spaces of "map1" and "map2".
9553 * If "remove_duplicates" is set then the result may contain duplicates
9554 * (even if the inputs do not) and so we try and remove the obvious
9555 * duplicates.
9557 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9558 __isl_take isl_map *map2,
9559 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9560 __isl_take isl_space *right),
9561 __isl_give isl_basic_map *(*basic_map_product)(
9562 __isl_take isl_basic_map *left,
9563 __isl_take isl_basic_map *right),
9564 int remove_duplicates)
9566 unsigned flags = 0;
9567 struct isl_map *result;
9568 int i, j;
9570 if (!map1 || !map2)
9571 goto error;
9573 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9574 map2->dim, isl_dim_param), goto error);
9576 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9577 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9578 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9580 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9581 isl_space_copy(map2->dim)),
9582 map1->n * map2->n, flags);
9583 if (!result)
9584 goto error;
9585 for (i = 0; i < map1->n; ++i)
9586 for (j = 0; j < map2->n; ++j) {
9587 struct isl_basic_map *part;
9588 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9589 isl_basic_map_copy(map2->p[j]));
9590 if (isl_basic_map_is_empty(part))
9591 isl_basic_map_free(part);
9592 else
9593 result = isl_map_add_basic_map(result, part);
9594 if (!result)
9595 goto error;
9597 if (remove_duplicates)
9598 result = isl_map_remove_obvious_duplicates(result);
9599 isl_map_free(map1);
9600 isl_map_free(map2);
9601 return result;
9602 error:
9603 isl_map_free(map1);
9604 isl_map_free(map2);
9605 return NULL;
9608 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9610 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9611 __isl_take isl_map *map2)
9613 return map_product(map1, map2, &isl_space_product,
9614 &isl_basic_map_product, 0);
9617 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9618 __isl_take isl_map *map2)
9620 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9623 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9625 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9626 __isl_take isl_map *map2)
9628 isl_map *prod;
9630 prod = isl_map_product(map1, map2);
9631 prod = isl_map_flatten(prod);
9632 return prod;
9635 /* Given two set A and B, construct its Cartesian product A x B.
9637 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9639 return isl_map_range_product(set1, set2);
9642 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9643 __isl_take isl_set *set2)
9645 return isl_map_flat_range_product(set1, set2);
9648 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9650 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9651 __isl_take isl_map *map2)
9653 return map_product(map1, map2, &isl_space_domain_product,
9654 &isl_basic_map_domain_product, 1);
9657 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9659 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9660 __isl_take isl_map *map2)
9662 return map_product(map1, map2, &isl_space_range_product,
9663 &isl_basic_map_range_product, 1);
9666 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9667 __isl_take isl_map *map2)
9669 return isl_map_align_params_map_map_and(map1, map2,
9670 &map_domain_product_aligned);
9673 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9674 __isl_take isl_map *map2)
9676 return isl_map_align_params_map_map_and(map1, map2,
9677 &map_range_product_aligned);
9680 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9682 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9684 isl_space *space;
9685 int total1, keep1, total2, keep2;
9687 if (!map)
9688 return NULL;
9689 if (!isl_space_domain_is_wrapping(map->dim) ||
9690 !isl_space_range_is_wrapping(map->dim))
9691 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9692 "not a product", return isl_map_free(map));
9694 space = isl_map_get_space(map);
9695 total1 = isl_space_dim(space, isl_dim_in);
9696 total2 = isl_space_dim(space, isl_dim_out);
9697 space = isl_space_factor_domain(space);
9698 keep1 = isl_space_dim(space, isl_dim_in);
9699 keep2 = isl_space_dim(space, isl_dim_out);
9700 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9701 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9702 map = isl_map_reset_space(map, space);
9704 return map;
9707 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9709 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9711 isl_space *space;
9712 int total1, keep1, total2, keep2;
9714 if (!map)
9715 return NULL;
9716 if (!isl_space_domain_is_wrapping(map->dim) ||
9717 !isl_space_range_is_wrapping(map->dim))
9718 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9719 "not a product", return isl_map_free(map));
9721 space = isl_map_get_space(map);
9722 total1 = isl_space_dim(space, isl_dim_in);
9723 total2 = isl_space_dim(space, isl_dim_out);
9724 space = isl_space_factor_range(space);
9725 keep1 = isl_space_dim(space, isl_dim_in);
9726 keep2 = isl_space_dim(space, isl_dim_out);
9727 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9728 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9729 map = isl_map_reset_space(map, space);
9731 return map;
9734 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9736 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9738 isl_space *space;
9739 int total, keep;
9741 if (!map)
9742 return NULL;
9743 if (!isl_space_domain_is_wrapping(map->dim))
9744 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9745 "domain is not a product", return isl_map_free(map));
9747 space = isl_map_get_space(map);
9748 total = isl_space_dim(space, isl_dim_in);
9749 space = isl_space_domain_factor_domain(space);
9750 keep = isl_space_dim(space, isl_dim_in);
9751 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9752 map = isl_map_reset_space(map, space);
9754 return map;
9757 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9759 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9761 isl_space *space;
9762 int total, keep;
9764 if (!map)
9765 return NULL;
9766 if (!isl_space_domain_is_wrapping(map->dim))
9767 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9768 "domain is not a product", return isl_map_free(map));
9770 space = isl_map_get_space(map);
9771 total = isl_space_dim(space, isl_dim_in);
9772 space = isl_space_domain_factor_range(space);
9773 keep = isl_space_dim(space, isl_dim_in);
9774 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9775 map = isl_map_reset_space(map, space);
9777 return map;
9780 /* Given a map A -> [B -> C], extract the map A -> B.
9782 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9784 isl_space *space;
9785 int total, keep;
9787 if (!map)
9788 return NULL;
9789 if (!isl_space_range_is_wrapping(map->dim))
9790 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9791 "range is not a product", return isl_map_free(map));
9793 space = isl_map_get_space(map);
9794 total = isl_space_dim(space, isl_dim_out);
9795 space = isl_space_range_factor_domain(space);
9796 keep = isl_space_dim(space, isl_dim_out);
9797 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9798 map = isl_map_reset_space(map, space);
9800 return map;
9803 /* Given a map A -> [B -> C], extract the map A -> C.
9805 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9807 isl_space *space;
9808 int total, keep;
9810 if (!map)
9811 return NULL;
9812 if (!isl_space_range_is_wrapping(map->dim))
9813 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9814 "range is not a product", return isl_map_free(map));
9816 space = isl_map_get_space(map);
9817 total = isl_space_dim(space, isl_dim_out);
9818 space = isl_space_range_factor_range(space);
9819 keep = isl_space_dim(space, isl_dim_out);
9820 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9821 map = isl_map_reset_space(map, space);
9823 return map;
9826 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9828 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9829 __isl_take isl_map *map2)
9831 isl_map *prod;
9833 prod = isl_map_domain_product(map1, map2);
9834 prod = isl_map_flatten_domain(prod);
9835 return prod;
9838 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9840 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9841 __isl_take isl_map *map2)
9843 isl_map *prod;
9845 prod = isl_map_range_product(map1, map2);
9846 prod = isl_map_flatten_range(prod);
9847 return prod;
9850 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9852 int i;
9853 uint32_t hash = isl_hash_init();
9854 unsigned total;
9856 if (!bmap)
9857 return 0;
9858 bmap = isl_basic_map_copy(bmap);
9859 bmap = isl_basic_map_normalize(bmap);
9860 if (!bmap)
9861 return 0;
9862 total = isl_basic_map_total_dim(bmap);
9863 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9864 for (i = 0; i < bmap->n_eq; ++i) {
9865 uint32_t c_hash;
9866 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9867 isl_hash_hash(hash, c_hash);
9869 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9870 for (i = 0; i < bmap->n_ineq; ++i) {
9871 uint32_t c_hash;
9872 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9873 isl_hash_hash(hash, c_hash);
9875 isl_hash_byte(hash, bmap->n_div & 0xFF);
9876 for (i = 0; i < bmap->n_div; ++i) {
9877 uint32_t c_hash;
9878 if (isl_int_is_zero(bmap->div[i][0]))
9879 continue;
9880 isl_hash_byte(hash, i & 0xFF);
9881 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9882 isl_hash_hash(hash, c_hash);
9884 isl_basic_map_free(bmap);
9885 return hash;
9888 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9890 return isl_basic_map_get_hash(bset_to_bmap(bset));
9893 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9895 int i;
9896 uint32_t hash;
9898 if (!map)
9899 return 0;
9900 map = isl_map_copy(map);
9901 map = isl_map_normalize(map);
9902 if (!map)
9903 return 0;
9905 hash = isl_hash_init();
9906 for (i = 0; i < map->n; ++i) {
9907 uint32_t bmap_hash;
9908 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9909 isl_hash_hash(hash, bmap_hash);
9912 isl_map_free(map);
9914 return hash;
9917 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9919 return isl_map_get_hash(set_to_map(set));
9922 /* Return the number of basic maps in the (current) representation of "map".
9924 int isl_map_n_basic_map(__isl_keep isl_map *map)
9926 return map ? map->n : 0;
9929 int isl_set_n_basic_set(__isl_keep isl_set *set)
9931 return set ? set->n : 0;
9934 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
9935 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9937 int i;
9939 if (!map)
9940 return isl_stat_error;
9942 for (i = 0; i < map->n; ++i)
9943 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9944 return isl_stat_error;
9946 return isl_stat_ok;
9949 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
9950 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9952 int i;
9954 if (!set)
9955 return isl_stat_error;
9957 for (i = 0; i < set->n; ++i)
9958 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9959 return isl_stat_error;
9961 return isl_stat_ok;
9964 /* Return a list of basic sets, the union of which is equal to "set".
9966 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
9967 __isl_keep isl_set *set)
9969 int i;
9970 isl_basic_set_list *list;
9972 if (!set)
9973 return NULL;
9975 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
9976 for (i = 0; i < set->n; ++i) {
9977 isl_basic_set *bset;
9979 bset = isl_basic_set_copy(set->p[i]);
9980 list = isl_basic_set_list_add(list, bset);
9983 return list;
9986 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9988 isl_space *dim;
9990 if (!bset)
9991 return NULL;
9993 bset = isl_basic_set_cow(bset);
9994 if (!bset)
9995 return NULL;
9997 dim = isl_basic_set_get_space(bset);
9998 dim = isl_space_lift(dim, bset->n_div);
9999 if (!dim)
10000 goto error;
10001 isl_space_free(bset->dim);
10002 bset->dim = dim;
10003 bset->extra -= bset->n_div;
10004 bset->n_div = 0;
10006 bset = isl_basic_set_finalize(bset);
10008 return bset;
10009 error:
10010 isl_basic_set_free(bset);
10011 return NULL;
10014 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10016 int i;
10017 isl_space *dim;
10018 unsigned n_div;
10020 set = isl_set_align_divs(set);
10022 if (!set)
10023 return NULL;
10025 set = isl_set_cow(set);
10026 if (!set)
10027 return NULL;
10029 n_div = set->p[0]->n_div;
10030 dim = isl_set_get_space(set);
10031 dim = isl_space_lift(dim, n_div);
10032 if (!dim)
10033 goto error;
10034 isl_space_free(set->dim);
10035 set->dim = dim;
10037 for (i = 0; i < set->n; ++i) {
10038 set->p[i] = isl_basic_set_lift(set->p[i]);
10039 if (!set->p[i])
10040 goto error;
10043 return set;
10044 error:
10045 isl_set_free(set);
10046 return NULL;
10049 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10051 unsigned dim;
10052 int size = 0;
10054 if (!bset)
10055 return -1;
10057 dim = isl_basic_set_total_dim(bset);
10058 size += bset->n_eq * (1 + dim);
10059 size += bset->n_ineq * (1 + dim);
10060 size += bset->n_div * (2 + dim);
10062 return size;
10065 int isl_set_size(__isl_keep isl_set *set)
10067 int i;
10068 int size = 0;
10070 if (!set)
10071 return -1;
10073 for (i = 0; i < set->n; ++i)
10074 size += isl_basic_set_size(set->p[i]);
10076 return size;
10079 /* Check if there is any lower bound (if lower == 0) and/or upper
10080 * bound (if upper == 0) on the specified dim.
10082 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10083 enum isl_dim_type type, unsigned pos, int lower, int upper)
10085 int i;
10087 if (isl_basic_map_check_range(bmap, type, pos, 1) < 0)
10088 return isl_bool_error;
10090 pos += isl_basic_map_offset(bmap, type);
10092 for (i = 0; i < bmap->n_div; ++i) {
10093 if (isl_int_is_zero(bmap->div[i][0]))
10094 continue;
10095 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10096 return isl_bool_true;
10099 for (i = 0; i < bmap->n_eq; ++i)
10100 if (!isl_int_is_zero(bmap->eq[i][pos]))
10101 return isl_bool_true;
10103 for (i = 0; i < bmap->n_ineq; ++i) {
10104 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10105 if (sgn > 0)
10106 lower = 1;
10107 if (sgn < 0)
10108 upper = 1;
10111 return lower && upper;
10114 isl_bool isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10115 enum isl_dim_type type, unsigned pos)
10117 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10120 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10121 enum isl_dim_type type, unsigned pos)
10123 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10126 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10127 enum isl_dim_type type, unsigned pos)
10129 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10132 isl_bool isl_map_dim_is_bounded(__isl_keep isl_map *map,
10133 enum isl_dim_type type, unsigned pos)
10135 int i;
10137 if (!map)
10138 return isl_bool_error;
10140 for (i = 0; i < map->n; ++i) {
10141 isl_bool bounded;
10142 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10143 if (bounded < 0 || !bounded)
10144 return bounded;
10147 return isl_bool_true;
10150 /* Return true if the specified dim is involved in both an upper bound
10151 * and a lower bound.
10153 isl_bool isl_set_dim_is_bounded(__isl_keep isl_set *set,
10154 enum isl_dim_type type, unsigned pos)
10156 return isl_map_dim_is_bounded(set_to_map(set), type, pos);
10159 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10161 static isl_bool has_any_bound(__isl_keep isl_map *map,
10162 enum isl_dim_type type, unsigned pos,
10163 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10164 enum isl_dim_type type, unsigned pos))
10166 int i;
10168 if (!map)
10169 return isl_bool_error;
10171 for (i = 0; i < map->n; ++i) {
10172 isl_bool bounded;
10173 bounded = fn(map->p[i], type, pos);
10174 if (bounded < 0 || bounded)
10175 return bounded;
10178 return isl_bool_false;
10181 /* Return 1 if the specified dim is involved in any lower bound.
10183 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10184 enum isl_dim_type type, unsigned pos)
10186 return has_any_bound(set, type, pos,
10187 &isl_basic_map_dim_has_lower_bound);
10190 /* Return 1 if the specified dim is involved in any upper bound.
10192 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10193 enum isl_dim_type type, unsigned pos)
10195 return has_any_bound(set, type, pos,
10196 &isl_basic_map_dim_has_upper_bound);
10199 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10201 static isl_bool has_bound(__isl_keep isl_map *map,
10202 enum isl_dim_type type, unsigned pos,
10203 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10204 enum isl_dim_type type, unsigned pos))
10206 int i;
10208 if (!map)
10209 return isl_bool_error;
10211 for (i = 0; i < map->n; ++i) {
10212 isl_bool bounded;
10213 bounded = fn(map->p[i], type, pos);
10214 if (bounded < 0 || !bounded)
10215 return bounded;
10218 return isl_bool_true;
10221 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10223 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10224 enum isl_dim_type type, unsigned pos)
10226 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10229 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10231 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10232 enum isl_dim_type type, unsigned pos)
10234 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10237 /* For each of the "n" variables starting at "first", determine
10238 * the sign of the variable and put the results in the first "n"
10239 * elements of the array "signs".
10240 * Sign
10241 * 1 means that the variable is non-negative
10242 * -1 means that the variable is non-positive
10243 * 0 means the variable attains both positive and negative values.
10245 isl_stat isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10246 unsigned first, unsigned n, int *signs)
10248 isl_vec *bound = NULL;
10249 struct isl_tab *tab = NULL;
10250 struct isl_tab_undo *snap;
10251 int i;
10253 if (!bset || !signs)
10254 return isl_stat_error;
10256 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10257 tab = isl_tab_from_basic_set(bset, 0);
10258 if (!bound || !tab)
10259 goto error;
10261 isl_seq_clr(bound->el, bound->size);
10262 isl_int_set_si(bound->el[0], -1);
10264 snap = isl_tab_snap(tab);
10265 for (i = 0; i < n; ++i) {
10266 int empty;
10268 isl_int_set_si(bound->el[1 + first + i], -1);
10269 if (isl_tab_add_ineq(tab, bound->el) < 0)
10270 goto error;
10271 empty = tab->empty;
10272 isl_int_set_si(bound->el[1 + first + i], 0);
10273 if (isl_tab_rollback(tab, snap) < 0)
10274 goto error;
10276 if (empty) {
10277 signs[i] = 1;
10278 continue;
10281 isl_int_set_si(bound->el[1 + first + i], 1);
10282 if (isl_tab_add_ineq(tab, bound->el) < 0)
10283 goto error;
10284 empty = tab->empty;
10285 isl_int_set_si(bound->el[1 + first + i], 0);
10286 if (isl_tab_rollback(tab, snap) < 0)
10287 goto error;
10289 signs[i] = empty ? -1 : 0;
10292 isl_tab_free(tab);
10293 isl_vec_free(bound);
10294 return isl_stat_ok;
10295 error:
10296 isl_tab_free(tab);
10297 isl_vec_free(bound);
10298 return isl_stat_error;
10301 isl_stat isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10302 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10304 if (!bset || !signs)
10305 return isl_stat_error;
10306 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10307 return isl_stat_error);
10309 first += pos(bset->dim, type) - 1;
10310 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10313 /* Is it possible for the integer division "div" to depend (possibly
10314 * indirectly) on any output dimensions?
10316 * If the div is undefined, then we conservatively assume that it
10317 * may depend on them.
10318 * Otherwise, we check if it actually depends on them or on any integer
10319 * divisions that may depend on them.
10321 static isl_bool div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10323 int i;
10324 unsigned n_out, o_out;
10325 unsigned n_div, o_div;
10327 if (isl_int_is_zero(bmap->div[div][0]))
10328 return isl_bool_true;
10330 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10331 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10333 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10334 return isl_bool_true;
10336 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10337 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10339 for (i = 0; i < n_div; ++i) {
10340 isl_bool may_involve;
10342 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10343 continue;
10344 may_involve = div_may_involve_output(bmap, i);
10345 if (may_involve < 0 || may_involve)
10346 return may_involve;
10349 return isl_bool_false;
10352 /* Return the first integer division of "bmap" in the range
10353 * [first, first + n[ that may depend on any output dimensions and
10354 * that has a non-zero coefficient in "c" (where the first coefficient
10355 * in "c" corresponds to integer division "first").
10357 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10358 isl_int *c, int first, int n)
10360 int k;
10362 if (!bmap)
10363 return -1;
10365 for (k = first; k < first + n; ++k) {
10366 isl_bool may_involve;
10368 if (isl_int_is_zero(c[k]))
10369 continue;
10370 may_involve = div_may_involve_output(bmap, k);
10371 if (may_involve < 0)
10372 return -1;
10373 if (may_involve)
10374 return k;
10377 return first + n;
10380 /* Look for a pair of inequality constraints in "bmap" of the form
10382 * -l + i >= 0 or i >= l
10383 * and
10384 * n + l - i >= 0 or i <= l + n
10386 * with n < "m" and i the output dimension at position "pos".
10387 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10388 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10389 * and earlier output dimensions, as well as integer divisions that do
10390 * not involve any of the output dimensions.
10392 * Return the index of the first inequality constraint or bmap->n_ineq
10393 * if no such pair can be found.
10395 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10396 int pos, isl_int m)
10398 int i, j;
10399 isl_ctx *ctx;
10400 unsigned total;
10401 unsigned n_div, o_div;
10402 unsigned n_out, o_out;
10403 int less;
10405 if (!bmap)
10406 return -1;
10408 ctx = isl_basic_map_get_ctx(bmap);
10409 total = isl_basic_map_total_dim(bmap);
10410 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10411 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10412 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10413 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10414 for (i = 0; i < bmap->n_ineq; ++i) {
10415 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10416 continue;
10417 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10418 n_out - (pos + 1)) != -1)
10419 continue;
10420 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10421 0, n_div) < n_div)
10422 continue;
10423 for (j = i + 1; j < bmap->n_ineq; ++j) {
10424 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10425 ctx->one))
10426 continue;
10427 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10428 bmap->ineq[j] + 1, total))
10429 continue;
10430 break;
10432 if (j >= bmap->n_ineq)
10433 continue;
10434 isl_int_add(bmap->ineq[i][0],
10435 bmap->ineq[i][0], bmap->ineq[j][0]);
10436 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10437 isl_int_sub(bmap->ineq[i][0],
10438 bmap->ineq[i][0], bmap->ineq[j][0]);
10439 if (!less)
10440 continue;
10441 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10442 return i;
10443 else
10444 return j;
10447 return bmap->n_ineq;
10450 /* Return the index of the equality of "bmap" that defines
10451 * the output dimension "pos" in terms of earlier dimensions.
10452 * The equality may also involve integer divisions, as long
10453 * as those integer divisions are defined in terms of
10454 * parameters or input dimensions.
10455 * In this case, *div is set to the number of integer divisions and
10456 * *ineq is set to the number of inequality constraints (provided
10457 * div and ineq are not NULL).
10459 * The equality may also involve a single integer division involving
10460 * the output dimensions (typically only output dimension "pos") as
10461 * long as the coefficient of output dimension "pos" is 1 or -1 and
10462 * there is a pair of constraints i >= l and i <= l + n, with i referring
10463 * to output dimension "pos", l an expression involving only earlier
10464 * dimensions and n smaller than the coefficient of the integer division
10465 * in the equality. In this case, the output dimension can be defined
10466 * in terms of a modulo expression that does not involve the integer division.
10467 * *div is then set to this single integer division and
10468 * *ineq is set to the index of constraint i >= l.
10470 * Return bmap->n_eq if there is no such equality.
10471 * Return -1 on error.
10473 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10474 int pos, int *div, int *ineq)
10476 int j, k, l;
10477 unsigned n_out, o_out;
10478 unsigned n_div, o_div;
10480 if (!bmap)
10481 return -1;
10483 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10484 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10485 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10486 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10488 if (ineq)
10489 *ineq = bmap->n_ineq;
10490 if (div)
10491 *div = n_div;
10492 for (j = 0; j < bmap->n_eq; ++j) {
10493 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10494 continue;
10495 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10496 n_out - (pos + 1)) != -1)
10497 continue;
10498 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10499 0, n_div);
10500 if (k >= n_div)
10501 return j;
10502 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10503 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10504 continue;
10505 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10506 k + 1, n_div - (k+1)) < n_div)
10507 continue;
10508 l = find_modulo_constraint_pair(bmap, pos,
10509 bmap->eq[j][o_div + k]);
10510 if (l < 0)
10511 return -1;
10512 if (l >= bmap->n_ineq)
10513 continue;
10514 if (div)
10515 *div = k;
10516 if (ineq)
10517 *ineq = l;
10518 return j;
10521 return bmap->n_eq;
10524 /* Check if the given basic map is obviously single-valued.
10525 * In particular, for each output dimension, check that there is
10526 * an equality that defines the output dimension in terms of
10527 * earlier dimensions.
10529 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10531 int i;
10532 unsigned n_out;
10534 if (!bmap)
10535 return isl_bool_error;
10537 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10539 for (i = 0; i < n_out; ++i) {
10540 int eq;
10542 eq = isl_basic_map_output_defining_equality(bmap, i,
10543 NULL, NULL);
10544 if (eq < 0)
10545 return isl_bool_error;
10546 if (eq >= bmap->n_eq)
10547 return isl_bool_false;
10550 return isl_bool_true;
10553 /* Check if the given basic map is single-valued.
10554 * We simply compute
10556 * M \circ M^-1
10558 * and check if the result is a subset of the identity mapping.
10560 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10562 isl_space *space;
10563 isl_basic_map *test;
10564 isl_basic_map *id;
10565 isl_bool sv;
10567 sv = isl_basic_map_plain_is_single_valued(bmap);
10568 if (sv < 0 || sv)
10569 return sv;
10571 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10572 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10574 space = isl_basic_map_get_space(bmap);
10575 space = isl_space_map_from_set(isl_space_range(space));
10576 id = isl_basic_map_identity(space);
10578 sv = isl_basic_map_is_subset(test, id);
10580 isl_basic_map_free(test);
10581 isl_basic_map_free(id);
10583 return sv;
10586 /* Check if the given map is obviously single-valued.
10588 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10590 if (!map)
10591 return isl_bool_error;
10592 if (map->n == 0)
10593 return isl_bool_true;
10594 if (map->n >= 2)
10595 return isl_bool_false;
10597 return isl_basic_map_plain_is_single_valued(map->p[0]);
10600 /* Check if the given map is single-valued.
10601 * We simply compute
10603 * M \circ M^-1
10605 * and check if the result is a subset of the identity mapping.
10607 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10609 isl_space *dim;
10610 isl_map *test;
10611 isl_map *id;
10612 isl_bool sv;
10614 sv = isl_map_plain_is_single_valued(map);
10615 if (sv < 0 || sv)
10616 return sv;
10618 test = isl_map_reverse(isl_map_copy(map));
10619 test = isl_map_apply_range(test, isl_map_copy(map));
10621 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10622 id = isl_map_identity(dim);
10624 sv = isl_map_is_subset(test, id);
10626 isl_map_free(test);
10627 isl_map_free(id);
10629 return sv;
10632 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10634 isl_bool in;
10636 map = isl_map_copy(map);
10637 map = isl_map_reverse(map);
10638 in = isl_map_is_single_valued(map);
10639 isl_map_free(map);
10641 return in;
10644 /* Check if the given map is obviously injective.
10646 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10648 isl_bool in;
10650 map = isl_map_copy(map);
10651 map = isl_map_reverse(map);
10652 in = isl_map_plain_is_single_valued(map);
10653 isl_map_free(map);
10655 return in;
10658 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10660 isl_bool sv;
10662 sv = isl_map_is_single_valued(map);
10663 if (sv < 0 || !sv)
10664 return sv;
10666 return isl_map_is_injective(map);
10669 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10671 return isl_map_is_single_valued(set_to_map(set));
10674 /* Does "map" only map elements to themselves?
10676 * If the domain and range spaces are different, then "map"
10677 * is considered not to be an identity relation, even if it is empty.
10678 * Otherwise, construct the maximal identity relation and
10679 * check whether "map" is a subset of this relation.
10681 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10683 isl_space *space;
10684 isl_map *id;
10685 isl_bool equal, is_identity;
10687 space = isl_map_get_space(map);
10688 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10689 isl_space_free(space);
10690 if (equal < 0 || !equal)
10691 return equal;
10693 id = isl_map_identity(isl_map_get_space(map));
10694 is_identity = isl_map_is_subset(map, id);
10695 isl_map_free(id);
10697 return is_identity;
10700 int isl_map_is_translation(__isl_keep isl_map *map)
10702 int ok;
10703 isl_set *delta;
10705 delta = isl_map_deltas(isl_map_copy(map));
10706 ok = isl_set_is_singleton(delta);
10707 isl_set_free(delta);
10709 return ok;
10712 static int unique(isl_int *p, unsigned pos, unsigned len)
10714 if (isl_seq_first_non_zero(p, pos) != -1)
10715 return 0;
10716 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10717 return 0;
10718 return 1;
10721 isl_bool isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10723 int i, j;
10724 unsigned nvar;
10725 unsigned ovar;
10727 if (!bset)
10728 return isl_bool_error;
10730 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10731 return isl_bool_false;
10733 nvar = isl_basic_set_dim(bset, isl_dim_set);
10734 ovar = isl_space_offset(bset->dim, isl_dim_set);
10735 for (j = 0; j < nvar; ++j) {
10736 int lower = 0, upper = 0;
10737 for (i = 0; i < bset->n_eq; ++i) {
10738 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10739 continue;
10740 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10741 return isl_bool_false;
10742 break;
10744 if (i < bset->n_eq)
10745 continue;
10746 for (i = 0; i < bset->n_ineq; ++i) {
10747 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10748 continue;
10749 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10750 return isl_bool_false;
10751 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10752 lower = 1;
10753 else
10754 upper = 1;
10756 if (!lower || !upper)
10757 return isl_bool_false;
10760 return isl_bool_true;
10763 isl_bool isl_set_is_box(__isl_keep isl_set *set)
10765 if (!set)
10766 return isl_bool_error;
10767 if (set->n != 1)
10768 return isl_bool_false;
10770 return isl_basic_set_is_box(set->p[0]);
10773 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10775 if (!bset)
10776 return isl_bool_error;
10778 return isl_space_is_wrapping(bset->dim);
10781 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10783 if (!set)
10784 return isl_bool_error;
10786 return isl_space_is_wrapping(set->dim);
10789 /* Modify the space of "map" through a call to "change".
10790 * If "can_change" is set (not NULL), then first call it to check
10791 * if the modification is allowed, printing the error message "cannot_change"
10792 * if it is not.
10794 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
10795 isl_bool (*can_change)(__isl_keep isl_map *map),
10796 const char *cannot_change,
10797 __isl_give isl_space *(*change)(__isl_take isl_space *space))
10799 isl_bool ok;
10800 isl_space *space;
10802 if (!map)
10803 return NULL;
10805 ok = can_change ? can_change(map) : isl_bool_true;
10806 if (ok < 0)
10807 return isl_map_free(map);
10808 if (!ok)
10809 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
10810 return isl_map_free(map));
10812 space = change(isl_map_get_space(map));
10813 map = isl_map_reset_space(map, space);
10815 return map;
10818 /* Is the domain of "map" a wrapped relation?
10820 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
10822 if (!map)
10823 return isl_bool_error;
10825 return isl_space_domain_is_wrapping(map->dim);
10828 /* Is the range of "map" a wrapped relation?
10830 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
10832 if (!map)
10833 return isl_bool_error;
10835 return isl_space_range_is_wrapping(map->dim);
10838 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10840 bmap = isl_basic_map_cow(bmap);
10841 if (!bmap)
10842 return NULL;
10844 bmap->dim = isl_space_wrap(bmap->dim);
10845 if (!bmap->dim)
10846 goto error;
10848 bmap = isl_basic_map_finalize(bmap);
10850 return bset_from_bmap(bmap);
10851 error:
10852 isl_basic_map_free(bmap);
10853 return NULL;
10856 /* Given a map A -> B, return the set (A -> B).
10858 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10860 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
10863 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10865 bset = isl_basic_set_cow(bset);
10866 if (!bset)
10867 return NULL;
10869 bset->dim = isl_space_unwrap(bset->dim);
10870 if (!bset->dim)
10871 goto error;
10873 bset = isl_basic_set_finalize(bset);
10875 return bset_to_bmap(bset);
10876 error:
10877 isl_basic_set_free(bset);
10878 return NULL;
10881 /* Given a set (A -> B), return the map A -> B.
10882 * Error out if "set" is not of the form (A -> B).
10884 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10886 return isl_map_change_space(set, &isl_set_is_wrapping,
10887 "not a wrapping set", &isl_space_unwrap);
10890 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10891 enum isl_dim_type type)
10893 if (!bmap)
10894 return NULL;
10896 if (!isl_space_is_named_or_nested(bmap->dim, type))
10897 return bmap;
10899 bmap = isl_basic_map_cow(bmap);
10900 if (!bmap)
10901 return NULL;
10903 bmap->dim = isl_space_reset(bmap->dim, type);
10904 if (!bmap->dim)
10905 goto error;
10907 bmap = isl_basic_map_finalize(bmap);
10909 return bmap;
10910 error:
10911 isl_basic_map_free(bmap);
10912 return NULL;
10915 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10916 enum isl_dim_type type)
10918 int i;
10920 if (!map)
10921 return NULL;
10923 if (!isl_space_is_named_or_nested(map->dim, type))
10924 return map;
10926 map = isl_map_cow(map);
10927 if (!map)
10928 return NULL;
10930 for (i = 0; i < map->n; ++i) {
10931 map->p[i] = isl_basic_map_reset(map->p[i], type);
10932 if (!map->p[i])
10933 goto error;
10935 map->dim = isl_space_reset(map->dim, type);
10936 if (!map->dim)
10937 goto error;
10939 return map;
10940 error:
10941 isl_map_free(map);
10942 return NULL;
10945 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10947 if (!bmap)
10948 return NULL;
10950 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10951 return bmap;
10953 bmap = isl_basic_map_cow(bmap);
10954 if (!bmap)
10955 return NULL;
10957 bmap->dim = isl_space_flatten(bmap->dim);
10958 if (!bmap->dim)
10959 goto error;
10961 bmap = isl_basic_map_finalize(bmap);
10963 return bmap;
10964 error:
10965 isl_basic_map_free(bmap);
10966 return NULL;
10969 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10971 return bset_from_bmap(isl_basic_map_flatten(bset_to_bmap(bset)));
10974 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10975 __isl_take isl_basic_map *bmap)
10977 if (!bmap)
10978 return NULL;
10980 if (!bmap->dim->nested[0])
10981 return bmap;
10983 bmap = isl_basic_map_cow(bmap);
10984 if (!bmap)
10985 return NULL;
10987 bmap->dim = isl_space_flatten_domain(bmap->dim);
10988 if (!bmap->dim)
10989 goto error;
10991 bmap = isl_basic_map_finalize(bmap);
10993 return bmap;
10994 error:
10995 isl_basic_map_free(bmap);
10996 return NULL;
10999 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11000 __isl_take isl_basic_map *bmap)
11002 if (!bmap)
11003 return NULL;
11005 if (!bmap->dim->nested[1])
11006 return bmap;
11008 bmap = isl_basic_map_cow(bmap);
11009 if (!bmap)
11010 return NULL;
11012 bmap->dim = isl_space_flatten_range(bmap->dim);
11013 if (!bmap->dim)
11014 goto error;
11016 bmap = isl_basic_map_finalize(bmap);
11018 return bmap;
11019 error:
11020 isl_basic_map_free(bmap);
11021 return NULL;
11024 /* Remove any internal structure from the spaces of domain and range of "map".
11026 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11028 if (!map)
11029 return NULL;
11031 if (!map->dim->nested[0] && !map->dim->nested[1])
11032 return map;
11034 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11037 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11039 return set_from_map(isl_map_flatten(set_to_map(set)));
11042 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11044 isl_space *dim, *flat_dim;
11045 isl_map *map;
11047 dim = isl_set_get_space(set);
11048 flat_dim = isl_space_flatten(isl_space_copy(dim));
11049 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11050 map = isl_map_intersect_domain(map, set);
11052 return map;
11055 /* Remove any internal structure from the space of the domain of "map".
11057 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11059 if (!map)
11060 return NULL;
11062 if (!map->dim->nested[0])
11063 return map;
11065 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11068 /* Remove any internal structure from the space of the range of "map".
11070 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11072 if (!map)
11073 return NULL;
11075 if (!map->dim->nested[1])
11076 return map;
11078 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11081 /* Reorder the dimensions of "bmap" according to the given dim_map
11082 * and set the dimension specification to "dim" and
11083 * perform Gaussian elimination on the result.
11085 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11086 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11088 isl_basic_map *res;
11089 unsigned flags;
11091 bmap = isl_basic_map_cow(bmap);
11092 if (!bmap || !dim || !dim_map)
11093 goto error;
11095 flags = bmap->flags;
11096 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11097 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11098 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11099 res = isl_basic_map_alloc_space(dim,
11100 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11101 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11102 if (res)
11103 res->flags = flags;
11104 res = isl_basic_map_gauss(res, NULL);
11105 res = isl_basic_map_finalize(res);
11106 return res;
11107 error:
11108 free(dim_map);
11109 isl_basic_map_free(bmap);
11110 isl_space_free(dim);
11111 return NULL;
11114 /* Reorder the dimensions of "map" according to given reordering.
11116 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11117 __isl_take isl_reordering *r)
11119 int i;
11120 struct isl_dim_map *dim_map;
11122 map = isl_map_cow(map);
11123 dim_map = isl_dim_map_from_reordering(r);
11124 if (!map || !r || !dim_map)
11125 goto error;
11127 for (i = 0; i < map->n; ++i) {
11128 struct isl_dim_map *dim_map_i;
11130 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11132 map->p[i] = isl_basic_map_realign(map->p[i],
11133 isl_space_copy(r->dim), dim_map_i);
11135 if (!map->p[i])
11136 goto error;
11139 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11141 isl_reordering_free(r);
11142 free(dim_map);
11143 return map;
11144 error:
11145 free(dim_map);
11146 isl_map_free(map);
11147 isl_reordering_free(r);
11148 return NULL;
11151 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11152 __isl_take isl_reordering *r)
11154 return set_from_map(isl_map_realign(set_to_map(set), r));
11157 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11158 __isl_take isl_space *model)
11160 isl_ctx *ctx;
11162 if (!map || !model)
11163 goto error;
11165 ctx = isl_space_get_ctx(model);
11166 if (!isl_space_has_named_params(model))
11167 isl_die(ctx, isl_error_invalid,
11168 "model has unnamed parameters", goto error);
11169 if (isl_map_check_named_params(map) < 0)
11170 goto error;
11171 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11172 isl_reordering *exp;
11174 model = isl_space_drop_dims(model, isl_dim_in,
11175 0, isl_space_dim(model, isl_dim_in));
11176 model = isl_space_drop_dims(model, isl_dim_out,
11177 0, isl_space_dim(model, isl_dim_out));
11178 exp = isl_parameter_alignment_reordering(map->dim, model);
11179 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11180 map = isl_map_realign(map, exp);
11183 isl_space_free(model);
11184 return map;
11185 error:
11186 isl_space_free(model);
11187 isl_map_free(map);
11188 return NULL;
11191 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11192 __isl_take isl_space *model)
11194 return isl_map_align_params(set, model);
11197 /* Align the parameters of "bmap" to those of "model", introducing
11198 * additional parameters if needed.
11200 __isl_give isl_basic_map *isl_basic_map_align_params(
11201 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11203 isl_ctx *ctx;
11205 if (!bmap || !model)
11206 goto error;
11208 ctx = isl_space_get_ctx(model);
11209 if (!isl_space_has_named_params(model))
11210 isl_die(ctx, isl_error_invalid,
11211 "model has unnamed parameters", goto error);
11212 if (!isl_space_has_named_params(bmap->dim))
11213 isl_die(ctx, isl_error_invalid,
11214 "relation has unnamed parameters", goto error);
11215 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11216 isl_reordering *exp;
11217 struct isl_dim_map *dim_map;
11219 model = isl_space_drop_dims(model, isl_dim_in,
11220 0, isl_space_dim(model, isl_dim_in));
11221 model = isl_space_drop_dims(model, isl_dim_out,
11222 0, isl_space_dim(model, isl_dim_out));
11223 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11224 exp = isl_reordering_extend_space(exp,
11225 isl_basic_map_get_space(bmap));
11226 dim_map = isl_dim_map_from_reordering(exp);
11227 bmap = isl_basic_map_realign(bmap,
11228 exp ? isl_space_copy(exp->dim) : NULL,
11229 isl_dim_map_extend(dim_map, bmap));
11230 isl_reordering_free(exp);
11231 free(dim_map);
11234 isl_space_free(model);
11235 return bmap;
11236 error:
11237 isl_space_free(model);
11238 isl_basic_map_free(bmap);
11239 return NULL;
11242 /* Align the parameters of "bset" to those of "model", introducing
11243 * additional parameters if needed.
11245 __isl_give isl_basic_set *isl_basic_set_align_params(
11246 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11248 return isl_basic_map_align_params(bset, model);
11251 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11252 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11253 enum isl_dim_type c2, enum isl_dim_type c3,
11254 enum isl_dim_type c4, enum isl_dim_type c5)
11256 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11257 struct isl_mat *mat;
11258 int i, j, k;
11259 int pos;
11261 if (!bmap)
11262 return NULL;
11263 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11264 isl_basic_map_total_dim(bmap) + 1);
11265 if (!mat)
11266 return NULL;
11267 for (i = 0; i < bmap->n_eq; ++i)
11268 for (j = 0, pos = 0; j < 5; ++j) {
11269 int off = isl_basic_map_offset(bmap, c[j]);
11270 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11271 isl_int_set(mat->row[i][pos],
11272 bmap->eq[i][off + k]);
11273 ++pos;
11277 return mat;
11280 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11281 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11282 enum isl_dim_type c2, enum isl_dim_type c3,
11283 enum isl_dim_type c4, enum isl_dim_type c5)
11285 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11286 struct isl_mat *mat;
11287 int i, j, k;
11288 int pos;
11290 if (!bmap)
11291 return NULL;
11292 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11293 isl_basic_map_total_dim(bmap) + 1);
11294 if (!mat)
11295 return NULL;
11296 for (i = 0; i < bmap->n_ineq; ++i)
11297 for (j = 0, pos = 0; j < 5; ++j) {
11298 int off = isl_basic_map_offset(bmap, c[j]);
11299 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11300 isl_int_set(mat->row[i][pos],
11301 bmap->ineq[i][off + k]);
11302 ++pos;
11306 return mat;
11309 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11310 __isl_take isl_space *dim,
11311 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11312 enum isl_dim_type c2, enum isl_dim_type c3,
11313 enum isl_dim_type c4, enum isl_dim_type c5)
11315 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11316 isl_basic_map *bmap;
11317 unsigned total;
11318 unsigned extra;
11319 int i, j, k, l;
11320 int pos;
11322 if (!dim || !eq || !ineq)
11323 goto error;
11325 if (eq->n_col != ineq->n_col)
11326 isl_die(dim->ctx, isl_error_invalid,
11327 "equalities and inequalities matrices should have "
11328 "same number of columns", goto error);
11330 total = 1 + isl_space_dim(dim, isl_dim_all);
11332 if (eq->n_col < total)
11333 isl_die(dim->ctx, isl_error_invalid,
11334 "number of columns too small", goto error);
11336 extra = eq->n_col - total;
11338 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11339 eq->n_row, ineq->n_row);
11340 if (!bmap)
11341 goto error;
11342 for (i = 0; i < extra; ++i) {
11343 k = isl_basic_map_alloc_div(bmap);
11344 if (k < 0)
11345 goto error;
11346 isl_int_set_si(bmap->div[k][0], 0);
11348 for (i = 0; i < eq->n_row; ++i) {
11349 l = isl_basic_map_alloc_equality(bmap);
11350 if (l < 0)
11351 goto error;
11352 for (j = 0, pos = 0; j < 5; ++j) {
11353 int off = isl_basic_map_offset(bmap, c[j]);
11354 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11355 isl_int_set(bmap->eq[l][off + k],
11356 eq->row[i][pos]);
11357 ++pos;
11361 for (i = 0; i < ineq->n_row; ++i) {
11362 l = isl_basic_map_alloc_inequality(bmap);
11363 if (l < 0)
11364 goto error;
11365 for (j = 0, pos = 0; j < 5; ++j) {
11366 int off = isl_basic_map_offset(bmap, c[j]);
11367 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11368 isl_int_set(bmap->ineq[l][off + k],
11369 ineq->row[i][pos]);
11370 ++pos;
11375 isl_space_free(dim);
11376 isl_mat_free(eq);
11377 isl_mat_free(ineq);
11379 bmap = isl_basic_map_simplify(bmap);
11380 return isl_basic_map_finalize(bmap);
11381 error:
11382 isl_space_free(dim);
11383 isl_mat_free(eq);
11384 isl_mat_free(ineq);
11385 return NULL;
11388 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11389 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11390 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11392 return isl_basic_map_equalities_matrix(bset_to_bmap(bset),
11393 c1, c2, c3, c4, isl_dim_in);
11396 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11397 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11398 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11400 return isl_basic_map_inequalities_matrix(bset_to_bmap(bset),
11401 c1, c2, c3, c4, isl_dim_in);
11404 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11405 __isl_take isl_space *dim,
11406 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11407 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11409 isl_basic_map *bmap;
11410 bmap = isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11411 c1, c2, c3, c4, isl_dim_in);
11412 return bset_from_bmap(bmap);
11415 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11417 if (!bmap)
11418 return isl_bool_error;
11420 return isl_space_can_zip(bmap->dim);
11423 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11425 if (!map)
11426 return isl_bool_error;
11428 return isl_space_can_zip(map->dim);
11431 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11432 * (A -> C) -> (B -> D).
11434 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11436 unsigned pos;
11437 unsigned n1;
11438 unsigned n2;
11440 if (!bmap)
11441 return NULL;
11443 if (!isl_basic_map_can_zip(bmap))
11444 isl_die(bmap->ctx, isl_error_invalid,
11445 "basic map cannot be zipped", goto error);
11446 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11447 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11448 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11449 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11450 bmap = isl_basic_map_cow(bmap);
11451 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11452 if (!bmap)
11453 return NULL;
11454 bmap->dim = isl_space_zip(bmap->dim);
11455 if (!bmap->dim)
11456 goto error;
11457 bmap = isl_basic_map_mark_final(bmap);
11458 return bmap;
11459 error:
11460 isl_basic_map_free(bmap);
11461 return NULL;
11464 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11465 * (A -> C) -> (B -> D).
11467 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11469 int i;
11471 if (!map)
11472 return NULL;
11474 if (!isl_map_can_zip(map))
11475 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11476 goto error);
11478 map = isl_map_cow(map);
11479 if (!map)
11480 return NULL;
11482 for (i = 0; i < map->n; ++i) {
11483 map->p[i] = isl_basic_map_zip(map->p[i]);
11484 if (!map->p[i])
11485 goto error;
11488 map->dim = isl_space_zip(map->dim);
11489 if (!map->dim)
11490 goto error;
11492 return map;
11493 error:
11494 isl_map_free(map);
11495 return NULL;
11498 /* Can we apply isl_basic_map_curry to "bmap"?
11499 * That is, does it have a nested relation in its domain?
11501 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11503 if (!bmap)
11504 return isl_bool_error;
11506 return isl_space_can_curry(bmap->dim);
11509 /* Can we apply isl_map_curry to "map"?
11510 * That is, does it have a nested relation in its domain?
11512 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11514 if (!map)
11515 return isl_bool_error;
11517 return isl_space_can_curry(map->dim);
11520 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11521 * A -> (B -> C).
11523 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11526 if (!bmap)
11527 return NULL;
11529 if (!isl_basic_map_can_curry(bmap))
11530 isl_die(bmap->ctx, isl_error_invalid,
11531 "basic map cannot be curried", goto error);
11532 bmap = isl_basic_map_cow(bmap);
11533 if (!bmap)
11534 return NULL;
11535 bmap->dim = isl_space_curry(bmap->dim);
11536 if (!bmap->dim)
11537 goto error;
11538 bmap = isl_basic_map_mark_final(bmap);
11539 return bmap;
11540 error:
11541 isl_basic_map_free(bmap);
11542 return NULL;
11545 /* Given a map (A -> B) -> C, return the corresponding map
11546 * A -> (B -> C).
11548 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11550 return isl_map_change_space(map, &isl_map_can_curry,
11551 "map cannot be curried", &isl_space_curry);
11554 /* Can isl_map_range_curry be applied to "map"?
11555 * That is, does it have a nested relation in its range,
11556 * the domain of which is itself a nested relation?
11558 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11560 if (!map)
11561 return isl_bool_error;
11563 return isl_space_can_range_curry(map->dim);
11566 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11567 * A -> (B -> (C -> D)).
11569 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11571 return isl_map_change_space(map, &isl_map_can_range_curry,
11572 "map range cannot be curried",
11573 &isl_space_range_curry);
11576 /* Can we apply isl_basic_map_uncurry to "bmap"?
11577 * That is, does it have a nested relation in its domain?
11579 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11581 if (!bmap)
11582 return isl_bool_error;
11584 return isl_space_can_uncurry(bmap->dim);
11587 /* Can we apply isl_map_uncurry to "map"?
11588 * That is, does it have a nested relation in its domain?
11590 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11592 if (!map)
11593 return isl_bool_error;
11595 return isl_space_can_uncurry(map->dim);
11598 /* Given a basic map A -> (B -> C), return the corresponding basic map
11599 * (A -> B) -> C.
11601 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11604 if (!bmap)
11605 return NULL;
11607 if (!isl_basic_map_can_uncurry(bmap))
11608 isl_die(bmap->ctx, isl_error_invalid,
11609 "basic map cannot be uncurried",
11610 return isl_basic_map_free(bmap));
11611 bmap = isl_basic_map_cow(bmap);
11612 if (!bmap)
11613 return NULL;
11614 bmap->dim = isl_space_uncurry(bmap->dim);
11615 if (!bmap->dim)
11616 return isl_basic_map_free(bmap);
11617 bmap = isl_basic_map_mark_final(bmap);
11618 return bmap;
11621 /* Given a map A -> (B -> C), return the corresponding map
11622 * (A -> B) -> C.
11624 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11626 return isl_map_change_space(map, &isl_map_can_uncurry,
11627 "map cannot be uncurried", &isl_space_uncurry);
11630 /* Construct a basic map mapping the domain of the affine expression
11631 * to a one-dimensional range prescribed by the affine expression.
11632 * If "rational" is set, then construct a rational basic map.
11634 * A NaN affine expression cannot be converted to a basic map.
11636 static __isl_give isl_basic_map *isl_basic_map_from_aff2(
11637 __isl_take isl_aff *aff, int rational)
11639 int k;
11640 int pos;
11641 isl_bool is_nan;
11642 isl_local_space *ls;
11643 isl_basic_map *bmap = NULL;
11645 if (!aff)
11646 return NULL;
11647 is_nan = isl_aff_is_nan(aff);
11648 if (is_nan < 0)
11649 goto error;
11650 if (is_nan)
11651 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11652 "cannot convert NaN", goto error);
11654 ls = isl_aff_get_local_space(aff);
11655 bmap = isl_basic_map_from_local_space(ls);
11656 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11657 k = isl_basic_map_alloc_equality(bmap);
11658 if (k < 0)
11659 goto error;
11661 pos = isl_basic_map_offset(bmap, isl_dim_out);
11662 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11663 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11664 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11665 aff->v->size - (pos + 1));
11667 isl_aff_free(aff);
11668 if (rational)
11669 bmap = isl_basic_map_set_rational(bmap);
11670 bmap = isl_basic_map_finalize(bmap);
11671 return bmap;
11672 error:
11673 isl_aff_free(aff);
11674 isl_basic_map_free(bmap);
11675 return NULL;
11678 /* Construct a basic map mapping the domain of the affine expression
11679 * to a one-dimensional range prescribed by the affine expression.
11681 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11683 return isl_basic_map_from_aff2(aff, 0);
11686 /* Construct a map mapping the domain of the affine expression
11687 * to a one-dimensional range prescribed by the affine expression.
11689 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11691 isl_basic_map *bmap;
11693 bmap = isl_basic_map_from_aff(aff);
11694 return isl_map_from_basic_map(bmap);
11697 /* Construct a basic map mapping the domain the multi-affine expression
11698 * to its range, with each dimension in the range equated to the
11699 * corresponding affine expression.
11700 * If "rational" is set, then construct a rational basic map.
11702 __isl_give isl_basic_map *isl_basic_map_from_multi_aff2(
11703 __isl_take isl_multi_aff *maff, int rational)
11705 int i;
11706 isl_space *space;
11707 isl_basic_map *bmap;
11709 if (!maff)
11710 return NULL;
11712 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11713 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11714 "invalid space", goto error);
11716 space = isl_space_domain(isl_multi_aff_get_space(maff));
11717 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11718 if (rational)
11719 bmap = isl_basic_map_set_rational(bmap);
11721 for (i = 0; i < maff->n; ++i) {
11722 isl_aff *aff;
11723 isl_basic_map *bmap_i;
11725 aff = isl_aff_copy(maff->p[i]);
11726 bmap_i = isl_basic_map_from_aff2(aff, rational);
11728 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11731 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11733 isl_multi_aff_free(maff);
11734 return bmap;
11735 error:
11736 isl_multi_aff_free(maff);
11737 return NULL;
11740 /* Construct a basic map mapping the domain the multi-affine expression
11741 * to its range, with each dimension in the range equated to the
11742 * corresponding affine expression.
11744 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11745 __isl_take isl_multi_aff *ma)
11747 return isl_basic_map_from_multi_aff2(ma, 0);
11750 /* Construct a map mapping the domain the multi-affine expression
11751 * to its range, with each dimension in the range equated to the
11752 * corresponding affine expression.
11754 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11756 isl_basic_map *bmap;
11758 bmap = isl_basic_map_from_multi_aff(maff);
11759 return isl_map_from_basic_map(bmap);
11762 /* Construct a basic map mapping a domain in the given space to
11763 * to an n-dimensional range, with n the number of elements in the list,
11764 * where each coordinate in the range is prescribed by the
11765 * corresponding affine expression.
11766 * The domains of all affine expressions in the list are assumed to match
11767 * domain_dim.
11769 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11770 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11772 int i;
11773 isl_space *dim;
11774 isl_basic_map *bmap;
11776 if (!list)
11777 return NULL;
11779 dim = isl_space_from_domain(domain_dim);
11780 bmap = isl_basic_map_universe(dim);
11782 for (i = 0; i < list->n; ++i) {
11783 isl_aff *aff;
11784 isl_basic_map *bmap_i;
11786 aff = isl_aff_copy(list->p[i]);
11787 bmap_i = isl_basic_map_from_aff(aff);
11789 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11792 isl_aff_list_free(list);
11793 return bmap;
11796 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11797 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11799 return isl_map_equate(set, type1, pos1, type2, pos2);
11802 /* Construct a basic map where the given dimensions are equal to each other.
11804 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11805 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11807 isl_basic_map *bmap = NULL;
11808 int i;
11810 if (!space)
11811 return NULL;
11813 if (pos1 >= isl_space_dim(space, type1))
11814 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11815 "index out of bounds", goto error);
11816 if (pos2 >= isl_space_dim(space, type2))
11817 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11818 "index out of bounds", goto error);
11820 if (type1 == type2 && pos1 == pos2)
11821 return isl_basic_map_universe(space);
11823 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11824 i = isl_basic_map_alloc_equality(bmap);
11825 if (i < 0)
11826 goto error;
11827 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11828 pos1 += isl_basic_map_offset(bmap, type1);
11829 pos2 += isl_basic_map_offset(bmap, type2);
11830 isl_int_set_si(bmap->eq[i][pos1], -1);
11831 isl_int_set_si(bmap->eq[i][pos2], 1);
11832 bmap = isl_basic_map_finalize(bmap);
11833 isl_space_free(space);
11834 return bmap;
11835 error:
11836 isl_space_free(space);
11837 isl_basic_map_free(bmap);
11838 return NULL;
11841 /* Add a constraint imposing that the given two dimensions are equal.
11843 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11844 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11846 isl_basic_map *eq;
11848 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11850 bmap = isl_basic_map_intersect(bmap, eq);
11852 return bmap;
11855 /* Add a constraint imposing that the given two dimensions are equal.
11857 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11858 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11860 isl_basic_map *bmap;
11862 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11864 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11866 return map;
11869 /* Add a constraint imposing that the given two dimensions have opposite values.
11871 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
11872 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11874 isl_basic_map *bmap = NULL;
11875 int i;
11877 if (!map)
11878 return NULL;
11880 if (pos1 >= isl_map_dim(map, type1))
11881 isl_die(map->ctx, isl_error_invalid,
11882 "index out of bounds", goto error);
11883 if (pos2 >= isl_map_dim(map, type2))
11884 isl_die(map->ctx, isl_error_invalid,
11885 "index out of bounds", goto error);
11887 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11888 i = isl_basic_map_alloc_equality(bmap);
11889 if (i < 0)
11890 goto error;
11891 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11892 pos1 += isl_basic_map_offset(bmap, type1);
11893 pos2 += isl_basic_map_offset(bmap, type2);
11894 isl_int_set_si(bmap->eq[i][pos1], 1);
11895 isl_int_set_si(bmap->eq[i][pos2], 1);
11896 bmap = isl_basic_map_finalize(bmap);
11898 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11900 return map;
11901 error:
11902 isl_basic_map_free(bmap);
11903 isl_map_free(map);
11904 return NULL;
11907 /* Construct a constraint imposing that the value of the first dimension is
11908 * greater than or equal to that of the second.
11910 static __isl_give isl_constraint *constraint_order_ge(
11911 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
11912 enum isl_dim_type type2, int pos2)
11914 isl_constraint *c;
11916 if (!space)
11917 return NULL;
11919 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
11921 if (pos1 >= isl_constraint_dim(c, type1))
11922 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11923 "index out of bounds", return isl_constraint_free(c));
11924 if (pos2 >= isl_constraint_dim(c, type2))
11925 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
11926 "index out of bounds", return isl_constraint_free(c));
11928 if (type1 == type2 && pos1 == pos2)
11929 return c;
11931 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11932 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11934 return c;
11937 /* Add a constraint imposing that the value of the first dimension is
11938 * greater than or equal to that of the second.
11940 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11941 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11943 isl_constraint *c;
11944 isl_space *space;
11946 if (type1 == type2 && pos1 == pos2)
11947 return bmap;
11948 space = isl_basic_map_get_space(bmap);
11949 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11950 bmap = isl_basic_map_add_constraint(bmap, c);
11952 return bmap;
11955 /* Add a constraint imposing that the value of the first dimension is
11956 * greater than or equal to that of the second.
11958 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
11959 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11961 isl_constraint *c;
11962 isl_space *space;
11964 if (type1 == type2 && pos1 == pos2)
11965 return map;
11966 space = isl_map_get_space(map);
11967 c = constraint_order_ge(space, type1, pos1, type2, pos2);
11968 map = isl_map_add_constraint(map, c);
11970 return map;
11973 /* Add a constraint imposing that the value of the first dimension is
11974 * less than or equal to that of the second.
11976 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
11977 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11979 return isl_map_order_ge(map, type2, pos2, type1, pos1);
11982 /* Construct a basic map where the value of the first dimension is
11983 * greater than that of the second.
11985 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11986 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11988 isl_basic_map *bmap = NULL;
11989 int i;
11991 if (!space)
11992 return NULL;
11994 if (pos1 >= isl_space_dim(space, type1))
11995 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11996 "index out of bounds", goto error);
11997 if (pos2 >= isl_space_dim(space, type2))
11998 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11999 "index out of bounds", goto error);
12001 if (type1 == type2 && pos1 == pos2)
12002 return isl_basic_map_empty(space);
12004 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12005 i = isl_basic_map_alloc_inequality(bmap);
12006 if (i < 0)
12007 return isl_basic_map_free(bmap);
12008 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12009 pos1 += isl_basic_map_offset(bmap, type1);
12010 pos2 += isl_basic_map_offset(bmap, type2);
12011 isl_int_set_si(bmap->ineq[i][pos1], 1);
12012 isl_int_set_si(bmap->ineq[i][pos2], -1);
12013 isl_int_set_si(bmap->ineq[i][0], -1);
12014 bmap = isl_basic_map_finalize(bmap);
12016 return bmap;
12017 error:
12018 isl_space_free(space);
12019 isl_basic_map_free(bmap);
12020 return NULL;
12023 /* Add a constraint imposing that the value of the first dimension is
12024 * greater than that of the second.
12026 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12027 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12029 isl_basic_map *gt;
12031 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12033 bmap = isl_basic_map_intersect(bmap, gt);
12035 return bmap;
12038 /* Add a constraint imposing that the value of the first dimension is
12039 * greater than that of the second.
12041 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12042 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12044 isl_basic_map *bmap;
12046 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12048 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12050 return map;
12053 /* Add a constraint imposing that the value of the first dimension is
12054 * smaller than that of the second.
12056 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12057 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12059 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12062 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12063 int pos)
12065 isl_aff *div;
12066 isl_local_space *ls;
12068 if (!bmap)
12069 return NULL;
12071 if (!isl_basic_map_divs_known(bmap))
12072 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12073 "some divs are unknown", return NULL);
12075 ls = isl_basic_map_get_local_space(bmap);
12076 div = isl_local_space_get_div(ls, pos);
12077 isl_local_space_free(ls);
12079 return div;
12082 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12083 int pos)
12085 return isl_basic_map_get_div(bset, pos);
12088 /* Plug in "subs" for dimension "type", "pos" of "bset".
12090 * Let i be the dimension to replace and let "subs" be of the form
12092 * f/d
12094 * Any integer division with a non-zero coefficient for i,
12096 * floor((a i + g)/m)
12098 * is replaced by
12100 * floor((a f + d g)/(m d))
12102 * Constraints of the form
12104 * a i + g
12106 * are replaced by
12108 * a f + d g
12110 * We currently require that "subs" is an integral expression.
12111 * Handling rational expressions may require us to add stride constraints
12112 * as we do in isl_basic_set_preimage_multi_aff.
12114 __isl_give isl_basic_set *isl_basic_set_substitute(
12115 __isl_take isl_basic_set *bset,
12116 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12118 int i;
12119 isl_int v;
12120 isl_ctx *ctx;
12122 if (bset && isl_basic_set_plain_is_empty(bset))
12123 return bset;
12125 bset = isl_basic_set_cow(bset);
12126 if (!bset || !subs)
12127 goto error;
12129 ctx = isl_basic_set_get_ctx(bset);
12130 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12131 isl_die(ctx, isl_error_invalid,
12132 "spaces don't match", goto error);
12133 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12134 isl_die(ctx, isl_error_unsupported,
12135 "cannot handle divs yet", goto error);
12136 if (!isl_int_is_one(subs->v->el[0]))
12137 isl_die(ctx, isl_error_invalid,
12138 "can only substitute integer expressions", goto error);
12140 pos += isl_basic_set_offset(bset, type);
12142 isl_int_init(v);
12144 for (i = 0; i < bset->n_eq; ++i) {
12145 if (isl_int_is_zero(bset->eq[i][pos]))
12146 continue;
12147 isl_int_set(v, bset->eq[i][pos]);
12148 isl_int_set_si(bset->eq[i][pos], 0);
12149 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12150 v, subs->v->el + 1, subs->v->size - 1);
12153 for (i = 0; i < bset->n_ineq; ++i) {
12154 if (isl_int_is_zero(bset->ineq[i][pos]))
12155 continue;
12156 isl_int_set(v, bset->ineq[i][pos]);
12157 isl_int_set_si(bset->ineq[i][pos], 0);
12158 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12159 v, subs->v->el + 1, subs->v->size - 1);
12162 for (i = 0; i < bset->n_div; ++i) {
12163 if (isl_int_is_zero(bset->div[i][1 + pos]))
12164 continue;
12165 isl_int_set(v, bset->div[i][1 + pos]);
12166 isl_int_set_si(bset->div[i][1 + pos], 0);
12167 isl_seq_combine(bset->div[i] + 1,
12168 subs->v->el[0], bset->div[i] + 1,
12169 v, subs->v->el + 1, subs->v->size - 1);
12170 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12173 isl_int_clear(v);
12175 bset = isl_basic_set_simplify(bset);
12176 return isl_basic_set_finalize(bset);
12177 error:
12178 isl_basic_set_free(bset);
12179 return NULL;
12182 /* Plug in "subs" for dimension "type", "pos" of "set".
12184 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12185 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12187 int i;
12189 if (set && isl_set_plain_is_empty(set))
12190 return set;
12192 set = isl_set_cow(set);
12193 if (!set || !subs)
12194 goto error;
12196 for (i = set->n - 1; i >= 0; --i) {
12197 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12198 if (remove_if_empty(set, i) < 0)
12199 goto error;
12202 return set;
12203 error:
12204 isl_set_free(set);
12205 return NULL;
12208 /* Check if the range of "ma" is compatible with the domain or range
12209 * (depending on "type") of "bmap".
12211 static isl_stat check_basic_map_compatible_range_multi_aff(
12212 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12213 __isl_keep isl_multi_aff *ma)
12215 isl_bool m;
12216 isl_space *ma_space;
12218 ma_space = isl_multi_aff_get_space(ma);
12220 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12221 if (m < 0)
12222 goto error;
12223 if (!m)
12224 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12225 "parameters don't match", goto error);
12226 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12227 if (m < 0)
12228 goto error;
12229 if (!m)
12230 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12231 "spaces don't match", goto error);
12233 isl_space_free(ma_space);
12234 return isl_stat_ok;
12235 error:
12236 isl_space_free(ma_space);
12237 return isl_stat_error;
12240 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12241 * coefficients before the transformed range of dimensions,
12242 * the "n_after" coefficients after the transformed range of dimensions
12243 * and the coefficients of the other divs in "bmap".
12245 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12246 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12248 int i;
12249 int n_param;
12250 int n_set;
12251 isl_local_space *ls;
12253 if (n_div == 0)
12254 return 0;
12256 ls = isl_aff_get_domain_local_space(ma->p[0]);
12257 if (!ls)
12258 return -1;
12260 n_param = isl_local_space_dim(ls, isl_dim_param);
12261 n_set = isl_local_space_dim(ls, isl_dim_set);
12262 for (i = 0; i < n_div; ++i) {
12263 int o_bmap = 0, o_ls = 0;
12265 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12266 o_bmap += 1 + 1 + n_param;
12267 o_ls += 1 + 1 + n_param;
12268 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12269 o_bmap += n_before;
12270 isl_seq_cpy(bmap->div[i] + o_bmap,
12271 ls->div->row[i] + o_ls, n_set);
12272 o_bmap += n_set;
12273 o_ls += n_set;
12274 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12275 o_bmap += n_after;
12276 isl_seq_cpy(bmap->div[i] + o_bmap,
12277 ls->div->row[i] + o_ls, n_div);
12278 o_bmap += n_div;
12279 o_ls += n_div;
12280 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12281 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
12282 goto error;
12285 isl_local_space_free(ls);
12286 return 0;
12287 error:
12288 isl_local_space_free(ls);
12289 return -1;
12292 /* How many stride constraints does "ma" enforce?
12293 * That is, how many of the affine expressions have a denominator
12294 * different from one?
12296 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12298 int i;
12299 int strides = 0;
12301 for (i = 0; i < ma->n; ++i)
12302 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12303 strides++;
12305 return strides;
12308 /* For each affine expression in ma of the form
12310 * x_i = (f_i y + h_i)/m_i
12312 * with m_i different from one, add a constraint to "bmap"
12313 * of the form
12315 * f_i y + h_i = m_i alpha_i
12317 * with alpha_i an additional existentially quantified variable.
12319 * The input variables of "ma" correspond to a subset of the variables
12320 * of "bmap". There are "n_before" variables in "bmap" before this
12321 * subset and "n_after" variables after this subset.
12322 * The integer divisions of the affine expressions in "ma" are assumed
12323 * to have been aligned. There are "n_div_ma" of them and
12324 * they appear first in "bmap", straight after the "n_after" variables.
12326 static __isl_give isl_basic_map *add_ma_strides(
12327 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12328 int n_before, int n_after, int n_div_ma)
12330 int i, k;
12331 int div;
12332 int total;
12333 int n_param;
12334 int n_in;
12336 total = isl_basic_map_total_dim(bmap);
12337 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12338 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12339 for (i = 0; i < ma->n; ++i) {
12340 int o_bmap = 0, o_ma = 1;
12342 if (isl_int_is_one(ma->p[i]->v->el[0]))
12343 continue;
12344 div = isl_basic_map_alloc_div(bmap);
12345 k = isl_basic_map_alloc_equality(bmap);
12346 if (div < 0 || k < 0)
12347 goto error;
12348 isl_int_set_si(bmap->div[div][0], 0);
12349 isl_seq_cpy(bmap->eq[k] + o_bmap,
12350 ma->p[i]->v->el + o_ma, 1 + n_param);
12351 o_bmap += 1 + n_param;
12352 o_ma += 1 + n_param;
12353 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12354 o_bmap += n_before;
12355 isl_seq_cpy(bmap->eq[k] + o_bmap,
12356 ma->p[i]->v->el + o_ma, n_in);
12357 o_bmap += n_in;
12358 o_ma += n_in;
12359 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12360 o_bmap += n_after;
12361 isl_seq_cpy(bmap->eq[k] + o_bmap,
12362 ma->p[i]->v->el + o_ma, n_div_ma);
12363 o_bmap += n_div_ma;
12364 o_ma += n_div_ma;
12365 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12366 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12367 total++;
12370 return bmap;
12371 error:
12372 isl_basic_map_free(bmap);
12373 return NULL;
12376 /* Replace the domain or range space (depending on "type) of "space" by "set".
12378 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12379 enum isl_dim_type type, __isl_take isl_space *set)
12381 if (type == isl_dim_in) {
12382 space = isl_space_range(space);
12383 space = isl_space_map_from_domain_and_range(set, space);
12384 } else {
12385 space = isl_space_domain(space);
12386 space = isl_space_map_from_domain_and_range(space, set);
12389 return space;
12392 /* Compute the preimage of the domain or range (depending on "type")
12393 * of "bmap" under the function represented by "ma".
12394 * In other words, plug in "ma" in the domain or range of "bmap".
12395 * The result is a basic map that lives in the same space as "bmap"
12396 * except that the domain or range has been replaced by
12397 * the domain space of "ma".
12399 * If bmap is represented by
12401 * A(p) + S u + B x + T v + C(divs) >= 0,
12403 * where u and x are input and output dimensions if type == isl_dim_out
12404 * while x and v are input and output dimensions if type == isl_dim_in,
12405 * and ma is represented by
12407 * x = D(p) + F(y) + G(divs')
12409 * then the result is
12411 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12413 * The divs in the input set are similarly adjusted.
12414 * In particular
12416 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12418 * becomes
12420 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12421 * B_i G(divs') + c_i(divs))/n_i)
12423 * If bmap is not a rational map and if F(y) involves any denominators
12425 * x_i = (f_i y + h_i)/m_i
12427 * then additional constraints are added to ensure that we only
12428 * map back integer points. That is we enforce
12430 * f_i y + h_i = m_i alpha_i
12432 * with alpha_i an additional existentially quantified variable.
12434 * We first copy over the divs from "ma".
12435 * Then we add the modified constraints and divs from "bmap".
12436 * Finally, we add the stride constraints, if needed.
12438 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12439 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12440 __isl_take isl_multi_aff *ma)
12442 int i, k;
12443 isl_space *space;
12444 isl_basic_map *res = NULL;
12445 int n_before, n_after, n_div_bmap, n_div_ma;
12446 isl_int f, c1, c2, g;
12447 isl_bool rational;
12448 int strides;
12450 isl_int_init(f);
12451 isl_int_init(c1);
12452 isl_int_init(c2);
12453 isl_int_init(g);
12455 ma = isl_multi_aff_align_divs(ma);
12456 if (!bmap || !ma)
12457 goto error;
12458 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12459 goto error;
12461 if (type == isl_dim_in) {
12462 n_before = 0;
12463 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12464 } else {
12465 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12466 n_after = 0;
12468 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12469 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12471 space = isl_multi_aff_get_domain_space(ma);
12472 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12473 rational = isl_basic_map_is_rational(bmap);
12474 strides = rational ? 0 : multi_aff_strides(ma);
12475 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12476 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12477 if (rational)
12478 res = isl_basic_map_set_rational(res);
12480 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12481 if (isl_basic_map_alloc_div(res) < 0)
12482 goto error;
12484 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12485 goto error;
12487 for (i = 0; i < bmap->n_eq; ++i) {
12488 k = isl_basic_map_alloc_equality(res);
12489 if (k < 0)
12490 goto error;
12491 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12492 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12495 for (i = 0; i < bmap->n_ineq; ++i) {
12496 k = isl_basic_map_alloc_inequality(res);
12497 if (k < 0)
12498 goto error;
12499 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12500 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12503 for (i = 0; i < bmap->n_div; ++i) {
12504 if (isl_int_is_zero(bmap->div[i][0])) {
12505 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12506 continue;
12508 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12509 n_before, n_after, n_div_ma, n_div_bmap,
12510 f, c1, c2, g, 1);
12513 if (strides)
12514 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12516 isl_int_clear(f);
12517 isl_int_clear(c1);
12518 isl_int_clear(c2);
12519 isl_int_clear(g);
12520 isl_basic_map_free(bmap);
12521 isl_multi_aff_free(ma);
12522 res = isl_basic_map_simplify(res);
12523 return isl_basic_map_finalize(res);
12524 error:
12525 isl_int_clear(f);
12526 isl_int_clear(c1);
12527 isl_int_clear(c2);
12528 isl_int_clear(g);
12529 isl_basic_map_free(bmap);
12530 isl_multi_aff_free(ma);
12531 isl_basic_map_free(res);
12532 return NULL;
12535 /* Compute the preimage of "bset" under the function represented by "ma".
12536 * In other words, plug in "ma" in "bset". The result is a basic set
12537 * that lives in the domain space of "ma".
12539 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12540 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12542 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12545 /* Compute the preimage of the domain of "bmap" under the function
12546 * represented by "ma".
12547 * In other words, plug in "ma" in the domain of "bmap".
12548 * The result is a basic map that lives in the same space as "bmap"
12549 * except that the domain has been replaced by the domain space of "ma".
12551 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12552 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12554 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12557 /* Compute the preimage of the range of "bmap" under the function
12558 * represented by "ma".
12559 * In other words, plug in "ma" in the range of "bmap".
12560 * The result is a basic map that lives in the same space as "bmap"
12561 * except that the range has been replaced by the domain space of "ma".
12563 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12564 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12566 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12569 /* Check if the range of "ma" is compatible with the domain or range
12570 * (depending on "type") of "map".
12571 * Return isl_stat_error if anything is wrong.
12573 static isl_stat check_map_compatible_range_multi_aff(
12574 __isl_keep isl_map *map, enum isl_dim_type type,
12575 __isl_keep isl_multi_aff *ma)
12577 isl_bool m;
12578 isl_space *ma_space;
12580 ma_space = isl_multi_aff_get_space(ma);
12581 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12582 isl_space_free(ma_space);
12583 if (m < 0)
12584 return isl_stat_error;
12585 if (!m)
12586 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12587 "spaces don't match", return isl_stat_error);
12588 return isl_stat_ok;
12591 /* Compute the preimage of the domain or range (depending on "type")
12592 * of "map" under the function represented by "ma".
12593 * In other words, plug in "ma" in the domain or range of "map".
12594 * The result is a map that lives in the same space as "map"
12595 * except that the domain or range has been replaced by
12596 * the domain space of "ma".
12598 * The parameters are assumed to have been aligned.
12600 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12601 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12603 int i;
12604 isl_space *space;
12606 map = isl_map_cow(map);
12607 ma = isl_multi_aff_align_divs(ma);
12608 if (!map || !ma)
12609 goto error;
12610 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12611 goto error;
12613 for (i = 0; i < map->n; ++i) {
12614 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12615 isl_multi_aff_copy(ma));
12616 if (!map->p[i])
12617 goto error;
12620 space = isl_multi_aff_get_domain_space(ma);
12621 space = isl_space_set(isl_map_get_space(map), type, space);
12623 isl_space_free(map->dim);
12624 map->dim = space;
12625 if (!map->dim)
12626 goto error;
12628 isl_multi_aff_free(ma);
12629 if (map->n > 1)
12630 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12631 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12632 return map;
12633 error:
12634 isl_multi_aff_free(ma);
12635 isl_map_free(map);
12636 return NULL;
12639 /* Compute the preimage of the domain or range (depending on "type")
12640 * of "map" under the function represented by "ma".
12641 * In other words, plug in "ma" in the domain or range of "map".
12642 * The result is a map that lives in the same space as "map"
12643 * except that the domain or range has been replaced by
12644 * the domain space of "ma".
12646 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12647 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12649 if (!map || !ma)
12650 goto error;
12652 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12653 return map_preimage_multi_aff(map, type, ma);
12655 if (isl_map_check_named_params(map) < 0)
12656 goto error;
12657 if (!isl_space_has_named_params(ma->space))
12658 isl_die(map->ctx, isl_error_invalid,
12659 "unaligned unnamed parameters", goto error);
12660 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12661 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12663 return map_preimage_multi_aff(map, type, ma);
12664 error:
12665 isl_multi_aff_free(ma);
12666 return isl_map_free(map);
12669 /* Compute the preimage of "set" under the function represented by "ma".
12670 * In other words, plug in "ma" in "set". The result is a set
12671 * that lives in the domain space of "ma".
12673 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12674 __isl_take isl_multi_aff *ma)
12676 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12679 /* Compute the preimage of the domain of "map" under the function
12680 * represented by "ma".
12681 * In other words, plug in "ma" in the domain of "map".
12682 * The result is a map that lives in the same space as "map"
12683 * except that the domain has been replaced by the domain space of "ma".
12685 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12686 __isl_take isl_multi_aff *ma)
12688 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12691 /* Compute the preimage of the range of "map" under the function
12692 * represented by "ma".
12693 * In other words, plug in "ma" in the range of "map".
12694 * The result is a map that lives in the same space as "map"
12695 * except that the range has been replaced by the domain space of "ma".
12697 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12698 __isl_take isl_multi_aff *ma)
12700 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12703 /* Compute the preimage of "map" under the function represented by "pma".
12704 * In other words, plug in "pma" in the domain or range of "map".
12705 * The result is a map that lives in the same space as "map",
12706 * except that the space of type "type" has been replaced by
12707 * the domain space of "pma".
12709 * The parameters of "map" and "pma" are assumed to have been aligned.
12711 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12712 __isl_take isl_map *map, enum isl_dim_type type,
12713 __isl_take isl_pw_multi_aff *pma)
12715 int i;
12716 isl_map *res;
12718 if (!pma)
12719 goto error;
12721 if (pma->n == 0) {
12722 isl_pw_multi_aff_free(pma);
12723 res = isl_map_empty(isl_map_get_space(map));
12724 isl_map_free(map);
12725 return res;
12728 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12729 isl_multi_aff_copy(pma->p[0].maff));
12730 if (type == isl_dim_in)
12731 res = isl_map_intersect_domain(res,
12732 isl_map_copy(pma->p[0].set));
12733 else
12734 res = isl_map_intersect_range(res,
12735 isl_map_copy(pma->p[0].set));
12737 for (i = 1; i < pma->n; ++i) {
12738 isl_map *res_i;
12740 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12741 isl_multi_aff_copy(pma->p[i].maff));
12742 if (type == isl_dim_in)
12743 res_i = isl_map_intersect_domain(res_i,
12744 isl_map_copy(pma->p[i].set));
12745 else
12746 res_i = isl_map_intersect_range(res_i,
12747 isl_map_copy(pma->p[i].set));
12748 res = isl_map_union(res, res_i);
12751 isl_pw_multi_aff_free(pma);
12752 isl_map_free(map);
12753 return res;
12754 error:
12755 isl_pw_multi_aff_free(pma);
12756 isl_map_free(map);
12757 return NULL;
12760 /* Compute the preimage of "map" under the function represented by "pma".
12761 * In other words, plug in "pma" in the domain or range of "map".
12762 * The result is a map that lives in the same space as "map",
12763 * except that the space of type "type" has been replaced by
12764 * the domain space of "pma".
12766 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12767 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12769 if (!map || !pma)
12770 goto error;
12772 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12773 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12775 if (isl_map_check_named_params(map) < 0)
12776 goto error;
12777 if (!isl_space_has_named_params(pma->dim))
12778 isl_die(map->ctx, isl_error_invalid,
12779 "unaligned unnamed parameters", goto error);
12780 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12781 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12783 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12784 error:
12785 isl_pw_multi_aff_free(pma);
12786 return isl_map_free(map);
12789 /* Compute the preimage of "set" under the function represented by "pma".
12790 * In other words, plug in "pma" in "set". The result is a set
12791 * that lives in the domain space of "pma".
12793 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12794 __isl_take isl_pw_multi_aff *pma)
12796 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12799 /* Compute the preimage of the domain of "map" under the function
12800 * represented by "pma".
12801 * In other words, plug in "pma" in the domain of "map".
12802 * The result is a map that lives in the same space as "map",
12803 * except that domain space has been replaced by the domain space of "pma".
12805 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12806 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12808 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12811 /* Compute the preimage of the range of "map" under the function
12812 * represented by "pma".
12813 * In other words, plug in "pma" in the range of "map".
12814 * The result is a map that lives in the same space as "map",
12815 * except that range space has been replaced by the domain space of "pma".
12817 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12818 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12820 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12823 /* Compute the preimage of "map" under the function represented by "mpa".
12824 * In other words, plug in "mpa" in the domain or range of "map".
12825 * The result is a map that lives in the same space as "map",
12826 * except that the space of type "type" has been replaced by
12827 * the domain space of "mpa".
12829 * If the map does not involve any constraints that refer to the
12830 * dimensions of the substituted space, then the only possible
12831 * effect of "mpa" on the map is to map the space to a different space.
12832 * We create a separate isl_multi_aff to effectuate this change
12833 * in order to avoid spurious splitting of the map along the pieces
12834 * of "mpa".
12836 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12837 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12839 int n;
12840 isl_pw_multi_aff *pma;
12842 if (!map || !mpa)
12843 goto error;
12845 n = isl_map_dim(map, type);
12846 if (!isl_map_involves_dims(map, type, 0, n)) {
12847 isl_space *space;
12848 isl_multi_aff *ma;
12850 space = isl_multi_pw_aff_get_space(mpa);
12851 isl_multi_pw_aff_free(mpa);
12852 ma = isl_multi_aff_zero(space);
12853 return isl_map_preimage_multi_aff(map, type, ma);
12856 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
12857 return isl_map_preimage_pw_multi_aff(map, type, pma);
12858 error:
12859 isl_map_free(map);
12860 isl_multi_pw_aff_free(mpa);
12861 return NULL;
12864 /* Compute the preimage of "map" under the function represented by "mpa".
12865 * In other words, plug in "mpa" in the domain "map".
12866 * The result is a map that lives in the same space as "map",
12867 * except that domain space has been replaced by the domain space of "mpa".
12869 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
12870 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
12872 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
12875 /* Compute the preimage of "set" by the function represented by "mpa".
12876 * In other words, plug in "mpa" in "set".
12878 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
12879 __isl_take isl_multi_pw_aff *mpa)
12881 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);
12884 /* Are the "n" "coefficients" starting at "first" of the integer division
12885 * expressions at position "pos1" in "bmap1" and "pos2" in "bmap2" equal
12886 * to each other?
12887 * The "coefficient" at position 0 is the denominator.
12888 * The "coefficient" at position 1 is the constant term.
12890 isl_bool isl_basic_map_equal_div_expr_part(__isl_keep isl_basic_map *bmap1,
12891 int pos1, __isl_keep isl_basic_map *bmap2, int pos2,
12892 unsigned first, unsigned n)
12894 if (isl_basic_map_check_range(bmap1, isl_dim_div, pos1, 1) < 0)
12895 return isl_bool_error;
12896 if (isl_basic_map_check_range(bmap2, isl_dim_div, pos2, 1) < 0)
12897 return isl_bool_error;
12898 return isl_seq_eq(bmap1->div[pos1] + first,
12899 bmap2->div[pos2] + first, n);
12902 /* Are the integer division expressions at position "pos1" in "bmap1" and
12903 * "pos2" in "bmap2" equal to each other, except that the constant terms
12904 * are different?
12906 isl_bool isl_basic_map_equal_div_expr_except_constant(
12907 __isl_keep isl_basic_map *bmap1, int pos1,
12908 __isl_keep isl_basic_map *bmap2, int pos2)
12910 isl_bool equal;
12911 unsigned total;
12913 if (!bmap1 || !bmap2)
12914 return isl_bool_error;
12915 total = isl_basic_map_total_dim(bmap1);
12916 if (total != isl_basic_map_total_dim(bmap2))
12917 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
12918 "incomparable div expressions", return isl_bool_error);
12919 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12920 0, 1);
12921 if (equal < 0 || !equal)
12922 return equal;
12923 equal = isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12924 1, 1);
12925 if (equal < 0 || equal)
12926 return isl_bool_not(equal);
12927 return isl_basic_map_equal_div_expr_part(bmap1, pos1, bmap2, pos2,
12928 2, total);
12931 /* Replace the numerator of the constant term of the integer division
12932 * expression at position "div" in "bmap" by "value".
12933 * The caller guarantees that this does not change the meaning
12934 * of the input.
12936 __isl_give isl_basic_map *isl_basic_map_set_div_expr_constant_num_si_inplace(
12937 __isl_take isl_basic_map *bmap, int div, int value)
12939 if (isl_basic_map_check_range(bmap, isl_dim_div, div, 1) < 0)
12940 return isl_basic_map_free(bmap);
12942 isl_int_set_si(bmap->div[div][1], value);
12944 return bmap;
12947 /* Is the point "inner" internal to inequality constraint "ineq"
12948 * of "bset"?
12949 * The point is considered to be internal to the inequality constraint,
12950 * if it strictly lies on the positive side of the inequality constraint,
12951 * or if it lies on the constraint and the constraint is lexico-positive.
12953 static isl_bool is_internal(__isl_keep isl_vec *inner,
12954 __isl_keep isl_basic_set *bset, int ineq)
12956 isl_ctx *ctx;
12957 int pos;
12958 unsigned total;
12960 if (!inner || !bset)
12961 return isl_bool_error;
12963 ctx = isl_basic_set_get_ctx(bset);
12964 isl_seq_inner_product(inner->el, bset->ineq[ineq], inner->size,
12965 &ctx->normalize_gcd);
12966 if (!isl_int_is_zero(ctx->normalize_gcd))
12967 return isl_int_is_nonneg(ctx->normalize_gcd);
12969 total = isl_basic_set_dim(bset, isl_dim_all);
12970 pos = isl_seq_first_non_zero(bset->ineq[ineq] + 1, total);
12971 return isl_int_is_pos(bset->ineq[ineq][1 + pos]);
12974 /* Tighten the inequality constraints of "bset" that are outward with respect
12975 * to the point "vec".
12976 * That is, tighten the constraints that are not satisfied by "vec".
12978 * "vec" is a point internal to some superset S of "bset" that is used
12979 * to make the subsets of S disjoint, by tightening one half of the constraints
12980 * that separate two subsets. In particular, the constraints of S
12981 * are all satisfied by "vec" and should not be tightened.
12982 * Of the internal constraints, those that have "vec" on the outside
12983 * are tightened. The shared facet is included in the adjacent subset
12984 * with the opposite constraint.
12985 * For constraints that saturate "vec", this criterion cannot be used
12986 * to determine which of the two sides should be tightened.
12987 * Instead, the sign of the first non-zero coefficient is used
12988 * to make this choice. Note that this second criterion is never used
12989 * on the constraints of S since "vec" is interior to "S".
12991 __isl_give isl_basic_set *isl_basic_set_tighten_outward(
12992 __isl_take isl_basic_set *bset, __isl_keep isl_vec *vec)
12994 int j;
12996 bset = isl_basic_set_cow(bset);
12997 if (!bset)
12998 return NULL;
12999 for (j = 0; j < bset->n_ineq; ++j) {
13000 isl_bool internal;
13002 internal = is_internal(vec, bset, j);
13003 if (internal < 0)
13004 return isl_basic_set_free(bset);
13005 if (internal)
13006 continue;
13007 isl_int_sub_ui(bset->ineq[j][0], bset->ineq[j][0], 1);
13010 return bset;