add isl_union_set_min_multi_union_pw_aff
[isl.git] / isl_map.c
bloba34368b0ef8ba62f80be27077257b74cc9a869da
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 Sven Verdoolaege
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
19 #include <string.h>
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_blk.h>
23 #include <isl/constraint.h>
24 #include "isl_space_private.h"
25 #include "isl_equalities.h"
26 #include <isl_lp_private.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl/map.h>
30 #include <isl_reordering.h>
31 #include "isl_sample.h"
32 #include <isl_sort.h>
33 #include "isl_tab.h"
34 #include <isl/vec.h>
35 #include <isl_mat_private.h>
36 #include <isl_vec_private.h>
37 #include <isl_dim_map.h>
38 #include <isl_local_space_private.h>
39 #include <isl_aff_private.h>
40 #include <isl_options_private.h>
41 #include <isl_morph.h>
42 #include <isl_val_private.h>
43 #include <isl/deprecated/map_int.h>
44 #include <isl/deprecated/set_int.h>
46 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
48 switch (type) {
49 case isl_dim_param: return dim->nparam;
50 case isl_dim_in: return dim->n_in;
51 case isl_dim_out: return dim->n_out;
52 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
53 default: return 0;
57 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
59 switch (type) {
60 case isl_dim_param: return 1;
61 case isl_dim_in: return 1 + dim->nparam;
62 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
63 default: return 0;
67 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
68 enum isl_dim_type type)
70 if (!bmap)
71 return 0;
72 switch (type) {
73 case isl_dim_cst: return 1;
74 case isl_dim_param:
75 case isl_dim_in:
76 case isl_dim_out: return isl_space_dim(bmap->dim, type);
77 case isl_dim_div: return bmap->n_div;
78 case isl_dim_all: return isl_basic_map_total_dim(bmap);
79 default: return 0;
83 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
85 return map ? n(map->dim, type) : 0;
88 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
90 return set ? n(set->dim, type) : 0;
93 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
94 enum isl_dim_type type)
96 isl_space *dim = bmap->dim;
97 switch (type) {
98 case isl_dim_cst: return 0;
99 case isl_dim_param: return 1;
100 case isl_dim_in: return 1 + dim->nparam;
101 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
102 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
103 default: return 0;
107 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
108 enum isl_dim_type type)
110 return isl_basic_map_offset(bset, type);
113 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
115 return pos(map->dim, type);
118 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
119 enum isl_dim_type type)
121 return isl_basic_map_dim(bset, type);
124 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
126 return isl_basic_set_dim(bset, isl_dim_set);
129 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
131 return isl_basic_set_dim(bset, isl_dim_param);
134 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
136 if (!bset)
137 return 0;
138 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
141 unsigned isl_set_n_dim(__isl_keep isl_set *set)
143 return isl_set_dim(set, isl_dim_set);
146 unsigned isl_set_n_param(__isl_keep isl_set *set)
148 return isl_set_dim(set, isl_dim_param);
151 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
153 return bmap ? bmap->dim->n_in : 0;
156 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
158 return bmap ? bmap->dim->n_out : 0;
161 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
163 return bmap ? bmap->dim->nparam : 0;
166 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
168 return bmap ? bmap->n_div : 0;
171 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
173 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
176 unsigned isl_map_n_in(const struct isl_map *map)
178 return map ? map->dim->n_in : 0;
181 unsigned isl_map_n_out(const struct isl_map *map)
183 return map ? map->dim->n_out : 0;
186 unsigned isl_map_n_param(const struct isl_map *map)
188 return map ? map->dim->nparam : 0;
191 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
193 int m;
194 if (!map || !set)
195 return -1;
196 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
197 if (m < 0 || !m)
198 return m;
199 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
200 set->dim, isl_dim_set);
203 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
204 struct isl_basic_set *bset)
206 int m;
207 if (!bmap || !bset)
208 return -1;
209 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
210 if (m < 0 || !m)
211 return m;
212 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
213 bset->dim, isl_dim_set);
216 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
218 int m;
219 if (!map || !set)
220 return -1;
221 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
222 if (m < 0 || !m)
223 return m;
224 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
225 set->dim, isl_dim_set);
228 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
229 struct isl_basic_set *bset)
231 int m;
232 if (!bmap || !bset)
233 return -1;
234 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
235 if (m < 0 || !m)
236 return m;
237 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
238 bset->dim, isl_dim_set);
241 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
243 return bmap ? bmap->ctx : NULL;
246 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
248 return bset ? bset->ctx : NULL;
251 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
253 return map ? map->ctx : NULL;
256 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
258 return set ? set->ctx : NULL;
261 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
263 if (!bmap)
264 return NULL;
265 return isl_space_copy(bmap->dim);
268 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
270 if (!bset)
271 return NULL;
272 return isl_space_copy(bset->dim);
275 /* Extract the divs in "bmap" as a matrix.
277 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
279 int i;
280 isl_ctx *ctx;
281 isl_mat *div;
282 unsigned total;
283 unsigned cols;
285 if (!bmap)
286 return NULL;
288 ctx = isl_basic_map_get_ctx(bmap);
289 total = isl_space_dim(bmap->dim, isl_dim_all);
290 cols = 1 + 1 + total + bmap->n_div;
291 div = isl_mat_alloc(ctx, bmap->n_div, cols);
292 if (!div)
293 return NULL;
295 for (i = 0; i < bmap->n_div; ++i)
296 isl_seq_cpy(div->row[i], bmap->div[i], cols);
298 return div;
301 /* Extract the divs in "bset" as a matrix.
303 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
305 return isl_basic_map_get_divs(bset);
308 __isl_give isl_local_space *isl_basic_map_get_local_space(
309 __isl_keep isl_basic_map *bmap)
311 isl_mat *div;
313 if (!bmap)
314 return NULL;
316 div = isl_basic_map_get_divs(bmap);
317 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
320 __isl_give isl_local_space *isl_basic_set_get_local_space(
321 __isl_keep isl_basic_set *bset)
323 return isl_basic_map_get_local_space(bset);
326 /* For each known div d = floor(f/m), add the constraints
328 * f - m d >= 0
329 * -(f-(n-1)) + m d >= 0
331 * Do not finalize the result.
333 static __isl_give isl_basic_map *add_known_div_constraints(
334 __isl_take isl_basic_map *bmap)
336 int i;
337 unsigned n_div;
339 if (!bmap)
340 return NULL;
341 n_div = isl_basic_map_dim(bmap, isl_dim_div);
342 if (n_div == 0)
343 return bmap;
344 bmap = isl_basic_map_cow(bmap);
345 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
346 if (!bmap)
347 return NULL;
348 for (i = 0; i < n_div; ++i) {
349 if (isl_int_is_zero(bmap->div[i][0]))
350 continue;
351 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
352 return isl_basic_map_free(bmap);
355 return bmap;
358 __isl_give isl_basic_map *isl_basic_map_from_local_space(
359 __isl_take isl_local_space *ls)
361 int i;
362 int n_div;
363 isl_basic_map *bmap;
365 if (!ls)
366 return NULL;
368 n_div = isl_local_space_dim(ls, isl_dim_div);
369 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
370 n_div, 0, 2 * n_div);
372 for (i = 0; i < n_div; ++i)
373 if (isl_basic_map_alloc_div(bmap) < 0)
374 goto error;
376 for (i = 0; i < n_div; ++i)
377 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
378 bmap = add_known_div_constraints(bmap);
380 isl_local_space_free(ls);
381 return bmap;
382 error:
383 isl_local_space_free(ls);
384 isl_basic_map_free(bmap);
385 return NULL;
388 __isl_give isl_basic_set *isl_basic_set_from_local_space(
389 __isl_take isl_local_space *ls)
391 return isl_basic_map_from_local_space(ls);
394 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
396 if (!map)
397 return NULL;
398 return isl_space_copy(map->dim);
401 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
403 if (!set)
404 return NULL;
405 return isl_space_copy(set->dim);
408 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
409 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
411 bmap = isl_basic_map_cow(bmap);
412 if (!bmap)
413 return NULL;
414 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
415 if (!bmap->dim)
416 goto error;
417 bmap = isl_basic_map_finalize(bmap);
418 return bmap;
419 error:
420 isl_basic_map_free(bmap);
421 return NULL;
424 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
425 __isl_take isl_basic_set *bset, const char *s)
427 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
430 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
431 enum isl_dim_type type)
433 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
436 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
437 enum isl_dim_type type, const char *s)
439 int i;
441 map = isl_map_cow(map);
442 if (!map)
443 return NULL;
445 map->dim = isl_space_set_tuple_name(map->dim, type, s);
446 if (!map->dim)
447 goto error;
449 for (i = 0; i < map->n; ++i) {
450 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
451 if (!map->p[i])
452 goto error;
455 return map;
456 error:
457 isl_map_free(map);
458 return NULL;
461 /* Replace the identifier of the tuple of type "type" by "id".
463 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
464 __isl_take isl_basic_map *bmap,
465 enum isl_dim_type type, __isl_take isl_id *id)
467 bmap = isl_basic_map_cow(bmap);
468 if (!bmap)
469 goto error;
470 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
471 if (!bmap->dim)
472 return isl_basic_map_free(bmap);
473 bmap = isl_basic_map_finalize(bmap);
474 return bmap;
475 error:
476 isl_id_free(id);
477 return NULL;
480 /* Replace the identifier of the tuple by "id".
482 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
483 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
485 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
488 /* Does the input or output tuple have a name?
490 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
492 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
495 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
496 enum isl_dim_type type)
498 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
501 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
502 const char *s)
504 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
507 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
508 enum isl_dim_type type, __isl_take isl_id *id)
510 map = isl_map_cow(map);
511 if (!map)
512 goto error;
514 map->dim = isl_space_set_tuple_id(map->dim, type, id);
516 return isl_map_reset_space(map, isl_space_copy(map->dim));
517 error:
518 isl_id_free(id);
519 return NULL;
522 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
523 __isl_take isl_id *id)
525 return isl_map_set_tuple_id(set, isl_dim_set, id);
528 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
529 enum isl_dim_type type)
531 map = isl_map_cow(map);
532 if (!map)
533 return NULL;
535 map->dim = isl_space_reset_tuple_id(map->dim, type);
537 return isl_map_reset_space(map, isl_space_copy(map->dim));
540 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
542 return isl_map_reset_tuple_id(set, isl_dim_set);
545 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
547 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
550 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
551 enum isl_dim_type type)
553 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
556 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
558 return isl_map_has_tuple_id(set, isl_dim_set);
561 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
563 return isl_map_get_tuple_id(set, isl_dim_set);
566 /* Does the set tuple have a name?
568 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
570 if (!set)
571 return isl_bool_error;
572 return isl_space_has_tuple_name(set->dim, isl_dim_set);
576 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
578 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
581 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
583 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
586 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
587 enum isl_dim_type type, unsigned pos)
589 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
592 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
593 enum isl_dim_type type, unsigned pos)
595 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
598 /* Does the given dimension have a name?
600 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
601 enum isl_dim_type type, unsigned pos)
603 if (!map)
604 return isl_bool_error;
605 return isl_space_has_dim_name(map->dim, type, pos);
608 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
609 enum isl_dim_type type, unsigned pos)
611 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
614 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
615 enum isl_dim_type type, unsigned pos)
617 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
620 /* Does the given dimension have a name?
622 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
623 enum isl_dim_type type, unsigned pos)
625 if (!set)
626 return isl_bool_error;
627 return isl_space_has_dim_name(set->dim, type, pos);
630 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
631 __isl_take isl_basic_map *bmap,
632 enum isl_dim_type type, unsigned pos, const char *s)
634 bmap = isl_basic_map_cow(bmap);
635 if (!bmap)
636 return NULL;
637 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
638 if (!bmap->dim)
639 goto error;
640 return isl_basic_map_finalize(bmap);
641 error:
642 isl_basic_map_free(bmap);
643 return NULL;
646 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
647 enum isl_dim_type type, unsigned pos, const char *s)
649 int i;
651 map = isl_map_cow(map);
652 if (!map)
653 return NULL;
655 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
656 if (!map->dim)
657 goto error;
659 for (i = 0; i < map->n; ++i) {
660 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
661 if (!map->p[i])
662 goto error;
665 return map;
666 error:
667 isl_map_free(map);
668 return NULL;
671 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
672 __isl_take isl_basic_set *bset,
673 enum isl_dim_type type, unsigned pos, const char *s)
675 return (isl_basic_set *)isl_basic_map_set_dim_name(
676 (isl_basic_map *)bset, type, pos, s);
679 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
680 enum isl_dim_type type, unsigned pos, const char *s)
682 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
685 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
686 enum isl_dim_type type, unsigned pos)
688 if (!bmap)
689 return isl_bool_error;
690 return isl_space_has_dim_id(bmap->dim, type, pos);
693 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
694 enum isl_dim_type type, unsigned pos)
696 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
699 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
700 enum isl_dim_type type, unsigned pos)
702 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
705 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
706 enum isl_dim_type type, unsigned pos)
708 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
711 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
712 enum isl_dim_type type, unsigned pos)
714 return isl_map_has_dim_id(set, type, pos);
717 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
718 enum isl_dim_type type, unsigned pos)
720 return isl_map_get_dim_id(set, type, pos);
723 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
724 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
726 map = isl_map_cow(map);
727 if (!map)
728 goto error;
730 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
732 return isl_map_reset_space(map, isl_space_copy(map->dim));
733 error:
734 isl_id_free(id);
735 return NULL;
738 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
739 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
741 return isl_map_set_dim_id(set, type, pos, id);
744 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
745 __isl_keep isl_id *id)
747 if (!map)
748 return -1;
749 return isl_space_find_dim_by_id(map->dim, type, id);
752 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
753 __isl_keep isl_id *id)
755 return isl_map_find_dim_by_id(set, type, id);
758 /* Return the position of the dimension of the given type and name
759 * in "bmap".
760 * Return -1 if no such dimension can be found.
762 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
763 enum isl_dim_type type, const char *name)
765 if (!bmap)
766 return -1;
767 return isl_space_find_dim_by_name(bmap->dim, type, name);
770 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
771 const char *name)
773 if (!map)
774 return -1;
775 return isl_space_find_dim_by_name(map->dim, type, name);
778 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
779 const char *name)
781 return isl_map_find_dim_by_name(set, type, name);
784 /* Reset the user pointer on all identifiers of parameters and tuples
785 * of the space of "map".
787 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
789 isl_space *space;
791 space = isl_map_get_space(map);
792 space = isl_space_reset_user(space);
793 map = isl_map_reset_space(map, space);
795 return map;
798 /* Reset the user pointer on all identifiers of parameters and tuples
799 * of the space of "set".
801 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
803 return isl_map_reset_user(set);
806 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
808 if (!bmap)
809 return -1;
810 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
813 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
815 return isl_basic_map_is_rational(bset);
818 /* Does "bmap" contain any rational points?
820 * If "bmap" has an equality for each dimension, equating the dimension
821 * to an integer constant, then it has no rational points, even if it
822 * is marked as rational.
824 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
826 int has_rational = 1;
827 unsigned total;
829 if (!bmap)
830 return -1;
831 if (isl_basic_map_plain_is_empty(bmap))
832 return 0;
833 if (!isl_basic_map_is_rational(bmap))
834 return 0;
835 bmap = isl_basic_map_copy(bmap);
836 bmap = isl_basic_map_implicit_equalities(bmap);
837 if (!bmap)
838 return -1;
839 total = isl_basic_map_total_dim(bmap);
840 if (bmap->n_eq == total) {
841 int i, j;
842 for (i = 0; i < bmap->n_eq; ++i) {
843 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
844 if (j < 0)
845 break;
846 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
847 !isl_int_is_negone(bmap->eq[i][1 + j]))
848 break;
849 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
850 total - j - 1);
851 if (j >= 0)
852 break;
854 if (i == bmap->n_eq)
855 has_rational = 0;
857 isl_basic_map_free(bmap);
859 return has_rational;
862 /* Does "map" contain any rational points?
864 int isl_map_has_rational(__isl_keep isl_map *map)
866 int i;
867 int has_rational;
869 if (!map)
870 return -1;
871 for (i = 0; i < map->n; ++i) {
872 has_rational = isl_basic_map_has_rational(map->p[i]);
873 if (has_rational < 0)
874 return -1;
875 if (has_rational)
876 return 1;
878 return 0;
881 /* Does "set" contain any rational points?
883 int isl_set_has_rational(__isl_keep isl_set *set)
885 return isl_map_has_rational(set);
888 /* Is this basic set a parameter domain?
890 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
892 if (!bset)
893 return -1;
894 return isl_space_is_params(bset->dim);
897 /* Is this set a parameter domain?
899 isl_bool isl_set_is_params(__isl_keep isl_set *set)
901 if (!set)
902 return isl_bool_error;
903 return isl_space_is_params(set->dim);
906 /* Is this map actually a parameter domain?
907 * Users should never call this function. Outside of isl,
908 * a map can never be a parameter domain.
910 int isl_map_is_params(__isl_keep isl_map *map)
912 if (!map)
913 return -1;
914 return isl_space_is_params(map->dim);
917 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
918 struct isl_basic_map *bmap, unsigned extra,
919 unsigned n_eq, unsigned n_ineq)
921 int i;
922 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
924 bmap->ctx = ctx;
925 isl_ctx_ref(ctx);
927 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
928 if (isl_blk_is_error(bmap->block))
929 goto error;
931 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
932 if ((n_ineq + n_eq) && !bmap->ineq)
933 goto error;
935 if (extra == 0) {
936 bmap->block2 = isl_blk_empty();
937 bmap->div = NULL;
938 } else {
939 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
940 if (isl_blk_is_error(bmap->block2))
941 goto error;
943 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
944 if (!bmap->div)
945 goto error;
948 for (i = 0; i < n_ineq + n_eq; ++i)
949 bmap->ineq[i] = bmap->block.data + i * row_size;
951 for (i = 0; i < extra; ++i)
952 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
954 bmap->ref = 1;
955 bmap->flags = 0;
956 bmap->c_size = n_eq + n_ineq;
957 bmap->eq = bmap->ineq + n_ineq;
958 bmap->extra = extra;
959 bmap->n_eq = 0;
960 bmap->n_ineq = 0;
961 bmap->n_div = 0;
962 bmap->sample = NULL;
964 return bmap;
965 error:
966 isl_basic_map_free(bmap);
967 return NULL;
970 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
971 unsigned nparam, unsigned dim, unsigned extra,
972 unsigned n_eq, unsigned n_ineq)
974 struct isl_basic_map *bmap;
975 isl_space *space;
977 space = isl_space_set_alloc(ctx, nparam, dim);
978 if (!space)
979 return NULL;
981 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
982 return (struct isl_basic_set *)bmap;
985 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
986 unsigned extra, unsigned n_eq, unsigned n_ineq)
988 struct isl_basic_map *bmap;
989 if (!dim)
990 return NULL;
991 isl_assert(dim->ctx, dim->n_in == 0, goto error);
992 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
993 return (struct isl_basic_set *)bmap;
994 error:
995 isl_space_free(dim);
996 return NULL;
999 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1000 unsigned extra, unsigned n_eq, unsigned n_ineq)
1002 struct isl_basic_map *bmap;
1004 if (!dim)
1005 return NULL;
1006 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1007 if (!bmap)
1008 goto error;
1009 bmap->dim = dim;
1011 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1012 error:
1013 isl_space_free(dim);
1014 return NULL;
1017 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1018 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1019 unsigned n_eq, unsigned n_ineq)
1021 struct isl_basic_map *bmap;
1022 isl_space *dim;
1024 dim = isl_space_alloc(ctx, nparam, in, out);
1025 if (!dim)
1026 return NULL;
1028 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1029 return bmap;
1032 static void dup_constraints(
1033 struct isl_basic_map *dst, struct isl_basic_map *src)
1035 int i;
1036 unsigned total = isl_basic_map_total_dim(src);
1038 for (i = 0; i < src->n_eq; ++i) {
1039 int j = isl_basic_map_alloc_equality(dst);
1040 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1043 for (i = 0; i < src->n_ineq; ++i) {
1044 int j = isl_basic_map_alloc_inequality(dst);
1045 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1048 for (i = 0; i < src->n_div; ++i) {
1049 int j = isl_basic_map_alloc_div(dst);
1050 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1052 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1055 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1057 struct isl_basic_map *dup;
1059 if (!bmap)
1060 return NULL;
1061 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1062 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1063 if (!dup)
1064 return NULL;
1065 dup_constraints(dup, bmap);
1066 dup->flags = bmap->flags;
1067 dup->sample = isl_vec_copy(bmap->sample);
1068 return dup;
1071 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1073 struct isl_basic_map *dup;
1075 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1076 return (struct isl_basic_set *)dup;
1079 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1081 if (!bset)
1082 return NULL;
1084 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1085 bset->ref++;
1086 return bset;
1088 return isl_basic_set_dup(bset);
1091 struct isl_set *isl_set_copy(struct isl_set *set)
1093 if (!set)
1094 return NULL;
1096 set->ref++;
1097 return set;
1100 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1102 if (!bmap)
1103 return NULL;
1105 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1106 bmap->ref++;
1107 return bmap;
1109 bmap = isl_basic_map_dup(bmap);
1110 if (bmap)
1111 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1112 return bmap;
1115 struct isl_map *isl_map_copy(struct isl_map *map)
1117 if (!map)
1118 return NULL;
1120 map->ref++;
1121 return map;
1124 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1126 if (!bmap)
1127 return NULL;
1129 if (--bmap->ref > 0)
1130 return NULL;
1132 isl_ctx_deref(bmap->ctx);
1133 free(bmap->div);
1134 isl_blk_free(bmap->ctx, bmap->block2);
1135 free(bmap->ineq);
1136 isl_blk_free(bmap->ctx, bmap->block);
1137 isl_vec_free(bmap->sample);
1138 isl_space_free(bmap->dim);
1139 free(bmap);
1141 return NULL;
1144 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1146 return isl_basic_map_free((struct isl_basic_map *)bset);
1149 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1151 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1154 __isl_give isl_map *isl_map_align_params_map_map_and(
1155 __isl_take isl_map *map1, __isl_take isl_map *map2,
1156 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1157 __isl_take isl_map *map2))
1159 if (!map1 || !map2)
1160 goto error;
1161 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1162 return fn(map1, map2);
1163 if (!isl_space_has_named_params(map1->dim) ||
1164 !isl_space_has_named_params(map2->dim))
1165 isl_die(map1->ctx, isl_error_invalid,
1166 "unaligned unnamed parameters", goto error);
1167 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1168 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1169 return fn(map1, map2);
1170 error:
1171 isl_map_free(map1);
1172 isl_map_free(map2);
1173 return NULL;
1176 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1177 __isl_keep isl_map *map2,
1178 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1180 isl_bool r;
1182 if (!map1 || !map2)
1183 return isl_bool_error;
1184 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1185 return fn(map1, map2);
1186 if (!isl_space_has_named_params(map1->dim) ||
1187 !isl_space_has_named_params(map2->dim))
1188 isl_die(map1->ctx, isl_error_invalid,
1189 "unaligned unnamed parameters", return isl_bool_error);
1190 map1 = isl_map_copy(map1);
1191 map2 = isl_map_copy(map2);
1192 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1193 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1194 r = fn(map1, map2);
1195 isl_map_free(map1);
1196 isl_map_free(map2);
1197 return r;
1200 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1202 struct isl_ctx *ctx;
1203 if (!bmap)
1204 return -1;
1205 ctx = bmap->ctx;
1206 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1207 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1208 return -1);
1209 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1210 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1211 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1212 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1213 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1214 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1215 isl_int *t;
1216 int j = isl_basic_map_alloc_inequality(bmap);
1217 if (j < 0)
1218 return -1;
1219 t = bmap->ineq[j];
1220 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1221 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1222 bmap->eq[-1] = t;
1223 bmap->n_eq++;
1224 bmap->n_ineq--;
1225 bmap->eq--;
1226 return 0;
1228 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1229 bmap->extra - bmap->n_div);
1230 return bmap->n_eq++;
1233 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1235 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1238 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1240 if (!bmap)
1241 return -1;
1242 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1243 bmap->n_eq -= n;
1244 return 0;
1247 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1249 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1252 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1254 isl_int *t;
1255 if (!bmap)
1256 return -1;
1257 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1259 if (pos != bmap->n_eq - 1) {
1260 t = bmap->eq[pos];
1261 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1262 bmap->eq[bmap->n_eq - 1] = t;
1264 bmap->n_eq--;
1265 return 0;
1268 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1270 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1273 /* Turn inequality "pos" of "bmap" into an equality.
1275 * In particular, we move the inequality in front of the equalities
1276 * and move the last inequality in the position of the moved inequality.
1277 * Note that isl_tab_make_equalities_explicit depends on this particular
1278 * change in the ordering of the constraints.
1280 void isl_basic_map_inequality_to_equality(
1281 struct isl_basic_map *bmap, unsigned pos)
1283 isl_int *t;
1285 t = bmap->ineq[pos];
1286 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1287 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1288 bmap->eq[-1] = t;
1289 bmap->n_eq++;
1290 bmap->n_ineq--;
1291 bmap->eq--;
1292 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1293 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1294 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1295 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1298 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1300 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1303 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1305 struct isl_ctx *ctx;
1306 if (!bmap)
1307 return -1;
1308 ctx = bmap->ctx;
1309 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1310 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1311 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1312 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1313 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1314 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1315 1 + isl_basic_map_total_dim(bmap),
1316 bmap->extra - bmap->n_div);
1317 return bmap->n_ineq++;
1320 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1322 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1325 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1327 if (!bmap)
1328 return -1;
1329 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1330 bmap->n_ineq -= n;
1331 return 0;
1334 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1336 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1339 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1341 isl_int *t;
1342 if (!bmap)
1343 return -1;
1344 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1346 if (pos != bmap->n_ineq - 1) {
1347 t = bmap->ineq[pos];
1348 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1349 bmap->ineq[bmap->n_ineq - 1] = t;
1350 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1352 bmap->n_ineq--;
1353 return 0;
1356 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1358 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1361 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1362 isl_int *eq)
1364 int k;
1366 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1367 if (!bmap)
1368 return NULL;
1369 k = isl_basic_map_alloc_equality(bmap);
1370 if (k < 0)
1371 goto error;
1372 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1373 return bmap;
1374 error:
1375 isl_basic_map_free(bmap);
1376 return NULL;
1379 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1380 isl_int *eq)
1382 return (isl_basic_set *)
1383 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1386 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1387 isl_int *ineq)
1389 int k;
1391 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1392 if (!bmap)
1393 return NULL;
1394 k = isl_basic_map_alloc_inequality(bmap);
1395 if (k < 0)
1396 goto error;
1397 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1398 return bmap;
1399 error:
1400 isl_basic_map_free(bmap);
1401 return NULL;
1404 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1405 isl_int *ineq)
1407 return (isl_basic_set *)
1408 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1411 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1413 if (!bmap)
1414 return -1;
1415 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1416 isl_seq_clr(bmap->div[bmap->n_div] +
1417 1 + 1 + isl_basic_map_total_dim(bmap),
1418 bmap->extra - bmap->n_div);
1419 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1420 return bmap->n_div++;
1423 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1425 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1428 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1430 if (!bmap)
1431 return -1;
1432 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1433 bmap->n_div -= n;
1434 return 0;
1437 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1439 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1442 /* Copy constraint from src to dst, putting the vars of src at offset
1443 * dim_off in dst and the divs of src at offset div_off in dst.
1444 * If both sets are actually map, then dim_off applies to the input
1445 * variables.
1447 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1448 struct isl_basic_map *src_map, isl_int *src,
1449 unsigned in_off, unsigned out_off, unsigned div_off)
1451 unsigned src_nparam = isl_basic_map_n_param(src_map);
1452 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1453 unsigned src_in = isl_basic_map_n_in(src_map);
1454 unsigned dst_in = isl_basic_map_n_in(dst_map);
1455 unsigned src_out = isl_basic_map_n_out(src_map);
1456 unsigned dst_out = isl_basic_map_n_out(dst_map);
1457 isl_int_set(dst[0], src[0]);
1458 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1459 if (dst_nparam > src_nparam)
1460 isl_seq_clr(dst+1+src_nparam,
1461 dst_nparam - src_nparam);
1462 isl_seq_clr(dst+1+dst_nparam, in_off);
1463 isl_seq_cpy(dst+1+dst_nparam+in_off,
1464 src+1+src_nparam,
1465 isl_min(dst_in-in_off, src_in));
1466 if (dst_in-in_off > src_in)
1467 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1468 dst_in - in_off - src_in);
1469 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1470 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1471 src+1+src_nparam+src_in,
1472 isl_min(dst_out-out_off, src_out));
1473 if (dst_out-out_off > src_out)
1474 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1475 dst_out - out_off - src_out);
1476 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1477 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1478 src+1+src_nparam+src_in+src_out,
1479 isl_min(dst_map->extra-div_off, src_map->n_div));
1480 if (dst_map->n_div-div_off > src_map->n_div)
1481 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1482 div_off+src_map->n_div,
1483 dst_map->n_div - div_off - src_map->n_div);
1486 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1487 struct isl_basic_map *src_map, isl_int *src,
1488 unsigned in_off, unsigned out_off, unsigned div_off)
1490 isl_int_set(dst[0], src[0]);
1491 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1494 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1495 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1497 int i;
1498 unsigned div_off;
1500 if (!bmap1 || !bmap2)
1501 goto error;
1503 div_off = bmap1->n_div;
1505 for (i = 0; i < bmap2->n_eq; ++i) {
1506 int i1 = isl_basic_map_alloc_equality(bmap1);
1507 if (i1 < 0)
1508 goto error;
1509 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1510 i_pos, o_pos, div_off);
1513 for (i = 0; i < bmap2->n_ineq; ++i) {
1514 int i1 = isl_basic_map_alloc_inequality(bmap1);
1515 if (i1 < 0)
1516 goto error;
1517 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1518 i_pos, o_pos, div_off);
1521 for (i = 0; i < bmap2->n_div; ++i) {
1522 int i1 = isl_basic_map_alloc_div(bmap1);
1523 if (i1 < 0)
1524 goto error;
1525 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1526 i_pos, o_pos, div_off);
1529 isl_basic_map_free(bmap2);
1531 return bmap1;
1533 error:
1534 isl_basic_map_free(bmap1);
1535 isl_basic_map_free(bmap2);
1536 return NULL;
1539 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1540 struct isl_basic_set *bset2, unsigned pos)
1542 return (struct isl_basic_set *)
1543 add_constraints((struct isl_basic_map *)bset1,
1544 (struct isl_basic_map *)bset2, 0, pos);
1547 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1548 __isl_take isl_space *dim, unsigned extra,
1549 unsigned n_eq, unsigned n_ineq)
1551 struct isl_basic_map *ext;
1552 unsigned flags;
1553 int dims_ok;
1555 if (!dim)
1556 goto error;
1558 if (!base)
1559 goto error;
1561 dims_ok = isl_space_is_equal(base->dim, dim) &&
1562 base->extra >= base->n_div + extra;
1564 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1565 room_for_ineq(base, n_ineq)) {
1566 isl_space_free(dim);
1567 return base;
1570 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1571 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1572 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1573 extra += base->extra;
1574 n_eq += base->n_eq;
1575 n_ineq += base->n_ineq;
1577 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1578 dim = NULL;
1579 if (!ext)
1580 goto error;
1582 if (dims_ok)
1583 ext->sample = isl_vec_copy(base->sample);
1584 flags = base->flags;
1585 ext = add_constraints(ext, base, 0, 0);
1586 if (ext) {
1587 ext->flags = flags;
1588 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1591 return ext;
1593 error:
1594 isl_space_free(dim);
1595 isl_basic_map_free(base);
1596 return NULL;
1599 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1600 __isl_take isl_space *dim, unsigned extra,
1601 unsigned n_eq, unsigned n_ineq)
1603 return (struct isl_basic_set *)
1604 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1605 extra, n_eq, n_ineq);
1608 struct isl_basic_map *isl_basic_map_extend_constraints(
1609 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1611 if (!base)
1612 return NULL;
1613 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1614 0, n_eq, n_ineq);
1617 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1618 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1619 unsigned n_eq, unsigned n_ineq)
1621 struct isl_basic_map *bmap;
1622 isl_space *dim;
1624 if (!base)
1625 return NULL;
1626 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1627 if (!dim)
1628 goto error;
1630 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1631 return bmap;
1632 error:
1633 isl_basic_map_free(base);
1634 return NULL;
1637 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1638 unsigned nparam, unsigned dim, unsigned extra,
1639 unsigned n_eq, unsigned n_ineq)
1641 return (struct isl_basic_set *)
1642 isl_basic_map_extend((struct isl_basic_map *)base,
1643 nparam, 0, dim, extra, n_eq, n_ineq);
1646 struct isl_basic_set *isl_basic_set_extend_constraints(
1647 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1649 return (struct isl_basic_set *)
1650 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1651 n_eq, n_ineq);
1654 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1656 return (struct isl_basic_set *)
1657 isl_basic_map_cow((struct isl_basic_map *)bset);
1660 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1662 if (!bmap)
1663 return NULL;
1665 if (bmap->ref > 1) {
1666 bmap->ref--;
1667 bmap = isl_basic_map_dup(bmap);
1669 if (bmap) {
1670 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1671 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1673 return bmap;
1676 struct isl_set *isl_set_cow(struct isl_set *set)
1678 if (!set)
1679 return NULL;
1681 if (set->ref == 1)
1682 return set;
1683 set->ref--;
1684 return isl_set_dup(set);
1687 struct isl_map *isl_map_cow(struct isl_map *map)
1689 if (!map)
1690 return NULL;
1692 if (map->ref == 1)
1693 return map;
1694 map->ref--;
1695 return isl_map_dup(map);
1698 static void swap_vars(struct isl_blk blk, isl_int *a,
1699 unsigned a_len, unsigned b_len)
1701 isl_seq_cpy(blk.data, a+a_len, b_len);
1702 isl_seq_cpy(blk.data+b_len, a, a_len);
1703 isl_seq_cpy(a, blk.data, b_len+a_len);
1706 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1707 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1709 int i;
1710 struct isl_blk blk;
1712 if (!bmap)
1713 goto error;
1715 isl_assert(bmap->ctx,
1716 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1718 if (n1 == 0 || n2 == 0)
1719 return bmap;
1721 bmap = isl_basic_map_cow(bmap);
1722 if (!bmap)
1723 return NULL;
1725 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1726 if (isl_blk_is_error(blk))
1727 goto error;
1729 for (i = 0; i < bmap->n_eq; ++i)
1730 swap_vars(blk,
1731 bmap->eq[i] + pos, n1, n2);
1733 for (i = 0; i < bmap->n_ineq; ++i)
1734 swap_vars(blk,
1735 bmap->ineq[i] + pos, n1, n2);
1737 for (i = 0; i < bmap->n_div; ++i)
1738 swap_vars(blk,
1739 bmap->div[i]+1 + pos, n1, n2);
1741 isl_blk_free(bmap->ctx, blk);
1743 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1744 bmap = isl_basic_map_gauss(bmap, NULL);
1745 return isl_basic_map_finalize(bmap);
1746 error:
1747 isl_basic_map_free(bmap);
1748 return NULL;
1751 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1753 int i = 0;
1754 unsigned total;
1755 if (!bmap)
1756 goto error;
1757 total = isl_basic_map_total_dim(bmap);
1758 isl_basic_map_free_div(bmap, bmap->n_div);
1759 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1760 if (bmap->n_eq > 0)
1761 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1762 else {
1763 i = isl_basic_map_alloc_equality(bmap);
1764 if (i < 0)
1765 goto error;
1767 isl_int_set_si(bmap->eq[i][0], 1);
1768 isl_seq_clr(bmap->eq[i]+1, total);
1769 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1770 isl_vec_free(bmap->sample);
1771 bmap->sample = NULL;
1772 return isl_basic_map_finalize(bmap);
1773 error:
1774 isl_basic_map_free(bmap);
1775 return NULL;
1778 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1780 return (struct isl_basic_set *)
1781 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1784 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1785 * of "bmap").
1787 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1789 isl_int *t = bmap->div[a];
1790 bmap->div[a] = bmap->div[b];
1791 bmap->div[b] = t;
1794 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1795 * div definitions accordingly.
1797 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1799 int i;
1800 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1802 swap_div(bmap, a, b);
1804 for (i = 0; i < bmap->n_eq; ++i)
1805 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1807 for (i = 0; i < bmap->n_ineq; ++i)
1808 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1810 for (i = 0; i < bmap->n_div; ++i)
1811 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1812 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1815 /* Eliminate the specified n dimensions starting at first from the
1816 * constraints, without removing the dimensions from the space.
1817 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1819 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1820 enum isl_dim_type type, unsigned first, unsigned n)
1822 int i;
1824 if (!map)
1825 return NULL;
1826 if (n == 0)
1827 return map;
1829 if (first + n > isl_map_dim(map, type) || first + n < first)
1830 isl_die(map->ctx, isl_error_invalid,
1831 "index out of bounds", goto error);
1833 map = isl_map_cow(map);
1834 if (!map)
1835 return NULL;
1837 for (i = 0; i < map->n; ++i) {
1838 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1839 if (!map->p[i])
1840 goto error;
1842 return map;
1843 error:
1844 isl_map_free(map);
1845 return NULL;
1848 /* Eliminate the specified n dimensions starting at first from the
1849 * constraints, without removing the dimensions from the space.
1850 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1852 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1853 enum isl_dim_type type, unsigned first, unsigned n)
1855 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1858 /* Eliminate the specified n dimensions starting at first from the
1859 * constraints, without removing the dimensions from the space.
1860 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1862 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1863 unsigned first, unsigned n)
1865 return isl_set_eliminate(set, isl_dim_set, first, n);
1868 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1869 __isl_take isl_basic_map *bmap)
1871 if (!bmap)
1872 return NULL;
1873 bmap = isl_basic_map_eliminate_vars(bmap,
1874 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1875 if (!bmap)
1876 return NULL;
1877 bmap->n_div = 0;
1878 return isl_basic_map_finalize(bmap);
1881 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1882 __isl_take isl_basic_set *bset)
1884 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1885 (struct isl_basic_map *)bset);
1888 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1890 int i;
1892 if (!map)
1893 return NULL;
1894 if (map->n == 0)
1895 return map;
1897 map = isl_map_cow(map);
1898 if (!map)
1899 return NULL;
1901 for (i = 0; i < map->n; ++i) {
1902 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1903 if (!map->p[i])
1904 goto error;
1906 return map;
1907 error:
1908 isl_map_free(map);
1909 return NULL;
1912 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1914 return isl_map_remove_divs(set);
1917 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1918 enum isl_dim_type type, unsigned first, unsigned n)
1920 if (!bmap)
1921 return NULL;
1922 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1923 goto error);
1924 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1925 return bmap;
1926 bmap = isl_basic_map_eliminate_vars(bmap,
1927 isl_basic_map_offset(bmap, type) - 1 + first, n);
1928 if (!bmap)
1929 return bmap;
1930 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1931 return bmap;
1932 bmap = isl_basic_map_drop(bmap, type, first, n);
1933 return bmap;
1934 error:
1935 isl_basic_map_free(bmap);
1936 return NULL;
1939 /* Return true if the definition of the given div (recursively) involves
1940 * any of the given variables.
1942 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1943 unsigned first, unsigned n)
1945 int i;
1946 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1948 if (isl_int_is_zero(bmap->div[div][0]))
1949 return 0;
1950 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1951 return 1;
1953 for (i = bmap->n_div - 1; i >= 0; --i) {
1954 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1955 continue;
1956 if (div_involves_vars(bmap, i, first, n))
1957 return 1;
1960 return 0;
1963 /* Try and add a lower and/or upper bound on "div" to "bmap"
1964 * based on inequality "i".
1965 * "total" is the total number of variables (excluding the divs).
1966 * "v" is a temporary object that can be used during the calculations.
1967 * If "lb" is set, then a lower bound should be constructed.
1968 * If "ub" is set, then an upper bound should be constructed.
1970 * The calling function has already checked that the inequality does not
1971 * reference "div", but we still need to check that the inequality is
1972 * of the right form. We'll consider the case where we want to construct
1973 * a lower bound. The construction of upper bounds is similar.
1975 * Let "div" be of the form
1977 * q = floor((a + f(x))/d)
1979 * We essentially check if constraint "i" is of the form
1981 * b + f(x) >= 0
1983 * so that we can use it to derive a lower bound on "div".
1984 * However, we allow a slightly more general form
1986 * b + g(x) >= 0
1988 * with the condition that the coefficients of g(x) - f(x) are all
1989 * divisible by d.
1990 * Rewriting this constraint as
1992 * 0 >= -b - g(x)
1994 * adding a + f(x) to both sides and dividing by d, we obtain
1996 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1998 * Taking the floor on both sides, we obtain
2000 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2002 * or
2004 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2006 * In the case of an upper bound, we construct the constraint
2008 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2011 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2012 __isl_take isl_basic_map *bmap, int div, int i,
2013 unsigned total, isl_int v, int lb, int ub)
2015 int j;
2017 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2018 if (lb) {
2019 isl_int_sub(v, bmap->ineq[i][1 + j],
2020 bmap->div[div][1 + 1 + j]);
2021 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2023 if (ub) {
2024 isl_int_add(v, bmap->ineq[i][1 + j],
2025 bmap->div[div][1 + 1 + j]);
2026 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2029 if (!lb && !ub)
2030 return bmap;
2032 bmap = isl_basic_map_cow(bmap);
2033 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2034 if (lb) {
2035 int k = isl_basic_map_alloc_inequality(bmap);
2036 if (k < 0)
2037 goto error;
2038 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2039 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2040 bmap->div[div][1 + j]);
2041 isl_int_cdiv_q(bmap->ineq[k][j],
2042 bmap->ineq[k][j], bmap->div[div][0]);
2044 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2046 if (ub) {
2047 int k = isl_basic_map_alloc_inequality(bmap);
2048 if (k < 0)
2049 goto error;
2050 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2051 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2052 bmap->div[div][1 + j]);
2053 isl_int_fdiv_q(bmap->ineq[k][j],
2054 bmap->ineq[k][j], bmap->div[div][0]);
2056 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2059 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2060 return bmap;
2061 error:
2062 isl_basic_map_free(bmap);
2063 return NULL;
2066 /* This function is called right before "div" is eliminated from "bmap"
2067 * using Fourier-Motzkin.
2068 * Look through the constraints of "bmap" for constraints on the argument
2069 * of the integer division and use them to construct constraints on the
2070 * integer division itself. These constraints can then be combined
2071 * during the Fourier-Motzkin elimination.
2072 * Note that it is only useful to introduce lower bounds on "div"
2073 * if "bmap" already contains upper bounds on "div" as the newly
2074 * introduce lower bounds can then be combined with the pre-existing
2075 * upper bounds. Similarly for upper bounds.
2076 * We therefore first check if "bmap" contains any lower and/or upper bounds
2077 * on "div".
2079 * It is interesting to note that the introduction of these constraints
2080 * can indeed lead to more accurate results, even when compared to
2081 * deriving constraints on the argument of "div" from constraints on "div".
2082 * Consider, for example, the set
2084 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2086 * The second constraint can be rewritten as
2088 * 2 * [(-i-2j+3)/4] + k >= 0
2090 * from which we can derive
2092 * -i - 2j + 3 >= -2k
2094 * or
2096 * i + 2j <= 3 + 2k
2098 * Combined with the first constraint, we obtain
2100 * -3 <= 3 + 2k or k >= -3
2102 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2103 * the first constraint, we obtain
2105 * [(i + 2j)/4] >= [-3/4] = -1
2107 * Combining this constraint with the second constraint, we obtain
2109 * k >= -2
2111 static __isl_give isl_basic_map *insert_bounds_on_div(
2112 __isl_take isl_basic_map *bmap, int div)
2114 int i;
2115 int check_lb, check_ub;
2116 isl_int v;
2117 unsigned total;
2119 if (!bmap)
2120 return NULL;
2122 if (isl_int_is_zero(bmap->div[div][0]))
2123 return bmap;
2125 total = isl_space_dim(bmap->dim, isl_dim_all);
2127 check_lb = 0;
2128 check_ub = 0;
2129 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2130 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2131 if (s > 0)
2132 check_ub = 1;
2133 if (s < 0)
2134 check_lb = 1;
2137 if (!check_lb && !check_ub)
2138 return bmap;
2140 isl_int_init(v);
2142 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2143 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2144 continue;
2146 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2147 check_lb, check_ub);
2150 isl_int_clear(v);
2152 return bmap;
2155 /* Remove all divs (recursively) involving any of the given dimensions
2156 * in their definitions.
2158 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2159 __isl_take isl_basic_map *bmap,
2160 enum isl_dim_type type, unsigned first, unsigned n)
2162 int i;
2164 if (!bmap)
2165 return NULL;
2166 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2167 goto error);
2168 first += isl_basic_map_offset(bmap, type);
2170 for (i = bmap->n_div - 1; i >= 0; --i) {
2171 if (!div_involves_vars(bmap, i, first, n))
2172 continue;
2173 bmap = insert_bounds_on_div(bmap, i);
2174 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2175 if (!bmap)
2176 return NULL;
2177 i = bmap->n_div;
2180 return bmap;
2181 error:
2182 isl_basic_map_free(bmap);
2183 return NULL;
2186 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2187 __isl_take isl_basic_set *bset,
2188 enum isl_dim_type type, unsigned first, unsigned n)
2190 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2193 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2194 enum isl_dim_type type, unsigned first, unsigned n)
2196 int i;
2198 if (!map)
2199 return NULL;
2200 if (map->n == 0)
2201 return map;
2203 map = isl_map_cow(map);
2204 if (!map)
2205 return NULL;
2207 for (i = 0; i < map->n; ++i) {
2208 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2209 type, first, n);
2210 if (!map->p[i])
2211 goto error;
2213 return map;
2214 error:
2215 isl_map_free(map);
2216 return NULL;
2219 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2220 enum isl_dim_type type, unsigned first, unsigned n)
2222 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2223 type, first, n);
2226 /* Does the desciption of "bmap" depend on the specified dimensions?
2227 * We also check whether the dimensions appear in any of the div definitions.
2228 * In principle there is no need for this check. If the dimensions appear
2229 * in a div definition, they also appear in the defining constraints of that
2230 * div.
2232 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2233 enum isl_dim_type type, unsigned first, unsigned n)
2235 int i;
2237 if (!bmap)
2238 return isl_bool_error;
2240 if (first + n > isl_basic_map_dim(bmap, type))
2241 isl_die(bmap->ctx, isl_error_invalid,
2242 "index out of bounds", return isl_bool_error);
2244 first += isl_basic_map_offset(bmap, type);
2245 for (i = 0; i < bmap->n_eq; ++i)
2246 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2247 return isl_bool_true;
2248 for (i = 0; i < bmap->n_ineq; ++i)
2249 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2250 return isl_bool_true;
2251 for (i = 0; i < bmap->n_div; ++i) {
2252 if (isl_int_is_zero(bmap->div[i][0]))
2253 continue;
2254 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2255 return isl_bool_true;
2258 return isl_bool_false;
2261 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2262 enum isl_dim_type type, unsigned first, unsigned n)
2264 int i;
2266 if (!map)
2267 return isl_bool_error;
2269 if (first + n > isl_map_dim(map, type))
2270 isl_die(map->ctx, isl_error_invalid,
2271 "index out of bounds", return isl_bool_error);
2273 for (i = 0; i < map->n; ++i) {
2274 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2275 type, first, n);
2276 if (involves < 0 || involves)
2277 return involves;
2280 return isl_bool_false;
2283 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2284 enum isl_dim_type type, unsigned first, unsigned n)
2286 return isl_basic_map_involves_dims(bset, type, first, n);
2289 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2290 enum isl_dim_type type, unsigned first, unsigned n)
2292 return isl_map_involves_dims(set, type, first, n);
2295 /* Return true if the definition of the given div is unknown or depends
2296 * on unknown divs.
2298 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2300 int i;
2301 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2303 if (isl_int_is_zero(bmap->div[div][0]))
2304 return 1;
2306 for (i = bmap->n_div - 1; i >= 0; --i) {
2307 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2308 continue;
2309 if (div_is_unknown(bmap, i))
2310 return 1;
2313 return 0;
2316 /* Remove all divs that are unknown or defined in terms of unknown divs.
2318 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2319 __isl_take isl_basic_map *bmap)
2321 int i;
2323 if (!bmap)
2324 return NULL;
2326 for (i = bmap->n_div - 1; i >= 0; --i) {
2327 if (!div_is_unknown(bmap, i))
2328 continue;
2329 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2330 if (!bmap)
2331 return NULL;
2332 i = bmap->n_div;
2335 return bmap;
2338 /* Remove all divs that are unknown or defined in terms of unknown divs.
2340 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2341 __isl_take isl_basic_set *bset)
2343 return isl_basic_map_remove_unknown_divs(bset);
2346 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2348 int i;
2350 if (!map)
2351 return NULL;
2352 if (map->n == 0)
2353 return map;
2355 map = isl_map_cow(map);
2356 if (!map)
2357 return NULL;
2359 for (i = 0; i < map->n; ++i) {
2360 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2361 if (!map->p[i])
2362 goto error;
2364 return map;
2365 error:
2366 isl_map_free(map);
2367 return NULL;
2370 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2372 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2375 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2376 __isl_take isl_basic_set *bset,
2377 enum isl_dim_type type, unsigned first, unsigned n)
2379 return (isl_basic_set *)
2380 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2383 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2384 enum isl_dim_type type, unsigned first, unsigned n)
2386 int i;
2388 if (n == 0)
2389 return map;
2391 map = isl_map_cow(map);
2392 if (!map)
2393 return NULL;
2394 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2396 for (i = 0; i < map->n; ++i) {
2397 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2398 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2399 if (!map->p[i])
2400 goto error;
2402 map = isl_map_drop(map, type, first, n);
2403 return map;
2404 error:
2405 isl_map_free(map);
2406 return NULL;
2409 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2410 enum isl_dim_type type, unsigned first, unsigned n)
2412 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2415 /* Project out n inputs starting at first using Fourier-Motzkin */
2416 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2417 unsigned first, unsigned n)
2419 return isl_map_remove_dims(map, isl_dim_in, first, n);
2422 static void dump_term(struct isl_basic_map *bmap,
2423 isl_int c, int pos, FILE *out)
2425 const char *name;
2426 unsigned in = isl_basic_map_n_in(bmap);
2427 unsigned dim = in + isl_basic_map_n_out(bmap);
2428 unsigned nparam = isl_basic_map_n_param(bmap);
2429 if (!pos)
2430 isl_int_print(out, c, 0);
2431 else {
2432 if (!isl_int_is_one(c))
2433 isl_int_print(out, c, 0);
2434 if (pos < 1 + nparam) {
2435 name = isl_space_get_dim_name(bmap->dim,
2436 isl_dim_param, pos - 1);
2437 if (name)
2438 fprintf(out, "%s", name);
2439 else
2440 fprintf(out, "p%d", pos - 1);
2441 } else if (pos < 1 + nparam + in)
2442 fprintf(out, "i%d", pos - 1 - nparam);
2443 else if (pos < 1 + nparam + dim)
2444 fprintf(out, "o%d", pos - 1 - nparam - in);
2445 else
2446 fprintf(out, "e%d", pos - 1 - nparam - dim);
2450 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2451 int sign, FILE *out)
2453 int i;
2454 int first;
2455 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2456 isl_int v;
2458 isl_int_init(v);
2459 for (i = 0, first = 1; i < len; ++i) {
2460 if (isl_int_sgn(c[i]) * sign <= 0)
2461 continue;
2462 if (!first)
2463 fprintf(out, " + ");
2464 first = 0;
2465 isl_int_abs(v, c[i]);
2466 dump_term(bmap, v, i, out);
2468 isl_int_clear(v);
2469 if (first)
2470 fprintf(out, "0");
2473 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2474 const char *op, FILE *out, int indent)
2476 int i;
2478 fprintf(out, "%*s", indent, "");
2480 dump_constraint_sign(bmap, c, 1, out);
2481 fprintf(out, " %s ", op);
2482 dump_constraint_sign(bmap, c, -1, out);
2484 fprintf(out, "\n");
2486 for (i = bmap->n_div; i < bmap->extra; ++i) {
2487 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2488 continue;
2489 fprintf(out, "%*s", indent, "");
2490 fprintf(out, "ERROR: unused div coefficient not zero\n");
2491 abort();
2495 static void dump_constraints(struct isl_basic_map *bmap,
2496 isl_int **c, unsigned n,
2497 const char *op, FILE *out, int indent)
2499 int i;
2501 for (i = 0; i < n; ++i)
2502 dump_constraint(bmap, c[i], op, out, indent);
2505 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2507 int j;
2508 int first = 1;
2509 unsigned total = isl_basic_map_total_dim(bmap);
2511 for (j = 0; j < 1 + total; ++j) {
2512 if (isl_int_is_zero(exp[j]))
2513 continue;
2514 if (!first && isl_int_is_pos(exp[j]))
2515 fprintf(out, "+");
2516 dump_term(bmap, exp[j], j, out);
2517 first = 0;
2521 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2523 int i;
2525 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2526 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2528 for (i = 0; i < bmap->n_div; ++i) {
2529 fprintf(out, "%*s", indent, "");
2530 fprintf(out, "e%d = [(", i);
2531 dump_affine(bmap, bmap->div[i]+1, out);
2532 fprintf(out, ")/");
2533 isl_int_print(out, bmap->div[i][0], 0);
2534 fprintf(out, "]\n");
2538 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2539 FILE *out, int indent)
2541 if (!bset) {
2542 fprintf(out, "null basic set\n");
2543 return;
2546 fprintf(out, "%*s", indent, "");
2547 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2548 bset->ref, bset->dim->nparam, bset->dim->n_out,
2549 bset->extra, bset->flags);
2550 dump((struct isl_basic_map *)bset, out, indent);
2553 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2554 FILE *out, int indent)
2556 if (!bmap) {
2557 fprintf(out, "null basic map\n");
2558 return;
2561 fprintf(out, "%*s", indent, "");
2562 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2563 "flags: %x, n_name: %d\n",
2564 bmap->ref,
2565 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2566 bmap->extra, bmap->flags, bmap->dim->n_id);
2567 dump(bmap, out, indent);
2570 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2572 unsigned total;
2573 if (!bmap)
2574 return -1;
2575 total = isl_basic_map_total_dim(bmap);
2576 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2577 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2578 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2579 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2580 return 0;
2583 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2584 unsigned flags)
2586 struct isl_set *set;
2588 if (!dim)
2589 return NULL;
2590 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2591 isl_assert(dim->ctx, n >= 0, goto error);
2592 set = isl_alloc(dim->ctx, struct isl_set,
2593 sizeof(struct isl_set) +
2594 (n - 1) * sizeof(struct isl_basic_set *));
2595 if (!set)
2596 goto error;
2598 set->ctx = dim->ctx;
2599 isl_ctx_ref(set->ctx);
2600 set->ref = 1;
2601 set->size = n;
2602 set->n = 0;
2603 set->dim = dim;
2604 set->flags = flags;
2605 return set;
2606 error:
2607 isl_space_free(dim);
2608 return NULL;
2611 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2612 unsigned nparam, unsigned dim, int n, unsigned flags)
2614 struct isl_set *set;
2615 isl_space *dims;
2617 dims = isl_space_alloc(ctx, nparam, 0, dim);
2618 if (!dims)
2619 return NULL;
2621 set = isl_set_alloc_space(dims, n, flags);
2622 return set;
2625 /* Make sure "map" has room for at least "n" more basic maps.
2627 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2629 int i;
2630 struct isl_map *grown = NULL;
2632 if (!map)
2633 return NULL;
2634 isl_assert(map->ctx, n >= 0, goto error);
2635 if (map->n + n <= map->size)
2636 return map;
2637 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2638 if (!grown)
2639 goto error;
2640 for (i = 0; i < map->n; ++i) {
2641 grown->p[i] = isl_basic_map_copy(map->p[i]);
2642 if (!grown->p[i])
2643 goto error;
2644 grown->n++;
2646 isl_map_free(map);
2647 return grown;
2648 error:
2649 isl_map_free(grown);
2650 isl_map_free(map);
2651 return NULL;
2654 /* Make sure "set" has room for at least "n" more basic sets.
2656 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2658 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2661 struct isl_set *isl_set_dup(struct isl_set *set)
2663 int i;
2664 struct isl_set *dup;
2666 if (!set)
2667 return NULL;
2669 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2670 if (!dup)
2671 return NULL;
2672 for (i = 0; i < set->n; ++i)
2673 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2674 return dup;
2677 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2679 return isl_map_from_basic_map(bset);
2682 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2684 struct isl_map *map;
2686 if (!bmap)
2687 return NULL;
2689 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2690 return isl_map_add_basic_map(map, bmap);
2693 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2694 __isl_take isl_basic_set *bset)
2696 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2697 (struct isl_basic_map *)bset);
2700 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2702 int i;
2704 if (!set)
2705 return NULL;
2707 if (--set->ref > 0)
2708 return NULL;
2710 isl_ctx_deref(set->ctx);
2711 for (i = 0; i < set->n; ++i)
2712 isl_basic_set_free(set->p[i]);
2713 isl_space_free(set->dim);
2714 free(set);
2716 return NULL;
2719 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2721 int i;
2723 if (!set) {
2724 fprintf(out, "null set\n");
2725 return;
2728 fprintf(out, "%*s", indent, "");
2729 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2730 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2731 set->flags);
2732 for (i = 0; i < set->n; ++i) {
2733 fprintf(out, "%*s", indent, "");
2734 fprintf(out, "basic set %d:\n", i);
2735 isl_basic_set_print_internal(set->p[i], out, indent+4);
2739 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2741 int i;
2743 if (!map) {
2744 fprintf(out, "null map\n");
2745 return;
2748 fprintf(out, "%*s", indent, "");
2749 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2750 "flags: %x, n_name: %d\n",
2751 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2752 map->dim->n_out, map->flags, map->dim->n_id);
2753 for (i = 0; i < map->n; ++i) {
2754 fprintf(out, "%*s", indent, "");
2755 fprintf(out, "basic map %d:\n", i);
2756 isl_basic_map_print_internal(map->p[i], out, indent+4);
2760 struct isl_basic_map *isl_basic_map_intersect_domain(
2761 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2763 struct isl_basic_map *bmap_domain;
2765 if (!bmap || !bset)
2766 goto error;
2768 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2769 bset->dim, isl_dim_param), goto error);
2771 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2772 isl_assert(bset->ctx,
2773 isl_basic_map_compatible_domain(bmap, bset), goto error);
2775 bmap = isl_basic_map_cow(bmap);
2776 if (!bmap)
2777 goto error;
2778 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2779 bset->n_div, bset->n_eq, bset->n_ineq);
2780 bmap_domain = isl_basic_map_from_domain(bset);
2781 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2783 bmap = isl_basic_map_simplify(bmap);
2784 return isl_basic_map_finalize(bmap);
2785 error:
2786 isl_basic_map_free(bmap);
2787 isl_basic_set_free(bset);
2788 return NULL;
2791 struct isl_basic_map *isl_basic_map_intersect_range(
2792 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2794 struct isl_basic_map *bmap_range;
2796 if (!bmap || !bset)
2797 goto error;
2799 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2800 bset->dim, isl_dim_param), goto error);
2802 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2803 isl_assert(bset->ctx,
2804 isl_basic_map_compatible_range(bmap, bset), goto error);
2806 if (isl_basic_set_plain_is_universe(bset)) {
2807 isl_basic_set_free(bset);
2808 return bmap;
2811 bmap = isl_basic_map_cow(bmap);
2812 if (!bmap)
2813 goto error;
2814 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2815 bset->n_div, bset->n_eq, bset->n_ineq);
2816 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2817 bmap = add_constraints(bmap, bmap_range, 0, 0);
2819 bmap = isl_basic_map_simplify(bmap);
2820 return isl_basic_map_finalize(bmap);
2821 error:
2822 isl_basic_map_free(bmap);
2823 isl_basic_set_free(bset);
2824 return NULL;
2827 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2828 __isl_keep isl_vec *vec)
2830 int i;
2831 unsigned total;
2832 isl_int s;
2834 if (!bmap || !vec)
2835 return isl_bool_error;
2837 total = 1 + isl_basic_map_total_dim(bmap);
2838 if (total != vec->size)
2839 return isl_bool_error;
2841 isl_int_init(s);
2843 for (i = 0; i < bmap->n_eq; ++i) {
2844 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2845 if (!isl_int_is_zero(s)) {
2846 isl_int_clear(s);
2847 return isl_bool_false;
2851 for (i = 0; i < bmap->n_ineq; ++i) {
2852 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2853 if (isl_int_is_neg(s)) {
2854 isl_int_clear(s);
2855 return isl_bool_false;
2859 isl_int_clear(s);
2861 return isl_bool_true;
2864 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2865 __isl_keep isl_vec *vec)
2867 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2870 struct isl_basic_map *isl_basic_map_intersect(
2871 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2873 struct isl_vec *sample = NULL;
2875 if (!bmap1 || !bmap2)
2876 goto error;
2878 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2879 bmap2->dim, isl_dim_param), goto error);
2880 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2881 isl_space_dim(bmap1->dim, isl_dim_param) &&
2882 isl_space_dim(bmap2->dim, isl_dim_all) !=
2883 isl_space_dim(bmap2->dim, isl_dim_param))
2884 return isl_basic_map_intersect(bmap2, bmap1);
2886 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2887 isl_space_dim(bmap2->dim, isl_dim_param))
2888 isl_assert(bmap1->ctx,
2889 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2891 if (isl_basic_map_plain_is_empty(bmap1)) {
2892 isl_basic_map_free(bmap2);
2893 return bmap1;
2895 if (isl_basic_map_plain_is_empty(bmap2)) {
2896 isl_basic_map_free(bmap1);
2897 return bmap2;
2900 if (bmap1->sample &&
2901 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2902 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2903 sample = isl_vec_copy(bmap1->sample);
2904 else if (bmap2->sample &&
2905 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2906 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2907 sample = isl_vec_copy(bmap2->sample);
2909 bmap1 = isl_basic_map_cow(bmap1);
2910 if (!bmap1)
2911 goto error;
2912 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2913 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2914 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2916 if (!bmap1)
2917 isl_vec_free(sample);
2918 else if (sample) {
2919 isl_vec_free(bmap1->sample);
2920 bmap1->sample = sample;
2923 bmap1 = isl_basic_map_simplify(bmap1);
2924 return isl_basic_map_finalize(bmap1);
2925 error:
2926 if (sample)
2927 isl_vec_free(sample);
2928 isl_basic_map_free(bmap1);
2929 isl_basic_map_free(bmap2);
2930 return NULL;
2933 struct isl_basic_set *isl_basic_set_intersect(
2934 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2936 return (struct isl_basic_set *)
2937 isl_basic_map_intersect(
2938 (struct isl_basic_map *)bset1,
2939 (struct isl_basic_map *)bset2);
2942 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2943 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2945 return isl_basic_set_intersect(bset1, bset2);
2948 /* Special case of isl_map_intersect, where both map1 and map2
2949 * are convex, without any divs and such that either map1 or map2
2950 * contains a single constraint. This constraint is then simply
2951 * added to the other map.
2953 static __isl_give isl_map *map_intersect_add_constraint(
2954 __isl_take isl_map *map1, __isl_take isl_map *map2)
2956 isl_assert(map1->ctx, map1->n == 1, goto error);
2957 isl_assert(map2->ctx, map1->n == 1, goto error);
2958 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2959 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2961 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2962 return isl_map_intersect(map2, map1);
2964 isl_assert(map2->ctx,
2965 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2967 map1 = isl_map_cow(map1);
2968 if (!map1)
2969 goto error;
2970 if (isl_map_plain_is_empty(map1)) {
2971 isl_map_free(map2);
2972 return map1;
2974 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2975 if (map2->p[0]->n_eq == 1)
2976 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2977 else
2978 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2979 map2->p[0]->ineq[0]);
2981 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2982 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2983 if (!map1->p[0])
2984 goto error;
2986 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2987 isl_basic_map_free(map1->p[0]);
2988 map1->n = 0;
2991 isl_map_free(map2);
2993 return map1;
2994 error:
2995 isl_map_free(map1);
2996 isl_map_free(map2);
2997 return NULL;
3000 /* map2 may be either a parameter domain or a map living in the same
3001 * space as map1.
3003 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3004 __isl_take isl_map *map2)
3006 unsigned flags = 0;
3007 isl_map *result;
3008 int i, j;
3010 if (!map1 || !map2)
3011 goto error;
3013 if ((isl_map_plain_is_empty(map1) ||
3014 isl_map_plain_is_universe(map2)) &&
3015 isl_space_is_equal(map1->dim, map2->dim)) {
3016 isl_map_free(map2);
3017 return map1;
3019 if ((isl_map_plain_is_empty(map2) ||
3020 isl_map_plain_is_universe(map1)) &&
3021 isl_space_is_equal(map1->dim, map2->dim)) {
3022 isl_map_free(map1);
3023 return map2;
3026 if (map1->n == 1 && map2->n == 1 &&
3027 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3028 isl_space_is_equal(map1->dim, map2->dim) &&
3029 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3030 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3031 return map_intersect_add_constraint(map1, map2);
3033 if (isl_space_dim(map2->dim, isl_dim_all) !=
3034 isl_space_dim(map2->dim, isl_dim_param))
3035 isl_assert(map1->ctx,
3036 isl_space_is_equal(map1->dim, map2->dim), goto error);
3038 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3039 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3040 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3042 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3043 map1->n * map2->n, flags);
3044 if (!result)
3045 goto error;
3046 for (i = 0; i < map1->n; ++i)
3047 for (j = 0; j < map2->n; ++j) {
3048 struct isl_basic_map *part;
3049 part = isl_basic_map_intersect(
3050 isl_basic_map_copy(map1->p[i]),
3051 isl_basic_map_copy(map2->p[j]));
3052 if (isl_basic_map_is_empty(part) < 0)
3053 part = isl_basic_map_free(part);
3054 result = isl_map_add_basic_map(result, part);
3055 if (!result)
3056 goto error;
3058 isl_map_free(map1);
3059 isl_map_free(map2);
3060 return result;
3061 error:
3062 isl_map_free(map1);
3063 isl_map_free(map2);
3064 return NULL;
3067 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3068 __isl_take isl_map *map2)
3070 if (!map1 || !map2)
3071 goto error;
3072 if (!isl_space_is_equal(map1->dim, map2->dim))
3073 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3074 "spaces don't match", goto error);
3075 return map_intersect_internal(map1, map2);
3076 error:
3077 isl_map_free(map1);
3078 isl_map_free(map2);
3079 return NULL;
3082 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3083 __isl_take isl_map *map2)
3085 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3088 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3090 return (struct isl_set *)
3091 isl_map_intersect((struct isl_map *)set1,
3092 (struct isl_map *)set2);
3095 /* map_intersect_internal accepts intersections
3096 * with parameter domains, so we can just call that function.
3098 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3099 __isl_take isl_set *params)
3101 return map_intersect_internal(map, params);
3104 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3105 __isl_take isl_map *map2)
3107 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3110 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3111 __isl_take isl_set *params)
3113 return isl_map_intersect_params(set, params);
3116 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3118 isl_space *space;
3119 unsigned pos, n1, n2;
3121 if (!bmap)
3122 return NULL;
3123 bmap = isl_basic_map_cow(bmap);
3124 if (!bmap)
3125 return NULL;
3126 space = isl_space_reverse(isl_space_copy(bmap->dim));
3127 pos = isl_basic_map_offset(bmap, isl_dim_in);
3128 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3129 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3130 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3131 return isl_basic_map_reset_space(bmap, space);
3134 static __isl_give isl_basic_map *basic_map_space_reset(
3135 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3137 isl_space *space;
3139 if (!bmap)
3140 return NULL;
3141 if (!isl_space_is_named_or_nested(bmap->dim, type))
3142 return bmap;
3144 space = isl_basic_map_get_space(bmap);
3145 space = isl_space_reset(space, type);
3146 bmap = isl_basic_map_reset_space(bmap, space);
3147 return bmap;
3150 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3151 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3152 unsigned pos, unsigned n)
3154 isl_space *res_dim;
3155 struct isl_basic_map *res;
3156 struct isl_dim_map *dim_map;
3157 unsigned total, off;
3158 enum isl_dim_type t;
3160 if (n == 0)
3161 return basic_map_space_reset(bmap, type);
3163 if (!bmap)
3164 return NULL;
3166 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3168 total = isl_basic_map_total_dim(bmap) + n;
3169 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3170 off = 0;
3171 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3172 if (t != type) {
3173 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3174 } else {
3175 unsigned size = isl_basic_map_dim(bmap, t);
3176 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3177 0, pos, off);
3178 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3179 pos, size - pos, off + pos + n);
3181 off += isl_space_dim(res_dim, t);
3183 isl_dim_map_div(dim_map, bmap, off);
3185 res = isl_basic_map_alloc_space(res_dim,
3186 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3187 if (isl_basic_map_is_rational(bmap))
3188 res = isl_basic_map_set_rational(res);
3189 if (isl_basic_map_plain_is_empty(bmap)) {
3190 isl_basic_map_free(bmap);
3191 free(dim_map);
3192 return isl_basic_map_set_to_empty(res);
3194 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3195 return isl_basic_map_finalize(res);
3198 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3199 __isl_take isl_basic_set *bset,
3200 enum isl_dim_type type, unsigned pos, unsigned n)
3202 return isl_basic_map_insert_dims(bset, type, pos, n);
3205 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3206 enum isl_dim_type type, unsigned n)
3208 if (!bmap)
3209 return NULL;
3210 return isl_basic_map_insert_dims(bmap, type,
3211 isl_basic_map_dim(bmap, type), n);
3214 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3215 enum isl_dim_type type, unsigned n)
3217 if (!bset)
3218 return NULL;
3219 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3220 return isl_basic_map_add_dims(bset, type, n);
3221 error:
3222 isl_basic_set_free(bset);
3223 return NULL;
3226 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3227 enum isl_dim_type type)
3229 isl_space *space;
3231 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3232 return map;
3234 space = isl_map_get_space(map);
3235 space = isl_space_reset(space, type);
3236 map = isl_map_reset_space(map, space);
3237 return map;
3240 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3241 enum isl_dim_type type, unsigned pos, unsigned n)
3243 int i;
3245 if (n == 0)
3246 return map_space_reset(map, type);
3248 map = isl_map_cow(map);
3249 if (!map)
3250 return NULL;
3252 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3253 if (!map->dim)
3254 goto error;
3256 for (i = 0; i < map->n; ++i) {
3257 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3258 if (!map->p[i])
3259 goto error;
3262 return map;
3263 error:
3264 isl_map_free(map);
3265 return NULL;
3268 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3269 enum isl_dim_type type, unsigned pos, unsigned n)
3271 return isl_map_insert_dims(set, type, pos, n);
3274 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3275 enum isl_dim_type type, unsigned n)
3277 if (!map)
3278 return NULL;
3279 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3282 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3283 enum isl_dim_type type, unsigned n)
3285 if (!set)
3286 return NULL;
3287 isl_assert(set->ctx, type != isl_dim_in, goto error);
3288 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3289 error:
3290 isl_set_free(set);
3291 return NULL;
3294 __isl_give isl_basic_map *isl_basic_map_move_dims(
3295 __isl_take isl_basic_map *bmap,
3296 enum isl_dim_type dst_type, unsigned dst_pos,
3297 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3299 struct isl_dim_map *dim_map;
3300 struct isl_basic_map *res;
3301 enum isl_dim_type t;
3302 unsigned total, off;
3304 if (!bmap)
3305 return NULL;
3306 if (n == 0)
3307 return bmap;
3309 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3310 goto error);
3312 if (dst_type == src_type && dst_pos == src_pos)
3313 return bmap;
3315 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3317 if (pos(bmap->dim, dst_type) + dst_pos ==
3318 pos(bmap->dim, src_type) + src_pos +
3319 ((src_type < dst_type) ? n : 0)) {
3320 bmap = isl_basic_map_cow(bmap);
3321 if (!bmap)
3322 return NULL;
3324 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3325 src_type, src_pos, n);
3326 if (!bmap->dim)
3327 goto error;
3329 bmap = isl_basic_map_finalize(bmap);
3331 return bmap;
3334 total = isl_basic_map_total_dim(bmap);
3335 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3337 off = 0;
3338 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3339 unsigned size = isl_space_dim(bmap->dim, t);
3340 if (t == dst_type) {
3341 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3342 0, dst_pos, off);
3343 off += dst_pos;
3344 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3345 src_pos, n, off);
3346 off += n;
3347 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3348 dst_pos, size - dst_pos, off);
3349 off += size - dst_pos;
3350 } else if (t == src_type) {
3351 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3352 0, src_pos, off);
3353 off += src_pos;
3354 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3355 src_pos + n, size - src_pos - n, off);
3356 off += size - src_pos - n;
3357 } else {
3358 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3359 off += size;
3362 isl_dim_map_div(dim_map, bmap, off);
3364 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3365 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3366 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3367 if (!bmap)
3368 goto error;
3370 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3371 src_type, src_pos, n);
3372 if (!bmap->dim)
3373 goto error;
3375 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3376 bmap = isl_basic_map_gauss(bmap, NULL);
3377 bmap = isl_basic_map_finalize(bmap);
3379 return bmap;
3380 error:
3381 isl_basic_map_free(bmap);
3382 return NULL;
3385 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3386 enum isl_dim_type dst_type, unsigned dst_pos,
3387 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3389 return (isl_basic_set *)isl_basic_map_move_dims(
3390 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3393 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3394 enum isl_dim_type dst_type, unsigned dst_pos,
3395 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3397 if (!set)
3398 return NULL;
3399 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3400 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3401 src_type, src_pos, n);
3402 error:
3403 isl_set_free(set);
3404 return NULL;
3407 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3408 enum isl_dim_type dst_type, unsigned dst_pos,
3409 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3411 int i;
3413 if (!map)
3414 return NULL;
3415 if (n == 0)
3416 return map;
3418 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3419 goto error);
3421 if (dst_type == src_type && dst_pos == src_pos)
3422 return map;
3424 isl_assert(map->ctx, dst_type != src_type, goto error);
3426 map = isl_map_cow(map);
3427 if (!map)
3428 return NULL;
3430 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3431 if (!map->dim)
3432 goto error;
3434 for (i = 0; i < map->n; ++i) {
3435 map->p[i] = isl_basic_map_move_dims(map->p[i],
3436 dst_type, dst_pos,
3437 src_type, src_pos, n);
3438 if (!map->p[i])
3439 goto error;
3442 return map;
3443 error:
3444 isl_map_free(map);
3445 return NULL;
3448 /* Move the specified dimensions to the last columns right before
3449 * the divs. Don't change the dimension specification of bmap.
3450 * That's the responsibility of the caller.
3452 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3453 enum isl_dim_type type, unsigned first, unsigned n)
3455 struct isl_dim_map *dim_map;
3456 struct isl_basic_map *res;
3457 enum isl_dim_type t;
3458 unsigned total, off;
3460 if (!bmap)
3461 return NULL;
3462 if (pos(bmap->dim, type) + first + n ==
3463 1 + isl_space_dim(bmap->dim, isl_dim_all))
3464 return bmap;
3466 total = isl_basic_map_total_dim(bmap);
3467 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3469 off = 0;
3470 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3471 unsigned size = isl_space_dim(bmap->dim, t);
3472 if (t == type) {
3473 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3474 0, first, off);
3475 off += first;
3476 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3477 first, n, total - bmap->n_div - n);
3478 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3479 first + n, size - (first + n), off);
3480 off += size - (first + n);
3481 } else {
3482 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3483 off += size;
3486 isl_dim_map_div(dim_map, bmap, off + n);
3488 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3489 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3490 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3491 return res;
3494 /* Insert "n" rows in the divs of "bmap".
3496 * The number of columns is not changed, which means that the last
3497 * dimensions of "bmap" are being reintepreted as the new divs.
3498 * The space of "bmap" is not adjusted, however, which means
3499 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3500 * from the space of "bmap" is the responsibility of the caller.
3502 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3503 int n)
3505 int i;
3506 size_t row_size;
3507 isl_int **new_div;
3508 isl_int *old;
3510 bmap = isl_basic_map_cow(bmap);
3511 if (!bmap)
3512 return NULL;
3514 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3515 old = bmap->block2.data;
3516 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3517 (bmap->extra + n) * (1 + row_size));
3518 if (!bmap->block2.data)
3519 return isl_basic_map_free(bmap);
3520 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3521 if (!new_div)
3522 return isl_basic_map_free(bmap);
3523 for (i = 0; i < n; ++i) {
3524 new_div[i] = bmap->block2.data +
3525 (bmap->extra + i) * (1 + row_size);
3526 isl_seq_clr(new_div[i], 1 + row_size);
3528 for (i = 0; i < bmap->extra; ++i)
3529 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3530 free(bmap->div);
3531 bmap->div = new_div;
3532 bmap->n_div += n;
3533 bmap->extra += n;
3535 return bmap;
3538 /* Drop constraints from "bmap" that only involve the variables
3539 * of "type" in the range [first, first + n] that are not related
3540 * to any of the variables outside that interval.
3541 * These constraints cannot influence the values for the variables
3542 * outside the interval, except in case they cause "bmap" to be empty.
3543 * Only drop the constraints if "bmap" is known to be non-empty.
3545 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3546 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3547 unsigned first, unsigned n)
3549 int i;
3550 int *groups;
3551 unsigned dim, n_div;
3552 isl_bool non_empty;
3554 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3555 if (non_empty < 0)
3556 return isl_basic_map_free(bmap);
3557 if (!non_empty)
3558 return bmap;
3560 dim = isl_basic_map_dim(bmap, isl_dim_all);
3561 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3562 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3563 if (!groups)
3564 return isl_basic_map_free(bmap);
3565 first += isl_basic_map_offset(bmap, type) - 1;
3566 for (i = 0; i < first; ++i)
3567 groups[i] = -1;
3568 for (i = first + n; i < dim - n_div; ++i)
3569 groups[i] = -1;
3571 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3573 return bmap;
3576 /* Turn the n dimensions of type type, starting at first
3577 * into existentially quantified variables.
3579 * If a subset of the projected out variables are unrelated
3580 * to any of the variables that remain, then the constraints
3581 * involving this subset are simply dropped first.
3583 __isl_give isl_basic_map *isl_basic_map_project_out(
3584 __isl_take isl_basic_map *bmap,
3585 enum isl_dim_type type, unsigned first, unsigned n)
3587 if (n == 0)
3588 return basic_map_space_reset(bmap, type);
3589 if (type == isl_dim_div)
3590 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3591 "cannot project out existentially quantified variables",
3592 return isl_basic_map_free(bmap));
3594 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3595 if (!bmap)
3596 return NULL;
3598 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3599 return isl_basic_map_remove_dims(bmap, type, first, n);
3601 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3602 goto error);
3604 bmap = move_last(bmap, type, first, n);
3605 bmap = isl_basic_map_cow(bmap);
3606 bmap = insert_div_rows(bmap, n);
3607 if (!bmap)
3608 return NULL;
3610 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3611 if (!bmap->dim)
3612 goto error;
3613 bmap = isl_basic_map_simplify(bmap);
3614 bmap = isl_basic_map_drop_redundant_divs(bmap);
3615 return isl_basic_map_finalize(bmap);
3616 error:
3617 isl_basic_map_free(bmap);
3618 return NULL;
3621 /* Turn the n dimensions of type type, starting at first
3622 * into existentially quantified variables.
3624 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3625 enum isl_dim_type type, unsigned first, unsigned n)
3627 return (isl_basic_set *)isl_basic_map_project_out(
3628 (isl_basic_map *)bset, type, first, n);
3631 /* Turn the n dimensions of type type, starting at first
3632 * into existentially quantified variables.
3634 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3635 enum isl_dim_type type, unsigned first, unsigned n)
3637 int i;
3639 if (!map)
3640 return NULL;
3642 if (n == 0)
3643 return map_space_reset(map, type);
3645 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3647 map = isl_map_cow(map);
3648 if (!map)
3649 return NULL;
3651 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3652 if (!map->dim)
3653 goto error;
3655 for (i = 0; i < map->n; ++i) {
3656 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3657 if (!map->p[i])
3658 goto error;
3661 return map;
3662 error:
3663 isl_map_free(map);
3664 return NULL;
3667 /* Turn the n dimensions of type type, starting at first
3668 * into existentially quantified variables.
3670 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3671 enum isl_dim_type type, unsigned first, unsigned n)
3673 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3676 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3678 int i, j;
3680 for (i = 0; i < n; ++i) {
3681 j = isl_basic_map_alloc_div(bmap);
3682 if (j < 0)
3683 goto error;
3684 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3686 return bmap;
3687 error:
3688 isl_basic_map_free(bmap);
3689 return NULL;
3692 struct isl_basic_map *isl_basic_map_apply_range(
3693 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3695 isl_space *dim_result = NULL;
3696 struct isl_basic_map *bmap;
3697 unsigned n_in, n_out, n, nparam, total, pos;
3698 struct isl_dim_map *dim_map1, *dim_map2;
3700 if (!bmap1 || !bmap2)
3701 goto error;
3702 if (!isl_space_match(bmap1->dim, isl_dim_param,
3703 bmap2->dim, isl_dim_param))
3704 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3705 "parameters don't match", goto error);
3706 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3707 bmap2->dim, isl_dim_in))
3708 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3709 "spaces don't match", goto error);
3711 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3712 isl_space_copy(bmap2->dim));
3714 n_in = isl_basic_map_n_in(bmap1);
3715 n_out = isl_basic_map_n_out(bmap2);
3716 n = isl_basic_map_n_out(bmap1);
3717 nparam = isl_basic_map_n_param(bmap1);
3719 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3720 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3721 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3722 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3723 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3724 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3725 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3726 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3727 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3728 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3729 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3731 bmap = isl_basic_map_alloc_space(dim_result,
3732 bmap1->n_div + bmap2->n_div + n,
3733 bmap1->n_eq + bmap2->n_eq,
3734 bmap1->n_ineq + bmap2->n_ineq);
3735 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3736 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3737 bmap = add_divs(bmap, n);
3738 bmap = isl_basic_map_simplify(bmap);
3739 bmap = isl_basic_map_drop_redundant_divs(bmap);
3740 return isl_basic_map_finalize(bmap);
3741 error:
3742 isl_basic_map_free(bmap1);
3743 isl_basic_map_free(bmap2);
3744 return NULL;
3747 struct isl_basic_set *isl_basic_set_apply(
3748 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3750 if (!bset || !bmap)
3751 goto error;
3753 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3754 goto error);
3756 return (struct isl_basic_set *)
3757 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3758 error:
3759 isl_basic_set_free(bset);
3760 isl_basic_map_free(bmap);
3761 return NULL;
3764 struct isl_basic_map *isl_basic_map_apply_domain(
3765 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3767 if (!bmap1 || !bmap2)
3768 goto error;
3770 isl_assert(bmap1->ctx,
3771 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3772 isl_assert(bmap1->ctx,
3773 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3774 goto error);
3776 bmap1 = isl_basic_map_reverse(bmap1);
3777 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3778 return isl_basic_map_reverse(bmap1);
3779 error:
3780 isl_basic_map_free(bmap1);
3781 isl_basic_map_free(bmap2);
3782 return NULL;
3785 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3786 * A \cap B -> f(A) + f(B)
3788 struct isl_basic_map *isl_basic_map_sum(
3789 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3791 unsigned n_in, n_out, nparam, total, pos;
3792 struct isl_basic_map *bmap = NULL;
3793 struct isl_dim_map *dim_map1, *dim_map2;
3794 int i;
3796 if (!bmap1 || !bmap2)
3797 goto error;
3799 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3800 goto error);
3802 nparam = isl_basic_map_n_param(bmap1);
3803 n_in = isl_basic_map_n_in(bmap1);
3804 n_out = isl_basic_map_n_out(bmap1);
3806 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3807 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3808 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3809 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3810 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3811 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3812 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3813 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3814 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3815 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3816 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3818 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3819 bmap1->n_div + bmap2->n_div + 2 * n_out,
3820 bmap1->n_eq + bmap2->n_eq + n_out,
3821 bmap1->n_ineq + bmap2->n_ineq);
3822 for (i = 0; i < n_out; ++i) {
3823 int j = isl_basic_map_alloc_equality(bmap);
3824 if (j < 0)
3825 goto error;
3826 isl_seq_clr(bmap->eq[j], 1+total);
3827 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3828 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3829 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3831 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3832 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3833 bmap = add_divs(bmap, 2 * n_out);
3835 bmap = isl_basic_map_simplify(bmap);
3836 return isl_basic_map_finalize(bmap);
3837 error:
3838 isl_basic_map_free(bmap);
3839 isl_basic_map_free(bmap1);
3840 isl_basic_map_free(bmap2);
3841 return NULL;
3844 /* Given two maps A -> f(A) and B -> g(B), construct a map
3845 * A \cap B -> f(A) + f(B)
3847 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3849 struct isl_map *result;
3850 int i, j;
3852 if (!map1 || !map2)
3853 goto error;
3855 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3857 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3858 map1->n * map2->n, 0);
3859 if (!result)
3860 goto error;
3861 for (i = 0; i < map1->n; ++i)
3862 for (j = 0; j < map2->n; ++j) {
3863 struct isl_basic_map *part;
3864 part = isl_basic_map_sum(
3865 isl_basic_map_copy(map1->p[i]),
3866 isl_basic_map_copy(map2->p[j]));
3867 if (isl_basic_map_is_empty(part))
3868 isl_basic_map_free(part);
3869 else
3870 result = isl_map_add_basic_map(result, part);
3871 if (!result)
3872 goto error;
3874 isl_map_free(map1);
3875 isl_map_free(map2);
3876 return result;
3877 error:
3878 isl_map_free(map1);
3879 isl_map_free(map2);
3880 return NULL;
3883 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3884 __isl_take isl_set *set2)
3886 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3889 /* Given a basic map A -> f(A), construct A -> -f(A).
3891 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3893 int i, j;
3894 unsigned off, n;
3896 bmap = isl_basic_map_cow(bmap);
3897 if (!bmap)
3898 return NULL;
3900 n = isl_basic_map_dim(bmap, isl_dim_out);
3901 off = isl_basic_map_offset(bmap, isl_dim_out);
3902 for (i = 0; i < bmap->n_eq; ++i)
3903 for (j = 0; j < n; ++j)
3904 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3905 for (i = 0; i < bmap->n_ineq; ++i)
3906 for (j = 0; j < n; ++j)
3907 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3908 for (i = 0; i < bmap->n_div; ++i)
3909 for (j = 0; j < n; ++j)
3910 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3911 bmap = isl_basic_map_gauss(bmap, NULL);
3912 return isl_basic_map_finalize(bmap);
3915 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3917 return isl_basic_map_neg(bset);
3920 /* Given a map A -> f(A), construct A -> -f(A).
3922 struct isl_map *isl_map_neg(struct isl_map *map)
3924 int i;
3926 map = isl_map_cow(map);
3927 if (!map)
3928 return NULL;
3930 for (i = 0; i < map->n; ++i) {
3931 map->p[i] = isl_basic_map_neg(map->p[i]);
3932 if (!map->p[i])
3933 goto error;
3936 return map;
3937 error:
3938 isl_map_free(map);
3939 return NULL;
3942 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3944 return (isl_set *)isl_map_neg((isl_map *)set);
3947 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3948 * A -> floor(f(A)/d).
3950 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3951 isl_int d)
3953 unsigned n_in, n_out, nparam, total, pos;
3954 struct isl_basic_map *result = NULL;
3955 struct isl_dim_map *dim_map;
3956 int i;
3958 if (!bmap)
3959 return NULL;
3961 nparam = isl_basic_map_n_param(bmap);
3962 n_in = isl_basic_map_n_in(bmap);
3963 n_out = isl_basic_map_n_out(bmap);
3965 total = nparam + n_in + n_out + bmap->n_div + n_out;
3966 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3967 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3968 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3969 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3970 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3972 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3973 bmap->n_div + n_out,
3974 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3975 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3976 result = add_divs(result, n_out);
3977 for (i = 0; i < n_out; ++i) {
3978 int j;
3979 j = isl_basic_map_alloc_inequality(result);
3980 if (j < 0)
3981 goto error;
3982 isl_seq_clr(result->ineq[j], 1+total);
3983 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3984 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3985 j = isl_basic_map_alloc_inequality(result);
3986 if (j < 0)
3987 goto error;
3988 isl_seq_clr(result->ineq[j], 1+total);
3989 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3990 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3991 isl_int_sub_ui(result->ineq[j][0], d, 1);
3994 result = isl_basic_map_simplify(result);
3995 return isl_basic_map_finalize(result);
3996 error:
3997 isl_basic_map_free(result);
3998 return NULL;
4001 /* Given a map A -> f(A) and an integer d, construct a map
4002 * A -> floor(f(A)/d).
4004 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4006 int i;
4008 map = isl_map_cow(map);
4009 if (!map)
4010 return NULL;
4012 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4013 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4014 for (i = 0; i < map->n; ++i) {
4015 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4016 if (!map->p[i])
4017 goto error;
4020 return map;
4021 error:
4022 isl_map_free(map);
4023 return NULL;
4026 /* Given a map A -> f(A) and an integer d, construct a map
4027 * A -> floor(f(A)/d).
4029 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4030 __isl_take isl_val *d)
4032 if (!map || !d)
4033 goto error;
4034 if (!isl_val_is_int(d))
4035 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4036 "expecting integer denominator", goto error);
4037 map = isl_map_floordiv(map, d->n);
4038 isl_val_free(d);
4039 return map;
4040 error:
4041 isl_map_free(map);
4042 isl_val_free(d);
4043 return NULL;
4046 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4048 int i;
4049 unsigned nparam;
4050 unsigned n_in;
4052 i = isl_basic_map_alloc_equality(bmap);
4053 if (i < 0)
4054 goto error;
4055 nparam = isl_basic_map_n_param(bmap);
4056 n_in = isl_basic_map_n_in(bmap);
4057 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4058 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4059 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4060 return isl_basic_map_finalize(bmap);
4061 error:
4062 isl_basic_map_free(bmap);
4063 return NULL;
4066 /* Add a constraints to "bmap" expressing i_pos < o_pos
4068 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4070 int i;
4071 unsigned nparam;
4072 unsigned n_in;
4074 i = isl_basic_map_alloc_inequality(bmap);
4075 if (i < 0)
4076 goto error;
4077 nparam = isl_basic_map_n_param(bmap);
4078 n_in = isl_basic_map_n_in(bmap);
4079 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4080 isl_int_set_si(bmap->ineq[i][0], -1);
4081 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4082 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4083 return isl_basic_map_finalize(bmap);
4084 error:
4085 isl_basic_map_free(bmap);
4086 return NULL;
4089 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4091 static __isl_give isl_basic_map *var_less_or_equal(
4092 __isl_take isl_basic_map *bmap, unsigned pos)
4094 int i;
4095 unsigned nparam;
4096 unsigned n_in;
4098 i = isl_basic_map_alloc_inequality(bmap);
4099 if (i < 0)
4100 goto error;
4101 nparam = isl_basic_map_n_param(bmap);
4102 n_in = isl_basic_map_n_in(bmap);
4103 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4104 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4105 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4106 return isl_basic_map_finalize(bmap);
4107 error:
4108 isl_basic_map_free(bmap);
4109 return NULL;
4112 /* Add a constraints to "bmap" expressing i_pos > o_pos
4114 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4116 int i;
4117 unsigned nparam;
4118 unsigned n_in;
4120 i = isl_basic_map_alloc_inequality(bmap);
4121 if (i < 0)
4122 goto error;
4123 nparam = isl_basic_map_n_param(bmap);
4124 n_in = isl_basic_map_n_in(bmap);
4125 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4126 isl_int_set_si(bmap->ineq[i][0], -1);
4127 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4128 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4129 return isl_basic_map_finalize(bmap);
4130 error:
4131 isl_basic_map_free(bmap);
4132 return NULL;
4135 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4137 static __isl_give isl_basic_map *var_more_or_equal(
4138 __isl_take isl_basic_map *bmap, unsigned pos)
4140 int i;
4141 unsigned nparam;
4142 unsigned n_in;
4144 i = isl_basic_map_alloc_inequality(bmap);
4145 if (i < 0)
4146 goto error;
4147 nparam = isl_basic_map_n_param(bmap);
4148 n_in = isl_basic_map_n_in(bmap);
4149 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4150 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4151 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4152 return isl_basic_map_finalize(bmap);
4153 error:
4154 isl_basic_map_free(bmap);
4155 return NULL;
4158 __isl_give isl_basic_map *isl_basic_map_equal(
4159 __isl_take isl_space *dim, unsigned n_equal)
4161 int i;
4162 struct isl_basic_map *bmap;
4163 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4164 if (!bmap)
4165 return NULL;
4166 for (i = 0; i < n_equal && bmap; ++i)
4167 bmap = var_equal(bmap, i);
4168 return isl_basic_map_finalize(bmap);
4171 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4173 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4174 unsigned pos)
4176 int i;
4177 struct isl_basic_map *bmap;
4178 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4179 if (!bmap)
4180 return NULL;
4181 for (i = 0; i < pos && bmap; ++i)
4182 bmap = var_equal(bmap, i);
4183 if (bmap)
4184 bmap = var_less(bmap, pos);
4185 return isl_basic_map_finalize(bmap);
4188 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4190 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4191 __isl_take isl_space *dim, unsigned pos)
4193 int i;
4194 isl_basic_map *bmap;
4196 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4197 for (i = 0; i < pos; ++i)
4198 bmap = var_equal(bmap, i);
4199 bmap = var_less_or_equal(bmap, pos);
4200 return isl_basic_map_finalize(bmap);
4203 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4205 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4206 unsigned pos)
4208 int i;
4209 struct isl_basic_map *bmap;
4210 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4211 if (!bmap)
4212 return NULL;
4213 for (i = 0; i < pos && bmap; ++i)
4214 bmap = var_equal(bmap, i);
4215 if (bmap)
4216 bmap = var_more(bmap, pos);
4217 return isl_basic_map_finalize(bmap);
4220 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4222 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4223 __isl_take isl_space *dim, unsigned pos)
4225 int i;
4226 isl_basic_map *bmap;
4228 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4229 for (i = 0; i < pos; ++i)
4230 bmap = var_equal(bmap, i);
4231 bmap = var_more_or_equal(bmap, pos);
4232 return isl_basic_map_finalize(bmap);
4235 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4236 unsigned n, int equal)
4238 struct isl_map *map;
4239 int i;
4241 if (n == 0 && equal)
4242 return isl_map_universe(dims);
4244 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4246 for (i = 0; i + 1 < n; ++i)
4247 map = isl_map_add_basic_map(map,
4248 isl_basic_map_less_at(isl_space_copy(dims), i));
4249 if (n > 0) {
4250 if (equal)
4251 map = isl_map_add_basic_map(map,
4252 isl_basic_map_less_or_equal_at(dims, n - 1));
4253 else
4254 map = isl_map_add_basic_map(map,
4255 isl_basic_map_less_at(dims, n - 1));
4256 } else
4257 isl_space_free(dims);
4259 return map;
4262 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4264 if (!dims)
4265 return NULL;
4266 return map_lex_lte_first(dims, dims->n_out, equal);
4269 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4271 return map_lex_lte_first(dim, n, 0);
4274 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4276 return map_lex_lte_first(dim, n, 1);
4279 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4281 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4284 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4286 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4289 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4290 unsigned n, int equal)
4292 struct isl_map *map;
4293 int i;
4295 if (n == 0 && equal)
4296 return isl_map_universe(dims);
4298 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4300 for (i = 0; i + 1 < n; ++i)
4301 map = isl_map_add_basic_map(map,
4302 isl_basic_map_more_at(isl_space_copy(dims), i));
4303 if (n > 0) {
4304 if (equal)
4305 map = isl_map_add_basic_map(map,
4306 isl_basic_map_more_or_equal_at(dims, n - 1));
4307 else
4308 map = isl_map_add_basic_map(map,
4309 isl_basic_map_more_at(dims, n - 1));
4310 } else
4311 isl_space_free(dims);
4313 return map;
4316 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4318 if (!dims)
4319 return NULL;
4320 return map_lex_gte_first(dims, dims->n_out, equal);
4323 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4325 return map_lex_gte_first(dim, n, 0);
4328 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4330 return map_lex_gte_first(dim, n, 1);
4333 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4335 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4338 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4340 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4343 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4344 __isl_take isl_set *set2)
4346 isl_map *map;
4347 map = isl_map_lex_le(isl_set_get_space(set1));
4348 map = isl_map_intersect_domain(map, set1);
4349 map = isl_map_intersect_range(map, set2);
4350 return map;
4353 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4354 __isl_take isl_set *set2)
4356 isl_map *map;
4357 map = isl_map_lex_lt(isl_set_get_space(set1));
4358 map = isl_map_intersect_domain(map, set1);
4359 map = isl_map_intersect_range(map, set2);
4360 return map;
4363 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4364 __isl_take isl_set *set2)
4366 isl_map *map;
4367 map = isl_map_lex_ge(isl_set_get_space(set1));
4368 map = isl_map_intersect_domain(map, set1);
4369 map = isl_map_intersect_range(map, set2);
4370 return map;
4373 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4374 __isl_take isl_set *set2)
4376 isl_map *map;
4377 map = isl_map_lex_gt(isl_set_get_space(set1));
4378 map = isl_map_intersect_domain(map, set1);
4379 map = isl_map_intersect_range(map, set2);
4380 return map;
4383 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4384 __isl_take isl_map *map2)
4386 isl_map *map;
4387 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4388 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4389 map = isl_map_apply_range(map, isl_map_reverse(map2));
4390 return map;
4393 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4394 __isl_take isl_map *map2)
4396 isl_map *map;
4397 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4398 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4399 map = isl_map_apply_range(map, isl_map_reverse(map2));
4400 return map;
4403 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4404 __isl_take isl_map *map2)
4406 isl_map *map;
4407 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4408 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4409 map = isl_map_apply_range(map, isl_map_reverse(map2));
4410 return map;
4413 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4414 __isl_take isl_map *map2)
4416 isl_map *map;
4417 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4418 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4419 map = isl_map_apply_range(map, isl_map_reverse(map2));
4420 return map;
4423 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4424 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4426 struct isl_basic_map *bmap;
4428 bset = isl_basic_set_cow(bset);
4429 if (!bset || !dim)
4430 goto error;
4432 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4433 isl_space_free(bset->dim);
4434 bmap = (struct isl_basic_map *) bset;
4435 bmap->dim = dim;
4436 return isl_basic_map_finalize(bmap);
4437 error:
4438 isl_basic_set_free(bset);
4439 isl_space_free(dim);
4440 return NULL;
4443 /* For a div d = floor(f/m), add the constraint
4445 * f - m d >= 0
4447 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4448 unsigned pos, isl_int *div)
4450 int i;
4451 unsigned total = isl_basic_map_total_dim(bmap);
4453 i = isl_basic_map_alloc_inequality(bmap);
4454 if (i < 0)
4455 return -1;
4456 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4457 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4459 return 0;
4462 /* For a div d = floor(f/m), add the constraint
4464 * -(f-(n-1)) + m d >= 0
4466 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4467 unsigned pos, isl_int *div)
4469 int i;
4470 unsigned total = isl_basic_map_total_dim(bmap);
4472 i = isl_basic_map_alloc_inequality(bmap);
4473 if (i < 0)
4474 return -1;
4475 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4476 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4477 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4478 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4480 return 0;
4483 /* For a div d = floor(f/m), add the constraints
4485 * f - m d >= 0
4486 * -(f-(n-1)) + m d >= 0
4488 * Note that the second constraint is the negation of
4490 * f - m d >= n
4492 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4493 unsigned pos, isl_int *div)
4495 if (add_upper_div_constraint(bmap, pos, div) < 0)
4496 return -1;
4497 if (add_lower_div_constraint(bmap, pos, div) < 0)
4498 return -1;
4499 return 0;
4502 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4503 unsigned pos, isl_int *div)
4505 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4506 pos, div);
4509 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4511 unsigned total = isl_basic_map_total_dim(bmap);
4512 unsigned div_pos = total - bmap->n_div + div;
4514 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4515 bmap->div[div]);
4518 /* For each known div d = floor(f/m), add the constraints
4520 * f - m d >= 0
4521 * -(f-(n-1)) + m d >= 0
4523 * Remove duplicate constraints in case of some these div constraints
4524 * already appear in "bmap".
4526 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4527 __isl_take isl_basic_map *bmap)
4529 unsigned n_div;
4531 if (!bmap)
4532 return NULL;
4533 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4534 if (n_div == 0)
4535 return bmap;
4537 bmap = add_known_div_constraints(bmap);
4538 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4539 bmap = isl_basic_map_finalize(bmap);
4540 return bmap;
4543 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4545 * In particular, if this div is of the form d = floor(f/m),
4546 * then add the constraint
4548 * f - m d >= 0
4550 * if sign < 0 or the constraint
4552 * -(f-(n-1)) + m d >= 0
4554 * if sign > 0.
4556 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4557 unsigned div, int sign)
4559 unsigned total;
4560 unsigned div_pos;
4562 if (!bmap)
4563 return -1;
4565 total = isl_basic_map_total_dim(bmap);
4566 div_pos = total - bmap->n_div + div;
4568 if (sign < 0)
4569 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4570 else
4571 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4574 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4576 return isl_basic_map_add_div_constraints(bset, div);
4579 struct isl_basic_set *isl_basic_map_underlying_set(
4580 struct isl_basic_map *bmap)
4582 if (!bmap)
4583 goto error;
4584 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4585 bmap->n_div == 0 &&
4586 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4587 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4588 return (struct isl_basic_set *)bmap;
4589 bmap = isl_basic_map_cow(bmap);
4590 if (!bmap)
4591 goto error;
4592 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4593 if (!bmap->dim)
4594 goto error;
4595 bmap->extra -= bmap->n_div;
4596 bmap->n_div = 0;
4597 bmap = isl_basic_map_finalize(bmap);
4598 return (struct isl_basic_set *)bmap;
4599 error:
4600 isl_basic_map_free(bmap);
4601 return NULL;
4604 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4605 __isl_take isl_basic_set *bset)
4607 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4610 /* Replace each element in "list" by the result of applying
4611 * isl_basic_map_underlying_set to the element.
4613 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4614 __isl_take isl_basic_map_list *list)
4616 int i, n;
4618 if (!list)
4619 return NULL;
4621 n = isl_basic_map_list_n_basic_map(list);
4622 for (i = 0; i < n; ++i) {
4623 isl_basic_map *bmap;
4624 isl_basic_set *bset;
4626 bmap = isl_basic_map_list_get_basic_map(list, i);
4627 bset = isl_basic_set_underlying_set(bmap);
4628 list = isl_basic_set_list_set_basic_set(list, i, bset);
4631 return list;
4634 struct isl_basic_map *isl_basic_map_overlying_set(
4635 struct isl_basic_set *bset, struct isl_basic_map *like)
4637 struct isl_basic_map *bmap;
4638 struct isl_ctx *ctx;
4639 unsigned total;
4640 int i;
4642 if (!bset || !like)
4643 goto error;
4644 ctx = bset->ctx;
4645 isl_assert(ctx, bset->n_div == 0, goto error);
4646 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4647 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4648 goto error);
4649 if (like->n_div == 0) {
4650 isl_space *space = isl_basic_map_get_space(like);
4651 isl_basic_map_free(like);
4652 return isl_basic_map_reset_space(bset, space);
4654 bset = isl_basic_set_cow(bset);
4655 if (!bset)
4656 goto error;
4657 total = bset->dim->n_out + bset->extra;
4658 bmap = (struct isl_basic_map *)bset;
4659 isl_space_free(bmap->dim);
4660 bmap->dim = isl_space_copy(like->dim);
4661 if (!bmap->dim)
4662 goto error;
4663 bmap->n_div = like->n_div;
4664 bmap->extra += like->n_div;
4665 if (bmap->extra) {
4666 unsigned ltotal;
4667 isl_int **div;
4668 ltotal = total - bmap->extra + like->extra;
4669 if (ltotal > total)
4670 ltotal = total;
4671 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4672 bmap->extra * (1 + 1 + total));
4673 if (isl_blk_is_error(bmap->block2))
4674 goto error;
4675 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4676 if (!div)
4677 goto error;
4678 bmap->div = div;
4679 for (i = 0; i < bmap->extra; ++i)
4680 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4681 for (i = 0; i < like->n_div; ++i) {
4682 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4683 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4685 bmap = isl_basic_map_add_known_div_constraints(bmap);
4687 isl_basic_map_free(like);
4688 bmap = isl_basic_map_simplify(bmap);
4689 bmap = isl_basic_map_finalize(bmap);
4690 return bmap;
4691 error:
4692 isl_basic_map_free(like);
4693 isl_basic_set_free(bset);
4694 return NULL;
4697 struct isl_basic_set *isl_basic_set_from_underlying_set(
4698 struct isl_basic_set *bset, struct isl_basic_set *like)
4700 return (struct isl_basic_set *)
4701 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4704 struct isl_set *isl_set_from_underlying_set(
4705 struct isl_set *set, struct isl_basic_set *like)
4707 int i;
4709 if (!set || !like)
4710 goto error;
4711 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4712 goto error);
4713 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4714 isl_basic_set_free(like);
4715 return set;
4717 set = isl_set_cow(set);
4718 if (!set)
4719 goto error;
4720 for (i = 0; i < set->n; ++i) {
4721 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4722 isl_basic_set_copy(like));
4723 if (!set->p[i])
4724 goto error;
4726 isl_space_free(set->dim);
4727 set->dim = isl_space_copy(like->dim);
4728 if (!set->dim)
4729 goto error;
4730 isl_basic_set_free(like);
4731 return set;
4732 error:
4733 isl_basic_set_free(like);
4734 isl_set_free(set);
4735 return NULL;
4738 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4740 int i;
4742 map = isl_map_cow(map);
4743 if (!map)
4744 return NULL;
4745 map->dim = isl_space_cow(map->dim);
4746 if (!map->dim)
4747 goto error;
4749 for (i = 1; i < map->n; ++i)
4750 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4751 goto error);
4752 for (i = 0; i < map->n; ++i) {
4753 map->p[i] = (struct isl_basic_map *)
4754 isl_basic_map_underlying_set(map->p[i]);
4755 if (!map->p[i])
4756 goto error;
4758 if (map->n == 0)
4759 map->dim = isl_space_underlying(map->dim, 0);
4760 else {
4761 isl_space_free(map->dim);
4762 map->dim = isl_space_copy(map->p[0]->dim);
4764 if (!map->dim)
4765 goto error;
4766 return (struct isl_set *)map;
4767 error:
4768 isl_map_free(map);
4769 return NULL;
4772 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4774 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4777 /* Replace the space of "bmap" by "space".
4779 * If the space of "bmap" is identical to "space" (including the identifiers
4780 * of the input and output dimensions), then simply return the original input.
4782 __isl_give isl_basic_map *isl_basic_map_reset_space(
4783 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4785 isl_bool equal;
4787 if (!bmap)
4788 goto error;
4789 equal = isl_space_is_equal(bmap->dim, space);
4790 if (equal >= 0 && equal)
4791 equal = isl_space_match(bmap->dim, isl_dim_in,
4792 space, isl_dim_in);
4793 if (equal >= 0 && equal)
4794 equal = isl_space_match(bmap->dim, isl_dim_out,
4795 space, isl_dim_out);
4796 if (equal < 0)
4797 goto error;
4798 if (equal) {
4799 isl_space_free(space);
4800 return bmap;
4802 bmap = isl_basic_map_cow(bmap);
4803 if (!bmap || !space)
4804 goto error;
4806 isl_space_free(bmap->dim);
4807 bmap->dim = space;
4809 bmap = isl_basic_map_finalize(bmap);
4811 return bmap;
4812 error:
4813 isl_basic_map_free(bmap);
4814 isl_space_free(space);
4815 return NULL;
4818 __isl_give isl_basic_set *isl_basic_set_reset_space(
4819 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4821 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4822 dim);
4825 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4826 __isl_take isl_space *dim)
4828 int i;
4830 map = isl_map_cow(map);
4831 if (!map || !dim)
4832 goto error;
4834 for (i = 0; i < map->n; ++i) {
4835 map->p[i] = isl_basic_map_reset_space(map->p[i],
4836 isl_space_copy(dim));
4837 if (!map->p[i])
4838 goto error;
4840 isl_space_free(map->dim);
4841 map->dim = dim;
4843 return map;
4844 error:
4845 isl_map_free(map);
4846 isl_space_free(dim);
4847 return NULL;
4850 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4851 __isl_take isl_space *dim)
4853 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4856 /* Compute the parameter domain of the given basic set.
4858 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4860 isl_space *space;
4861 unsigned n;
4863 if (isl_basic_set_is_params(bset))
4864 return bset;
4866 n = isl_basic_set_dim(bset, isl_dim_set);
4867 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4868 space = isl_basic_set_get_space(bset);
4869 space = isl_space_params(space);
4870 bset = isl_basic_set_reset_space(bset, space);
4871 return bset;
4874 /* Construct a zero-dimensional basic set with the given parameter domain.
4876 __isl_give isl_basic_set *isl_basic_set_from_params(
4877 __isl_take isl_basic_set *bset)
4879 isl_space *space;
4880 space = isl_basic_set_get_space(bset);
4881 space = isl_space_set_from_params(space);
4882 bset = isl_basic_set_reset_space(bset, space);
4883 return bset;
4886 /* Compute the parameter domain of the given set.
4888 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4890 isl_space *space;
4891 unsigned n;
4893 if (isl_set_is_params(set))
4894 return set;
4896 n = isl_set_dim(set, isl_dim_set);
4897 set = isl_set_project_out(set, isl_dim_set, 0, n);
4898 space = isl_set_get_space(set);
4899 space = isl_space_params(space);
4900 set = isl_set_reset_space(set, space);
4901 return set;
4904 /* Construct a zero-dimensional set with the given parameter domain.
4906 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4908 isl_space *space;
4909 space = isl_set_get_space(set);
4910 space = isl_space_set_from_params(space);
4911 set = isl_set_reset_space(set, space);
4912 return set;
4915 /* Compute the parameter domain of the given map.
4917 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4919 isl_space *space;
4920 unsigned n;
4922 n = isl_map_dim(map, isl_dim_in);
4923 map = isl_map_project_out(map, isl_dim_in, 0, n);
4924 n = isl_map_dim(map, isl_dim_out);
4925 map = isl_map_project_out(map, isl_dim_out, 0, n);
4926 space = isl_map_get_space(map);
4927 space = isl_space_params(space);
4928 map = isl_map_reset_space(map, space);
4929 return map;
4932 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4934 isl_space *space;
4935 unsigned n_out;
4937 if (!bmap)
4938 return NULL;
4939 space = isl_space_domain(isl_basic_map_get_space(bmap));
4941 n_out = isl_basic_map_n_out(bmap);
4942 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
4944 return isl_basic_map_reset_space(bmap, space);
4947 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4949 if (!bmap)
4950 return -1;
4951 return isl_space_may_be_set(bmap->dim);
4954 /* Is this basic map actually a set?
4955 * Users should never call this function. Outside of isl,
4956 * the type should indicate whether something is a set or a map.
4958 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4960 if (!bmap)
4961 return -1;
4962 return isl_space_is_set(bmap->dim);
4965 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4967 if (!bmap)
4968 return NULL;
4969 if (isl_basic_map_is_set(bmap))
4970 return bmap;
4971 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4974 __isl_give isl_basic_map *isl_basic_map_domain_map(
4975 __isl_take isl_basic_map *bmap)
4977 int i, k;
4978 isl_space *dim;
4979 isl_basic_map *domain;
4980 int nparam, n_in, n_out;
4981 unsigned total;
4983 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4984 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4985 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4987 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4988 domain = isl_basic_map_universe(dim);
4990 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4991 bmap = isl_basic_map_apply_range(bmap, domain);
4992 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4994 total = isl_basic_map_total_dim(bmap);
4996 for (i = 0; i < n_in; ++i) {
4997 k = isl_basic_map_alloc_equality(bmap);
4998 if (k < 0)
4999 goto error;
5000 isl_seq_clr(bmap->eq[k], 1 + total);
5001 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5002 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5005 bmap = isl_basic_map_gauss(bmap, NULL);
5006 return isl_basic_map_finalize(bmap);
5007 error:
5008 isl_basic_map_free(bmap);
5009 return NULL;
5012 __isl_give isl_basic_map *isl_basic_map_range_map(
5013 __isl_take isl_basic_map *bmap)
5015 int i, k;
5016 isl_space *dim;
5017 isl_basic_map *range;
5018 int nparam, n_in, n_out;
5019 unsigned total;
5021 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5022 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5023 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5025 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5026 range = isl_basic_map_universe(dim);
5028 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5029 bmap = isl_basic_map_apply_range(bmap, range);
5030 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5032 total = isl_basic_map_total_dim(bmap);
5034 for (i = 0; i < n_out; ++i) {
5035 k = isl_basic_map_alloc_equality(bmap);
5036 if (k < 0)
5037 goto error;
5038 isl_seq_clr(bmap->eq[k], 1 + total);
5039 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5040 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5043 bmap = isl_basic_map_gauss(bmap, NULL);
5044 return isl_basic_map_finalize(bmap);
5045 error:
5046 isl_basic_map_free(bmap);
5047 return NULL;
5050 int isl_map_may_be_set(__isl_keep isl_map *map)
5052 if (!map)
5053 return -1;
5054 return isl_space_may_be_set(map->dim);
5057 /* Is this map actually a set?
5058 * Users should never call this function. Outside of isl,
5059 * the type should indicate whether something is a set or a map.
5061 int isl_map_is_set(__isl_keep isl_map *map)
5063 if (!map)
5064 return -1;
5065 return isl_space_is_set(map->dim);
5068 struct isl_set *isl_map_range(struct isl_map *map)
5070 int i;
5071 struct isl_set *set;
5073 if (!map)
5074 goto error;
5075 if (isl_map_is_set(map))
5076 return (isl_set *)map;
5078 map = isl_map_cow(map);
5079 if (!map)
5080 goto error;
5082 set = (struct isl_set *) map;
5083 set->dim = isl_space_range(set->dim);
5084 if (!set->dim)
5085 goto error;
5086 for (i = 0; i < map->n; ++i) {
5087 set->p[i] = isl_basic_map_range(map->p[i]);
5088 if (!set->p[i])
5089 goto error;
5091 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5092 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5093 return set;
5094 error:
5095 isl_map_free(map);
5096 return NULL;
5099 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5101 int i;
5103 map = isl_map_cow(map);
5104 if (!map)
5105 return NULL;
5107 map->dim = isl_space_domain_map(map->dim);
5108 if (!map->dim)
5109 goto error;
5110 for (i = 0; i < map->n; ++i) {
5111 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5112 if (!map->p[i])
5113 goto error;
5115 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5116 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5117 return map;
5118 error:
5119 isl_map_free(map);
5120 return NULL;
5123 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5125 int i;
5126 isl_space *range_dim;
5128 map = isl_map_cow(map);
5129 if (!map)
5130 return NULL;
5132 range_dim = isl_space_range(isl_map_get_space(map));
5133 range_dim = isl_space_from_range(range_dim);
5134 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5135 map->dim = isl_space_join(map->dim, range_dim);
5136 if (!map->dim)
5137 goto error;
5138 for (i = 0; i < map->n; ++i) {
5139 map->p[i] = isl_basic_map_range_map(map->p[i]);
5140 if (!map->p[i])
5141 goto error;
5143 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5144 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5145 return map;
5146 error:
5147 isl_map_free(map);
5148 return NULL;
5151 /* Given a wrapped map of the form A[B -> C],
5152 * return the map A[B -> C] -> B.
5154 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5156 isl_id *id;
5157 isl_map *map;
5159 if (!set)
5160 return NULL;
5161 if (!isl_set_has_tuple_id(set))
5162 return isl_map_domain_map(isl_set_unwrap(set));
5164 id = isl_set_get_tuple_id(set);
5165 map = isl_map_domain_map(isl_set_unwrap(set));
5166 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5168 return map;
5171 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5172 __isl_take isl_space *dim)
5174 int i;
5175 struct isl_map *map = NULL;
5177 set = isl_set_cow(set);
5178 if (!set || !dim)
5179 goto error;
5180 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5181 map = (struct isl_map *)set;
5182 for (i = 0; i < set->n; ++i) {
5183 map->p[i] = isl_basic_map_from_basic_set(
5184 set->p[i], isl_space_copy(dim));
5185 if (!map->p[i])
5186 goto error;
5188 isl_space_free(map->dim);
5189 map->dim = dim;
5190 return map;
5191 error:
5192 isl_space_free(dim);
5193 isl_set_free(set);
5194 return NULL;
5197 __isl_give isl_basic_map *isl_basic_map_from_domain(
5198 __isl_take isl_basic_set *bset)
5200 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5203 __isl_give isl_basic_map *isl_basic_map_from_range(
5204 __isl_take isl_basic_set *bset)
5206 isl_space *space;
5207 space = isl_basic_set_get_space(bset);
5208 space = isl_space_from_range(space);
5209 bset = isl_basic_set_reset_space(bset, space);
5210 return (isl_basic_map *)bset;
5213 /* Create a relation with the given set as range.
5214 * The domain of the created relation is a zero-dimensional
5215 * flat anonymous space.
5217 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5219 isl_space *space;
5220 space = isl_set_get_space(set);
5221 space = isl_space_from_range(space);
5222 set = isl_set_reset_space(set, space);
5223 return (struct isl_map *)set;
5226 /* Create a relation with the given set as domain.
5227 * The range of the created relation is a zero-dimensional
5228 * flat anonymous space.
5230 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5232 return isl_map_reverse(isl_map_from_range(set));
5235 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5236 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5238 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5241 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5242 __isl_take isl_set *range)
5244 return isl_map_apply_range(isl_map_reverse(domain), range);
5247 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5248 unsigned flags)
5250 struct isl_map *map;
5252 if (!dim)
5253 return NULL;
5254 if (n < 0)
5255 isl_die(dim->ctx, isl_error_internal,
5256 "negative number of basic maps", goto error);
5257 map = isl_alloc(dim->ctx, struct isl_map,
5258 sizeof(struct isl_map) +
5259 (n - 1) * sizeof(struct isl_basic_map *));
5260 if (!map)
5261 goto error;
5263 map->ctx = dim->ctx;
5264 isl_ctx_ref(map->ctx);
5265 map->ref = 1;
5266 map->size = n;
5267 map->n = 0;
5268 map->dim = dim;
5269 map->flags = flags;
5270 return map;
5271 error:
5272 isl_space_free(dim);
5273 return NULL;
5276 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5277 unsigned nparam, unsigned in, unsigned out, int n,
5278 unsigned flags)
5280 struct isl_map *map;
5281 isl_space *dims;
5283 dims = isl_space_alloc(ctx, nparam, in, out);
5284 if (!dims)
5285 return NULL;
5287 map = isl_map_alloc_space(dims, n, flags);
5288 return map;
5291 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5293 struct isl_basic_map *bmap;
5294 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5295 bmap = isl_basic_map_set_to_empty(bmap);
5296 return bmap;
5299 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5301 struct isl_basic_set *bset;
5302 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5303 bset = isl_basic_set_set_to_empty(bset);
5304 return bset;
5307 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5309 struct isl_basic_map *bmap;
5310 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5311 bmap = isl_basic_map_finalize(bmap);
5312 return bmap;
5315 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5317 struct isl_basic_set *bset;
5318 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5319 bset = isl_basic_set_finalize(bset);
5320 return bset;
5323 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5325 int i;
5326 unsigned total = isl_space_dim(dim, isl_dim_all);
5327 isl_basic_map *bmap;
5329 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5330 for (i = 0; i < total; ++i) {
5331 int k = isl_basic_map_alloc_inequality(bmap);
5332 if (k < 0)
5333 goto error;
5334 isl_seq_clr(bmap->ineq[k], 1 + total);
5335 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5337 return bmap;
5338 error:
5339 isl_basic_map_free(bmap);
5340 return NULL;
5343 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5345 return isl_basic_map_nat_universe(dim);
5348 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5350 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5353 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5355 return isl_map_nat_universe(dim);
5358 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5360 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5363 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5365 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5368 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5370 struct isl_map *map;
5371 if (!dim)
5372 return NULL;
5373 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5374 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5375 return map;
5378 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5380 struct isl_set *set;
5381 if (!dim)
5382 return NULL;
5383 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5384 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5385 return set;
5388 struct isl_map *isl_map_dup(struct isl_map *map)
5390 int i;
5391 struct isl_map *dup;
5393 if (!map)
5394 return NULL;
5395 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5396 for (i = 0; i < map->n; ++i)
5397 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5398 return dup;
5401 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5402 __isl_take isl_basic_map *bmap)
5404 if (!bmap || !map)
5405 goto error;
5406 if (isl_basic_map_plain_is_empty(bmap)) {
5407 isl_basic_map_free(bmap);
5408 return map;
5410 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5411 isl_assert(map->ctx, map->n < map->size, goto error);
5412 map->p[map->n] = bmap;
5413 map->n++;
5414 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5415 return map;
5416 error:
5417 if (map)
5418 isl_map_free(map);
5419 if (bmap)
5420 isl_basic_map_free(bmap);
5421 return NULL;
5424 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5426 int i;
5428 if (!map)
5429 return NULL;
5431 if (--map->ref > 0)
5432 return NULL;
5434 isl_ctx_deref(map->ctx);
5435 for (i = 0; i < map->n; ++i)
5436 isl_basic_map_free(map->p[i]);
5437 isl_space_free(map->dim);
5438 free(map);
5440 return NULL;
5443 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5444 struct isl_basic_map *bmap, unsigned pos, int value)
5446 int j;
5448 bmap = isl_basic_map_cow(bmap);
5449 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5450 j = isl_basic_map_alloc_equality(bmap);
5451 if (j < 0)
5452 goto error;
5453 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5454 isl_int_set_si(bmap->eq[j][pos], -1);
5455 isl_int_set_si(bmap->eq[j][0], value);
5456 bmap = isl_basic_map_simplify(bmap);
5457 return isl_basic_map_finalize(bmap);
5458 error:
5459 isl_basic_map_free(bmap);
5460 return NULL;
5463 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5464 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5466 int j;
5468 bmap = isl_basic_map_cow(bmap);
5469 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5470 j = isl_basic_map_alloc_equality(bmap);
5471 if (j < 0)
5472 goto error;
5473 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5474 isl_int_set_si(bmap->eq[j][pos], -1);
5475 isl_int_set(bmap->eq[j][0], value);
5476 bmap = isl_basic_map_simplify(bmap);
5477 return isl_basic_map_finalize(bmap);
5478 error:
5479 isl_basic_map_free(bmap);
5480 return NULL;
5483 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5484 enum isl_dim_type type, unsigned pos, int value)
5486 if (!bmap)
5487 return NULL;
5488 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5489 return isl_basic_map_fix_pos_si(bmap,
5490 isl_basic_map_offset(bmap, type) + pos, value);
5491 error:
5492 isl_basic_map_free(bmap);
5493 return NULL;
5496 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5497 enum isl_dim_type type, unsigned pos, isl_int value)
5499 if (!bmap)
5500 return NULL;
5501 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5502 return isl_basic_map_fix_pos(bmap,
5503 isl_basic_map_offset(bmap, type) + pos, value);
5504 error:
5505 isl_basic_map_free(bmap);
5506 return NULL;
5509 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5510 * to be equal to "v".
5512 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5513 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5515 if (!bmap || !v)
5516 goto error;
5517 if (!isl_val_is_int(v))
5518 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5519 "expecting integer value", goto error);
5520 if (pos >= isl_basic_map_dim(bmap, type))
5521 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5522 "index out of bounds", goto error);
5523 pos += isl_basic_map_offset(bmap, type);
5524 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5525 isl_val_free(v);
5526 return bmap;
5527 error:
5528 isl_basic_map_free(bmap);
5529 isl_val_free(v);
5530 return NULL;
5533 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5534 * to be equal to "v".
5536 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5537 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5539 return isl_basic_map_fix_val(bset, type, pos, v);
5542 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5543 enum isl_dim_type type, unsigned pos, int value)
5545 return (struct isl_basic_set *)
5546 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5547 type, pos, value);
5550 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5551 enum isl_dim_type type, unsigned pos, isl_int value)
5553 return (struct isl_basic_set *)
5554 isl_basic_map_fix((struct isl_basic_map *)bset,
5555 type, pos, value);
5558 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5559 unsigned input, int value)
5561 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5564 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5565 unsigned dim, int value)
5567 return (struct isl_basic_set *)
5568 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5569 isl_dim_set, dim, value);
5572 static int remove_if_empty(__isl_keep isl_map *map, int i)
5574 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5576 if (empty < 0)
5577 return -1;
5578 if (!empty)
5579 return 0;
5581 isl_basic_map_free(map->p[i]);
5582 if (i != map->n - 1) {
5583 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5584 map->p[i] = map->p[map->n - 1];
5586 map->n--;
5588 return 0;
5591 /* Perform "fn" on each basic map of "map", where we may not be holding
5592 * the only reference to "map".
5593 * In particular, "fn" should be a semantics preserving operation
5594 * that we want to apply to all copies of "map". We therefore need
5595 * to be careful not to modify "map" in a way that breaks "map"
5596 * in case anything goes wrong.
5598 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5599 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5601 struct isl_basic_map *bmap;
5602 int i;
5604 if (!map)
5605 return NULL;
5607 for (i = map->n - 1; i >= 0; --i) {
5608 bmap = isl_basic_map_copy(map->p[i]);
5609 bmap = fn(bmap);
5610 if (!bmap)
5611 goto error;
5612 isl_basic_map_free(map->p[i]);
5613 map->p[i] = bmap;
5614 if (remove_if_empty(map, i) < 0)
5615 goto error;
5618 return map;
5619 error:
5620 isl_map_free(map);
5621 return NULL;
5624 struct isl_map *isl_map_fix_si(struct isl_map *map,
5625 enum isl_dim_type type, unsigned pos, int value)
5627 int i;
5629 map = isl_map_cow(map);
5630 if (!map)
5631 return NULL;
5633 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5634 for (i = map->n - 1; i >= 0; --i) {
5635 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5636 if (remove_if_empty(map, i) < 0)
5637 goto error;
5639 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5640 return map;
5641 error:
5642 isl_map_free(map);
5643 return NULL;
5646 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5647 enum isl_dim_type type, unsigned pos, int value)
5649 return (struct isl_set *)
5650 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5653 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5654 enum isl_dim_type type, unsigned pos, isl_int value)
5656 int i;
5658 map = isl_map_cow(map);
5659 if (!map)
5660 return NULL;
5662 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5663 for (i = 0; i < map->n; ++i) {
5664 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5665 if (!map->p[i])
5666 goto error;
5668 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5669 return map;
5670 error:
5671 isl_map_free(map);
5672 return NULL;
5675 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5676 enum isl_dim_type type, unsigned pos, isl_int value)
5678 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5681 /* Fix the value of the variable at position "pos" of type "type" of "map"
5682 * to be equal to "v".
5684 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5685 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5687 int i;
5689 map = isl_map_cow(map);
5690 if (!map || !v)
5691 goto error;
5693 if (!isl_val_is_int(v))
5694 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5695 "expecting integer value", goto error);
5696 if (pos >= isl_map_dim(map, type))
5697 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5698 "index out of bounds", goto error);
5699 for (i = map->n - 1; i >= 0; --i) {
5700 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5701 isl_val_copy(v));
5702 if (remove_if_empty(map, i) < 0)
5703 goto error;
5705 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5706 isl_val_free(v);
5707 return map;
5708 error:
5709 isl_map_free(map);
5710 isl_val_free(v);
5711 return NULL;
5714 /* Fix the value of the variable at position "pos" of type "type" of "set"
5715 * to be equal to "v".
5717 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5718 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5720 return isl_map_fix_val(set, type, pos, v);
5723 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5724 unsigned input, int value)
5726 return isl_map_fix_si(map, isl_dim_in, input, value);
5729 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5731 return (struct isl_set *)
5732 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5735 static __isl_give isl_basic_map *basic_map_bound_si(
5736 __isl_take isl_basic_map *bmap,
5737 enum isl_dim_type type, unsigned pos, int value, int upper)
5739 int j;
5741 if (!bmap)
5742 return NULL;
5743 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5744 pos += isl_basic_map_offset(bmap, type);
5745 bmap = isl_basic_map_cow(bmap);
5746 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5747 j = isl_basic_map_alloc_inequality(bmap);
5748 if (j < 0)
5749 goto error;
5750 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5751 if (upper) {
5752 isl_int_set_si(bmap->ineq[j][pos], -1);
5753 isl_int_set_si(bmap->ineq[j][0], value);
5754 } else {
5755 isl_int_set_si(bmap->ineq[j][pos], 1);
5756 isl_int_set_si(bmap->ineq[j][0], -value);
5758 bmap = isl_basic_map_simplify(bmap);
5759 return isl_basic_map_finalize(bmap);
5760 error:
5761 isl_basic_map_free(bmap);
5762 return NULL;
5765 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5766 __isl_take isl_basic_map *bmap,
5767 enum isl_dim_type type, unsigned pos, int value)
5769 return basic_map_bound_si(bmap, type, pos, value, 0);
5772 /* Constrain the values of the given dimension to be no greater than "value".
5774 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5775 __isl_take isl_basic_map *bmap,
5776 enum isl_dim_type type, unsigned pos, int value)
5778 return basic_map_bound_si(bmap, type, pos, value, 1);
5781 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5782 unsigned dim, isl_int value)
5784 int j;
5786 bset = isl_basic_set_cow(bset);
5787 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5788 j = isl_basic_set_alloc_inequality(bset);
5789 if (j < 0)
5790 goto error;
5791 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5792 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5793 isl_int_neg(bset->ineq[j][0], value);
5794 bset = isl_basic_set_simplify(bset);
5795 return isl_basic_set_finalize(bset);
5796 error:
5797 isl_basic_set_free(bset);
5798 return NULL;
5801 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5802 enum isl_dim_type type, unsigned pos, int value, int upper)
5804 int i;
5806 map = isl_map_cow(map);
5807 if (!map)
5808 return NULL;
5810 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5811 for (i = 0; i < map->n; ++i) {
5812 map->p[i] = basic_map_bound_si(map->p[i],
5813 type, pos, value, upper);
5814 if (!map->p[i])
5815 goto error;
5817 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5818 return map;
5819 error:
5820 isl_map_free(map);
5821 return NULL;
5824 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5825 enum isl_dim_type type, unsigned pos, int value)
5827 return map_bound_si(map, type, pos, value, 0);
5830 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5831 enum isl_dim_type type, unsigned pos, int value)
5833 return map_bound_si(map, type, pos, value, 1);
5836 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5837 enum isl_dim_type type, unsigned pos, int value)
5839 return (struct isl_set *)
5840 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5843 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5844 enum isl_dim_type type, unsigned pos, int value)
5846 return isl_map_upper_bound_si(set, type, pos, value);
5849 /* Bound the given variable of "bmap" from below (or above is "upper"
5850 * is set) to "value".
5852 static __isl_give isl_basic_map *basic_map_bound(
5853 __isl_take isl_basic_map *bmap,
5854 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5856 int j;
5858 if (!bmap)
5859 return NULL;
5860 if (pos >= isl_basic_map_dim(bmap, type))
5861 isl_die(bmap->ctx, isl_error_invalid,
5862 "index out of bounds", goto error);
5863 pos += isl_basic_map_offset(bmap, type);
5864 bmap = isl_basic_map_cow(bmap);
5865 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5866 j = isl_basic_map_alloc_inequality(bmap);
5867 if (j < 0)
5868 goto error;
5869 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5870 if (upper) {
5871 isl_int_set_si(bmap->ineq[j][pos], -1);
5872 isl_int_set(bmap->ineq[j][0], value);
5873 } else {
5874 isl_int_set_si(bmap->ineq[j][pos], 1);
5875 isl_int_neg(bmap->ineq[j][0], value);
5877 bmap = isl_basic_map_simplify(bmap);
5878 return isl_basic_map_finalize(bmap);
5879 error:
5880 isl_basic_map_free(bmap);
5881 return NULL;
5884 /* Bound the given variable of "map" from below (or above is "upper"
5885 * is set) to "value".
5887 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5888 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5890 int i;
5892 map = isl_map_cow(map);
5893 if (!map)
5894 return NULL;
5896 if (pos >= isl_map_dim(map, type))
5897 isl_die(map->ctx, isl_error_invalid,
5898 "index out of bounds", goto error);
5899 for (i = map->n - 1; i >= 0; --i) {
5900 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5901 if (remove_if_empty(map, i) < 0)
5902 goto error;
5904 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5905 return map;
5906 error:
5907 isl_map_free(map);
5908 return NULL;
5911 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5912 enum isl_dim_type type, unsigned pos, isl_int value)
5914 return map_bound(map, type, pos, value, 0);
5917 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5918 enum isl_dim_type type, unsigned pos, isl_int value)
5920 return map_bound(map, type, pos, value, 1);
5923 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5924 enum isl_dim_type type, unsigned pos, isl_int value)
5926 return isl_map_lower_bound(set, type, pos, value);
5929 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5930 enum isl_dim_type type, unsigned pos, isl_int value)
5932 return isl_map_upper_bound(set, type, pos, value);
5935 /* Force the values of the variable at position "pos" of type "type" of "set"
5936 * to be no smaller than "value".
5938 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5939 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5941 if (!value)
5942 goto error;
5943 if (!isl_val_is_int(value))
5944 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5945 "expecting integer value", goto error);
5946 set = isl_set_lower_bound(set, type, pos, value->n);
5947 isl_val_free(value);
5948 return set;
5949 error:
5950 isl_val_free(value);
5951 isl_set_free(set);
5952 return NULL;
5955 /* Force the values of the variable at position "pos" of type "type" of "set"
5956 * to be no greater than "value".
5958 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5959 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5961 if (!value)
5962 goto error;
5963 if (!isl_val_is_int(value))
5964 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5965 "expecting integer value", goto error);
5966 set = isl_set_upper_bound(set, type, pos, value->n);
5967 isl_val_free(value);
5968 return set;
5969 error:
5970 isl_val_free(value);
5971 isl_set_free(set);
5972 return NULL;
5975 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5976 isl_int value)
5978 int i;
5980 set = isl_set_cow(set);
5981 if (!set)
5982 return NULL;
5984 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5985 for (i = 0; i < set->n; ++i) {
5986 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5987 if (!set->p[i])
5988 goto error;
5990 return set;
5991 error:
5992 isl_set_free(set);
5993 return NULL;
5996 struct isl_map *isl_map_reverse(struct isl_map *map)
5998 int i;
6000 map = isl_map_cow(map);
6001 if (!map)
6002 return NULL;
6004 map->dim = isl_space_reverse(map->dim);
6005 if (!map->dim)
6006 goto error;
6007 for (i = 0; i < map->n; ++i) {
6008 map->p[i] = isl_basic_map_reverse(map->p[i]);
6009 if (!map->p[i])
6010 goto error;
6012 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6013 return map;
6014 error:
6015 isl_map_free(map);
6016 return NULL;
6019 static struct isl_map *isl_basic_map_partial_lexopt(
6020 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6021 struct isl_set **empty, int max)
6023 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6026 struct isl_map *isl_basic_map_partial_lexmax(
6027 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6028 struct isl_set **empty)
6030 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6033 struct isl_map *isl_basic_map_partial_lexmin(
6034 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6035 struct isl_set **empty)
6037 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6040 struct isl_set *isl_basic_set_partial_lexmin(
6041 struct isl_basic_set *bset, struct isl_basic_set *dom,
6042 struct isl_set **empty)
6044 return (struct isl_set *)
6045 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6046 dom, empty);
6049 struct isl_set *isl_basic_set_partial_lexmax(
6050 struct isl_basic_set *bset, struct isl_basic_set *dom,
6051 struct isl_set **empty)
6053 return (struct isl_set *)
6054 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6055 dom, empty);
6058 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6059 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6060 __isl_give isl_set **empty)
6062 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6065 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6066 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6067 __isl_give isl_set **empty)
6069 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6072 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6073 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6074 __isl_give isl_set **empty)
6076 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6079 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6080 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6081 __isl_give isl_set **empty)
6083 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6086 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6087 __isl_take isl_basic_map *bmap, int max)
6089 isl_basic_set *dom = NULL;
6090 isl_space *dom_space;
6092 if (!bmap)
6093 goto error;
6094 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6095 dom = isl_basic_set_universe(dom_space);
6096 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6097 error:
6098 isl_basic_map_free(bmap);
6099 return NULL;
6102 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6103 __isl_take isl_basic_map *bmap)
6105 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6108 #undef TYPE
6109 #define TYPE isl_pw_multi_aff
6110 #undef SUFFIX
6111 #define SUFFIX _pw_multi_aff
6112 #undef EMPTY
6113 #define EMPTY isl_pw_multi_aff_empty
6114 #undef ADD
6115 #define ADD isl_pw_multi_aff_union_add
6116 #include "isl_map_lexopt_templ.c"
6118 /* Given a map "map", compute the lexicographically minimal
6119 * (or maximal) image element for each domain element in dom,
6120 * in the form of an isl_pw_multi_aff.
6121 * If "empty" is not NULL, then set *empty to those elements in dom that
6122 * do not have an image element.
6124 * We first compute the lexicographically minimal or maximal element
6125 * in the first basic map. This results in a partial solution "res"
6126 * and a subset "todo" of dom that still need to be handled.
6127 * We then consider each of the remaining maps in "map" and successively
6128 * update both "res" and "todo".
6129 * If "empty" is NULL, then the todo sets are not needed and therefore
6130 * also not computed.
6132 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6133 __isl_take isl_map *map, __isl_take isl_set *dom,
6134 __isl_give isl_set **empty, int max)
6136 int i;
6137 isl_pw_multi_aff *res;
6138 isl_set *todo;
6140 if (!map || !dom)
6141 goto error;
6143 if (isl_map_plain_is_empty(map)) {
6144 if (empty)
6145 *empty = dom;
6146 else
6147 isl_set_free(dom);
6148 return isl_pw_multi_aff_from_map(map);
6151 res = basic_map_partial_lexopt_pw_multi_aff(
6152 isl_basic_map_copy(map->p[0]),
6153 isl_set_copy(dom), empty, max);
6155 if (empty)
6156 todo = *empty;
6157 for (i = 1; i < map->n; ++i) {
6158 isl_pw_multi_aff *res_i;
6160 res_i = basic_map_partial_lexopt_pw_multi_aff(
6161 isl_basic_map_copy(map->p[i]),
6162 isl_set_copy(dom), empty, max);
6164 if (max)
6165 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6166 else
6167 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6169 if (empty)
6170 todo = isl_set_intersect(todo, *empty);
6173 isl_set_free(dom);
6174 isl_map_free(map);
6176 if (empty)
6177 *empty = todo;
6179 return res;
6180 error:
6181 if (empty)
6182 *empty = NULL;
6183 isl_set_free(dom);
6184 isl_map_free(map);
6185 return NULL;
6188 #undef TYPE
6189 #define TYPE isl_map
6190 #undef SUFFIX
6191 #define SUFFIX
6192 #undef EMPTY
6193 #define EMPTY isl_map_empty
6194 #undef ADD
6195 #define ADD isl_map_union_disjoint
6196 #include "isl_map_lexopt_templ.c"
6198 /* Given a map "map", compute the lexicographically minimal
6199 * (or maximal) image element for each domain element in dom.
6200 * Set *empty to those elements in dom that do not have an image element.
6202 * We first compute the lexicographically minimal or maximal element
6203 * in the first basic map. This results in a partial solution "res"
6204 * and a subset "todo" of dom that still need to be handled.
6205 * We then consider each of the remaining maps in "map" and successively
6206 * update both "res" and "todo".
6208 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6209 * Assume we are computing the lexicographical maximum.
6210 * We first compute the lexicographically maximal element in basic map i.
6211 * This results in a partial solution res_i and a subset todo_i.
6212 * Then we combine these results with those obtain for the first k basic maps
6213 * to obtain a result that is valid for the first k+1 basic maps.
6214 * In particular, the set where there is no solution is the set where
6215 * there is no solution for the first k basic maps and also no solution
6216 * for the ith basic map, i.e.,
6218 * todo^i = todo^k * todo_i
6220 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6221 * solutions, arbitrarily breaking ties in favor of res^k.
6222 * That is, when res^k(a) >= res_i(a), we pick res^k and
6223 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6224 * the lexicographic order.)
6225 * In practice, we compute
6227 * res^k * (res_i . "<=")
6229 * and
6231 * res_i * (res^k . "<")
6233 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6234 * where only one of res^k and res_i provides a solution and we simply pick
6235 * that one, i.e.,
6237 * res^k * todo_i
6238 * and
6239 * res_i * todo^k
6241 * Note that we only compute these intersections when dom(res^k) intersects
6242 * dom(res_i). Otherwise, the only effect of these intersections is to
6243 * potentially break up res^k and res_i into smaller pieces.
6244 * We want to avoid such splintering as much as possible.
6245 * In fact, an earlier implementation of this function would look for
6246 * better results in the domain of res^k and for extra results in todo^k,
6247 * but this would always result in a splintering according to todo^k,
6248 * even when the domain of basic map i is disjoint from the domains of
6249 * the previous basic maps.
6251 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6252 __isl_take isl_map *map, __isl_take isl_set *dom,
6253 __isl_give isl_set **empty, int max)
6255 int i;
6256 struct isl_map *res;
6257 struct isl_set *todo;
6259 if (!map || !dom)
6260 goto error;
6262 if (isl_map_plain_is_empty(map)) {
6263 if (empty)
6264 *empty = dom;
6265 else
6266 isl_set_free(dom);
6267 return map;
6270 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6271 isl_set_copy(dom), &todo, max);
6273 for (i = 1; i < map->n; ++i) {
6274 isl_map *lt, *le;
6275 isl_map *res_i;
6276 isl_set *todo_i;
6277 isl_space *dim = isl_space_range(isl_map_get_space(res));
6279 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6280 isl_set_copy(dom), &todo_i, max);
6282 if (max) {
6283 lt = isl_map_lex_lt(isl_space_copy(dim));
6284 le = isl_map_lex_le(dim);
6285 } else {
6286 lt = isl_map_lex_gt(isl_space_copy(dim));
6287 le = isl_map_lex_ge(dim);
6289 lt = isl_map_apply_range(isl_map_copy(res), lt);
6290 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6291 le = isl_map_apply_range(isl_map_copy(res_i), le);
6292 le = isl_map_intersect(le, isl_map_copy(res));
6294 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6295 res = isl_map_intersect_domain(res,
6296 isl_set_copy(todo_i));
6297 res_i = isl_map_intersect_domain(res_i,
6298 isl_set_copy(todo));
6301 res = isl_map_union_disjoint(res, res_i);
6302 res = isl_map_union_disjoint(res, lt);
6303 res = isl_map_union_disjoint(res, le);
6305 todo = isl_set_intersect(todo, todo_i);
6308 isl_set_free(dom);
6309 isl_map_free(map);
6311 if (empty)
6312 *empty = todo;
6313 else
6314 isl_set_free(todo);
6316 return res;
6317 error:
6318 if (empty)
6319 *empty = NULL;
6320 isl_set_free(dom);
6321 isl_map_free(map);
6322 return NULL;
6325 __isl_give isl_map *isl_map_partial_lexmax(
6326 __isl_take isl_map *map, __isl_take isl_set *dom,
6327 __isl_give isl_set **empty)
6329 return isl_map_partial_lexopt(map, dom, empty, 1);
6332 __isl_give isl_map *isl_map_partial_lexmin(
6333 __isl_take isl_map *map, __isl_take isl_set *dom,
6334 __isl_give isl_set **empty)
6336 return isl_map_partial_lexopt(map, dom, empty, 0);
6339 __isl_give isl_set *isl_set_partial_lexmin(
6340 __isl_take isl_set *set, __isl_take isl_set *dom,
6341 __isl_give isl_set **empty)
6343 return (struct isl_set *)
6344 isl_map_partial_lexmin((struct isl_map *)set,
6345 dom, empty);
6348 __isl_give isl_set *isl_set_partial_lexmax(
6349 __isl_take isl_set *set, __isl_take isl_set *dom,
6350 __isl_give isl_set **empty)
6352 return (struct isl_set *)
6353 isl_map_partial_lexmax((struct isl_map *)set,
6354 dom, empty);
6357 /* Compute the lexicographic minimum (or maximum if "max" is set)
6358 * of "bmap" over its domain.
6360 * Since we are not interested in the part of the domain space where
6361 * there is no solution, we initialize the domain to those constraints
6362 * of "bmap" that only involve the parameters and the input dimensions.
6363 * This relieves the parametric programming engine from detecting those
6364 * inequalities and transferring them to the context. More importantly,
6365 * it ensures that those inequalities are transferred first and not
6366 * intermixed with inequalities that actually split the domain.
6368 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6370 int n_div;
6371 int n_out;
6372 isl_basic_map *copy;
6373 isl_basic_set *dom;
6375 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6376 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6377 copy = isl_basic_map_copy(bmap);
6378 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6379 isl_dim_div, 0, n_div);
6380 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6381 isl_dim_out, 0, n_out);
6382 dom = isl_basic_map_domain(copy);
6383 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6386 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6388 return isl_basic_map_lexopt(bmap, 0);
6391 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6393 return isl_basic_map_lexopt(bmap, 1);
6396 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6398 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6401 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6403 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6406 /* Extract the first and only affine expression from list
6407 * and then add it to *pwaff with the given dom.
6408 * This domain is known to be disjoint from other domains
6409 * because of the way isl_basic_map_foreach_lexmax works.
6411 static int update_dim_opt(__isl_take isl_basic_set *dom,
6412 __isl_take isl_aff_list *list, void *user)
6414 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6415 isl_aff *aff;
6416 isl_pw_aff **pwaff = user;
6417 isl_pw_aff *pwaff_i;
6419 if (!list)
6420 goto error;
6421 if (isl_aff_list_n_aff(list) != 1)
6422 isl_die(ctx, isl_error_internal,
6423 "expecting single element list", goto error);
6425 aff = isl_aff_list_get_aff(list, 0);
6426 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6428 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6430 isl_aff_list_free(list);
6432 return 0;
6433 error:
6434 isl_basic_set_free(dom);
6435 isl_aff_list_free(list);
6436 return -1;
6439 /* Given a basic map with one output dimension, compute the minimum or
6440 * maximum of that dimension as an isl_pw_aff.
6442 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6443 * call update_dim_opt on each leaf of the result.
6445 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6446 int max)
6448 isl_space *dim = isl_basic_map_get_space(bmap);
6449 isl_pw_aff *pwaff;
6450 int r;
6452 dim = isl_space_from_domain(isl_space_domain(dim));
6453 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6454 pwaff = isl_pw_aff_empty(dim);
6456 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6457 if (r < 0)
6458 return isl_pw_aff_free(pwaff);
6460 return pwaff;
6463 /* Compute the minimum or maximum of the given output dimension
6464 * as a function of the parameters and the input dimensions,
6465 * but independently of the other output dimensions.
6467 * We first project out the other output dimension and then compute
6468 * the "lexicographic" maximum in each basic map, combining the results
6469 * using isl_pw_aff_union_max.
6471 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6472 int max)
6474 int i;
6475 isl_pw_aff *pwaff;
6476 unsigned n_out;
6478 n_out = isl_map_dim(map, isl_dim_out);
6479 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6480 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6481 if (!map)
6482 return NULL;
6484 if (map->n == 0) {
6485 isl_space *dim = isl_map_get_space(map);
6486 isl_map_free(map);
6487 return isl_pw_aff_empty(dim);
6490 pwaff = basic_map_dim_opt(map->p[0], max);
6491 for (i = 1; i < map->n; ++i) {
6492 isl_pw_aff *pwaff_i;
6494 pwaff_i = basic_map_dim_opt(map->p[i], max);
6495 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6498 isl_map_free(map);
6500 return pwaff;
6503 /* Compute the maximum of the given output dimension as a function of the
6504 * parameters and input dimensions, but independently of
6505 * the other output dimensions.
6507 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6509 return map_dim_opt(map, pos, 1);
6512 /* Compute the minimum or maximum of the given set dimension
6513 * as a function of the parameters,
6514 * but independently of the other set dimensions.
6516 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6517 int max)
6519 return map_dim_opt(set, pos, max);
6522 /* Compute the maximum of the given set dimension as a function of the
6523 * parameters, but independently of the other set dimensions.
6525 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6527 return set_dim_opt(set, pos, 1);
6530 /* Compute the minimum of the given set dimension as a function of the
6531 * parameters, but independently of the other set dimensions.
6533 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6535 return set_dim_opt(set, pos, 0);
6538 /* Apply a preimage specified by "mat" on the parameters of "bset".
6539 * bset is assumed to have only parameters and divs.
6541 static struct isl_basic_set *basic_set_parameter_preimage(
6542 struct isl_basic_set *bset, struct isl_mat *mat)
6544 unsigned nparam;
6546 if (!bset || !mat)
6547 goto error;
6549 bset->dim = isl_space_cow(bset->dim);
6550 if (!bset->dim)
6551 goto error;
6553 nparam = isl_basic_set_dim(bset, isl_dim_param);
6555 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6557 bset->dim->nparam = 0;
6558 bset->dim->n_out = nparam;
6559 bset = isl_basic_set_preimage(bset, mat);
6560 if (bset) {
6561 bset->dim->nparam = bset->dim->n_out;
6562 bset->dim->n_out = 0;
6564 return bset;
6565 error:
6566 isl_mat_free(mat);
6567 isl_basic_set_free(bset);
6568 return NULL;
6571 /* Apply a preimage specified by "mat" on the parameters of "set".
6572 * set is assumed to have only parameters and divs.
6574 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6575 __isl_take isl_mat *mat)
6577 isl_space *space;
6578 unsigned nparam;
6580 if (!set || !mat)
6581 goto error;
6583 nparam = isl_set_dim(set, isl_dim_param);
6585 if (mat->n_row != 1 + nparam)
6586 isl_die(isl_set_get_ctx(set), isl_error_internal,
6587 "unexpected number of rows", goto error);
6589 space = isl_set_get_space(set);
6590 space = isl_space_move_dims(space, isl_dim_set, 0,
6591 isl_dim_param, 0, nparam);
6592 set = isl_set_reset_space(set, space);
6593 set = isl_set_preimage(set, mat);
6594 nparam = isl_set_dim(set, isl_dim_out);
6595 space = isl_set_get_space(set);
6596 space = isl_space_move_dims(space, isl_dim_param, 0,
6597 isl_dim_out, 0, nparam);
6598 set = isl_set_reset_space(set, space);
6599 return set;
6600 error:
6601 isl_mat_free(mat);
6602 isl_set_free(set);
6603 return NULL;
6606 /* Intersect the basic set "bset" with the affine space specified by the
6607 * equalities in "eq".
6609 static struct isl_basic_set *basic_set_append_equalities(
6610 struct isl_basic_set *bset, struct isl_mat *eq)
6612 int i, k;
6613 unsigned len;
6615 if (!bset || !eq)
6616 goto error;
6618 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6619 eq->n_row, 0);
6620 if (!bset)
6621 goto error;
6623 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6624 for (i = 0; i < eq->n_row; ++i) {
6625 k = isl_basic_set_alloc_equality(bset);
6626 if (k < 0)
6627 goto error;
6628 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6629 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6631 isl_mat_free(eq);
6633 bset = isl_basic_set_gauss(bset, NULL);
6634 bset = isl_basic_set_finalize(bset);
6636 return bset;
6637 error:
6638 isl_mat_free(eq);
6639 isl_basic_set_free(bset);
6640 return NULL;
6643 /* Intersect the set "set" with the affine space specified by the
6644 * equalities in "eq".
6646 static struct isl_set *set_append_equalities(struct isl_set *set,
6647 struct isl_mat *eq)
6649 int i;
6651 if (!set || !eq)
6652 goto error;
6654 for (i = 0; i < set->n; ++i) {
6655 set->p[i] = basic_set_append_equalities(set->p[i],
6656 isl_mat_copy(eq));
6657 if (!set->p[i])
6658 goto error;
6660 isl_mat_free(eq);
6661 return set;
6662 error:
6663 isl_mat_free(eq);
6664 isl_set_free(set);
6665 return NULL;
6668 /* Given a basic set "bset" that only involves parameters and existentially
6669 * quantified variables, return the index of the first equality
6670 * that only involves parameters. If there is no such equality then
6671 * return bset->n_eq.
6673 * This function assumes that isl_basic_set_gauss has been called on "bset".
6675 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6677 int i, j;
6678 unsigned nparam, n_div;
6680 if (!bset)
6681 return -1;
6683 nparam = isl_basic_set_dim(bset, isl_dim_param);
6684 n_div = isl_basic_set_dim(bset, isl_dim_div);
6686 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6687 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6688 ++i;
6691 return i;
6694 /* Compute an explicit representation for the existentially quantified
6695 * variables in "bset" by computing the "minimal value" of the set
6696 * variables. Since there are no set variables, the computation of
6697 * the minimal value essentially computes an explicit representation
6698 * of the non-empty part(s) of "bset".
6700 * The input only involves parameters and existentially quantified variables.
6701 * All equalities among parameters have been removed.
6703 * Since the existentially quantified variables in the result are in general
6704 * going to be different from those in the input, we first replace
6705 * them by the minimal number of variables based on their equalities.
6706 * This should simplify the parametric integer programming.
6708 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6710 isl_morph *morph1, *morph2;
6711 isl_set *set;
6712 unsigned n;
6714 if (!bset)
6715 return NULL;
6716 if (bset->n_eq == 0)
6717 return isl_basic_set_lexmin(bset);
6719 morph1 = isl_basic_set_parameter_compression(bset);
6720 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6721 bset = isl_basic_set_lift(bset);
6722 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6723 bset = isl_morph_basic_set(morph2, bset);
6724 n = isl_basic_set_dim(bset, isl_dim_set);
6725 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6727 set = isl_basic_set_lexmin(bset);
6729 set = isl_morph_set(isl_morph_inverse(morph1), set);
6731 return set;
6734 /* Project the given basic set onto its parameter domain, possibly introducing
6735 * new, explicit, existential variables in the constraints.
6736 * The input has parameters and (possibly implicit) existential variables.
6737 * The output has the same parameters, but only
6738 * explicit existentially quantified variables.
6740 * The actual projection is performed by pip, but pip doesn't seem
6741 * to like equalities very much, so we first remove the equalities
6742 * among the parameters by performing a variable compression on
6743 * the parameters. Afterward, an inverse transformation is performed
6744 * and the equalities among the parameters are inserted back in.
6746 * The variable compression on the parameters may uncover additional
6747 * equalities that were only implicit before. We therefore check
6748 * if there are any new parameter equalities in the result and
6749 * if so recurse. The removal of parameter equalities is required
6750 * for the parameter compression performed by base_compute_divs.
6752 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6754 int i;
6755 struct isl_mat *eq;
6756 struct isl_mat *T, *T2;
6757 struct isl_set *set;
6758 unsigned nparam;
6760 bset = isl_basic_set_cow(bset);
6761 if (!bset)
6762 return NULL;
6764 if (bset->n_eq == 0)
6765 return base_compute_divs(bset);
6767 bset = isl_basic_set_gauss(bset, NULL);
6768 if (!bset)
6769 return NULL;
6770 if (isl_basic_set_plain_is_empty(bset))
6771 return isl_set_from_basic_set(bset);
6773 i = first_parameter_equality(bset);
6774 if (i == bset->n_eq)
6775 return base_compute_divs(bset);
6777 nparam = isl_basic_set_dim(bset, isl_dim_param);
6778 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6779 0, 1 + nparam);
6780 eq = isl_mat_cow(eq);
6781 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6782 if (T && T->n_col == 0) {
6783 isl_mat_free(T);
6784 isl_mat_free(T2);
6785 isl_mat_free(eq);
6786 bset = isl_basic_set_set_to_empty(bset);
6787 return isl_set_from_basic_set(bset);
6789 bset = basic_set_parameter_preimage(bset, T);
6791 i = first_parameter_equality(bset);
6792 if (!bset)
6793 set = NULL;
6794 else if (i == bset->n_eq)
6795 set = base_compute_divs(bset);
6796 else
6797 set = parameter_compute_divs(bset);
6798 set = set_parameter_preimage(set, T2);
6799 set = set_append_equalities(set, eq);
6800 return set;
6803 /* Insert the divs from "ls" before those of "bmap".
6805 * The number of columns is not changed, which means that the last
6806 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6807 * The caller is responsible for removing the same number of dimensions
6808 * from the space of "bmap".
6810 static __isl_give isl_basic_map *insert_divs_from_local_space(
6811 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6813 int i;
6814 int n_div;
6815 int old_n_div;
6817 n_div = isl_local_space_dim(ls, isl_dim_div);
6818 if (n_div == 0)
6819 return bmap;
6821 old_n_div = bmap->n_div;
6822 bmap = insert_div_rows(bmap, n_div);
6823 if (!bmap)
6824 return NULL;
6826 for (i = 0; i < n_div; ++i) {
6827 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6828 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6831 return bmap;
6834 /* Replace the space of "bmap" by the space and divs of "ls".
6836 * If "ls" has any divs, then we simplify the result since we may
6837 * have discovered some additional equalities that could simplify
6838 * the div expressions.
6840 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6841 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6843 int n_div;
6845 bmap = isl_basic_map_cow(bmap);
6846 if (!bmap || !ls)
6847 goto error;
6849 n_div = isl_local_space_dim(ls, isl_dim_div);
6850 bmap = insert_divs_from_local_space(bmap, ls);
6851 if (!bmap)
6852 goto error;
6854 isl_space_free(bmap->dim);
6855 bmap->dim = isl_local_space_get_space(ls);
6856 if (!bmap->dim)
6857 goto error;
6859 isl_local_space_free(ls);
6860 if (n_div > 0)
6861 bmap = isl_basic_map_simplify(bmap);
6862 bmap = isl_basic_map_finalize(bmap);
6863 return bmap;
6864 error:
6865 isl_basic_map_free(bmap);
6866 isl_local_space_free(ls);
6867 return NULL;
6870 /* Replace the space of "map" by the space and divs of "ls".
6872 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6873 __isl_take isl_local_space *ls)
6875 int i;
6877 map = isl_map_cow(map);
6878 if (!map || !ls)
6879 goto error;
6881 for (i = 0; i < map->n; ++i) {
6882 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6883 isl_local_space_copy(ls));
6884 if (!map->p[i])
6885 goto error;
6887 isl_space_free(map->dim);
6888 map->dim = isl_local_space_get_space(ls);
6889 if (!map->dim)
6890 goto error;
6892 isl_local_space_free(ls);
6893 return map;
6894 error:
6895 isl_local_space_free(ls);
6896 isl_map_free(map);
6897 return NULL;
6900 /* Compute an explicit representation for the existentially
6901 * quantified variables for which do not know any explicit representation yet.
6903 * We first sort the existentially quantified variables so that the
6904 * existentially quantified variables for which we already have an explicit
6905 * representation are placed before those for which we do not.
6906 * The input dimensions, the output dimensions and the existentially
6907 * quantified variables for which we already have an explicit
6908 * representation are then turned into parameters.
6909 * compute_divs returns a map with the same parameters and
6910 * no input or output dimensions and the dimension specification
6911 * is reset to that of the input, including the existentially quantified
6912 * variables for which we already had an explicit representation.
6914 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6916 struct isl_basic_set *bset;
6917 struct isl_set *set;
6918 struct isl_map *map;
6919 isl_space *dim;
6920 isl_local_space *ls;
6921 unsigned nparam;
6922 unsigned n_in;
6923 unsigned n_out;
6924 int n_known;
6925 int i;
6927 bmap = isl_basic_map_sort_divs(bmap);
6928 bmap = isl_basic_map_cow(bmap);
6929 if (!bmap)
6930 return NULL;
6932 n_known = isl_basic_map_first_unknown_div(bmap);
6933 if (n_known < 0)
6934 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6936 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6937 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6938 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6939 dim = isl_space_set_alloc(bmap->ctx,
6940 nparam + n_in + n_out + n_known, 0);
6941 if (!dim)
6942 goto error;
6944 ls = isl_basic_map_get_local_space(bmap);
6945 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6946 n_known, bmap->n_div - n_known);
6947 if (n_known > 0) {
6948 for (i = n_known; i < bmap->n_div; ++i)
6949 swap_div(bmap, i - n_known, i);
6950 bmap->n_div -= n_known;
6951 bmap->extra -= n_known;
6953 bmap = isl_basic_map_reset_space(bmap, dim);
6954 bset = (struct isl_basic_set *)bmap;
6956 set = parameter_compute_divs(bset);
6957 map = (struct isl_map *)set;
6958 map = replace_space_by_local_space(map, ls);
6960 return map;
6961 error:
6962 isl_basic_map_free(bmap);
6963 return NULL;
6966 /* Remove the explicit representation of local variable "div",
6967 * if there is any.
6969 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6970 __isl_take isl_basic_map *bmap, int div)
6972 isl_bool known;
6974 known = isl_basic_map_div_is_known(bmap, div);
6975 if (known < 0)
6976 return isl_basic_map_free(bmap);
6977 if (!known)
6978 return bmap;
6980 bmap = isl_basic_map_cow(bmap);
6981 if (!bmap)
6982 return NULL;
6983 isl_int_set_si(bmap->div[div][0], 0);
6984 return bmap;
6987 /* Does local variable "div" of "bmap" have an explicit representation?
6989 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
6991 if (!bmap)
6992 return isl_bool_error;
6993 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6994 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6995 "position out of bounds", return isl_bool_error);
6996 return !isl_int_is_zero(bmap->div[div][0]);
6999 /* Return the position of the first local variable that does not
7000 * have an explicit representation.
7001 * Return the total number of local variables if they all have
7002 * an explicit representation.
7003 * Return -1 on error.
7005 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7007 int i;
7009 if (!bmap)
7010 return -1;
7012 for (i = 0; i < bmap->n_div; ++i) {
7013 if (!isl_basic_map_div_is_known(bmap, i))
7014 return i;
7016 return bmap->n_div;
7019 /* Does "bmap" have an explicit representation for all local variables?
7021 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7023 int first, n;
7025 n = isl_basic_map_dim(bmap, isl_dim_div);
7026 first = isl_basic_map_first_unknown_div(bmap);
7027 if (first < 0)
7028 return isl_bool_error;
7029 return first == n;
7032 /* Do all basic maps in "map" have an explicit representation
7033 * for all local variables?
7035 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7037 int i;
7039 if (!map)
7040 return isl_bool_error;
7042 for (i = 0; i < map->n; ++i) {
7043 int known = isl_basic_map_divs_known(map->p[i]);
7044 if (known <= 0)
7045 return known;
7048 return isl_bool_true;
7051 /* If bmap contains any unknown divs, then compute explicit
7052 * expressions for them. However, this computation may be
7053 * quite expensive, so first try to remove divs that aren't
7054 * strictly needed.
7056 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7058 int known;
7059 struct isl_map *map;
7061 known = isl_basic_map_divs_known(bmap);
7062 if (known < 0)
7063 goto error;
7064 if (known)
7065 return isl_map_from_basic_map(bmap);
7067 bmap = isl_basic_map_drop_redundant_divs(bmap);
7069 known = isl_basic_map_divs_known(bmap);
7070 if (known < 0)
7071 goto error;
7072 if (known)
7073 return isl_map_from_basic_map(bmap);
7075 map = compute_divs(bmap);
7076 return map;
7077 error:
7078 isl_basic_map_free(bmap);
7079 return NULL;
7082 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7084 int i;
7085 int known;
7086 struct isl_map *res;
7088 if (!map)
7089 return NULL;
7090 if (map->n == 0)
7091 return map;
7093 known = isl_map_divs_known(map);
7094 if (known < 0) {
7095 isl_map_free(map);
7096 return NULL;
7098 if (known)
7099 return map;
7101 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7102 for (i = 1 ; i < map->n; ++i) {
7103 struct isl_map *r2;
7104 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7105 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7106 res = isl_map_union_disjoint(res, r2);
7107 else
7108 res = isl_map_union(res, r2);
7110 isl_map_free(map);
7112 return res;
7115 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7117 return (struct isl_set *)
7118 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7121 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7123 return (struct isl_set *)
7124 isl_map_compute_divs((struct isl_map *)set);
7127 struct isl_set *isl_map_domain(struct isl_map *map)
7129 int i;
7130 struct isl_set *set;
7132 if (!map)
7133 goto error;
7135 map = isl_map_cow(map);
7136 if (!map)
7137 return NULL;
7139 set = (struct isl_set *)map;
7140 set->dim = isl_space_domain(set->dim);
7141 if (!set->dim)
7142 goto error;
7143 for (i = 0; i < map->n; ++i) {
7144 set->p[i] = isl_basic_map_domain(map->p[i]);
7145 if (!set->p[i])
7146 goto error;
7148 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7149 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7150 return set;
7151 error:
7152 isl_map_free(map);
7153 return NULL;
7156 /* Return the union of "map1" and "map2", where we assume for now that
7157 * "map1" and "map2" are disjoint. Note that the basic maps inside
7158 * "map1" or "map2" may not be disjoint from each other.
7159 * Also note that this function is also called from isl_map_union,
7160 * which takes care of handling the situation where "map1" and "map2"
7161 * may not be disjoint.
7163 * If one of the inputs is empty, we can simply return the other input.
7164 * Similarly, if one of the inputs is universal, then it is equal to the union.
7166 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7167 __isl_take isl_map *map2)
7169 int i;
7170 unsigned flags = 0;
7171 struct isl_map *map = NULL;
7172 int is_universe;
7174 if (!map1 || !map2)
7175 goto error;
7177 if (!isl_space_is_equal(map1->dim, map2->dim))
7178 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7179 "spaces don't match", goto error);
7181 if (map1->n == 0) {
7182 isl_map_free(map1);
7183 return map2;
7185 if (map2->n == 0) {
7186 isl_map_free(map2);
7187 return map1;
7190 is_universe = isl_map_plain_is_universe(map1);
7191 if (is_universe < 0)
7192 goto error;
7193 if (is_universe) {
7194 isl_map_free(map2);
7195 return map1;
7198 is_universe = isl_map_plain_is_universe(map2);
7199 if (is_universe < 0)
7200 goto error;
7201 if (is_universe) {
7202 isl_map_free(map1);
7203 return map2;
7206 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7207 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7208 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7210 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7211 map1->n + map2->n, flags);
7212 if (!map)
7213 goto error;
7214 for (i = 0; i < map1->n; ++i) {
7215 map = isl_map_add_basic_map(map,
7216 isl_basic_map_copy(map1->p[i]));
7217 if (!map)
7218 goto error;
7220 for (i = 0; i < map2->n; ++i) {
7221 map = isl_map_add_basic_map(map,
7222 isl_basic_map_copy(map2->p[i]));
7223 if (!map)
7224 goto error;
7226 isl_map_free(map1);
7227 isl_map_free(map2);
7228 return map;
7229 error:
7230 isl_map_free(map);
7231 isl_map_free(map1);
7232 isl_map_free(map2);
7233 return NULL;
7236 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7237 * guaranteed to be disjoint by the caller.
7239 * Note that this functions is called from within isl_map_make_disjoint,
7240 * so we have to be careful not to touch the constraints of the inputs
7241 * in any way.
7243 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7244 __isl_take isl_map *map2)
7246 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7249 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7250 * not be disjoint. The parameters are assumed to have been aligned.
7252 * We currently simply call map_union_disjoint, the internal operation
7253 * of which does not really depend on the inputs being disjoint.
7254 * If the result contains more than one basic map, then we clear
7255 * the disjoint flag since the result may contain basic maps from
7256 * both inputs and these are not guaranteed to be disjoint.
7258 * As a special case, if "map1" and "map2" are obviously equal,
7259 * then we simply return "map1".
7261 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7262 __isl_take isl_map *map2)
7264 int equal;
7266 if (!map1 || !map2)
7267 goto error;
7269 equal = isl_map_plain_is_equal(map1, map2);
7270 if (equal < 0)
7271 goto error;
7272 if (equal) {
7273 isl_map_free(map2);
7274 return map1;
7277 map1 = map_union_disjoint(map1, map2);
7278 if (!map1)
7279 return NULL;
7280 if (map1->n > 1)
7281 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7282 return map1;
7283 error:
7284 isl_map_free(map1);
7285 isl_map_free(map2);
7286 return NULL;
7289 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7290 * not be disjoint.
7292 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7293 __isl_take isl_map *map2)
7295 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7298 struct isl_set *isl_set_union_disjoint(
7299 struct isl_set *set1, struct isl_set *set2)
7301 return (struct isl_set *)
7302 isl_map_union_disjoint(
7303 (struct isl_map *)set1, (struct isl_map *)set2);
7306 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7308 return (struct isl_set *)
7309 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7312 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7313 * the results.
7315 * "map" and "set" are assumed to be compatible and non-NULL.
7317 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7318 __isl_take isl_set *set,
7319 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7320 __isl_take isl_basic_set *bset))
7322 unsigned flags = 0;
7323 struct isl_map *result;
7324 int i, j;
7326 if (isl_set_plain_is_universe(set)) {
7327 isl_set_free(set);
7328 return map;
7331 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7332 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7333 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7335 result = isl_map_alloc_space(isl_space_copy(map->dim),
7336 map->n * set->n, flags);
7337 for (i = 0; result && i < map->n; ++i)
7338 for (j = 0; j < set->n; ++j) {
7339 result = isl_map_add_basic_map(result,
7340 fn(isl_basic_map_copy(map->p[i]),
7341 isl_basic_set_copy(set->p[j])));
7342 if (!result)
7343 break;
7346 isl_map_free(map);
7347 isl_set_free(set);
7348 return result;
7351 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7352 __isl_take isl_set *set)
7354 if (!map || !set)
7355 goto error;
7357 if (!isl_map_compatible_range(map, set))
7358 isl_die(set->ctx, isl_error_invalid,
7359 "incompatible spaces", goto error);
7361 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7362 error:
7363 isl_map_free(map);
7364 isl_set_free(set);
7365 return NULL;
7368 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7369 __isl_take isl_set *set)
7371 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7374 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7375 __isl_take isl_set *set)
7377 if (!map || !set)
7378 goto error;
7380 if (!isl_map_compatible_domain(map, set))
7381 isl_die(set->ctx, isl_error_invalid,
7382 "incompatible spaces", goto error);
7384 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7385 error:
7386 isl_map_free(map);
7387 isl_set_free(set);
7388 return NULL;
7391 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7392 __isl_take isl_set *set)
7394 return isl_map_align_params_map_map_and(map, set,
7395 &map_intersect_domain);
7398 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7399 __isl_take isl_map *map2)
7401 if (!map1 || !map2)
7402 goto error;
7403 map1 = isl_map_reverse(map1);
7404 map1 = isl_map_apply_range(map1, map2);
7405 return isl_map_reverse(map1);
7406 error:
7407 isl_map_free(map1);
7408 isl_map_free(map2);
7409 return NULL;
7412 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7413 __isl_take isl_map *map2)
7415 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7418 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7419 __isl_take isl_map *map2)
7421 isl_space *dim_result;
7422 struct isl_map *result;
7423 int i, j;
7425 if (!map1 || !map2)
7426 goto error;
7428 dim_result = isl_space_join(isl_space_copy(map1->dim),
7429 isl_space_copy(map2->dim));
7431 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7432 if (!result)
7433 goto error;
7434 for (i = 0; i < map1->n; ++i)
7435 for (j = 0; j < map2->n; ++j) {
7436 result = isl_map_add_basic_map(result,
7437 isl_basic_map_apply_range(
7438 isl_basic_map_copy(map1->p[i]),
7439 isl_basic_map_copy(map2->p[j])));
7440 if (!result)
7441 goto error;
7443 isl_map_free(map1);
7444 isl_map_free(map2);
7445 if (result && result->n <= 1)
7446 ISL_F_SET(result, ISL_MAP_DISJOINT);
7447 return result;
7448 error:
7449 isl_map_free(map1);
7450 isl_map_free(map2);
7451 return NULL;
7454 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7455 __isl_take isl_map *map2)
7457 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7461 * returns range - domain
7463 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7465 isl_space *target_space;
7466 struct isl_basic_set *bset;
7467 unsigned dim;
7468 unsigned nparam;
7469 int i;
7471 if (!bmap)
7472 goto error;
7473 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7474 bmap->dim, isl_dim_out),
7475 goto error);
7476 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7477 dim = isl_basic_map_n_in(bmap);
7478 nparam = isl_basic_map_n_param(bmap);
7479 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7480 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7481 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7482 for (i = 0; i < dim; ++i) {
7483 int j = isl_basic_map_alloc_equality(bmap);
7484 if (j < 0) {
7485 bmap = isl_basic_map_free(bmap);
7486 break;
7488 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7489 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7490 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7491 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7493 bset = isl_basic_map_domain(bmap);
7494 bset = isl_basic_set_reset_space(bset, target_space);
7495 return bset;
7496 error:
7497 isl_basic_map_free(bmap);
7498 return NULL;
7502 * returns range - domain
7504 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7506 int i;
7507 isl_space *dim;
7508 struct isl_set *result;
7510 if (!map)
7511 return NULL;
7513 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7514 map->dim, isl_dim_out),
7515 goto error);
7516 dim = isl_map_get_space(map);
7517 dim = isl_space_domain(dim);
7518 result = isl_set_alloc_space(dim, map->n, 0);
7519 if (!result)
7520 goto error;
7521 for (i = 0; i < map->n; ++i)
7522 result = isl_set_add_basic_set(result,
7523 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7524 isl_map_free(map);
7525 return result;
7526 error:
7527 isl_map_free(map);
7528 return NULL;
7532 * returns [domain -> range] -> range - domain
7534 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7535 __isl_take isl_basic_map *bmap)
7537 int i, k;
7538 isl_space *dim;
7539 isl_basic_map *domain;
7540 int nparam, n;
7541 unsigned total;
7543 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7544 bmap->dim, isl_dim_out))
7545 isl_die(bmap->ctx, isl_error_invalid,
7546 "domain and range don't match", goto error);
7548 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7549 n = isl_basic_map_dim(bmap, isl_dim_in);
7551 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7552 domain = isl_basic_map_universe(dim);
7554 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7555 bmap = isl_basic_map_apply_range(bmap, domain);
7556 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7558 total = isl_basic_map_total_dim(bmap);
7560 for (i = 0; i < n; ++i) {
7561 k = isl_basic_map_alloc_equality(bmap);
7562 if (k < 0)
7563 goto error;
7564 isl_seq_clr(bmap->eq[k], 1 + total);
7565 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7566 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7567 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7570 bmap = isl_basic_map_gauss(bmap, NULL);
7571 return isl_basic_map_finalize(bmap);
7572 error:
7573 isl_basic_map_free(bmap);
7574 return NULL;
7578 * returns [domain -> range] -> range - domain
7580 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7582 int i;
7583 isl_space *domain_dim;
7585 if (!map)
7586 return NULL;
7588 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7589 map->dim, isl_dim_out))
7590 isl_die(map->ctx, isl_error_invalid,
7591 "domain and range don't match", goto error);
7593 map = isl_map_cow(map);
7594 if (!map)
7595 return NULL;
7597 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7598 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7599 map->dim = isl_space_join(map->dim, domain_dim);
7600 if (!map->dim)
7601 goto error;
7602 for (i = 0; i < map->n; ++i) {
7603 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7604 if (!map->p[i])
7605 goto error;
7607 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7608 return map;
7609 error:
7610 isl_map_free(map);
7611 return NULL;
7614 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7616 struct isl_basic_map *bmap;
7617 unsigned nparam;
7618 unsigned dim;
7619 int i;
7621 if (!dims)
7622 return NULL;
7624 nparam = dims->nparam;
7625 dim = dims->n_out;
7626 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7627 if (!bmap)
7628 goto error;
7630 for (i = 0; i < dim; ++i) {
7631 int j = isl_basic_map_alloc_equality(bmap);
7632 if (j < 0)
7633 goto error;
7634 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7635 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7636 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7638 return isl_basic_map_finalize(bmap);
7639 error:
7640 isl_basic_map_free(bmap);
7641 return NULL;
7644 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7646 if (!dim)
7647 return NULL;
7648 if (dim->n_in != dim->n_out)
7649 isl_die(dim->ctx, isl_error_invalid,
7650 "number of input and output dimensions needs to be "
7651 "the same", goto error);
7652 return basic_map_identity(dim);
7653 error:
7654 isl_space_free(dim);
7655 return NULL;
7658 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7660 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7663 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7665 isl_space *dim = isl_set_get_space(set);
7666 isl_map *id;
7667 id = isl_map_identity(isl_space_map_from_set(dim));
7668 return isl_map_intersect_range(id, set);
7671 /* Construct a basic set with all set dimensions having only non-negative
7672 * values.
7674 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7675 __isl_take isl_space *space)
7677 int i;
7678 unsigned nparam;
7679 unsigned dim;
7680 struct isl_basic_set *bset;
7682 if (!space)
7683 return NULL;
7684 nparam = space->nparam;
7685 dim = space->n_out;
7686 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7687 if (!bset)
7688 return NULL;
7689 for (i = 0; i < dim; ++i) {
7690 int k = isl_basic_set_alloc_inequality(bset);
7691 if (k < 0)
7692 goto error;
7693 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7694 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7696 return bset;
7697 error:
7698 isl_basic_set_free(bset);
7699 return NULL;
7702 /* Construct the half-space x_pos >= 0.
7704 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7705 int pos)
7707 int k;
7708 isl_basic_set *nonneg;
7710 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7711 k = isl_basic_set_alloc_inequality(nonneg);
7712 if (k < 0)
7713 goto error;
7714 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7715 isl_int_set_si(nonneg->ineq[k][pos], 1);
7717 return isl_basic_set_finalize(nonneg);
7718 error:
7719 isl_basic_set_free(nonneg);
7720 return NULL;
7723 /* Construct the half-space x_pos <= -1.
7725 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7727 int k;
7728 isl_basic_set *neg;
7730 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7731 k = isl_basic_set_alloc_inequality(neg);
7732 if (k < 0)
7733 goto error;
7734 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7735 isl_int_set_si(neg->ineq[k][0], -1);
7736 isl_int_set_si(neg->ineq[k][pos], -1);
7738 return isl_basic_set_finalize(neg);
7739 error:
7740 isl_basic_set_free(neg);
7741 return NULL;
7744 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7745 enum isl_dim_type type, unsigned first, unsigned n)
7747 int i;
7748 unsigned offset;
7749 isl_basic_set *nonneg;
7750 isl_basic_set *neg;
7752 if (!set)
7753 return NULL;
7754 if (n == 0)
7755 return set;
7757 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7759 offset = pos(set->dim, type);
7760 for (i = 0; i < n; ++i) {
7761 nonneg = nonneg_halfspace(isl_set_get_space(set),
7762 offset + first + i);
7763 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7765 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7768 return set;
7769 error:
7770 isl_set_free(set);
7771 return NULL;
7774 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7775 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7776 void *user)
7778 isl_set *half;
7780 if (!set)
7781 return -1;
7782 if (isl_set_plain_is_empty(set)) {
7783 isl_set_free(set);
7784 return 0;
7786 if (first == len)
7787 return fn(set, signs, user);
7789 signs[first] = 1;
7790 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7791 1 + first));
7792 half = isl_set_intersect(half, isl_set_copy(set));
7793 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7794 goto error;
7796 signs[first] = -1;
7797 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7798 1 + first));
7799 half = isl_set_intersect(half, set);
7800 return foreach_orthant(half, signs, first + 1, len, fn, user);
7801 error:
7802 isl_set_free(set);
7803 return -1;
7806 /* Call "fn" on the intersections of "set" with each of the orthants
7807 * (except for obviously empty intersections). The orthant is identified
7808 * by the signs array, with each entry having value 1 or -1 according
7809 * to the sign of the corresponding variable.
7811 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7812 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7813 void *user)
7815 unsigned nparam;
7816 unsigned nvar;
7817 int *signs;
7818 int r;
7820 if (!set)
7821 return -1;
7822 if (isl_set_plain_is_empty(set))
7823 return 0;
7825 nparam = isl_set_dim(set, isl_dim_param);
7826 nvar = isl_set_dim(set, isl_dim_set);
7828 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7830 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7831 fn, user);
7833 free(signs);
7835 return r;
7838 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7840 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7843 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7844 __isl_keep isl_basic_map *bmap2)
7846 int is_subset;
7847 struct isl_map *map1;
7848 struct isl_map *map2;
7850 if (!bmap1 || !bmap2)
7851 return isl_bool_error;
7853 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7854 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7856 is_subset = isl_map_is_subset(map1, map2);
7858 isl_map_free(map1);
7859 isl_map_free(map2);
7861 return is_subset;
7864 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7865 __isl_keep isl_basic_set *bset2)
7867 return isl_basic_map_is_subset(bset1, bset2);
7870 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7871 __isl_keep isl_basic_map *bmap2)
7873 isl_bool is_subset;
7875 if (!bmap1 || !bmap2)
7876 return isl_bool_error;
7877 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7878 if (is_subset != isl_bool_true)
7879 return is_subset;
7880 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7881 return is_subset;
7884 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7885 __isl_keep isl_basic_set *bset2)
7887 return isl_basic_map_is_equal(
7888 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7891 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7893 int i;
7894 int is_empty;
7896 if (!map)
7897 return isl_bool_error;
7898 for (i = 0; i < map->n; ++i) {
7899 is_empty = isl_basic_map_is_empty(map->p[i]);
7900 if (is_empty < 0)
7901 return isl_bool_error;
7902 if (!is_empty)
7903 return isl_bool_false;
7905 return isl_bool_true;
7908 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7910 return map ? map->n == 0 : isl_bool_error;
7913 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7915 return set ? set->n == 0 : isl_bool_error;
7918 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7920 return isl_map_is_empty((struct isl_map *)set);
7923 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7925 if (!map1 || !map2)
7926 return -1;
7928 return isl_space_is_equal(map1->dim, map2->dim);
7931 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7933 if (!set1 || !set2)
7934 return -1;
7936 return isl_space_is_equal(set1->dim, set2->dim);
7939 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7941 isl_bool is_subset;
7943 if (!map1 || !map2)
7944 return isl_bool_error;
7945 is_subset = isl_map_is_subset(map1, map2);
7946 if (is_subset != isl_bool_true)
7947 return is_subset;
7948 is_subset = isl_map_is_subset(map2, map1);
7949 return is_subset;
7952 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7954 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7957 isl_bool isl_basic_map_is_strict_subset(
7958 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7960 isl_bool is_subset;
7962 if (!bmap1 || !bmap2)
7963 return isl_bool_error;
7964 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7965 if (is_subset != isl_bool_true)
7966 return is_subset;
7967 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7968 if (is_subset == isl_bool_error)
7969 return is_subset;
7970 return !is_subset;
7973 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7974 __isl_keep isl_map *map2)
7976 isl_bool is_subset;
7978 if (!map1 || !map2)
7979 return isl_bool_error;
7980 is_subset = isl_map_is_subset(map1, map2);
7981 if (is_subset != isl_bool_true)
7982 return is_subset;
7983 is_subset = isl_map_is_subset(map2, map1);
7984 if (is_subset == isl_bool_error)
7985 return is_subset;
7986 return !is_subset;
7989 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7990 __isl_keep isl_set *set2)
7992 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7995 /* Is "bmap" obviously equal to the universe with the same space?
7997 * That is, does it not have any constraints?
7999 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
8001 if (!bmap)
8002 return isl_bool_error;
8003 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8006 /* Is "bset" obviously equal to the universe with the same space?
8008 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8010 return isl_basic_map_plain_is_universe(bset);
8013 /* If "c" does not involve any existentially quantified variables,
8014 * then set *univ to false and abort
8016 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8018 isl_bool *univ = user;
8019 unsigned n;
8021 n = isl_constraint_dim(c, isl_dim_div);
8022 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8023 isl_constraint_free(c);
8024 if (*univ < 0 || !*univ)
8025 return isl_stat_error;
8026 return isl_stat_ok;
8029 /* Is "bmap" equal to the universe with the same space?
8031 * First check if it is obviously equal to the universe.
8032 * If not and if there are any constraints not involving
8033 * existentially quantified variables, then it is certainly
8034 * not equal to the universe.
8035 * Otherwise, check if the universe is a subset of "bmap".
8037 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8039 isl_bool univ;
8040 isl_basic_map *test;
8042 univ = isl_basic_map_plain_is_universe(bmap);
8043 if (univ < 0 || univ)
8044 return univ;
8045 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8046 return isl_bool_false;
8047 univ = isl_bool_true;
8048 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8049 univ)
8050 return isl_bool_error;
8051 if (univ < 0 || !univ)
8052 return univ;
8053 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8054 univ = isl_basic_map_is_subset(test, bmap);
8055 isl_basic_map_free(test);
8056 return univ;
8059 /* Is "bset" equal to the universe with the same space?
8061 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8063 return isl_basic_map_is_universe(bset);
8066 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8068 int i;
8070 if (!map)
8071 return isl_bool_error;
8073 for (i = 0; i < map->n; ++i) {
8074 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8075 if (r < 0 || r)
8076 return r;
8079 return isl_bool_false;
8082 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8084 return isl_map_plain_is_universe((isl_map *) set);
8087 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8089 struct isl_basic_set *bset = NULL;
8090 struct isl_vec *sample = NULL;
8091 isl_bool empty, non_empty;
8093 if (!bmap)
8094 return isl_bool_error;
8096 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8097 return isl_bool_true;
8099 if (isl_basic_map_plain_is_universe(bmap))
8100 return isl_bool_false;
8102 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8103 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8104 copy = isl_basic_map_remove_redundancies(copy);
8105 empty = isl_basic_map_plain_is_empty(copy);
8106 isl_basic_map_free(copy);
8107 return empty;
8110 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8111 if (non_empty < 0)
8112 return isl_bool_error;
8113 if (non_empty)
8114 return isl_bool_false;
8115 isl_vec_free(bmap->sample);
8116 bmap->sample = NULL;
8117 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8118 if (!bset)
8119 return isl_bool_error;
8120 sample = isl_basic_set_sample_vec(bset);
8121 if (!sample)
8122 return isl_bool_error;
8123 empty = sample->size == 0;
8124 isl_vec_free(bmap->sample);
8125 bmap->sample = sample;
8126 if (empty)
8127 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8129 return empty;
8132 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8134 if (!bmap)
8135 return isl_bool_error;
8136 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8139 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8141 if (!bset)
8142 return isl_bool_error;
8143 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8146 /* Is "bmap" known to be non-empty?
8148 * That is, is the cached sample still valid?
8150 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8152 unsigned total;
8154 if (!bmap)
8155 return isl_bool_error;
8156 if (!bmap->sample)
8157 return isl_bool_false;
8158 total = 1 + isl_basic_map_total_dim(bmap);
8159 if (bmap->sample->size != total)
8160 return isl_bool_false;
8161 return isl_basic_map_contains(bmap, bmap->sample);
8164 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8166 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8169 struct isl_map *isl_basic_map_union(
8170 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8172 struct isl_map *map;
8173 if (!bmap1 || !bmap2)
8174 goto error;
8176 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8178 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8179 if (!map)
8180 goto error;
8181 map = isl_map_add_basic_map(map, bmap1);
8182 map = isl_map_add_basic_map(map, bmap2);
8183 return map;
8184 error:
8185 isl_basic_map_free(bmap1);
8186 isl_basic_map_free(bmap2);
8187 return NULL;
8190 struct isl_set *isl_basic_set_union(
8191 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8193 return (struct isl_set *)isl_basic_map_union(
8194 (struct isl_basic_map *)bset1,
8195 (struct isl_basic_map *)bset2);
8198 /* Order divs such that any div only depends on previous divs */
8199 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8201 int i;
8202 unsigned off;
8204 if (!bmap)
8205 return NULL;
8207 off = isl_space_dim(bmap->dim, isl_dim_all);
8209 for (i = 0; i < bmap->n_div; ++i) {
8210 int pos;
8211 if (isl_int_is_zero(bmap->div[i][0]))
8212 continue;
8213 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8214 bmap->n_div-i);
8215 if (pos == -1)
8216 continue;
8217 if (pos == 0)
8218 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8219 "integer division depends on itself",
8220 return isl_basic_map_free(bmap));
8221 isl_basic_map_swap_div(bmap, i, i + pos);
8222 --i;
8224 return bmap;
8227 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8229 return (struct isl_basic_set *)
8230 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8233 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8235 int i;
8237 if (!map)
8238 return 0;
8240 for (i = 0; i < map->n; ++i) {
8241 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8242 if (!map->p[i])
8243 goto error;
8246 return map;
8247 error:
8248 isl_map_free(map);
8249 return NULL;
8252 /* Apply the expansion computed by isl_merge_divs.
8253 * The expansion itself is given by "exp" while the resulting
8254 * list of divs is given by "div".
8256 * Move the integer divisions of "bset" into the right position
8257 * according to "exp" and then introduce the additional integer
8258 * divisions, adding div constraints.
8259 * The moving should be done first to avoid moving coefficients
8260 * in the definitions of the extra integer divisions.
8262 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8263 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8265 int i, j;
8266 int n_div;
8268 bset = isl_basic_set_cow(bset);
8269 if (!bset || !div)
8270 goto error;
8272 if (div->n_row < bset->n_div)
8273 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8274 "not an expansion", goto error);
8276 n_div = bset->n_div;
8277 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8278 div->n_row - n_div, 0,
8279 2 * (div->n_row - n_div));
8281 for (i = n_div; i < div->n_row; ++i)
8282 if (isl_basic_set_alloc_div(bset) < 0)
8283 goto error;
8285 for (j = n_div - 1; j >= 0; --j) {
8286 if (exp[j] == j)
8287 break;
8288 isl_basic_map_swap_div(bset, j, exp[j]);
8290 j = 0;
8291 for (i = 0; i < div->n_row; ++i) {
8292 if (j < n_div && exp[j] == i) {
8293 j++;
8294 } else {
8295 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8296 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8297 goto error;
8301 isl_mat_free(div);
8302 return bset;
8303 error:
8304 isl_basic_set_free(bset);
8305 isl_mat_free(div);
8306 return NULL;
8309 /* Look for a div in dst that corresponds to the div "div" in src.
8310 * The divs before "div" in src and dst are assumed to be the same.
8312 * Returns -1 if no corresponding div was found and the position
8313 * of the corresponding div in dst otherwise.
8315 static int find_div(struct isl_basic_map *dst,
8316 struct isl_basic_map *src, unsigned div)
8318 int i;
8320 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8322 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8323 for (i = div; i < dst->n_div; ++i)
8324 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8325 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8326 dst->n_div - div) == -1)
8327 return i;
8328 return -1;
8331 /* Align the divs of "dst" to those of "src", adding divs from "src"
8332 * if needed. That is, make sure that the first src->n_div divs
8333 * of the result are equal to those of src.
8335 * The result is not finalized as by design it will have redundant
8336 * divs if any divs from "src" were copied.
8338 __isl_give isl_basic_map *isl_basic_map_align_divs(
8339 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8341 int i;
8342 int known, extended;
8343 unsigned total;
8345 if (!dst || !src)
8346 return isl_basic_map_free(dst);
8348 if (src->n_div == 0)
8349 return dst;
8351 known = isl_basic_map_divs_known(src);
8352 if (known < 0)
8353 return isl_basic_map_free(dst);
8354 if (!known)
8355 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8356 "some src divs are unknown",
8357 return isl_basic_map_free(dst));
8359 src = isl_basic_map_order_divs(src);
8361 extended = 0;
8362 total = isl_space_dim(src->dim, isl_dim_all);
8363 for (i = 0; i < src->n_div; ++i) {
8364 int j = find_div(dst, src, i);
8365 if (j < 0) {
8366 if (!extended) {
8367 int extra = src->n_div - i;
8368 dst = isl_basic_map_cow(dst);
8369 if (!dst)
8370 return NULL;
8371 dst = isl_basic_map_extend_space(dst,
8372 isl_space_copy(dst->dim),
8373 extra, 0, 2 * extra);
8374 extended = 1;
8376 j = isl_basic_map_alloc_div(dst);
8377 if (j < 0)
8378 return isl_basic_map_free(dst);
8379 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8380 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8381 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8382 return isl_basic_map_free(dst);
8384 if (j != i)
8385 isl_basic_map_swap_div(dst, i, j);
8387 return dst;
8390 struct isl_basic_set *isl_basic_set_align_divs(
8391 struct isl_basic_set *dst, struct isl_basic_set *src)
8393 return (struct isl_basic_set *)isl_basic_map_align_divs(
8394 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8397 struct isl_map *isl_map_align_divs(struct isl_map *map)
8399 int i;
8401 if (!map)
8402 return NULL;
8403 if (map->n == 0)
8404 return map;
8405 map = isl_map_compute_divs(map);
8406 map = isl_map_cow(map);
8407 if (!map)
8408 return NULL;
8410 for (i = 1; i < map->n; ++i)
8411 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8412 for (i = 1; i < map->n; ++i) {
8413 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8414 if (!map->p[i])
8415 return isl_map_free(map);
8418 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8419 return map;
8422 struct isl_set *isl_set_align_divs(struct isl_set *set)
8424 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8427 /* Align the divs of the basic maps in "map" to those
8428 * of the basic maps in "list", as well as to the other basic maps in "map".
8429 * The elements in "list" are assumed to have known divs.
8431 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8432 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8434 int i, n;
8436 map = isl_map_compute_divs(map);
8437 map = isl_map_cow(map);
8438 if (!map || !list)
8439 return isl_map_free(map);
8440 if (map->n == 0)
8441 return map;
8443 n = isl_basic_map_list_n_basic_map(list);
8444 for (i = 0; i < n; ++i) {
8445 isl_basic_map *bmap;
8447 bmap = isl_basic_map_list_get_basic_map(list, i);
8448 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8449 isl_basic_map_free(bmap);
8451 if (!map->p[0])
8452 return isl_map_free(map);
8454 return isl_map_align_divs(map);
8457 /* Align the divs of each element of "list" to those of "bmap".
8458 * Both "bmap" and the elements of "list" are assumed to have known divs.
8460 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8461 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8463 int i, n;
8465 if (!list || !bmap)
8466 return isl_basic_map_list_free(list);
8468 n = isl_basic_map_list_n_basic_map(list);
8469 for (i = 0; i < n; ++i) {
8470 isl_basic_map *bmap_i;
8472 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8473 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8474 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8477 return list;
8480 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8481 __isl_take isl_map *map)
8483 if (!set || !map)
8484 goto error;
8485 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8486 map = isl_map_intersect_domain(map, set);
8487 set = isl_map_range(map);
8488 return set;
8489 error:
8490 isl_set_free(set);
8491 isl_map_free(map);
8492 return NULL;
8495 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8496 __isl_take isl_map *map)
8498 return isl_map_align_params_map_map_and(set, map, &set_apply);
8501 /* There is no need to cow as removing empty parts doesn't change
8502 * the meaning of the set.
8504 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8506 int i;
8508 if (!map)
8509 return NULL;
8511 for (i = map->n - 1; i >= 0; --i)
8512 remove_if_empty(map, i);
8514 return map;
8517 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8519 return (struct isl_set *)
8520 isl_map_remove_empty_parts((struct isl_map *)set);
8523 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8525 struct isl_basic_map *bmap;
8526 if (!map || map->n == 0)
8527 return NULL;
8528 bmap = map->p[map->n-1];
8529 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8530 return isl_basic_map_copy(bmap);
8533 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8535 return (struct isl_basic_set *)
8536 isl_map_copy_basic_map((struct isl_map *)set);
8539 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8540 __isl_keep isl_basic_map *bmap)
8542 int i;
8544 if (!map || !bmap)
8545 goto error;
8546 for (i = map->n-1; i >= 0; --i) {
8547 if (map->p[i] != bmap)
8548 continue;
8549 map = isl_map_cow(map);
8550 if (!map)
8551 goto error;
8552 isl_basic_map_free(map->p[i]);
8553 if (i != map->n-1) {
8554 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8555 map->p[i] = map->p[map->n-1];
8557 map->n--;
8558 return map;
8560 return map;
8561 error:
8562 isl_map_free(map);
8563 return NULL;
8566 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8567 struct isl_basic_set *bset)
8569 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8570 (struct isl_basic_map *)bset);
8573 /* Given two basic sets bset1 and bset2, compute the maximal difference
8574 * between the values of dimension pos in bset1 and those in bset2
8575 * for any common value of the parameters and dimensions preceding pos.
8577 static enum isl_lp_result basic_set_maximal_difference_at(
8578 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8579 int pos, isl_int *opt)
8581 isl_space *dims;
8582 struct isl_basic_map *bmap1 = NULL;
8583 struct isl_basic_map *bmap2 = NULL;
8584 struct isl_ctx *ctx;
8585 struct isl_vec *obj;
8586 unsigned total;
8587 unsigned nparam;
8588 unsigned dim1, dim2;
8589 enum isl_lp_result res;
8591 if (!bset1 || !bset2)
8592 return isl_lp_error;
8594 nparam = isl_basic_set_n_param(bset1);
8595 dim1 = isl_basic_set_n_dim(bset1);
8596 dim2 = isl_basic_set_n_dim(bset2);
8597 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8598 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8599 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8600 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8601 if (!bmap1 || !bmap2)
8602 goto error;
8603 bmap1 = isl_basic_map_cow(bmap1);
8604 bmap1 = isl_basic_map_extend(bmap1, nparam,
8605 pos, (dim1 - pos) + (dim2 - pos),
8606 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8607 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8608 if (!bmap1)
8609 goto error2;
8610 total = isl_basic_map_total_dim(bmap1);
8611 ctx = bmap1->ctx;
8612 obj = isl_vec_alloc(ctx, 1 + total);
8613 if (!obj)
8614 goto error2;
8615 isl_seq_clr(obj->block.data, 1 + total);
8616 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8617 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8618 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8619 opt, NULL, NULL);
8620 isl_basic_map_free(bmap1);
8621 isl_vec_free(obj);
8622 return res;
8623 error:
8624 isl_basic_map_free(bmap2);
8625 error2:
8626 isl_basic_map_free(bmap1);
8627 return isl_lp_error;
8630 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8631 * for any common value of the parameters and dimensions preceding pos
8632 * in both basic sets, the values of dimension pos in bset1 are
8633 * smaller or larger than those in bset2.
8635 * Returns
8636 * 1 if bset1 follows bset2
8637 * -1 if bset1 precedes bset2
8638 * 0 if bset1 and bset2 are incomparable
8639 * -2 if some error occurred.
8641 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8642 struct isl_basic_set *bset2, int pos)
8644 isl_int opt;
8645 enum isl_lp_result res;
8646 int cmp;
8648 isl_int_init(opt);
8650 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8652 if (res == isl_lp_empty)
8653 cmp = 0;
8654 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8655 res == isl_lp_unbounded)
8656 cmp = 1;
8657 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8658 cmp = -1;
8659 else
8660 cmp = -2;
8662 isl_int_clear(opt);
8663 return cmp;
8666 /* Given two basic sets bset1 and bset2, check whether
8667 * for any common value of the parameters and dimensions preceding pos
8668 * there is a value of dimension pos in bset1 that is larger
8669 * than a value of the same dimension in bset2.
8671 * Return
8672 * 1 if there exists such a pair
8673 * 0 if there is no such pair, but there is a pair of equal values
8674 * -1 otherwise
8675 * -2 if some error occurred.
8677 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8678 __isl_keep isl_basic_set *bset2, int pos)
8680 isl_int opt;
8681 enum isl_lp_result res;
8682 int cmp;
8684 isl_int_init(opt);
8686 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8688 if (res == isl_lp_empty)
8689 cmp = -1;
8690 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8691 res == isl_lp_unbounded)
8692 cmp = 1;
8693 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8694 cmp = -1;
8695 else if (res == isl_lp_ok)
8696 cmp = 0;
8697 else
8698 cmp = -2;
8700 isl_int_clear(opt);
8701 return cmp;
8704 /* Given two sets set1 and set2, check whether
8705 * for any common value of the parameters and dimensions preceding pos
8706 * there is a value of dimension pos in set1 that is larger
8707 * than a value of the same dimension in set2.
8709 * Return
8710 * 1 if there exists such a pair
8711 * 0 if there is no such pair, but there is a pair of equal values
8712 * -1 otherwise
8713 * -2 if some error occurred.
8715 int isl_set_follows_at(__isl_keep isl_set *set1,
8716 __isl_keep isl_set *set2, int pos)
8718 int i, j;
8719 int follows = -1;
8721 if (!set1 || !set2)
8722 return -2;
8724 for (i = 0; i < set1->n; ++i)
8725 for (j = 0; j < set2->n; ++j) {
8726 int f;
8727 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8728 if (f == 1 || f == -2)
8729 return f;
8730 if (f > follows)
8731 follows = f;
8734 return follows;
8737 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8738 unsigned pos, isl_int *val)
8740 int i;
8741 int d;
8742 unsigned total;
8744 if (!bmap)
8745 return -1;
8746 total = isl_basic_map_total_dim(bmap);
8747 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8748 for (; d+1 > pos; --d)
8749 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8750 break;
8751 if (d != pos)
8752 continue;
8753 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8754 return 0;
8755 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8756 return 0;
8757 if (!isl_int_is_one(bmap->eq[i][1+d]))
8758 return 0;
8759 if (val)
8760 isl_int_neg(*val, bmap->eq[i][0]);
8761 return 1;
8763 return 0;
8766 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8767 unsigned pos, isl_int *val)
8769 int i;
8770 isl_int v;
8771 isl_int tmp;
8772 int fixed;
8774 if (!map)
8775 return -1;
8776 if (map->n == 0)
8777 return 0;
8778 if (map->n == 1)
8779 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8780 isl_int_init(v);
8781 isl_int_init(tmp);
8782 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8783 for (i = 1; fixed == 1 && i < map->n; ++i) {
8784 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8785 if (fixed == 1 && isl_int_ne(tmp, v))
8786 fixed = 0;
8788 if (val)
8789 isl_int_set(*val, v);
8790 isl_int_clear(tmp);
8791 isl_int_clear(v);
8792 return fixed;
8795 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8796 unsigned pos, isl_int *val)
8798 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8799 pos, val);
8802 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8803 isl_int *val)
8805 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8808 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8809 enum isl_dim_type type, unsigned pos, isl_int *val)
8811 if (pos >= isl_basic_map_dim(bmap, type))
8812 return -1;
8813 return isl_basic_map_plain_has_fixed_var(bmap,
8814 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8817 /* If "bmap" obviously lies on a hyperplane where the given dimension
8818 * has a fixed value, then return that value.
8819 * Otherwise return NaN.
8821 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8822 __isl_keep isl_basic_map *bmap,
8823 enum isl_dim_type type, unsigned pos)
8825 isl_ctx *ctx;
8826 isl_val *v;
8827 int fixed;
8829 if (!bmap)
8830 return NULL;
8831 ctx = isl_basic_map_get_ctx(bmap);
8832 v = isl_val_alloc(ctx);
8833 if (!v)
8834 return NULL;
8835 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8836 if (fixed < 0)
8837 return isl_val_free(v);
8838 if (fixed) {
8839 isl_int_set_si(v->d, 1);
8840 return v;
8842 isl_val_free(v);
8843 return isl_val_nan(ctx);
8846 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8847 enum isl_dim_type type, unsigned pos, isl_int *val)
8849 if (pos >= isl_map_dim(map, type))
8850 return -1;
8851 return isl_map_plain_has_fixed_var(map,
8852 map_offset(map, type) - 1 + pos, val);
8855 /* If "map" obviously lies on a hyperplane where the given dimension
8856 * has a fixed value, then return that value.
8857 * Otherwise return NaN.
8859 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8860 enum isl_dim_type type, unsigned pos)
8862 isl_ctx *ctx;
8863 isl_val *v;
8864 int fixed;
8866 if (!map)
8867 return NULL;
8868 ctx = isl_map_get_ctx(map);
8869 v = isl_val_alloc(ctx);
8870 if (!v)
8871 return NULL;
8872 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8873 if (fixed < 0)
8874 return isl_val_free(v);
8875 if (fixed) {
8876 isl_int_set_si(v->d, 1);
8877 return v;
8879 isl_val_free(v);
8880 return isl_val_nan(ctx);
8883 /* If "set" obviously lies on a hyperplane where the given dimension
8884 * has a fixed value, then return that value.
8885 * Otherwise return NaN.
8887 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8888 enum isl_dim_type type, unsigned pos)
8890 return isl_map_plain_get_val_if_fixed(set, type, pos);
8893 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8894 enum isl_dim_type type, unsigned pos, isl_int *val)
8896 return isl_map_plain_is_fixed(set, type, pos, val);
8899 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8900 * then return this fixed value in *val.
8902 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8903 unsigned dim, isl_int *val)
8905 return isl_basic_set_plain_has_fixed_var(bset,
8906 isl_basic_set_n_param(bset) + dim, val);
8909 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8910 * then return this fixed value in *val.
8912 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8913 unsigned dim, isl_int *val)
8915 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8918 /* Check if input variable in has fixed value and if so and if val is not NULL,
8919 * then return this fixed value in *val.
8921 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8922 unsigned in, isl_int *val)
8924 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8927 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8928 * and if val is not NULL, then return this lower bound in *val.
8930 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8931 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8933 int i, i_eq = -1, i_ineq = -1;
8934 isl_int *c;
8935 unsigned total;
8936 unsigned nparam;
8938 if (!bset)
8939 return -1;
8940 total = isl_basic_set_total_dim(bset);
8941 nparam = isl_basic_set_n_param(bset);
8942 for (i = 0; i < bset->n_eq; ++i) {
8943 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8944 continue;
8945 if (i_eq != -1)
8946 return 0;
8947 i_eq = i;
8949 for (i = 0; i < bset->n_ineq; ++i) {
8950 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8951 continue;
8952 if (i_eq != -1 || i_ineq != -1)
8953 return 0;
8954 i_ineq = i;
8956 if (i_eq == -1 && i_ineq == -1)
8957 return 0;
8958 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8959 /* The coefficient should always be one due to normalization. */
8960 if (!isl_int_is_one(c[1+nparam+dim]))
8961 return 0;
8962 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8963 return 0;
8964 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8965 total - nparam - dim - 1) != -1)
8966 return 0;
8967 if (val)
8968 isl_int_neg(*val, c[0]);
8969 return 1;
8972 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8973 unsigned dim, isl_int *val)
8975 int i;
8976 isl_int v;
8977 isl_int tmp;
8978 int fixed;
8980 if (!set)
8981 return -1;
8982 if (set->n == 0)
8983 return 0;
8984 if (set->n == 1)
8985 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8986 dim, val);
8987 isl_int_init(v);
8988 isl_int_init(tmp);
8989 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8990 dim, &v);
8991 for (i = 1; fixed == 1 && i < set->n; ++i) {
8992 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8993 dim, &tmp);
8994 if (fixed == 1 && isl_int_ne(tmp, v))
8995 fixed = 0;
8997 if (val)
8998 isl_int_set(*val, v);
8999 isl_int_clear(tmp);
9000 isl_int_clear(v);
9001 return fixed;
9004 /* Return -1 if the constraint "c1" should be sorted before "c2"
9005 * and 1 if it should be sorted after "c2".
9006 * Return 0 if the two constraints are the same (up to the constant term).
9008 * In particular, if a constraint involves later variables than another
9009 * then it is sorted after this other constraint.
9010 * uset_gist depends on constraints without existentially quantified
9011 * variables sorting first.
9013 * For constraints that have the same latest variable, those
9014 * with the same coefficient for this latest variable (first in absolute value
9015 * and then in actual value) are grouped together.
9016 * This is useful for detecting pairs of constraints that can
9017 * be chained in their printed representation.
9019 * Finally, within a group, constraints are sorted according to
9020 * their coefficients (excluding the constant term).
9022 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9024 isl_int **c1 = (isl_int **) p1;
9025 isl_int **c2 = (isl_int **) p2;
9026 int l1, l2;
9027 unsigned size = *(unsigned *) arg;
9028 int cmp;
9030 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9031 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9033 if (l1 != l2)
9034 return l1 - l2;
9036 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9037 if (cmp != 0)
9038 return cmp;
9039 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9040 if (cmp != 0)
9041 return -cmp;
9043 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9046 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9047 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9048 * and 0 if the two constraints are the same (up to the constant term).
9050 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9051 isl_int *c1, isl_int *c2)
9053 unsigned total;
9055 if (!bmap)
9056 return -2;
9057 total = isl_basic_map_total_dim(bmap);
9058 return sort_constraint_cmp(&c1, &c2, &total);
9061 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9062 __isl_take isl_basic_map *bmap)
9064 unsigned total;
9066 if (!bmap)
9067 return NULL;
9068 if (bmap->n_ineq == 0)
9069 return bmap;
9070 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9071 return bmap;
9072 total = isl_basic_map_total_dim(bmap);
9073 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9074 &sort_constraint_cmp, &total) < 0)
9075 return isl_basic_map_free(bmap);
9076 return bmap;
9079 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9080 __isl_take isl_basic_set *bset)
9082 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9083 (struct isl_basic_map *)bset);
9086 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9088 if (!bmap)
9089 return NULL;
9090 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9091 return bmap;
9092 bmap = isl_basic_map_remove_redundancies(bmap);
9093 bmap = isl_basic_map_sort_constraints(bmap);
9094 if (bmap)
9095 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9096 return bmap;
9099 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9101 return (struct isl_basic_set *)isl_basic_map_normalize(
9102 (struct isl_basic_map *)bset);
9105 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9106 const __isl_keep isl_basic_map *bmap2)
9108 int i, cmp;
9109 unsigned total;
9111 if (!bmap1 || !bmap2)
9112 return -1;
9114 if (bmap1 == bmap2)
9115 return 0;
9116 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9117 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9118 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9119 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9120 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9121 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9122 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9123 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9124 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9125 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9126 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9127 return 0;
9128 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9129 return 1;
9130 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9131 return -1;
9132 if (bmap1->n_eq != bmap2->n_eq)
9133 return bmap1->n_eq - bmap2->n_eq;
9134 if (bmap1->n_ineq != bmap2->n_ineq)
9135 return bmap1->n_ineq - bmap2->n_ineq;
9136 if (bmap1->n_div != bmap2->n_div)
9137 return bmap1->n_div - bmap2->n_div;
9138 total = isl_basic_map_total_dim(bmap1);
9139 for (i = 0; i < bmap1->n_eq; ++i) {
9140 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9141 if (cmp)
9142 return cmp;
9144 for (i = 0; i < bmap1->n_ineq; ++i) {
9145 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9146 if (cmp)
9147 return cmp;
9149 for (i = 0; i < bmap1->n_div; ++i) {
9150 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9151 if (cmp)
9152 return cmp;
9154 return 0;
9157 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9158 const __isl_keep isl_basic_set *bset2)
9160 return isl_basic_map_plain_cmp(bset1, bset2);
9163 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9165 int i, cmp;
9167 if (set1 == set2)
9168 return 0;
9169 if (set1->n != set2->n)
9170 return set1->n - set2->n;
9172 for (i = 0; i < set1->n; ++i) {
9173 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9174 if (cmp)
9175 return cmp;
9178 return 0;
9181 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9182 __isl_keep isl_basic_map *bmap2)
9184 if (!bmap1 || !bmap2)
9185 return isl_bool_error;
9186 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9189 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9190 __isl_keep isl_basic_set *bset2)
9192 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9193 (isl_basic_map *)bset2);
9196 static int qsort_bmap_cmp(const void *p1, const void *p2)
9198 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9199 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9201 return isl_basic_map_plain_cmp(bmap1, bmap2);
9204 /* Sort the basic maps of "map" and remove duplicate basic maps.
9206 * While removing basic maps, we make sure that the basic maps remain
9207 * sorted because isl_map_normalize expects the basic maps of the result
9208 * to be sorted.
9210 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9212 int i, j;
9214 map = isl_map_remove_empty_parts(map);
9215 if (!map)
9216 return NULL;
9217 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9218 for (i = map->n - 1; i >= 1; --i) {
9219 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9220 continue;
9221 isl_basic_map_free(map->p[i-1]);
9222 for (j = i; j < map->n; ++j)
9223 map->p[j - 1] = map->p[j];
9224 map->n--;
9227 return map;
9230 /* Remove obvious duplicates among the basic maps of "map".
9232 * Unlike isl_map_normalize, this function does not remove redundant
9233 * constraints and only removes duplicates that have exactly the same
9234 * constraints in the input. It does sort the constraints and
9235 * the basic maps to ease the detection of duplicates.
9237 * If "map" has already been normalized or if the basic maps are
9238 * disjoint, then there can be no duplicates.
9240 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9242 int i;
9243 isl_basic_map *bmap;
9245 if (!map)
9246 return NULL;
9247 if (map->n <= 1)
9248 return map;
9249 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9250 return map;
9251 for (i = 0; i < map->n; ++i) {
9252 bmap = isl_basic_map_copy(map->p[i]);
9253 bmap = isl_basic_map_sort_constraints(bmap);
9254 if (!bmap)
9255 return isl_map_free(map);
9256 isl_basic_map_free(map->p[i]);
9257 map->p[i] = bmap;
9260 map = sort_and_remove_duplicates(map);
9261 return map;
9264 /* We normalize in place, but if anything goes wrong we need
9265 * to return NULL, so we need to make sure we don't change the
9266 * meaning of any possible other copies of map.
9268 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9270 int i;
9271 struct isl_basic_map *bmap;
9273 if (!map)
9274 return NULL;
9275 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9276 return map;
9277 for (i = 0; i < map->n; ++i) {
9278 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9279 if (!bmap)
9280 goto error;
9281 isl_basic_map_free(map->p[i]);
9282 map->p[i] = bmap;
9285 map = sort_and_remove_duplicates(map);
9286 if (map)
9287 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9288 return map;
9289 error:
9290 isl_map_free(map);
9291 return NULL;
9294 struct isl_set *isl_set_normalize(struct isl_set *set)
9296 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9299 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9300 __isl_keep isl_map *map2)
9302 int i;
9303 isl_bool equal;
9305 if (!map1 || !map2)
9306 return isl_bool_error;
9308 if (map1 == map2)
9309 return isl_bool_true;
9310 if (!isl_space_is_equal(map1->dim, map2->dim))
9311 return isl_bool_false;
9313 map1 = isl_map_copy(map1);
9314 map2 = isl_map_copy(map2);
9315 map1 = isl_map_normalize(map1);
9316 map2 = isl_map_normalize(map2);
9317 if (!map1 || !map2)
9318 goto error;
9319 equal = map1->n == map2->n;
9320 for (i = 0; equal && i < map1->n; ++i) {
9321 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9322 if (equal < 0)
9323 goto error;
9325 isl_map_free(map1);
9326 isl_map_free(map2);
9327 return equal;
9328 error:
9329 isl_map_free(map1);
9330 isl_map_free(map2);
9331 return isl_bool_error;
9334 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9335 __isl_keep isl_set *set2)
9337 return isl_map_plain_is_equal((struct isl_map *)set1,
9338 (struct isl_map *)set2);
9341 /* Return an interval that ranges from min to max (inclusive)
9343 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9344 isl_int min, isl_int max)
9346 int k;
9347 struct isl_basic_set *bset = NULL;
9349 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9350 if (!bset)
9351 goto error;
9353 k = isl_basic_set_alloc_inequality(bset);
9354 if (k < 0)
9355 goto error;
9356 isl_int_set_si(bset->ineq[k][1], 1);
9357 isl_int_neg(bset->ineq[k][0], min);
9359 k = isl_basic_set_alloc_inequality(bset);
9360 if (k < 0)
9361 goto error;
9362 isl_int_set_si(bset->ineq[k][1], -1);
9363 isl_int_set(bset->ineq[k][0], max);
9365 return bset;
9366 error:
9367 isl_basic_set_free(bset);
9368 return NULL;
9371 /* Return the basic maps in "map" as a list.
9373 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9374 __isl_keep isl_map *map)
9376 int i;
9377 isl_ctx *ctx;
9378 isl_basic_map_list *list;
9380 if (!map)
9381 return NULL;
9382 ctx = isl_map_get_ctx(map);
9383 list = isl_basic_map_list_alloc(ctx, map->n);
9385 for (i = 0; i < map->n; ++i) {
9386 isl_basic_map *bmap;
9388 bmap = isl_basic_map_copy(map->p[i]);
9389 list = isl_basic_map_list_add(list, bmap);
9392 return list;
9395 /* Return the intersection of the elements in the non-empty list "list".
9396 * All elements are assumed to live in the same space.
9398 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9399 __isl_take isl_basic_map_list *list)
9401 int i, n;
9402 isl_basic_map *bmap;
9404 if (!list)
9405 return NULL;
9406 n = isl_basic_map_list_n_basic_map(list);
9407 if (n < 1)
9408 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9409 "expecting non-empty list", goto error);
9411 bmap = isl_basic_map_list_get_basic_map(list, 0);
9412 for (i = 1; i < n; ++i) {
9413 isl_basic_map *bmap_i;
9415 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9416 bmap = isl_basic_map_intersect(bmap, bmap_i);
9419 isl_basic_map_list_free(list);
9420 return bmap;
9421 error:
9422 isl_basic_map_list_free(list);
9423 return NULL;
9426 /* Return the intersection of the elements in the non-empty list "list".
9427 * All elements are assumed to live in the same space.
9429 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9430 __isl_take isl_basic_set_list *list)
9432 return isl_basic_map_list_intersect(list);
9435 /* Return the union of the elements in the non-empty list "list".
9436 * All elements are assumed to live in the same space.
9438 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9440 int i, n;
9441 isl_set *set;
9443 if (!list)
9444 return NULL;
9445 n = isl_set_list_n_set(list);
9446 if (n < 1)
9447 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9448 "expecting non-empty list", goto error);
9450 set = isl_set_list_get_set(list, 0);
9451 for (i = 1; i < n; ++i) {
9452 isl_set *set_i;
9454 set_i = isl_set_list_get_set(list, i);
9455 set = isl_set_union(set, set_i);
9458 isl_set_list_free(list);
9459 return set;
9460 error:
9461 isl_set_list_free(list);
9462 return NULL;
9465 /* Return the Cartesian product of the basic sets in list (in the given order).
9467 __isl_give isl_basic_set *isl_basic_set_list_product(
9468 __isl_take struct isl_basic_set_list *list)
9470 int i;
9471 unsigned dim;
9472 unsigned nparam;
9473 unsigned extra;
9474 unsigned n_eq;
9475 unsigned n_ineq;
9476 struct isl_basic_set *product = NULL;
9478 if (!list)
9479 goto error;
9480 isl_assert(list->ctx, list->n > 0, goto error);
9481 isl_assert(list->ctx, list->p[0], goto error);
9482 nparam = isl_basic_set_n_param(list->p[0]);
9483 dim = isl_basic_set_n_dim(list->p[0]);
9484 extra = list->p[0]->n_div;
9485 n_eq = list->p[0]->n_eq;
9486 n_ineq = list->p[0]->n_ineq;
9487 for (i = 1; i < list->n; ++i) {
9488 isl_assert(list->ctx, list->p[i], goto error);
9489 isl_assert(list->ctx,
9490 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9491 dim += isl_basic_set_n_dim(list->p[i]);
9492 extra += list->p[i]->n_div;
9493 n_eq += list->p[i]->n_eq;
9494 n_ineq += list->p[i]->n_ineq;
9496 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9497 n_eq, n_ineq);
9498 if (!product)
9499 goto error;
9500 dim = 0;
9501 for (i = 0; i < list->n; ++i) {
9502 isl_basic_set_add_constraints(product,
9503 isl_basic_set_copy(list->p[i]), dim);
9504 dim += isl_basic_set_n_dim(list->p[i]);
9506 isl_basic_set_list_free(list);
9507 return product;
9508 error:
9509 isl_basic_set_free(product);
9510 isl_basic_set_list_free(list);
9511 return NULL;
9514 struct isl_basic_map *isl_basic_map_product(
9515 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9517 isl_space *dim_result = NULL;
9518 struct isl_basic_map *bmap;
9519 unsigned in1, in2, out1, out2, nparam, total, pos;
9520 struct isl_dim_map *dim_map1, *dim_map2;
9522 if (!bmap1 || !bmap2)
9523 goto error;
9525 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9526 bmap2->dim, isl_dim_param), goto error);
9527 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9528 isl_space_copy(bmap2->dim));
9530 in1 = isl_basic_map_n_in(bmap1);
9531 in2 = isl_basic_map_n_in(bmap2);
9532 out1 = isl_basic_map_n_out(bmap1);
9533 out2 = isl_basic_map_n_out(bmap2);
9534 nparam = isl_basic_map_n_param(bmap1);
9536 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9537 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9538 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9539 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9540 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9541 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9542 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9543 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9544 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9545 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9546 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9548 bmap = isl_basic_map_alloc_space(dim_result,
9549 bmap1->n_div + bmap2->n_div,
9550 bmap1->n_eq + bmap2->n_eq,
9551 bmap1->n_ineq + bmap2->n_ineq);
9552 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9553 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9554 bmap = isl_basic_map_simplify(bmap);
9555 return isl_basic_map_finalize(bmap);
9556 error:
9557 isl_basic_map_free(bmap1);
9558 isl_basic_map_free(bmap2);
9559 return NULL;
9562 __isl_give isl_basic_map *isl_basic_map_flat_product(
9563 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9565 isl_basic_map *prod;
9567 prod = isl_basic_map_product(bmap1, bmap2);
9568 prod = isl_basic_map_flatten(prod);
9569 return prod;
9572 __isl_give isl_basic_set *isl_basic_set_flat_product(
9573 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9575 return isl_basic_map_flat_range_product(bset1, bset2);
9578 __isl_give isl_basic_map *isl_basic_map_domain_product(
9579 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9581 isl_space *space_result = NULL;
9582 isl_basic_map *bmap;
9583 unsigned in1, in2, out, nparam, total, pos;
9584 struct isl_dim_map *dim_map1, *dim_map2;
9586 if (!bmap1 || !bmap2)
9587 goto error;
9589 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9590 isl_space_copy(bmap2->dim));
9592 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9593 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9594 out = isl_basic_map_dim(bmap1, isl_dim_out);
9595 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9597 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9598 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9599 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9600 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9601 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9602 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9603 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9604 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9605 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9606 isl_dim_map_div(dim_map1, bmap1, pos += out);
9607 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9609 bmap = isl_basic_map_alloc_space(space_result,
9610 bmap1->n_div + bmap2->n_div,
9611 bmap1->n_eq + bmap2->n_eq,
9612 bmap1->n_ineq + bmap2->n_ineq);
9613 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9614 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9615 bmap = isl_basic_map_simplify(bmap);
9616 return isl_basic_map_finalize(bmap);
9617 error:
9618 isl_basic_map_free(bmap1);
9619 isl_basic_map_free(bmap2);
9620 return NULL;
9623 __isl_give isl_basic_map *isl_basic_map_range_product(
9624 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9626 isl_space *dim_result = NULL;
9627 isl_basic_map *bmap;
9628 unsigned in, out1, out2, nparam, total, pos;
9629 struct isl_dim_map *dim_map1, *dim_map2;
9631 if (!bmap1 || !bmap2)
9632 goto error;
9634 if (!isl_space_match(bmap1->dim, isl_dim_param,
9635 bmap2->dim, isl_dim_param))
9636 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9637 "parameters don't match", goto error);
9639 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9640 isl_space_copy(bmap2->dim));
9642 in = isl_basic_map_dim(bmap1, isl_dim_in);
9643 out1 = isl_basic_map_n_out(bmap1);
9644 out2 = isl_basic_map_n_out(bmap2);
9645 nparam = isl_basic_map_n_param(bmap1);
9647 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9648 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9649 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9650 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9651 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9652 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9653 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9654 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9655 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9656 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9657 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9659 bmap = isl_basic_map_alloc_space(dim_result,
9660 bmap1->n_div + bmap2->n_div,
9661 bmap1->n_eq + bmap2->n_eq,
9662 bmap1->n_ineq + bmap2->n_ineq);
9663 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9664 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9665 bmap = isl_basic_map_simplify(bmap);
9666 return isl_basic_map_finalize(bmap);
9667 error:
9668 isl_basic_map_free(bmap1);
9669 isl_basic_map_free(bmap2);
9670 return NULL;
9673 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9674 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9676 isl_basic_map *prod;
9678 prod = isl_basic_map_range_product(bmap1, bmap2);
9679 prod = isl_basic_map_flatten_range(prod);
9680 return prod;
9683 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9684 * and collect the results.
9685 * The result live in the space obtained by calling "space_product"
9686 * on the spaces of "map1" and "map2".
9687 * If "remove_duplicates" is set then the result may contain duplicates
9688 * (even if the inputs do not) and so we try and remove the obvious
9689 * duplicates.
9691 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9692 __isl_take isl_map *map2,
9693 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9694 __isl_take isl_space *right),
9695 __isl_give isl_basic_map *(*basic_map_product)(
9696 __isl_take isl_basic_map *left,
9697 __isl_take isl_basic_map *right),
9698 int remove_duplicates)
9700 unsigned flags = 0;
9701 struct isl_map *result;
9702 int i, j;
9704 if (!map1 || !map2)
9705 goto error;
9707 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9708 map2->dim, isl_dim_param), goto error);
9710 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9711 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9712 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9714 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9715 isl_space_copy(map2->dim)),
9716 map1->n * map2->n, flags);
9717 if (!result)
9718 goto error;
9719 for (i = 0; i < map1->n; ++i)
9720 for (j = 0; j < map2->n; ++j) {
9721 struct isl_basic_map *part;
9722 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9723 isl_basic_map_copy(map2->p[j]));
9724 if (isl_basic_map_is_empty(part))
9725 isl_basic_map_free(part);
9726 else
9727 result = isl_map_add_basic_map(result, part);
9728 if (!result)
9729 goto error;
9731 if (remove_duplicates)
9732 result = isl_map_remove_obvious_duplicates(result);
9733 isl_map_free(map1);
9734 isl_map_free(map2);
9735 return result;
9736 error:
9737 isl_map_free(map1);
9738 isl_map_free(map2);
9739 return NULL;
9742 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9744 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9745 __isl_take isl_map *map2)
9747 return map_product(map1, map2, &isl_space_product,
9748 &isl_basic_map_product, 0);
9751 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9752 __isl_take isl_map *map2)
9754 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9757 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9759 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9760 __isl_take isl_map *map2)
9762 isl_map *prod;
9764 prod = isl_map_product(map1, map2);
9765 prod = isl_map_flatten(prod);
9766 return prod;
9769 /* Given two set A and B, construct its Cartesian product A x B.
9771 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9773 return isl_map_range_product(set1, set2);
9776 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9777 __isl_take isl_set *set2)
9779 return isl_map_flat_range_product(set1, set2);
9782 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9784 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9785 __isl_take isl_map *map2)
9787 return map_product(map1, map2, &isl_space_domain_product,
9788 &isl_basic_map_domain_product, 1);
9791 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9793 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9794 __isl_take isl_map *map2)
9796 return map_product(map1, map2, &isl_space_range_product,
9797 &isl_basic_map_range_product, 1);
9800 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9801 __isl_take isl_map *map2)
9803 return isl_map_align_params_map_map_and(map1, map2,
9804 &map_domain_product_aligned);
9807 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9808 __isl_take isl_map *map2)
9810 return isl_map_align_params_map_map_and(map1, map2,
9811 &map_range_product_aligned);
9814 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9816 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9818 isl_space *space;
9819 int total1, keep1, total2, keep2;
9821 if (!map)
9822 return NULL;
9823 if (!isl_space_domain_is_wrapping(map->dim) ||
9824 !isl_space_range_is_wrapping(map->dim))
9825 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9826 "not a product", return isl_map_free(map));
9828 space = isl_map_get_space(map);
9829 total1 = isl_space_dim(space, isl_dim_in);
9830 total2 = isl_space_dim(space, isl_dim_out);
9831 space = isl_space_factor_domain(space);
9832 keep1 = isl_space_dim(space, isl_dim_in);
9833 keep2 = isl_space_dim(space, isl_dim_out);
9834 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9835 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9836 map = isl_map_reset_space(map, space);
9838 return map;
9841 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9843 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9845 isl_space *space;
9846 int total1, keep1, total2, keep2;
9848 if (!map)
9849 return NULL;
9850 if (!isl_space_domain_is_wrapping(map->dim) ||
9851 !isl_space_range_is_wrapping(map->dim))
9852 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9853 "not a product", return isl_map_free(map));
9855 space = isl_map_get_space(map);
9856 total1 = isl_space_dim(space, isl_dim_in);
9857 total2 = isl_space_dim(space, isl_dim_out);
9858 space = isl_space_factor_range(space);
9859 keep1 = isl_space_dim(space, isl_dim_in);
9860 keep2 = isl_space_dim(space, isl_dim_out);
9861 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9862 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9863 map = isl_map_reset_space(map, space);
9865 return map;
9868 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9870 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9872 isl_space *space;
9873 int total, keep;
9875 if (!map)
9876 return NULL;
9877 if (!isl_space_domain_is_wrapping(map->dim))
9878 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9879 "domain is not a product", return isl_map_free(map));
9881 space = isl_map_get_space(map);
9882 total = isl_space_dim(space, isl_dim_in);
9883 space = isl_space_domain_factor_domain(space);
9884 keep = isl_space_dim(space, isl_dim_in);
9885 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9886 map = isl_map_reset_space(map, space);
9888 return map;
9891 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9893 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9895 isl_space *space;
9896 int total, keep;
9898 if (!map)
9899 return NULL;
9900 if (!isl_space_domain_is_wrapping(map->dim))
9901 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9902 "domain is not a product", return isl_map_free(map));
9904 space = isl_map_get_space(map);
9905 total = isl_space_dim(space, isl_dim_in);
9906 space = isl_space_domain_factor_range(space);
9907 keep = isl_space_dim(space, isl_dim_in);
9908 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9909 map = isl_map_reset_space(map, space);
9911 return map;
9914 /* Given a map A -> [B -> C], extract the map A -> B.
9916 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9918 isl_space *space;
9919 int total, keep;
9921 if (!map)
9922 return NULL;
9923 if (!isl_space_range_is_wrapping(map->dim))
9924 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9925 "range is not a product", return isl_map_free(map));
9927 space = isl_map_get_space(map);
9928 total = isl_space_dim(space, isl_dim_out);
9929 space = isl_space_range_factor_domain(space);
9930 keep = isl_space_dim(space, isl_dim_out);
9931 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9932 map = isl_map_reset_space(map, space);
9934 return map;
9937 /* Given a map A -> [B -> C], extract the map A -> C.
9939 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9941 isl_space *space;
9942 int total, keep;
9944 if (!map)
9945 return NULL;
9946 if (!isl_space_range_is_wrapping(map->dim))
9947 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9948 "range is not a product", return isl_map_free(map));
9950 space = isl_map_get_space(map);
9951 total = isl_space_dim(space, isl_dim_out);
9952 space = isl_space_range_factor_range(space);
9953 keep = isl_space_dim(space, isl_dim_out);
9954 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9955 map = isl_map_reset_space(map, space);
9957 return map;
9960 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9962 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9963 __isl_take isl_map *map2)
9965 isl_map *prod;
9967 prod = isl_map_domain_product(map1, map2);
9968 prod = isl_map_flatten_domain(prod);
9969 return prod;
9972 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9974 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9975 __isl_take isl_map *map2)
9977 isl_map *prod;
9979 prod = isl_map_range_product(map1, map2);
9980 prod = isl_map_flatten_range(prod);
9981 return prod;
9984 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9986 int i;
9987 uint32_t hash = isl_hash_init();
9988 unsigned total;
9990 if (!bmap)
9991 return 0;
9992 bmap = isl_basic_map_copy(bmap);
9993 bmap = isl_basic_map_normalize(bmap);
9994 if (!bmap)
9995 return 0;
9996 total = isl_basic_map_total_dim(bmap);
9997 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9998 for (i = 0; i < bmap->n_eq; ++i) {
9999 uint32_t c_hash;
10000 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10001 isl_hash_hash(hash, c_hash);
10003 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10004 for (i = 0; i < bmap->n_ineq; ++i) {
10005 uint32_t c_hash;
10006 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10007 isl_hash_hash(hash, c_hash);
10009 isl_hash_byte(hash, bmap->n_div & 0xFF);
10010 for (i = 0; i < bmap->n_div; ++i) {
10011 uint32_t c_hash;
10012 if (isl_int_is_zero(bmap->div[i][0]))
10013 continue;
10014 isl_hash_byte(hash, i & 0xFF);
10015 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10016 isl_hash_hash(hash, c_hash);
10018 isl_basic_map_free(bmap);
10019 return hash;
10022 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10024 return isl_basic_map_get_hash((isl_basic_map *)bset);
10027 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10029 int i;
10030 uint32_t hash;
10032 if (!map)
10033 return 0;
10034 map = isl_map_copy(map);
10035 map = isl_map_normalize(map);
10036 if (!map)
10037 return 0;
10039 hash = isl_hash_init();
10040 for (i = 0; i < map->n; ++i) {
10041 uint32_t bmap_hash;
10042 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10043 isl_hash_hash(hash, bmap_hash);
10046 isl_map_free(map);
10048 return hash;
10051 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10053 return isl_map_get_hash((isl_map *)set);
10056 /* Check if the value for dimension dim is completely determined
10057 * by the values of the other parameters and variables.
10058 * That is, check if dimension dim is involved in an equality.
10060 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10062 int i;
10063 unsigned nparam;
10065 if (!bset)
10066 return -1;
10067 nparam = isl_basic_set_n_param(bset);
10068 for (i = 0; i < bset->n_eq; ++i)
10069 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10070 return 1;
10071 return 0;
10074 /* Check if the value for dimension dim is completely determined
10075 * by the values of the other parameters and variables.
10076 * That is, check if dimension dim is involved in an equality
10077 * for each of the subsets.
10079 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10081 int i;
10083 if (!set)
10084 return -1;
10085 for (i = 0; i < set->n; ++i) {
10086 int unique;
10087 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10088 if (unique != 1)
10089 return unique;
10091 return 1;
10094 /* Return the number of basic maps in the (current) representation of "map".
10096 int isl_map_n_basic_map(__isl_keep isl_map *map)
10098 return map ? map->n : 0;
10101 int isl_set_n_basic_set(__isl_keep isl_set *set)
10103 return set ? set->n : 0;
10106 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10107 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10109 int i;
10111 if (!map)
10112 return isl_stat_error;
10114 for (i = 0; i < map->n; ++i)
10115 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10116 return isl_stat_error;
10118 return isl_stat_ok;
10121 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10122 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10124 int i;
10126 if (!set)
10127 return isl_stat_error;
10129 for (i = 0; i < set->n; ++i)
10130 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10131 return isl_stat_error;
10133 return isl_stat_ok;
10136 /* Return a list of basic sets, the union of which is equal to "set".
10138 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10139 __isl_keep isl_set *set)
10141 int i;
10142 isl_basic_set_list *list;
10144 if (!set)
10145 return NULL;
10147 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10148 for (i = 0; i < set->n; ++i) {
10149 isl_basic_set *bset;
10151 bset = isl_basic_set_copy(set->p[i]);
10152 list = isl_basic_set_list_add(list, bset);
10155 return list;
10158 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10160 isl_space *dim;
10162 if (!bset)
10163 return NULL;
10165 bset = isl_basic_set_cow(bset);
10166 if (!bset)
10167 return NULL;
10169 dim = isl_basic_set_get_space(bset);
10170 dim = isl_space_lift(dim, bset->n_div);
10171 if (!dim)
10172 goto error;
10173 isl_space_free(bset->dim);
10174 bset->dim = dim;
10175 bset->extra -= bset->n_div;
10176 bset->n_div = 0;
10178 bset = isl_basic_set_finalize(bset);
10180 return bset;
10181 error:
10182 isl_basic_set_free(bset);
10183 return NULL;
10186 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10188 int i;
10189 isl_space *dim;
10190 unsigned n_div;
10192 set = isl_set_align_divs(set);
10194 if (!set)
10195 return NULL;
10197 set = isl_set_cow(set);
10198 if (!set)
10199 return NULL;
10201 n_div = set->p[0]->n_div;
10202 dim = isl_set_get_space(set);
10203 dim = isl_space_lift(dim, n_div);
10204 if (!dim)
10205 goto error;
10206 isl_space_free(set->dim);
10207 set->dim = dim;
10209 for (i = 0; i < set->n; ++i) {
10210 set->p[i] = isl_basic_set_lift(set->p[i]);
10211 if (!set->p[i])
10212 goto error;
10215 return set;
10216 error:
10217 isl_set_free(set);
10218 return NULL;
10221 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10223 isl_space *dim;
10224 struct isl_basic_map *bmap;
10225 unsigned n_set;
10226 unsigned n_div;
10227 unsigned n_param;
10228 unsigned total;
10229 int i, k, l;
10231 set = isl_set_align_divs(set);
10233 if (!set)
10234 return NULL;
10236 dim = isl_set_get_space(set);
10237 if (set->n == 0 || set->p[0]->n_div == 0) {
10238 isl_set_free(set);
10239 return isl_map_identity(isl_space_map_from_set(dim));
10242 n_div = set->p[0]->n_div;
10243 dim = isl_space_map_from_set(dim);
10244 n_param = isl_space_dim(dim, isl_dim_param);
10245 n_set = isl_space_dim(dim, isl_dim_in);
10246 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10247 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10248 for (i = 0; i < n_set; ++i)
10249 bmap = var_equal(bmap, i);
10251 total = n_param + n_set + n_set + n_div;
10252 for (i = 0; i < n_div; ++i) {
10253 k = isl_basic_map_alloc_inequality(bmap);
10254 if (k < 0)
10255 goto error;
10256 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10257 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10258 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10259 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10260 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10261 set->p[0]->div[i][0]);
10263 l = isl_basic_map_alloc_inequality(bmap);
10264 if (l < 0)
10265 goto error;
10266 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10267 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10268 set->p[0]->div[i][0]);
10269 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10272 isl_set_free(set);
10273 bmap = isl_basic_map_simplify(bmap);
10274 bmap = isl_basic_map_finalize(bmap);
10275 return isl_map_from_basic_map(bmap);
10276 error:
10277 isl_set_free(set);
10278 isl_basic_map_free(bmap);
10279 return NULL;
10282 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10284 unsigned dim;
10285 int size = 0;
10287 if (!bset)
10288 return -1;
10290 dim = isl_basic_set_total_dim(bset);
10291 size += bset->n_eq * (1 + dim);
10292 size += bset->n_ineq * (1 + dim);
10293 size += bset->n_div * (2 + dim);
10295 return size;
10298 int isl_set_size(__isl_keep isl_set *set)
10300 int i;
10301 int size = 0;
10303 if (!set)
10304 return -1;
10306 for (i = 0; i < set->n; ++i)
10307 size += isl_basic_set_size(set->p[i]);
10309 return size;
10312 /* Check if there is any lower bound (if lower == 0) and/or upper
10313 * bound (if upper == 0) on the specified dim.
10315 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10316 enum isl_dim_type type, unsigned pos, int lower, int upper)
10318 int i;
10320 if (!bmap)
10321 return isl_bool_error;
10323 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10324 return isl_bool_error);
10326 pos += isl_basic_map_offset(bmap, type);
10328 for (i = 0; i < bmap->n_div; ++i) {
10329 if (isl_int_is_zero(bmap->div[i][0]))
10330 continue;
10331 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10332 return isl_bool_true;
10335 for (i = 0; i < bmap->n_eq; ++i)
10336 if (!isl_int_is_zero(bmap->eq[i][pos]))
10337 return isl_bool_true;
10339 for (i = 0; i < bmap->n_ineq; ++i) {
10340 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10341 if (sgn > 0)
10342 lower = 1;
10343 if (sgn < 0)
10344 upper = 1;
10347 return lower && upper;
10350 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10351 enum isl_dim_type type, unsigned pos)
10353 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10356 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10357 enum isl_dim_type type, unsigned pos)
10359 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10362 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10363 enum isl_dim_type type, unsigned pos)
10365 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10368 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10369 enum isl_dim_type type, unsigned pos)
10371 int i;
10373 if (!map)
10374 return -1;
10376 for (i = 0; i < map->n; ++i) {
10377 int bounded;
10378 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10379 if (bounded < 0 || !bounded)
10380 return bounded;
10383 return 1;
10386 /* Return 1 if the specified dim is involved in both an upper bound
10387 * and a lower bound.
10389 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10390 enum isl_dim_type type, unsigned pos)
10392 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10395 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10397 static isl_bool has_any_bound(__isl_keep isl_map *map,
10398 enum isl_dim_type type, unsigned pos,
10399 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10400 enum isl_dim_type type, unsigned pos))
10402 int i;
10404 if (!map)
10405 return isl_bool_error;
10407 for (i = 0; i < map->n; ++i) {
10408 isl_bool bounded;
10409 bounded = fn(map->p[i], type, pos);
10410 if (bounded < 0 || bounded)
10411 return bounded;
10414 return isl_bool_false;
10417 /* Return 1 if the specified dim is involved in any lower bound.
10419 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10420 enum isl_dim_type type, unsigned pos)
10422 return has_any_bound(set, type, pos,
10423 &isl_basic_map_dim_has_lower_bound);
10426 /* Return 1 if the specified dim is involved in any upper bound.
10428 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10429 enum isl_dim_type type, unsigned pos)
10431 return has_any_bound(set, type, pos,
10432 &isl_basic_map_dim_has_upper_bound);
10435 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10437 static isl_bool has_bound(__isl_keep isl_map *map,
10438 enum isl_dim_type type, unsigned pos,
10439 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10440 enum isl_dim_type type, unsigned pos))
10442 int i;
10444 if (!map)
10445 return isl_bool_error;
10447 for (i = 0; i < map->n; ++i) {
10448 isl_bool bounded;
10449 bounded = fn(map->p[i], type, pos);
10450 if (bounded < 0 || !bounded)
10451 return bounded;
10454 return isl_bool_true;
10457 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10459 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10460 enum isl_dim_type type, unsigned pos)
10462 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10465 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10467 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10468 enum isl_dim_type type, unsigned pos)
10470 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10473 /* For each of the "n" variables starting at "first", determine
10474 * the sign of the variable and put the results in the first "n"
10475 * elements of the array "signs".
10476 * Sign
10477 * 1 means that the variable is non-negative
10478 * -1 means that the variable is non-positive
10479 * 0 means the variable attains both positive and negative values.
10481 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10482 unsigned first, unsigned n, int *signs)
10484 isl_vec *bound = NULL;
10485 struct isl_tab *tab = NULL;
10486 struct isl_tab_undo *snap;
10487 int i;
10489 if (!bset || !signs)
10490 return -1;
10492 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10493 tab = isl_tab_from_basic_set(bset, 0);
10494 if (!bound || !tab)
10495 goto error;
10497 isl_seq_clr(bound->el, bound->size);
10498 isl_int_set_si(bound->el[0], -1);
10500 snap = isl_tab_snap(tab);
10501 for (i = 0; i < n; ++i) {
10502 int empty;
10504 isl_int_set_si(bound->el[1 + first + i], -1);
10505 if (isl_tab_add_ineq(tab, bound->el) < 0)
10506 goto error;
10507 empty = tab->empty;
10508 isl_int_set_si(bound->el[1 + first + i], 0);
10509 if (isl_tab_rollback(tab, snap) < 0)
10510 goto error;
10512 if (empty) {
10513 signs[i] = 1;
10514 continue;
10517 isl_int_set_si(bound->el[1 + first + i], 1);
10518 if (isl_tab_add_ineq(tab, bound->el) < 0)
10519 goto error;
10520 empty = tab->empty;
10521 isl_int_set_si(bound->el[1 + first + i], 0);
10522 if (isl_tab_rollback(tab, snap) < 0)
10523 goto error;
10525 signs[i] = empty ? -1 : 0;
10528 isl_tab_free(tab);
10529 isl_vec_free(bound);
10530 return 0;
10531 error:
10532 isl_tab_free(tab);
10533 isl_vec_free(bound);
10534 return -1;
10537 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10538 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10540 if (!bset || !signs)
10541 return -1;
10542 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10543 return -1);
10545 first += pos(bset->dim, type) - 1;
10546 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10549 /* Is it possible for the integer division "div" to depend (possibly
10550 * indirectly) on any output dimensions?
10552 * If the div is undefined, then we conservatively assume that it
10553 * may depend on them.
10554 * Otherwise, we check if it actually depends on them or on any integer
10555 * divisions that may depend on them.
10557 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10559 int i;
10560 unsigned n_out, o_out;
10561 unsigned n_div, o_div;
10563 if (isl_int_is_zero(bmap->div[div][0]))
10564 return 1;
10566 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10567 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10569 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10570 return 1;
10572 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10573 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10575 for (i = 0; i < n_div; ++i) {
10576 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10577 continue;
10578 if (div_may_involve_output(bmap, i))
10579 return 1;
10582 return 0;
10585 /* Return the first integer division of "bmap" in the range
10586 * [first, first + n[ that may depend on any output dimensions and
10587 * that has a non-zero coefficient in "c" (where the first coefficient
10588 * in "c" corresponds to integer division "first").
10590 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10591 isl_int *c, int first, int n)
10593 int k;
10595 if (!bmap)
10596 return -1;
10598 for (k = first; k < first + n; ++k) {
10599 if (isl_int_is_zero(c[k]))
10600 continue;
10601 if (div_may_involve_output(bmap, k))
10602 return k;
10605 return first + n;
10608 /* Look for a pair of inequality constraints in "bmap" of the form
10610 * -l + i >= 0 or i >= l
10611 * and
10612 * n + l - i >= 0 or i <= l + n
10614 * with n < "m" and i the output dimension at position "pos".
10615 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10616 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10617 * and earlier output dimensions, as well as integer divisions that do
10618 * not involve any of the output dimensions.
10620 * Return the index of the first inequality constraint or bmap->n_ineq
10621 * if no such pair can be found.
10623 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10624 int pos, isl_int m)
10626 int i, j;
10627 isl_ctx *ctx;
10628 unsigned total;
10629 unsigned n_div, o_div;
10630 unsigned n_out, o_out;
10631 int less;
10633 if (!bmap)
10634 return -1;
10636 ctx = isl_basic_map_get_ctx(bmap);
10637 total = isl_basic_map_total_dim(bmap);
10638 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10639 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10640 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10641 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10642 for (i = 0; i < bmap->n_ineq; ++i) {
10643 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10644 continue;
10645 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10646 n_out - (pos + 1)) != -1)
10647 continue;
10648 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10649 0, n_div) < n_div)
10650 continue;
10651 for (j = i + 1; j < bmap->n_ineq; ++j) {
10652 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10653 ctx->one))
10654 continue;
10655 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10656 bmap->ineq[j] + 1, total))
10657 continue;
10658 break;
10660 if (j >= bmap->n_ineq)
10661 continue;
10662 isl_int_add(bmap->ineq[i][0],
10663 bmap->ineq[i][0], bmap->ineq[j][0]);
10664 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10665 isl_int_sub(bmap->ineq[i][0],
10666 bmap->ineq[i][0], bmap->ineq[j][0]);
10667 if (!less)
10668 continue;
10669 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10670 return i;
10671 else
10672 return j;
10675 return bmap->n_ineq;
10678 /* Return the index of the equality of "bmap" that defines
10679 * the output dimension "pos" in terms of earlier dimensions.
10680 * The equality may also involve integer divisions, as long
10681 * as those integer divisions are defined in terms of
10682 * parameters or input dimensions.
10683 * In this case, *div is set to the number of integer divisions and
10684 * *ineq is set to the number of inequality constraints (provided
10685 * div and ineq are not NULL).
10687 * The equality may also involve a single integer division involving
10688 * the output dimensions (typically only output dimension "pos") as
10689 * long as the coefficient of output dimension "pos" is 1 or -1 and
10690 * there is a pair of constraints i >= l and i <= l + n, with i referring
10691 * to output dimension "pos", l an expression involving only earlier
10692 * dimensions and n smaller than the coefficient of the integer division
10693 * in the equality. In this case, the output dimension can be defined
10694 * in terms of a modulo expression that does not involve the integer division.
10695 * *div is then set to this single integer division and
10696 * *ineq is set to the index of constraint i >= l.
10698 * Return bmap->n_eq if there is no such equality.
10699 * Return -1 on error.
10701 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10702 int pos, int *div, int *ineq)
10704 int j, k, l;
10705 unsigned n_out, o_out;
10706 unsigned n_div, o_div;
10708 if (!bmap)
10709 return -1;
10711 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10712 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10713 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10714 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10716 if (ineq)
10717 *ineq = bmap->n_ineq;
10718 if (div)
10719 *div = n_div;
10720 for (j = 0; j < bmap->n_eq; ++j) {
10721 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10722 continue;
10723 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10724 n_out - (pos + 1)) != -1)
10725 continue;
10726 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10727 0, n_div);
10728 if (k >= n_div)
10729 return j;
10730 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10731 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10732 continue;
10733 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10734 k + 1, n_div - (k+1)) < n_div)
10735 continue;
10736 l = find_modulo_constraint_pair(bmap, pos,
10737 bmap->eq[j][o_div + k]);
10738 if (l < 0)
10739 return -1;
10740 if (l >= bmap->n_ineq)
10741 continue;
10742 if (div)
10743 *div = k;
10744 if (ineq)
10745 *ineq = l;
10746 return j;
10749 return bmap->n_eq;
10752 /* Check if the given basic map is obviously single-valued.
10753 * In particular, for each output dimension, check that there is
10754 * an equality that defines the output dimension in terms of
10755 * earlier dimensions.
10757 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10759 int i;
10760 unsigned n_out;
10762 if (!bmap)
10763 return isl_bool_error;
10765 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10767 for (i = 0; i < n_out; ++i) {
10768 int eq;
10770 eq = isl_basic_map_output_defining_equality(bmap, i,
10771 NULL, NULL);
10772 if (eq < 0)
10773 return isl_bool_error;
10774 if (eq >= bmap->n_eq)
10775 return isl_bool_false;
10778 return isl_bool_true;
10781 /* Check if the given basic map is single-valued.
10782 * We simply compute
10784 * M \circ M^-1
10786 * and check if the result is a subset of the identity mapping.
10788 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10790 isl_space *space;
10791 isl_basic_map *test;
10792 isl_basic_map *id;
10793 isl_bool sv;
10795 sv = isl_basic_map_plain_is_single_valued(bmap);
10796 if (sv < 0 || sv)
10797 return sv;
10799 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10800 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10802 space = isl_basic_map_get_space(bmap);
10803 space = isl_space_map_from_set(isl_space_range(space));
10804 id = isl_basic_map_identity(space);
10806 sv = isl_basic_map_is_subset(test, id);
10808 isl_basic_map_free(test);
10809 isl_basic_map_free(id);
10811 return sv;
10814 /* Check if the given map is obviously single-valued.
10816 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10818 if (!map)
10819 return isl_bool_error;
10820 if (map->n == 0)
10821 return isl_bool_true;
10822 if (map->n >= 2)
10823 return isl_bool_false;
10825 return isl_basic_map_plain_is_single_valued(map->p[0]);
10828 /* Check if the given map is single-valued.
10829 * We simply compute
10831 * M \circ M^-1
10833 * and check if the result is a subset of the identity mapping.
10835 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10837 isl_space *dim;
10838 isl_map *test;
10839 isl_map *id;
10840 isl_bool sv;
10842 sv = isl_map_plain_is_single_valued(map);
10843 if (sv < 0 || sv)
10844 return sv;
10846 test = isl_map_reverse(isl_map_copy(map));
10847 test = isl_map_apply_range(test, isl_map_copy(map));
10849 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10850 id = isl_map_identity(dim);
10852 sv = isl_map_is_subset(test, id);
10854 isl_map_free(test);
10855 isl_map_free(id);
10857 return sv;
10860 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10862 isl_bool in;
10864 map = isl_map_copy(map);
10865 map = isl_map_reverse(map);
10866 in = isl_map_is_single_valued(map);
10867 isl_map_free(map);
10869 return in;
10872 /* Check if the given map is obviously injective.
10874 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10876 isl_bool in;
10878 map = isl_map_copy(map);
10879 map = isl_map_reverse(map);
10880 in = isl_map_plain_is_single_valued(map);
10881 isl_map_free(map);
10883 return in;
10886 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10888 isl_bool sv;
10890 sv = isl_map_is_single_valued(map);
10891 if (sv < 0 || !sv)
10892 return sv;
10894 return isl_map_is_injective(map);
10897 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10899 return isl_map_is_single_valued((isl_map *)set);
10902 /* Does "map" only map elements to themselves?
10904 * If the domain and range spaces are different, then "map"
10905 * is considered not to be an identity relation, even if it is empty.
10906 * Otherwise, construct the maximal identity relation and
10907 * check whether "map" is a subset of this relation.
10909 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10911 isl_space *space;
10912 isl_map *id;
10913 isl_bool equal, is_identity;
10915 space = isl_map_get_space(map);
10916 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10917 isl_space_free(space);
10918 if (equal < 0 || !equal)
10919 return equal;
10921 id = isl_map_identity(isl_map_get_space(map));
10922 is_identity = isl_map_is_subset(map, id);
10923 isl_map_free(id);
10925 return is_identity;
10928 int isl_map_is_translation(__isl_keep isl_map *map)
10930 int ok;
10931 isl_set *delta;
10933 delta = isl_map_deltas(isl_map_copy(map));
10934 ok = isl_set_is_singleton(delta);
10935 isl_set_free(delta);
10937 return ok;
10940 static int unique(isl_int *p, unsigned pos, unsigned len)
10942 if (isl_seq_first_non_zero(p, pos) != -1)
10943 return 0;
10944 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10945 return 0;
10946 return 1;
10949 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10951 int i, j;
10952 unsigned nvar;
10953 unsigned ovar;
10955 if (!bset)
10956 return -1;
10958 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10959 return 0;
10961 nvar = isl_basic_set_dim(bset, isl_dim_set);
10962 ovar = isl_space_offset(bset->dim, isl_dim_set);
10963 for (j = 0; j < nvar; ++j) {
10964 int lower = 0, upper = 0;
10965 for (i = 0; i < bset->n_eq; ++i) {
10966 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10967 continue;
10968 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10969 return 0;
10970 break;
10972 if (i < bset->n_eq)
10973 continue;
10974 for (i = 0; i < bset->n_ineq; ++i) {
10975 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10976 continue;
10977 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10978 return 0;
10979 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10980 lower = 1;
10981 else
10982 upper = 1;
10984 if (!lower || !upper)
10985 return 0;
10988 return 1;
10991 int isl_set_is_box(__isl_keep isl_set *set)
10993 if (!set)
10994 return -1;
10995 if (set->n != 1)
10996 return 0;
10998 return isl_basic_set_is_box(set->p[0]);
11001 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11003 if (!bset)
11004 return isl_bool_error;
11006 return isl_space_is_wrapping(bset->dim);
11009 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11011 if (!set)
11012 return isl_bool_error;
11014 return isl_space_is_wrapping(set->dim);
11017 /* Modify the space of "map" through a call to "change".
11018 * If "can_change" is set (not NULL), then first call it to check
11019 * if the modification is allowed, printing the error message "cannot_change"
11020 * if it is not.
11022 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11023 isl_bool (*can_change)(__isl_keep isl_map *map),
11024 const char *cannot_change,
11025 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11027 isl_bool ok;
11028 isl_space *space;
11030 if (!map)
11031 return NULL;
11033 ok = can_change ? can_change(map) : isl_bool_true;
11034 if (ok < 0)
11035 return isl_map_free(map);
11036 if (!ok)
11037 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11038 return isl_map_free(map));
11040 space = change(isl_map_get_space(map));
11041 map = isl_map_reset_space(map, space);
11043 return map;
11046 /* Is the domain of "map" a wrapped relation?
11048 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11050 if (!map)
11051 return isl_bool_error;
11053 return isl_space_domain_is_wrapping(map->dim);
11056 /* Is the range of "map" a wrapped relation?
11058 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11060 if (!map)
11061 return isl_bool_error;
11063 return isl_space_range_is_wrapping(map->dim);
11066 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11068 bmap = isl_basic_map_cow(bmap);
11069 if (!bmap)
11070 return NULL;
11072 bmap->dim = isl_space_wrap(bmap->dim);
11073 if (!bmap->dim)
11074 goto error;
11076 bmap = isl_basic_map_finalize(bmap);
11078 return (isl_basic_set *)bmap;
11079 error:
11080 isl_basic_map_free(bmap);
11081 return NULL;
11084 /* Given a map A -> B, return the set (A -> B).
11086 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11088 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11091 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11093 bset = isl_basic_set_cow(bset);
11094 if (!bset)
11095 return NULL;
11097 bset->dim = isl_space_unwrap(bset->dim);
11098 if (!bset->dim)
11099 goto error;
11101 bset = isl_basic_set_finalize(bset);
11103 return (isl_basic_map *)bset;
11104 error:
11105 isl_basic_set_free(bset);
11106 return NULL;
11109 /* Given a set (A -> B), return the map A -> B.
11110 * Error out if "set" is not of the form (A -> B).
11112 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11114 return isl_map_change_space(set, &isl_set_is_wrapping,
11115 "not a wrapping set", &isl_space_unwrap);
11118 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11119 enum isl_dim_type type)
11121 if (!bmap)
11122 return NULL;
11124 if (!isl_space_is_named_or_nested(bmap->dim, type))
11125 return bmap;
11127 bmap = isl_basic_map_cow(bmap);
11128 if (!bmap)
11129 return NULL;
11131 bmap->dim = isl_space_reset(bmap->dim, type);
11132 if (!bmap->dim)
11133 goto error;
11135 bmap = isl_basic_map_finalize(bmap);
11137 return bmap;
11138 error:
11139 isl_basic_map_free(bmap);
11140 return NULL;
11143 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11144 enum isl_dim_type type)
11146 int i;
11148 if (!map)
11149 return NULL;
11151 if (!isl_space_is_named_or_nested(map->dim, type))
11152 return map;
11154 map = isl_map_cow(map);
11155 if (!map)
11156 return NULL;
11158 for (i = 0; i < map->n; ++i) {
11159 map->p[i] = isl_basic_map_reset(map->p[i], type);
11160 if (!map->p[i])
11161 goto error;
11163 map->dim = isl_space_reset(map->dim, type);
11164 if (!map->dim)
11165 goto error;
11167 return map;
11168 error:
11169 isl_map_free(map);
11170 return NULL;
11173 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11175 if (!bmap)
11176 return NULL;
11178 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11179 return bmap;
11181 bmap = isl_basic_map_cow(bmap);
11182 if (!bmap)
11183 return NULL;
11185 bmap->dim = isl_space_flatten(bmap->dim);
11186 if (!bmap->dim)
11187 goto error;
11189 bmap = isl_basic_map_finalize(bmap);
11191 return bmap;
11192 error:
11193 isl_basic_map_free(bmap);
11194 return NULL;
11197 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11199 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
11202 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11203 __isl_take isl_basic_map *bmap)
11205 if (!bmap)
11206 return NULL;
11208 if (!bmap->dim->nested[0])
11209 return bmap;
11211 bmap = isl_basic_map_cow(bmap);
11212 if (!bmap)
11213 return NULL;
11215 bmap->dim = isl_space_flatten_domain(bmap->dim);
11216 if (!bmap->dim)
11217 goto error;
11219 bmap = isl_basic_map_finalize(bmap);
11221 return bmap;
11222 error:
11223 isl_basic_map_free(bmap);
11224 return NULL;
11227 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11228 __isl_take isl_basic_map *bmap)
11230 if (!bmap)
11231 return NULL;
11233 if (!bmap->dim->nested[1])
11234 return bmap;
11236 bmap = isl_basic_map_cow(bmap);
11237 if (!bmap)
11238 return NULL;
11240 bmap->dim = isl_space_flatten_range(bmap->dim);
11241 if (!bmap->dim)
11242 goto error;
11244 bmap = isl_basic_map_finalize(bmap);
11246 return bmap;
11247 error:
11248 isl_basic_map_free(bmap);
11249 return NULL;
11252 /* Remove any internal structure from the spaces of domain and range of "map".
11254 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11256 if (!map)
11257 return NULL;
11259 if (!map->dim->nested[0] && !map->dim->nested[1])
11260 return map;
11262 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11265 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11267 return (isl_set *)isl_map_flatten((isl_map *)set);
11270 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11272 isl_space *dim, *flat_dim;
11273 isl_map *map;
11275 dim = isl_set_get_space(set);
11276 flat_dim = isl_space_flatten(isl_space_copy(dim));
11277 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11278 map = isl_map_intersect_domain(map, set);
11280 return map;
11283 /* Remove any internal structure from the space of the domain of "map".
11285 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11287 if (!map)
11288 return NULL;
11290 if (!map->dim->nested[0])
11291 return map;
11293 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11296 /* Remove any internal structure from the space of the range of "map".
11298 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11300 if (!map)
11301 return NULL;
11303 if (!map->dim->nested[1])
11304 return map;
11306 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11309 /* Reorder the dimensions of "bmap" according to the given dim_map
11310 * and set the dimension specification to "dim" and
11311 * perform Gaussian elimination on the result.
11313 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11314 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11316 isl_basic_map *res;
11317 unsigned flags;
11319 bmap = isl_basic_map_cow(bmap);
11320 if (!bmap || !dim || !dim_map)
11321 goto error;
11323 flags = bmap->flags;
11324 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11325 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11326 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11327 res = isl_basic_map_alloc_space(dim,
11328 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11329 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11330 if (res)
11331 res->flags = flags;
11332 res = isl_basic_map_gauss(res, NULL);
11333 res = isl_basic_map_finalize(res);
11334 return res;
11335 error:
11336 free(dim_map);
11337 isl_basic_map_free(bmap);
11338 isl_space_free(dim);
11339 return NULL;
11342 /* Reorder the dimensions of "map" according to given reordering.
11344 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11345 __isl_take isl_reordering *r)
11347 int i;
11348 struct isl_dim_map *dim_map;
11350 map = isl_map_cow(map);
11351 dim_map = isl_dim_map_from_reordering(r);
11352 if (!map || !r || !dim_map)
11353 goto error;
11355 for (i = 0; i < map->n; ++i) {
11356 struct isl_dim_map *dim_map_i;
11358 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11360 map->p[i] = isl_basic_map_realign(map->p[i],
11361 isl_space_copy(r->dim), dim_map_i);
11363 if (!map->p[i])
11364 goto error;
11367 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11369 isl_reordering_free(r);
11370 free(dim_map);
11371 return map;
11372 error:
11373 free(dim_map);
11374 isl_map_free(map);
11375 isl_reordering_free(r);
11376 return NULL;
11379 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11380 __isl_take isl_reordering *r)
11382 return (isl_set *)isl_map_realign((isl_map *)set, r);
11385 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11386 __isl_take isl_space *model)
11388 isl_ctx *ctx;
11390 if (!map || !model)
11391 goto error;
11393 ctx = isl_space_get_ctx(model);
11394 if (!isl_space_has_named_params(model))
11395 isl_die(ctx, isl_error_invalid,
11396 "model has unnamed parameters", goto error);
11397 if (!isl_space_has_named_params(map->dim))
11398 isl_die(ctx, isl_error_invalid,
11399 "relation has unnamed parameters", goto error);
11400 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11401 isl_reordering *exp;
11403 model = isl_space_drop_dims(model, isl_dim_in,
11404 0, isl_space_dim(model, isl_dim_in));
11405 model = isl_space_drop_dims(model, isl_dim_out,
11406 0, isl_space_dim(model, isl_dim_out));
11407 exp = isl_parameter_alignment_reordering(map->dim, model);
11408 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11409 map = isl_map_realign(map, exp);
11412 isl_space_free(model);
11413 return map;
11414 error:
11415 isl_space_free(model);
11416 isl_map_free(map);
11417 return NULL;
11420 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11421 __isl_take isl_space *model)
11423 return isl_map_align_params(set, model);
11426 /* Align the parameters of "bmap" to those of "model", introducing
11427 * additional parameters if needed.
11429 __isl_give isl_basic_map *isl_basic_map_align_params(
11430 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11432 isl_ctx *ctx;
11434 if (!bmap || !model)
11435 goto error;
11437 ctx = isl_space_get_ctx(model);
11438 if (!isl_space_has_named_params(model))
11439 isl_die(ctx, isl_error_invalid,
11440 "model has unnamed parameters", goto error);
11441 if (!isl_space_has_named_params(bmap->dim))
11442 isl_die(ctx, isl_error_invalid,
11443 "relation has unnamed parameters", goto error);
11444 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11445 isl_reordering *exp;
11446 struct isl_dim_map *dim_map;
11448 model = isl_space_drop_dims(model, isl_dim_in,
11449 0, isl_space_dim(model, isl_dim_in));
11450 model = isl_space_drop_dims(model, isl_dim_out,
11451 0, isl_space_dim(model, isl_dim_out));
11452 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11453 exp = isl_reordering_extend_space(exp,
11454 isl_basic_map_get_space(bmap));
11455 dim_map = isl_dim_map_from_reordering(exp);
11456 bmap = isl_basic_map_realign(bmap,
11457 exp ? isl_space_copy(exp->dim) : NULL,
11458 isl_dim_map_extend(dim_map, bmap));
11459 isl_reordering_free(exp);
11460 free(dim_map);
11463 isl_space_free(model);
11464 return bmap;
11465 error:
11466 isl_space_free(model);
11467 isl_basic_map_free(bmap);
11468 return NULL;
11471 /* Align the parameters of "bset" to those of "model", introducing
11472 * additional parameters if needed.
11474 __isl_give isl_basic_set *isl_basic_set_align_params(
11475 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11477 return isl_basic_map_align_params(bset, model);
11480 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11481 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11482 enum isl_dim_type c2, enum isl_dim_type c3,
11483 enum isl_dim_type c4, enum isl_dim_type c5)
11485 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11486 struct isl_mat *mat;
11487 int i, j, k;
11488 int pos;
11490 if (!bmap)
11491 return NULL;
11492 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11493 isl_basic_map_total_dim(bmap) + 1);
11494 if (!mat)
11495 return NULL;
11496 for (i = 0; i < bmap->n_eq; ++i)
11497 for (j = 0, pos = 0; j < 5; ++j) {
11498 int off = isl_basic_map_offset(bmap, c[j]);
11499 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11500 isl_int_set(mat->row[i][pos],
11501 bmap->eq[i][off + k]);
11502 ++pos;
11506 return mat;
11509 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11510 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11511 enum isl_dim_type c2, enum isl_dim_type c3,
11512 enum isl_dim_type c4, enum isl_dim_type c5)
11514 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11515 struct isl_mat *mat;
11516 int i, j, k;
11517 int pos;
11519 if (!bmap)
11520 return NULL;
11521 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11522 isl_basic_map_total_dim(bmap) + 1);
11523 if (!mat)
11524 return NULL;
11525 for (i = 0; i < bmap->n_ineq; ++i)
11526 for (j = 0, pos = 0; j < 5; ++j) {
11527 int off = isl_basic_map_offset(bmap, c[j]);
11528 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11529 isl_int_set(mat->row[i][pos],
11530 bmap->ineq[i][off + k]);
11531 ++pos;
11535 return mat;
11538 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11539 __isl_take isl_space *dim,
11540 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11541 enum isl_dim_type c2, enum isl_dim_type c3,
11542 enum isl_dim_type c4, enum isl_dim_type c5)
11544 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11545 isl_basic_map *bmap;
11546 unsigned total;
11547 unsigned extra;
11548 int i, j, k, l;
11549 int pos;
11551 if (!dim || !eq || !ineq)
11552 goto error;
11554 if (eq->n_col != ineq->n_col)
11555 isl_die(dim->ctx, isl_error_invalid,
11556 "equalities and inequalities matrices should have "
11557 "same number of columns", goto error);
11559 total = 1 + isl_space_dim(dim, isl_dim_all);
11561 if (eq->n_col < total)
11562 isl_die(dim->ctx, isl_error_invalid,
11563 "number of columns too small", goto error);
11565 extra = eq->n_col - total;
11567 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11568 eq->n_row, ineq->n_row);
11569 if (!bmap)
11570 goto error;
11571 for (i = 0; i < extra; ++i) {
11572 k = isl_basic_map_alloc_div(bmap);
11573 if (k < 0)
11574 goto error;
11575 isl_int_set_si(bmap->div[k][0], 0);
11577 for (i = 0; i < eq->n_row; ++i) {
11578 l = isl_basic_map_alloc_equality(bmap);
11579 if (l < 0)
11580 goto error;
11581 for (j = 0, pos = 0; j < 5; ++j) {
11582 int off = isl_basic_map_offset(bmap, c[j]);
11583 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11584 isl_int_set(bmap->eq[l][off + k],
11585 eq->row[i][pos]);
11586 ++pos;
11590 for (i = 0; i < ineq->n_row; ++i) {
11591 l = isl_basic_map_alloc_inequality(bmap);
11592 if (l < 0)
11593 goto error;
11594 for (j = 0, pos = 0; j < 5; ++j) {
11595 int off = isl_basic_map_offset(bmap, c[j]);
11596 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11597 isl_int_set(bmap->ineq[l][off + k],
11598 ineq->row[i][pos]);
11599 ++pos;
11604 isl_space_free(dim);
11605 isl_mat_free(eq);
11606 isl_mat_free(ineq);
11608 bmap = isl_basic_map_simplify(bmap);
11609 return isl_basic_map_finalize(bmap);
11610 error:
11611 isl_space_free(dim);
11612 isl_mat_free(eq);
11613 isl_mat_free(ineq);
11614 return NULL;
11617 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11618 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11619 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11621 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11622 c1, c2, c3, c4, isl_dim_in);
11625 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11626 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11627 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11629 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11630 c1, c2, c3, c4, isl_dim_in);
11633 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11634 __isl_take isl_space *dim,
11635 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11636 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11638 return (isl_basic_set*)
11639 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11640 c1, c2, c3, c4, isl_dim_in);
11643 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11645 if (!bmap)
11646 return isl_bool_error;
11648 return isl_space_can_zip(bmap->dim);
11651 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11653 if (!map)
11654 return isl_bool_error;
11656 return isl_space_can_zip(map->dim);
11659 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11660 * (A -> C) -> (B -> D).
11662 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11664 unsigned pos;
11665 unsigned n1;
11666 unsigned n2;
11668 if (!bmap)
11669 return NULL;
11671 if (!isl_basic_map_can_zip(bmap))
11672 isl_die(bmap->ctx, isl_error_invalid,
11673 "basic map cannot be zipped", goto error);
11674 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11675 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11676 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11677 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11678 bmap = isl_basic_map_cow(bmap);
11679 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11680 if (!bmap)
11681 return NULL;
11682 bmap->dim = isl_space_zip(bmap->dim);
11683 if (!bmap->dim)
11684 goto error;
11685 bmap = isl_basic_map_mark_final(bmap);
11686 return bmap;
11687 error:
11688 isl_basic_map_free(bmap);
11689 return NULL;
11692 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11693 * (A -> C) -> (B -> D).
11695 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11697 int i;
11699 if (!map)
11700 return NULL;
11702 if (!isl_map_can_zip(map))
11703 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11704 goto error);
11706 map = isl_map_cow(map);
11707 if (!map)
11708 return NULL;
11710 for (i = 0; i < map->n; ++i) {
11711 map->p[i] = isl_basic_map_zip(map->p[i]);
11712 if (!map->p[i])
11713 goto error;
11716 map->dim = isl_space_zip(map->dim);
11717 if (!map->dim)
11718 goto error;
11720 return map;
11721 error:
11722 isl_map_free(map);
11723 return NULL;
11726 /* Can we apply isl_basic_map_curry to "bmap"?
11727 * That is, does it have a nested relation in its domain?
11729 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11731 if (!bmap)
11732 return isl_bool_error;
11734 return isl_space_can_curry(bmap->dim);
11737 /* Can we apply isl_map_curry to "map"?
11738 * That is, does it have a nested relation in its domain?
11740 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11742 if (!map)
11743 return isl_bool_error;
11745 return isl_space_can_curry(map->dim);
11748 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11749 * A -> (B -> C).
11751 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11754 if (!bmap)
11755 return NULL;
11757 if (!isl_basic_map_can_curry(bmap))
11758 isl_die(bmap->ctx, isl_error_invalid,
11759 "basic map cannot be curried", goto error);
11760 bmap = isl_basic_map_cow(bmap);
11761 if (!bmap)
11762 return NULL;
11763 bmap->dim = isl_space_curry(bmap->dim);
11764 if (!bmap->dim)
11765 goto error;
11766 bmap = isl_basic_map_mark_final(bmap);
11767 return bmap;
11768 error:
11769 isl_basic_map_free(bmap);
11770 return NULL;
11773 /* Given a map (A -> B) -> C, return the corresponding map
11774 * A -> (B -> C).
11776 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11778 return isl_map_change_space(map, &isl_map_can_curry,
11779 "map cannot be curried", &isl_space_curry);
11782 /* Can isl_map_range_curry be applied to "map"?
11783 * That is, does it have a nested relation in its range,
11784 * the domain of which is itself a nested relation?
11786 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11788 if (!map)
11789 return isl_bool_error;
11791 return isl_space_can_range_curry(map->dim);
11794 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11795 * A -> (B -> (C -> D)).
11797 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11799 return isl_map_change_space(map, &isl_map_can_range_curry,
11800 "map range cannot be curried",
11801 &isl_space_range_curry);
11804 /* Can we apply isl_basic_map_uncurry to "bmap"?
11805 * That is, does it have a nested relation in its domain?
11807 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11809 if (!bmap)
11810 return isl_bool_error;
11812 return isl_space_can_uncurry(bmap->dim);
11815 /* Can we apply isl_map_uncurry to "map"?
11816 * That is, does it have a nested relation in its domain?
11818 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11820 if (!map)
11821 return isl_bool_error;
11823 return isl_space_can_uncurry(map->dim);
11826 /* Given a basic map A -> (B -> C), return the corresponding basic map
11827 * (A -> B) -> C.
11829 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11832 if (!bmap)
11833 return NULL;
11835 if (!isl_basic_map_can_uncurry(bmap))
11836 isl_die(bmap->ctx, isl_error_invalid,
11837 "basic map cannot be uncurried",
11838 return isl_basic_map_free(bmap));
11839 bmap = isl_basic_map_cow(bmap);
11840 if (!bmap)
11841 return NULL;
11842 bmap->dim = isl_space_uncurry(bmap->dim);
11843 if (!bmap->dim)
11844 return isl_basic_map_free(bmap);
11845 bmap = isl_basic_map_mark_final(bmap);
11846 return bmap;
11849 /* Given a map A -> (B -> C), return the corresponding map
11850 * (A -> B) -> C.
11852 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11854 return isl_map_change_space(map, &isl_map_can_uncurry,
11855 "map cannot be uncurried", &isl_space_uncurry);
11858 /* Construct a basic map mapping the domain of the affine expression
11859 * to a one-dimensional range prescribed by the affine expression.
11861 * A NaN affine expression cannot be converted to a basic map.
11863 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11865 int k;
11866 int pos;
11867 isl_bool is_nan;
11868 isl_local_space *ls;
11869 isl_basic_map *bmap = NULL;
11871 if (!aff)
11872 return NULL;
11873 is_nan = isl_aff_is_nan(aff);
11874 if (is_nan < 0)
11875 goto error;
11876 if (is_nan)
11877 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11878 "cannot convert NaN", goto error);
11880 ls = isl_aff_get_local_space(aff);
11881 bmap = isl_basic_map_from_local_space(ls);
11882 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11883 k = isl_basic_map_alloc_equality(bmap);
11884 if (k < 0)
11885 goto error;
11887 pos = isl_basic_map_offset(bmap, isl_dim_out);
11888 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11889 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11890 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11891 aff->v->size - (pos + 1));
11893 isl_aff_free(aff);
11894 bmap = isl_basic_map_finalize(bmap);
11895 return bmap;
11896 error:
11897 isl_aff_free(aff);
11898 isl_basic_map_free(bmap);
11899 return NULL;
11902 /* Construct a map mapping the domain of the affine expression
11903 * to a one-dimensional range prescribed by the affine expression.
11905 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11907 isl_basic_map *bmap;
11909 bmap = isl_basic_map_from_aff(aff);
11910 return isl_map_from_basic_map(bmap);
11913 /* Construct a basic map mapping the domain the multi-affine expression
11914 * to its range, with each dimension in the range equated to the
11915 * corresponding affine expression.
11917 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11918 __isl_take isl_multi_aff *maff)
11920 int i;
11921 isl_space *space;
11922 isl_basic_map *bmap;
11924 if (!maff)
11925 return NULL;
11927 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11928 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11929 "invalid space", goto error);
11931 space = isl_space_domain(isl_multi_aff_get_space(maff));
11932 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11934 for (i = 0; i < maff->n; ++i) {
11935 isl_aff *aff;
11936 isl_basic_map *bmap_i;
11938 aff = isl_aff_copy(maff->p[i]);
11939 bmap_i = isl_basic_map_from_aff(aff);
11941 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11944 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11946 isl_multi_aff_free(maff);
11947 return bmap;
11948 error:
11949 isl_multi_aff_free(maff);
11950 return NULL;
11953 /* Construct a map mapping the domain the multi-affine expression
11954 * to its range, with each dimension in the range equated to the
11955 * corresponding affine expression.
11957 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11959 isl_basic_map *bmap;
11961 bmap = isl_basic_map_from_multi_aff(maff);
11962 return isl_map_from_basic_map(bmap);
11965 /* Construct a basic map mapping a domain in the given space to
11966 * to an n-dimensional range, with n the number of elements in the list,
11967 * where each coordinate in the range is prescribed by the
11968 * corresponding affine expression.
11969 * The domains of all affine expressions in the list are assumed to match
11970 * domain_dim.
11972 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11973 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11975 int i;
11976 isl_space *dim;
11977 isl_basic_map *bmap;
11979 if (!list)
11980 return NULL;
11982 dim = isl_space_from_domain(domain_dim);
11983 bmap = isl_basic_map_universe(dim);
11985 for (i = 0; i < list->n; ++i) {
11986 isl_aff *aff;
11987 isl_basic_map *bmap_i;
11989 aff = isl_aff_copy(list->p[i]);
11990 bmap_i = isl_basic_map_from_aff(aff);
11992 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11995 isl_aff_list_free(list);
11996 return bmap;
11999 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12000 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12002 return isl_map_equate(set, type1, pos1, type2, pos2);
12005 /* Construct a basic map where the given dimensions are equal to each other.
12007 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12008 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12010 isl_basic_map *bmap = NULL;
12011 int i;
12013 if (!space)
12014 return NULL;
12016 if (pos1 >= isl_space_dim(space, type1))
12017 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12018 "index out of bounds", goto error);
12019 if (pos2 >= isl_space_dim(space, type2))
12020 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12021 "index out of bounds", goto error);
12023 if (type1 == type2 && pos1 == pos2)
12024 return isl_basic_map_universe(space);
12026 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12027 i = isl_basic_map_alloc_equality(bmap);
12028 if (i < 0)
12029 goto error;
12030 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12031 pos1 += isl_basic_map_offset(bmap, type1);
12032 pos2 += isl_basic_map_offset(bmap, type2);
12033 isl_int_set_si(bmap->eq[i][pos1], -1);
12034 isl_int_set_si(bmap->eq[i][pos2], 1);
12035 bmap = isl_basic_map_finalize(bmap);
12036 isl_space_free(space);
12037 return bmap;
12038 error:
12039 isl_space_free(space);
12040 isl_basic_map_free(bmap);
12041 return NULL;
12044 /* Add a constraint imposing that the given two dimensions are equal.
12046 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12047 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12049 isl_basic_map *eq;
12051 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12053 bmap = isl_basic_map_intersect(bmap, eq);
12055 return bmap;
12058 /* Add a constraint imposing that the given two dimensions are equal.
12060 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12061 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12063 isl_basic_map *bmap;
12065 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12067 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12069 return map;
12072 /* Add a constraint imposing that the given two dimensions have opposite values.
12074 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12075 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12077 isl_basic_map *bmap = NULL;
12078 int i;
12080 if (!map)
12081 return NULL;
12083 if (pos1 >= isl_map_dim(map, type1))
12084 isl_die(map->ctx, isl_error_invalid,
12085 "index out of bounds", goto error);
12086 if (pos2 >= isl_map_dim(map, type2))
12087 isl_die(map->ctx, isl_error_invalid,
12088 "index out of bounds", goto error);
12090 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12091 i = isl_basic_map_alloc_equality(bmap);
12092 if (i < 0)
12093 goto error;
12094 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12095 pos1 += isl_basic_map_offset(bmap, type1);
12096 pos2 += isl_basic_map_offset(bmap, type2);
12097 isl_int_set_si(bmap->eq[i][pos1], 1);
12098 isl_int_set_si(bmap->eq[i][pos2], 1);
12099 bmap = isl_basic_map_finalize(bmap);
12101 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12103 return map;
12104 error:
12105 isl_basic_map_free(bmap);
12106 isl_map_free(map);
12107 return NULL;
12110 /* Construct a constraint imposing that the value of the first dimension is
12111 * greater than or equal to that of the second.
12113 static __isl_give isl_constraint *constraint_order_ge(
12114 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12115 enum isl_dim_type type2, int pos2)
12117 isl_constraint *c;
12119 if (!space)
12120 return NULL;
12122 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12124 if (pos1 >= isl_constraint_dim(c, type1))
12125 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12126 "index out of bounds", return isl_constraint_free(c));
12127 if (pos2 >= isl_constraint_dim(c, type2))
12128 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12129 "index out of bounds", return isl_constraint_free(c));
12131 if (type1 == type2 && pos1 == pos2)
12132 return c;
12134 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12135 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12137 return c;
12140 /* Add a constraint imposing that the value of the first dimension is
12141 * greater than or equal to that of the second.
12143 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12144 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12146 isl_constraint *c;
12147 isl_space *space;
12149 if (type1 == type2 && pos1 == pos2)
12150 return bmap;
12151 space = isl_basic_map_get_space(bmap);
12152 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12153 bmap = isl_basic_map_add_constraint(bmap, c);
12155 return bmap;
12158 /* Add a constraint imposing that the value of the first dimension is
12159 * greater than or equal to that of the second.
12161 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12162 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12164 isl_constraint *c;
12165 isl_space *space;
12167 if (type1 == type2 && pos1 == pos2)
12168 return map;
12169 space = isl_map_get_space(map);
12170 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12171 map = isl_map_add_constraint(map, c);
12173 return map;
12176 /* Add a constraint imposing that the value of the first dimension is
12177 * less than or equal to that of the second.
12179 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12180 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12182 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12185 /* Construct a basic map where the value of the first dimension is
12186 * greater than that of the second.
12188 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12189 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12191 isl_basic_map *bmap = NULL;
12192 int i;
12194 if (!space)
12195 return NULL;
12197 if (pos1 >= isl_space_dim(space, type1))
12198 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12199 "index out of bounds", goto error);
12200 if (pos2 >= isl_space_dim(space, type2))
12201 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12202 "index out of bounds", goto error);
12204 if (type1 == type2 && pos1 == pos2)
12205 return isl_basic_map_empty(space);
12207 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12208 i = isl_basic_map_alloc_inequality(bmap);
12209 if (i < 0)
12210 return isl_basic_map_free(bmap);
12211 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12212 pos1 += isl_basic_map_offset(bmap, type1);
12213 pos2 += isl_basic_map_offset(bmap, type2);
12214 isl_int_set_si(bmap->ineq[i][pos1], 1);
12215 isl_int_set_si(bmap->ineq[i][pos2], -1);
12216 isl_int_set_si(bmap->ineq[i][0], -1);
12217 bmap = isl_basic_map_finalize(bmap);
12219 return bmap;
12220 error:
12221 isl_space_free(space);
12222 isl_basic_map_free(bmap);
12223 return NULL;
12226 /* Add a constraint imposing that the value of the first dimension is
12227 * greater than that of the second.
12229 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12230 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12232 isl_basic_map *gt;
12234 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12236 bmap = isl_basic_map_intersect(bmap, gt);
12238 return bmap;
12241 /* Add a constraint imposing that the value of the first dimension is
12242 * greater than that of the second.
12244 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12245 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12247 isl_basic_map *bmap;
12249 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12251 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12253 return map;
12256 /* Add a constraint imposing that the value of the first dimension is
12257 * smaller than that of the second.
12259 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12260 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12262 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12265 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12266 int pos)
12268 isl_aff *div;
12269 isl_local_space *ls;
12271 if (!bmap)
12272 return NULL;
12274 if (!isl_basic_map_divs_known(bmap))
12275 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12276 "some divs are unknown", return NULL);
12278 ls = isl_basic_map_get_local_space(bmap);
12279 div = isl_local_space_get_div(ls, pos);
12280 isl_local_space_free(ls);
12282 return div;
12285 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12286 int pos)
12288 return isl_basic_map_get_div(bset, pos);
12291 /* Plug in "subs" for dimension "type", "pos" of "bset".
12293 * Let i be the dimension to replace and let "subs" be of the form
12295 * f/d
12297 * Any integer division with a non-zero coefficient for i,
12299 * floor((a i + g)/m)
12301 * is replaced by
12303 * floor((a f + d g)/(m d))
12305 * Constraints of the form
12307 * a i + g
12309 * are replaced by
12311 * a f + d g
12313 * We currently require that "subs" is an integral expression.
12314 * Handling rational expressions may require us to add stride constraints
12315 * as we do in isl_basic_set_preimage_multi_aff.
12317 __isl_give isl_basic_set *isl_basic_set_substitute(
12318 __isl_take isl_basic_set *bset,
12319 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12321 int i;
12322 isl_int v;
12323 isl_ctx *ctx;
12325 if (bset && isl_basic_set_plain_is_empty(bset))
12326 return bset;
12328 bset = isl_basic_set_cow(bset);
12329 if (!bset || !subs)
12330 goto error;
12332 ctx = isl_basic_set_get_ctx(bset);
12333 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12334 isl_die(ctx, isl_error_invalid,
12335 "spaces don't match", goto error);
12336 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12337 isl_die(ctx, isl_error_unsupported,
12338 "cannot handle divs yet", goto error);
12339 if (!isl_int_is_one(subs->v->el[0]))
12340 isl_die(ctx, isl_error_invalid,
12341 "can only substitute integer expressions", goto error);
12343 pos += isl_basic_set_offset(bset, type);
12345 isl_int_init(v);
12347 for (i = 0; i < bset->n_eq; ++i) {
12348 if (isl_int_is_zero(bset->eq[i][pos]))
12349 continue;
12350 isl_int_set(v, bset->eq[i][pos]);
12351 isl_int_set_si(bset->eq[i][pos], 0);
12352 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12353 v, subs->v->el + 1, subs->v->size - 1);
12356 for (i = 0; i < bset->n_ineq; ++i) {
12357 if (isl_int_is_zero(bset->ineq[i][pos]))
12358 continue;
12359 isl_int_set(v, bset->ineq[i][pos]);
12360 isl_int_set_si(bset->ineq[i][pos], 0);
12361 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12362 v, subs->v->el + 1, subs->v->size - 1);
12365 for (i = 0; i < bset->n_div; ++i) {
12366 if (isl_int_is_zero(bset->div[i][1 + pos]))
12367 continue;
12368 isl_int_set(v, bset->div[i][1 + pos]);
12369 isl_int_set_si(bset->div[i][1 + pos], 0);
12370 isl_seq_combine(bset->div[i] + 1,
12371 subs->v->el[0], bset->div[i] + 1,
12372 v, subs->v->el + 1, subs->v->size - 1);
12373 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12376 isl_int_clear(v);
12378 bset = isl_basic_set_simplify(bset);
12379 return isl_basic_set_finalize(bset);
12380 error:
12381 isl_basic_set_free(bset);
12382 return NULL;
12385 /* Plug in "subs" for dimension "type", "pos" of "set".
12387 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12388 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12390 int i;
12392 if (set && isl_set_plain_is_empty(set))
12393 return set;
12395 set = isl_set_cow(set);
12396 if (!set || !subs)
12397 goto error;
12399 for (i = set->n - 1; i >= 0; --i) {
12400 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12401 if (remove_if_empty(set, i) < 0)
12402 goto error;
12405 return set;
12406 error:
12407 isl_set_free(set);
12408 return NULL;
12411 /* Check if the range of "ma" is compatible with the domain or range
12412 * (depending on "type") of "bmap".
12413 * Return -1 if anything is wrong.
12415 static int check_basic_map_compatible_range_multi_aff(
12416 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12417 __isl_keep isl_multi_aff *ma)
12419 int m;
12420 isl_space *ma_space;
12422 ma_space = isl_multi_aff_get_space(ma);
12424 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12425 if (m < 0)
12426 goto error;
12427 if (!m)
12428 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12429 "parameters don't match", goto error);
12430 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12431 if (m < 0)
12432 goto error;
12433 if (!m)
12434 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12435 "spaces don't match", goto error);
12437 isl_space_free(ma_space);
12438 return m;
12439 error:
12440 isl_space_free(ma_space);
12441 return -1;
12444 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12445 * coefficients before the transformed range of dimensions,
12446 * the "n_after" coefficients after the transformed range of dimensions
12447 * and the coefficients of the other divs in "bmap".
12449 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12450 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12452 int i;
12453 int n_param;
12454 int n_set;
12455 isl_local_space *ls;
12457 if (n_div == 0)
12458 return 0;
12460 ls = isl_aff_get_domain_local_space(ma->p[0]);
12461 if (!ls)
12462 return -1;
12464 n_param = isl_local_space_dim(ls, isl_dim_param);
12465 n_set = isl_local_space_dim(ls, isl_dim_set);
12466 for (i = 0; i < n_div; ++i) {
12467 int o_bmap = 0, o_ls = 0;
12469 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12470 o_bmap += 1 + 1 + n_param;
12471 o_ls += 1 + 1 + n_param;
12472 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12473 o_bmap += n_before;
12474 isl_seq_cpy(bmap->div[i] + o_bmap,
12475 ls->div->row[i] + o_ls, n_set);
12476 o_bmap += n_set;
12477 o_ls += n_set;
12478 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12479 o_bmap += n_after;
12480 isl_seq_cpy(bmap->div[i] + o_bmap,
12481 ls->div->row[i] + o_ls, n_div);
12482 o_bmap += n_div;
12483 o_ls += n_div;
12484 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12485 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12486 goto error;
12489 isl_local_space_free(ls);
12490 return 0;
12491 error:
12492 isl_local_space_free(ls);
12493 return -1;
12496 /* How many stride constraints does "ma" enforce?
12497 * That is, how many of the affine expressions have a denominator
12498 * different from one?
12500 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12502 int i;
12503 int strides = 0;
12505 for (i = 0; i < ma->n; ++i)
12506 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12507 strides++;
12509 return strides;
12512 /* For each affine expression in ma of the form
12514 * x_i = (f_i y + h_i)/m_i
12516 * with m_i different from one, add a constraint to "bmap"
12517 * of the form
12519 * f_i y + h_i = m_i alpha_i
12521 * with alpha_i an additional existentially quantified variable.
12523 * The input variables of "ma" correspond to a subset of the variables
12524 * of "bmap". There are "n_before" variables in "bmap" before this
12525 * subset and "n_after" variables after this subset.
12526 * The integer divisions of the affine expressions in "ma" are assumed
12527 * to have been aligned. There are "n_div_ma" of them and
12528 * they appear first in "bmap", straight after the "n_after" variables.
12530 static __isl_give isl_basic_map *add_ma_strides(
12531 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12532 int n_before, int n_after, int n_div_ma)
12534 int i, k;
12535 int div;
12536 int total;
12537 int n_param;
12538 int n_in;
12540 total = isl_basic_map_total_dim(bmap);
12541 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12542 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12543 for (i = 0; i < ma->n; ++i) {
12544 int o_bmap = 0, o_ma = 1;
12546 if (isl_int_is_one(ma->p[i]->v->el[0]))
12547 continue;
12548 div = isl_basic_map_alloc_div(bmap);
12549 k = isl_basic_map_alloc_equality(bmap);
12550 if (div < 0 || k < 0)
12551 goto error;
12552 isl_int_set_si(bmap->div[div][0], 0);
12553 isl_seq_cpy(bmap->eq[k] + o_bmap,
12554 ma->p[i]->v->el + o_ma, 1 + n_param);
12555 o_bmap += 1 + n_param;
12556 o_ma += 1 + n_param;
12557 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12558 o_bmap += n_before;
12559 isl_seq_cpy(bmap->eq[k] + o_bmap,
12560 ma->p[i]->v->el + o_ma, n_in);
12561 o_bmap += n_in;
12562 o_ma += n_in;
12563 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12564 o_bmap += n_after;
12565 isl_seq_cpy(bmap->eq[k] + o_bmap,
12566 ma->p[i]->v->el + o_ma, n_div_ma);
12567 o_bmap += n_div_ma;
12568 o_ma += n_div_ma;
12569 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12570 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12571 total++;
12574 return bmap;
12575 error:
12576 isl_basic_map_free(bmap);
12577 return NULL;
12580 /* Replace the domain or range space (depending on "type) of "space" by "set".
12582 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12583 enum isl_dim_type type, __isl_take isl_space *set)
12585 if (type == isl_dim_in) {
12586 space = isl_space_range(space);
12587 space = isl_space_map_from_domain_and_range(set, space);
12588 } else {
12589 space = isl_space_domain(space);
12590 space = isl_space_map_from_domain_and_range(space, set);
12593 return space;
12596 /* Compute the preimage of the domain or range (depending on "type")
12597 * of "bmap" under the function represented by "ma".
12598 * In other words, plug in "ma" in the domain or range of "bmap".
12599 * The result is a basic map that lives in the same space as "bmap"
12600 * except that the domain or range has been replaced by
12601 * the domain space of "ma".
12603 * If bmap is represented by
12605 * A(p) + S u + B x + T v + C(divs) >= 0,
12607 * where u and x are input and output dimensions if type == isl_dim_out
12608 * while x and v are input and output dimensions if type == isl_dim_in,
12609 * and ma is represented by
12611 * x = D(p) + F(y) + G(divs')
12613 * then the result is
12615 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12617 * The divs in the input set are similarly adjusted.
12618 * In particular
12620 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12622 * becomes
12624 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12625 * B_i G(divs') + c_i(divs))/n_i)
12627 * If bmap is not a rational map and if F(y) involves any denominators
12629 * x_i = (f_i y + h_i)/m_i
12631 * then additional constraints are added to ensure that we only
12632 * map back integer points. That is we enforce
12634 * f_i y + h_i = m_i alpha_i
12636 * with alpha_i an additional existentially quantified variable.
12638 * We first copy over the divs from "ma".
12639 * Then we add the modified constraints and divs from "bmap".
12640 * Finally, we add the stride constraints, if needed.
12642 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12643 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12644 __isl_take isl_multi_aff *ma)
12646 int i, k;
12647 isl_space *space;
12648 isl_basic_map *res = NULL;
12649 int n_before, n_after, n_div_bmap, n_div_ma;
12650 isl_int f, c1, c2, g;
12651 int rational, strides;
12653 isl_int_init(f);
12654 isl_int_init(c1);
12655 isl_int_init(c2);
12656 isl_int_init(g);
12658 ma = isl_multi_aff_align_divs(ma);
12659 if (!bmap || !ma)
12660 goto error;
12661 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12662 goto error;
12664 if (type == isl_dim_in) {
12665 n_before = 0;
12666 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12667 } else {
12668 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12669 n_after = 0;
12671 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12672 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12674 space = isl_multi_aff_get_domain_space(ma);
12675 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12676 rational = isl_basic_map_is_rational(bmap);
12677 strides = rational ? 0 : multi_aff_strides(ma);
12678 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12679 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12680 if (rational)
12681 res = isl_basic_map_set_rational(res);
12683 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12684 if (isl_basic_map_alloc_div(res) < 0)
12685 goto error;
12687 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12688 goto error;
12690 for (i = 0; i < bmap->n_eq; ++i) {
12691 k = isl_basic_map_alloc_equality(res);
12692 if (k < 0)
12693 goto error;
12694 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12695 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12698 for (i = 0; i < bmap->n_ineq; ++i) {
12699 k = isl_basic_map_alloc_inequality(res);
12700 if (k < 0)
12701 goto error;
12702 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12703 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12706 for (i = 0; i < bmap->n_div; ++i) {
12707 if (isl_int_is_zero(bmap->div[i][0])) {
12708 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12709 continue;
12711 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12712 n_before, n_after, n_div_ma, n_div_bmap,
12713 f, c1, c2, g, 1);
12716 if (strides)
12717 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12719 isl_int_clear(f);
12720 isl_int_clear(c1);
12721 isl_int_clear(c2);
12722 isl_int_clear(g);
12723 isl_basic_map_free(bmap);
12724 isl_multi_aff_free(ma);
12725 res = isl_basic_set_simplify(res);
12726 return isl_basic_map_finalize(res);
12727 error:
12728 isl_int_clear(f);
12729 isl_int_clear(c1);
12730 isl_int_clear(c2);
12731 isl_int_clear(g);
12732 isl_basic_map_free(bmap);
12733 isl_multi_aff_free(ma);
12734 isl_basic_map_free(res);
12735 return NULL;
12738 /* Compute the preimage of "bset" under the function represented by "ma".
12739 * In other words, plug in "ma" in "bset". The result is a basic set
12740 * that lives in the domain space of "ma".
12742 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12743 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12745 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12748 /* Compute the preimage of the domain of "bmap" under the function
12749 * represented by "ma".
12750 * In other words, plug in "ma" in the domain of "bmap".
12751 * The result is a basic map that lives in the same space as "bmap"
12752 * except that the domain has been replaced by the domain space of "ma".
12754 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12755 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12757 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12760 /* Compute the preimage of the range of "bmap" under the function
12761 * represented by "ma".
12762 * In other words, plug in "ma" in the range of "bmap".
12763 * The result is a basic map that lives in the same space as "bmap"
12764 * except that the range has been replaced by the domain space of "ma".
12766 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12767 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12769 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12772 /* Check if the range of "ma" is compatible with the domain or range
12773 * (depending on "type") of "map".
12774 * Return -1 if anything is wrong.
12776 static int check_map_compatible_range_multi_aff(
12777 __isl_keep isl_map *map, enum isl_dim_type type,
12778 __isl_keep isl_multi_aff *ma)
12780 int m;
12781 isl_space *ma_space;
12783 ma_space = isl_multi_aff_get_space(ma);
12784 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12785 isl_space_free(ma_space);
12786 if (m >= 0 && !m)
12787 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12788 "spaces don't match", return -1);
12789 return m;
12792 /* Compute the preimage of the domain or range (depending on "type")
12793 * of "map" under the function represented by "ma".
12794 * In other words, plug in "ma" in the domain or range of "map".
12795 * The result is a map that lives in the same space as "map"
12796 * except that the domain or range has been replaced by
12797 * the domain space of "ma".
12799 * The parameters are assumed to have been aligned.
12801 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12802 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12804 int i;
12805 isl_space *space;
12807 map = isl_map_cow(map);
12808 ma = isl_multi_aff_align_divs(ma);
12809 if (!map || !ma)
12810 goto error;
12811 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12812 goto error;
12814 for (i = 0; i < map->n; ++i) {
12815 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12816 isl_multi_aff_copy(ma));
12817 if (!map->p[i])
12818 goto error;
12821 space = isl_multi_aff_get_domain_space(ma);
12822 space = isl_space_set(isl_map_get_space(map), type, space);
12824 isl_space_free(map->dim);
12825 map->dim = space;
12826 if (!map->dim)
12827 goto error;
12829 isl_multi_aff_free(ma);
12830 if (map->n > 1)
12831 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12832 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12833 return map;
12834 error:
12835 isl_multi_aff_free(ma);
12836 isl_map_free(map);
12837 return NULL;
12840 /* Compute the preimage of the domain or range (depending on "type")
12841 * of "map" under the function represented by "ma".
12842 * In other words, plug in "ma" in the domain or range of "map".
12843 * The result is a map that lives in the same space as "map"
12844 * except that the domain or range has been replaced by
12845 * the domain space of "ma".
12847 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12848 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12850 if (!map || !ma)
12851 goto error;
12853 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12854 return map_preimage_multi_aff(map, type, ma);
12856 if (!isl_space_has_named_params(map->dim) ||
12857 !isl_space_has_named_params(ma->space))
12858 isl_die(map->ctx, isl_error_invalid,
12859 "unaligned unnamed parameters", goto error);
12860 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12861 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12863 return map_preimage_multi_aff(map, type, ma);
12864 error:
12865 isl_multi_aff_free(ma);
12866 return isl_map_free(map);
12869 /* Compute the preimage of "set" under the function represented by "ma".
12870 * In other words, plug in "ma" in "set". The result is a set
12871 * that lives in the domain space of "ma".
12873 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12874 __isl_take isl_multi_aff *ma)
12876 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12879 /* Compute the preimage of the domain of "map" under the function
12880 * represented by "ma".
12881 * In other words, plug in "ma" in the domain of "map".
12882 * The result is a map that lives in the same space as "map"
12883 * except that the domain has been replaced by the domain space of "ma".
12885 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12886 __isl_take isl_multi_aff *ma)
12888 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12891 /* Compute the preimage of the range of "map" under the function
12892 * represented by "ma".
12893 * In other words, plug in "ma" in the range of "map".
12894 * The result is a map that lives in the same space as "map"
12895 * except that the range has been replaced by the domain space of "ma".
12897 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12898 __isl_take isl_multi_aff *ma)
12900 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12903 /* Compute the preimage of "map" under the function represented by "pma".
12904 * In other words, plug in "pma" in the domain or range of "map".
12905 * The result is a map that lives in the same space as "map",
12906 * except that the space of type "type" has been replaced by
12907 * the domain space of "pma".
12909 * The parameters of "map" and "pma" are assumed to have been aligned.
12911 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12912 __isl_take isl_map *map, enum isl_dim_type type,
12913 __isl_take isl_pw_multi_aff *pma)
12915 int i;
12916 isl_map *res;
12918 if (!pma)
12919 goto error;
12921 if (pma->n == 0) {
12922 isl_pw_multi_aff_free(pma);
12923 res = isl_map_empty(isl_map_get_space(map));
12924 isl_map_free(map);
12925 return res;
12928 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12929 isl_multi_aff_copy(pma->p[0].maff));
12930 if (type == isl_dim_in)
12931 res = isl_map_intersect_domain(res,
12932 isl_map_copy(pma->p[0].set));
12933 else
12934 res = isl_map_intersect_range(res,
12935 isl_map_copy(pma->p[0].set));
12937 for (i = 1; i < pma->n; ++i) {
12938 isl_map *res_i;
12940 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12941 isl_multi_aff_copy(pma->p[i].maff));
12942 if (type == isl_dim_in)
12943 res_i = isl_map_intersect_domain(res_i,
12944 isl_map_copy(pma->p[i].set));
12945 else
12946 res_i = isl_map_intersect_range(res_i,
12947 isl_map_copy(pma->p[i].set));
12948 res = isl_map_union(res, res_i);
12951 isl_pw_multi_aff_free(pma);
12952 isl_map_free(map);
12953 return res;
12954 error:
12955 isl_pw_multi_aff_free(pma);
12956 isl_map_free(map);
12957 return NULL;
12960 /* Compute the preimage of "map" under the function represented by "pma".
12961 * In other words, plug in "pma" in the domain or range of "map".
12962 * The result is a map that lives in the same space as "map",
12963 * except that the space of type "type" has been replaced by
12964 * the domain space of "pma".
12966 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12967 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12969 if (!map || !pma)
12970 goto error;
12972 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12973 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12975 if (!isl_space_has_named_params(map->dim) ||
12976 !isl_space_has_named_params(pma->dim))
12977 isl_die(map->ctx, isl_error_invalid,
12978 "unaligned unnamed parameters", goto error);
12979 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12980 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12982 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12983 error:
12984 isl_pw_multi_aff_free(pma);
12985 return isl_map_free(map);
12988 /* Compute the preimage of "set" under the function represented by "pma".
12989 * In other words, plug in "pma" in "set". The result is a set
12990 * that lives in the domain space of "pma".
12992 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12993 __isl_take isl_pw_multi_aff *pma)
12995 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12998 /* Compute the preimage of the domain of "map" under the function
12999 * represented by "pma".
13000 * In other words, plug in "pma" in the domain of "map".
13001 * The result is a map that lives in the same space as "map",
13002 * except that domain space has been replaced by the domain space of "pma".
13004 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13005 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13007 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13010 /* Compute the preimage of the range of "map" under the function
13011 * represented by "pma".
13012 * In other words, plug in "pma" in the range of "map".
13013 * The result is a map that lives in the same space as "map",
13014 * except that range space has been replaced by the domain space of "pma".
13016 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13017 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13019 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13022 /* Compute the preimage of "map" under the function represented by "mpa".
13023 * In other words, plug in "mpa" in the domain or range of "map".
13024 * The result is a map that lives in the same space as "map",
13025 * except that the space of type "type" has been replaced by
13026 * the domain space of "mpa".
13028 * If the map does not involve any constraints that refer to the
13029 * dimensions of the substituted space, then the only possible
13030 * effect of "mpa" on the map is to map the space to a different space.
13031 * We create a separate isl_multi_aff to effectuate this change
13032 * in order to avoid spurious splitting of the map along the pieces
13033 * of "mpa".
13035 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13036 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13038 int n;
13039 isl_pw_multi_aff *pma;
13041 if (!map || !mpa)
13042 goto error;
13044 n = isl_map_dim(map, type);
13045 if (!isl_map_involves_dims(map, type, 0, n)) {
13046 isl_space *space;
13047 isl_multi_aff *ma;
13049 space = isl_multi_pw_aff_get_space(mpa);
13050 isl_multi_pw_aff_free(mpa);
13051 ma = isl_multi_aff_zero(space);
13052 return isl_map_preimage_multi_aff(map, type, ma);
13055 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13056 return isl_map_preimage_pw_multi_aff(map, type, pma);
13057 error:
13058 isl_map_free(map);
13059 isl_multi_pw_aff_free(mpa);
13060 return NULL;
13063 /* Compute the preimage of "map" under the function represented by "mpa".
13064 * In other words, plug in "mpa" in the domain "map".
13065 * The result is a map that lives in the same space as "map",
13066 * except that domain space has been replaced by the domain space of "mpa".
13068 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13069 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13071 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13074 /* Compute the preimage of "set" by the function represented by "mpa".
13075 * In other words, plug in "mpa" in "set".
13077 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13078 __isl_take isl_multi_pw_aff *mpa)
13080 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);