isl_pip: allow existentially quantified variables in the context
[isl.git] / isl_map.c
blob0e32bfe61c947ac5da606e698e975de1acd046a8
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-(m-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 /* Clear all cached information in "map", either because it is about
1677 * to be modified or because it is being freed.
1678 * Always return the same pointer that is passed in.
1679 * This is needed for the use in isl_map_free.
1681 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1683 isl_basic_map_free(map->cached_simple_hull[0]);
1684 isl_basic_map_free(map->cached_simple_hull[1]);
1685 map->cached_simple_hull[0] = NULL;
1686 map->cached_simple_hull[1] = NULL;
1687 return map;
1690 struct isl_set *isl_set_cow(struct isl_set *set)
1692 return isl_map_cow(set);
1695 /* Return an isl_map that is equal to "map" and that has only
1696 * a single reference.
1698 * If the original input already has only one reference, then
1699 * simply return it, but clear all cached information, since
1700 * it may be rendered invalid by the operations that will be
1701 * performed on the result.
1703 * Otherwise, create a duplicate (without any cached information).
1705 struct isl_map *isl_map_cow(struct isl_map *map)
1707 if (!map)
1708 return NULL;
1710 if (map->ref == 1)
1711 return clear_caches(map);
1712 map->ref--;
1713 return isl_map_dup(map);
1716 static void swap_vars(struct isl_blk blk, isl_int *a,
1717 unsigned a_len, unsigned b_len)
1719 isl_seq_cpy(blk.data, a+a_len, b_len);
1720 isl_seq_cpy(blk.data+b_len, a, a_len);
1721 isl_seq_cpy(a, blk.data, b_len+a_len);
1724 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1725 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1727 int i;
1728 struct isl_blk blk;
1730 if (!bmap)
1731 goto error;
1733 isl_assert(bmap->ctx,
1734 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1736 if (n1 == 0 || n2 == 0)
1737 return bmap;
1739 bmap = isl_basic_map_cow(bmap);
1740 if (!bmap)
1741 return NULL;
1743 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1744 if (isl_blk_is_error(blk))
1745 goto error;
1747 for (i = 0; i < bmap->n_eq; ++i)
1748 swap_vars(blk,
1749 bmap->eq[i] + pos, n1, n2);
1751 for (i = 0; i < bmap->n_ineq; ++i)
1752 swap_vars(blk,
1753 bmap->ineq[i] + pos, n1, n2);
1755 for (i = 0; i < bmap->n_div; ++i)
1756 swap_vars(blk,
1757 bmap->div[i]+1 + pos, n1, n2);
1759 isl_blk_free(bmap->ctx, blk);
1761 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1762 bmap = isl_basic_map_gauss(bmap, NULL);
1763 return isl_basic_map_finalize(bmap);
1764 error:
1765 isl_basic_map_free(bmap);
1766 return NULL;
1769 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1771 int i = 0;
1772 unsigned total;
1773 if (!bmap)
1774 goto error;
1775 total = isl_basic_map_total_dim(bmap);
1776 isl_basic_map_free_div(bmap, bmap->n_div);
1777 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1778 if (bmap->n_eq > 0)
1779 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1780 else {
1781 i = isl_basic_map_alloc_equality(bmap);
1782 if (i < 0)
1783 goto error;
1785 isl_int_set_si(bmap->eq[i][0], 1);
1786 isl_seq_clr(bmap->eq[i]+1, total);
1787 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1788 isl_vec_free(bmap->sample);
1789 bmap->sample = NULL;
1790 return isl_basic_map_finalize(bmap);
1791 error:
1792 isl_basic_map_free(bmap);
1793 return NULL;
1796 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1798 return (struct isl_basic_set *)
1799 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1802 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1803 * of "bmap").
1805 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1807 isl_int *t = bmap->div[a];
1808 bmap->div[a] = bmap->div[b];
1809 bmap->div[b] = t;
1812 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1813 * div definitions accordingly.
1815 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1817 int i;
1818 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1820 swap_div(bmap, a, b);
1822 for (i = 0; i < bmap->n_eq; ++i)
1823 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1825 for (i = 0; i < bmap->n_ineq; ++i)
1826 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1828 for (i = 0; i < bmap->n_div; ++i)
1829 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1830 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1833 /* Eliminate the specified n dimensions starting at first from the
1834 * constraints, without removing the dimensions from the space.
1835 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1837 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1838 enum isl_dim_type type, unsigned first, unsigned n)
1840 int i;
1842 if (!map)
1843 return NULL;
1844 if (n == 0)
1845 return map;
1847 if (first + n > isl_map_dim(map, type) || first + n < first)
1848 isl_die(map->ctx, isl_error_invalid,
1849 "index out of bounds", goto error);
1851 map = isl_map_cow(map);
1852 if (!map)
1853 return NULL;
1855 for (i = 0; i < map->n; ++i) {
1856 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1857 if (!map->p[i])
1858 goto error;
1860 return map;
1861 error:
1862 isl_map_free(map);
1863 return NULL;
1866 /* Eliminate the specified n dimensions starting at first from the
1867 * constraints, without removing the dimensions from the space.
1868 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1870 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1871 enum isl_dim_type type, unsigned first, unsigned n)
1873 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1876 /* Eliminate the specified n dimensions starting at first from the
1877 * constraints, without removing the dimensions from the space.
1878 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1880 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1881 unsigned first, unsigned n)
1883 return isl_set_eliminate(set, isl_dim_set, first, n);
1886 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1887 __isl_take isl_basic_map *bmap)
1889 if (!bmap)
1890 return NULL;
1891 bmap = isl_basic_map_eliminate_vars(bmap,
1892 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1893 if (!bmap)
1894 return NULL;
1895 bmap->n_div = 0;
1896 return isl_basic_map_finalize(bmap);
1899 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1900 __isl_take isl_basic_set *bset)
1902 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1903 (struct isl_basic_map *)bset);
1906 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1908 int i;
1910 if (!map)
1911 return NULL;
1912 if (map->n == 0)
1913 return map;
1915 map = isl_map_cow(map);
1916 if (!map)
1917 return NULL;
1919 for (i = 0; i < map->n; ++i) {
1920 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1921 if (!map->p[i])
1922 goto error;
1924 return map;
1925 error:
1926 isl_map_free(map);
1927 return NULL;
1930 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1932 return isl_map_remove_divs(set);
1935 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1936 enum isl_dim_type type, unsigned first, unsigned n)
1938 if (!bmap)
1939 return NULL;
1940 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1941 goto error);
1942 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1943 return bmap;
1944 bmap = isl_basic_map_eliminate_vars(bmap,
1945 isl_basic_map_offset(bmap, type) - 1 + first, n);
1946 if (!bmap)
1947 return bmap;
1948 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1949 return bmap;
1950 bmap = isl_basic_map_drop(bmap, type, first, n);
1951 return bmap;
1952 error:
1953 isl_basic_map_free(bmap);
1954 return NULL;
1957 /* Return true if the definition of the given div (recursively) involves
1958 * any of the given variables.
1960 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1961 unsigned first, unsigned n)
1963 int i;
1964 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1966 if (isl_int_is_zero(bmap->div[div][0]))
1967 return 0;
1968 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1969 return 1;
1971 for (i = bmap->n_div - 1; i >= 0; --i) {
1972 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1973 continue;
1974 if (div_involves_vars(bmap, i, first, n))
1975 return 1;
1978 return 0;
1981 /* Try and add a lower and/or upper bound on "div" to "bmap"
1982 * based on inequality "i".
1983 * "total" is the total number of variables (excluding the divs).
1984 * "v" is a temporary object that can be used during the calculations.
1985 * If "lb" is set, then a lower bound should be constructed.
1986 * If "ub" is set, then an upper bound should be constructed.
1988 * The calling function has already checked that the inequality does not
1989 * reference "div", but we still need to check that the inequality is
1990 * of the right form. We'll consider the case where we want to construct
1991 * a lower bound. The construction of upper bounds is similar.
1993 * Let "div" be of the form
1995 * q = floor((a + f(x))/d)
1997 * We essentially check if constraint "i" is of the form
1999 * b + f(x) >= 0
2001 * so that we can use it to derive a lower bound on "div".
2002 * However, we allow a slightly more general form
2004 * b + g(x) >= 0
2006 * with the condition that the coefficients of g(x) - f(x) are all
2007 * divisible by d.
2008 * Rewriting this constraint as
2010 * 0 >= -b - g(x)
2012 * adding a + f(x) to both sides and dividing by d, we obtain
2014 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2016 * Taking the floor on both sides, we obtain
2018 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2020 * or
2022 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2024 * In the case of an upper bound, we construct the constraint
2026 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2029 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2030 __isl_take isl_basic_map *bmap, int div, int i,
2031 unsigned total, isl_int v, int lb, int ub)
2033 int j;
2035 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2036 if (lb) {
2037 isl_int_sub(v, bmap->ineq[i][1 + j],
2038 bmap->div[div][1 + 1 + j]);
2039 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2041 if (ub) {
2042 isl_int_add(v, bmap->ineq[i][1 + j],
2043 bmap->div[div][1 + 1 + j]);
2044 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2047 if (!lb && !ub)
2048 return bmap;
2050 bmap = isl_basic_map_cow(bmap);
2051 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2052 if (lb) {
2053 int k = isl_basic_map_alloc_inequality(bmap);
2054 if (k < 0)
2055 goto error;
2056 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2057 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2058 bmap->div[div][1 + j]);
2059 isl_int_cdiv_q(bmap->ineq[k][j],
2060 bmap->ineq[k][j], bmap->div[div][0]);
2062 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2064 if (ub) {
2065 int k = isl_basic_map_alloc_inequality(bmap);
2066 if (k < 0)
2067 goto error;
2068 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2069 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2070 bmap->div[div][1 + j]);
2071 isl_int_fdiv_q(bmap->ineq[k][j],
2072 bmap->ineq[k][j], bmap->div[div][0]);
2074 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2077 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2078 return bmap;
2079 error:
2080 isl_basic_map_free(bmap);
2081 return NULL;
2084 /* This function is called right before "div" is eliminated from "bmap"
2085 * using Fourier-Motzkin.
2086 * Look through the constraints of "bmap" for constraints on the argument
2087 * of the integer division and use them to construct constraints on the
2088 * integer division itself. These constraints can then be combined
2089 * during the Fourier-Motzkin elimination.
2090 * Note that it is only useful to introduce lower bounds on "div"
2091 * if "bmap" already contains upper bounds on "div" as the newly
2092 * introduce lower bounds can then be combined with the pre-existing
2093 * upper bounds. Similarly for upper bounds.
2094 * We therefore first check if "bmap" contains any lower and/or upper bounds
2095 * on "div".
2097 * It is interesting to note that the introduction of these constraints
2098 * can indeed lead to more accurate results, even when compared to
2099 * deriving constraints on the argument of "div" from constraints on "div".
2100 * Consider, for example, the set
2102 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2104 * The second constraint can be rewritten as
2106 * 2 * [(-i-2j+3)/4] + k >= 0
2108 * from which we can derive
2110 * -i - 2j + 3 >= -2k
2112 * or
2114 * i + 2j <= 3 + 2k
2116 * Combined with the first constraint, we obtain
2118 * -3 <= 3 + 2k or k >= -3
2120 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2121 * the first constraint, we obtain
2123 * [(i + 2j)/4] >= [-3/4] = -1
2125 * Combining this constraint with the second constraint, we obtain
2127 * k >= -2
2129 static __isl_give isl_basic_map *insert_bounds_on_div(
2130 __isl_take isl_basic_map *bmap, int div)
2132 int i;
2133 int check_lb, check_ub;
2134 isl_int v;
2135 unsigned total;
2137 if (!bmap)
2138 return NULL;
2140 if (isl_int_is_zero(bmap->div[div][0]))
2141 return bmap;
2143 total = isl_space_dim(bmap->dim, isl_dim_all);
2145 check_lb = 0;
2146 check_ub = 0;
2147 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2148 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2149 if (s > 0)
2150 check_ub = 1;
2151 if (s < 0)
2152 check_lb = 1;
2155 if (!check_lb && !check_ub)
2156 return bmap;
2158 isl_int_init(v);
2160 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2161 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2162 continue;
2164 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2165 check_lb, check_ub);
2168 isl_int_clear(v);
2170 return bmap;
2173 /* Remove all divs (recursively) involving any of the given dimensions
2174 * in their definitions.
2176 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2177 __isl_take isl_basic_map *bmap,
2178 enum isl_dim_type type, unsigned first, unsigned n)
2180 int i;
2182 if (!bmap)
2183 return NULL;
2184 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2185 goto error);
2186 first += isl_basic_map_offset(bmap, type);
2188 for (i = bmap->n_div - 1; i >= 0; --i) {
2189 if (!div_involves_vars(bmap, i, first, n))
2190 continue;
2191 bmap = insert_bounds_on_div(bmap, i);
2192 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2193 if (!bmap)
2194 return NULL;
2195 i = bmap->n_div;
2198 return bmap;
2199 error:
2200 isl_basic_map_free(bmap);
2201 return NULL;
2204 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2205 __isl_take isl_basic_set *bset,
2206 enum isl_dim_type type, unsigned first, unsigned n)
2208 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2211 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2212 enum isl_dim_type type, unsigned first, unsigned n)
2214 int i;
2216 if (!map)
2217 return NULL;
2218 if (map->n == 0)
2219 return map;
2221 map = isl_map_cow(map);
2222 if (!map)
2223 return NULL;
2225 for (i = 0; i < map->n; ++i) {
2226 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2227 type, first, n);
2228 if (!map->p[i])
2229 goto error;
2231 return map;
2232 error:
2233 isl_map_free(map);
2234 return NULL;
2237 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2238 enum isl_dim_type type, unsigned first, unsigned n)
2240 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2241 type, first, n);
2244 /* Does the desciption of "bmap" depend on the specified dimensions?
2245 * We also check whether the dimensions appear in any of the div definitions.
2246 * In principle there is no need for this check. If the dimensions appear
2247 * in a div definition, they also appear in the defining constraints of that
2248 * div.
2250 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2251 enum isl_dim_type type, unsigned first, unsigned n)
2253 int i;
2255 if (!bmap)
2256 return isl_bool_error;
2258 if (first + n > isl_basic_map_dim(bmap, type))
2259 isl_die(bmap->ctx, isl_error_invalid,
2260 "index out of bounds", return isl_bool_error);
2262 first += isl_basic_map_offset(bmap, type);
2263 for (i = 0; i < bmap->n_eq; ++i)
2264 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2265 return isl_bool_true;
2266 for (i = 0; i < bmap->n_ineq; ++i)
2267 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2268 return isl_bool_true;
2269 for (i = 0; i < bmap->n_div; ++i) {
2270 if (isl_int_is_zero(bmap->div[i][0]))
2271 continue;
2272 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2273 return isl_bool_true;
2276 return isl_bool_false;
2279 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2280 enum isl_dim_type type, unsigned first, unsigned n)
2282 int i;
2284 if (!map)
2285 return isl_bool_error;
2287 if (first + n > isl_map_dim(map, type))
2288 isl_die(map->ctx, isl_error_invalid,
2289 "index out of bounds", return isl_bool_error);
2291 for (i = 0; i < map->n; ++i) {
2292 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2293 type, first, n);
2294 if (involves < 0 || involves)
2295 return involves;
2298 return isl_bool_false;
2301 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2302 enum isl_dim_type type, unsigned first, unsigned n)
2304 return isl_basic_map_involves_dims(bset, type, first, n);
2307 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2308 enum isl_dim_type type, unsigned first, unsigned n)
2310 return isl_map_involves_dims(set, type, first, n);
2313 /* Does local variable "div" of "bmap" have a complete explicit representation?
2314 * Having a complete explicit representation requires not only
2315 * an explicit representation, but also that all local variables
2316 * that appear in this explicit representation in turn have
2317 * a complete explicit representation.
2319 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2321 int i;
2322 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2323 isl_bool marked;
2325 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2326 if (marked < 0 || marked)
2327 return isl_bool_not(marked);
2329 for (i = bmap->n_div - 1; i >= 0; --i) {
2330 isl_bool known;
2332 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2333 continue;
2334 known = isl_basic_map_div_is_known(bmap, i);
2335 if (known < 0 || !known)
2336 return known;
2339 return isl_bool_true;
2342 /* Remove all divs that are unknown or defined in terms of unknown divs.
2344 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2345 __isl_take isl_basic_map *bmap)
2347 int i;
2349 if (!bmap)
2350 return NULL;
2352 for (i = bmap->n_div - 1; i >= 0; --i) {
2353 if (isl_basic_map_div_is_known(bmap, i))
2354 continue;
2355 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2356 if (!bmap)
2357 return NULL;
2358 i = bmap->n_div;
2361 return bmap;
2364 /* Remove all divs that are unknown or defined in terms of unknown divs.
2366 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2367 __isl_take isl_basic_set *bset)
2369 return isl_basic_map_remove_unknown_divs(bset);
2372 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2374 int i;
2376 if (!map)
2377 return NULL;
2378 if (map->n == 0)
2379 return map;
2381 map = isl_map_cow(map);
2382 if (!map)
2383 return NULL;
2385 for (i = 0; i < map->n; ++i) {
2386 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2387 if (!map->p[i])
2388 goto error;
2390 return map;
2391 error:
2392 isl_map_free(map);
2393 return NULL;
2396 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2398 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2401 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2402 __isl_take isl_basic_set *bset,
2403 enum isl_dim_type type, unsigned first, unsigned n)
2405 return (isl_basic_set *)
2406 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2409 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2410 enum isl_dim_type type, unsigned first, unsigned n)
2412 int i;
2414 if (n == 0)
2415 return map;
2417 map = isl_map_cow(map);
2418 if (!map)
2419 return NULL;
2420 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2422 for (i = 0; i < map->n; ++i) {
2423 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2424 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2425 if (!map->p[i])
2426 goto error;
2428 map = isl_map_drop(map, type, first, n);
2429 return map;
2430 error:
2431 isl_map_free(map);
2432 return NULL;
2435 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2436 enum isl_dim_type type, unsigned first, unsigned n)
2438 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2441 /* Project out n inputs starting at first using Fourier-Motzkin */
2442 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2443 unsigned first, unsigned n)
2445 return isl_map_remove_dims(map, isl_dim_in, first, n);
2448 static void dump_term(struct isl_basic_map *bmap,
2449 isl_int c, int pos, FILE *out)
2451 const char *name;
2452 unsigned in = isl_basic_map_n_in(bmap);
2453 unsigned dim = in + isl_basic_map_n_out(bmap);
2454 unsigned nparam = isl_basic_map_n_param(bmap);
2455 if (!pos)
2456 isl_int_print(out, c, 0);
2457 else {
2458 if (!isl_int_is_one(c))
2459 isl_int_print(out, c, 0);
2460 if (pos < 1 + nparam) {
2461 name = isl_space_get_dim_name(bmap->dim,
2462 isl_dim_param, pos - 1);
2463 if (name)
2464 fprintf(out, "%s", name);
2465 else
2466 fprintf(out, "p%d", pos - 1);
2467 } else if (pos < 1 + nparam + in)
2468 fprintf(out, "i%d", pos - 1 - nparam);
2469 else if (pos < 1 + nparam + dim)
2470 fprintf(out, "o%d", pos - 1 - nparam - in);
2471 else
2472 fprintf(out, "e%d", pos - 1 - nparam - dim);
2476 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2477 int sign, FILE *out)
2479 int i;
2480 int first;
2481 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2482 isl_int v;
2484 isl_int_init(v);
2485 for (i = 0, first = 1; i < len; ++i) {
2486 if (isl_int_sgn(c[i]) * sign <= 0)
2487 continue;
2488 if (!first)
2489 fprintf(out, " + ");
2490 first = 0;
2491 isl_int_abs(v, c[i]);
2492 dump_term(bmap, v, i, out);
2494 isl_int_clear(v);
2495 if (first)
2496 fprintf(out, "0");
2499 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2500 const char *op, FILE *out, int indent)
2502 int i;
2504 fprintf(out, "%*s", indent, "");
2506 dump_constraint_sign(bmap, c, 1, out);
2507 fprintf(out, " %s ", op);
2508 dump_constraint_sign(bmap, c, -1, out);
2510 fprintf(out, "\n");
2512 for (i = bmap->n_div; i < bmap->extra; ++i) {
2513 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2514 continue;
2515 fprintf(out, "%*s", indent, "");
2516 fprintf(out, "ERROR: unused div coefficient not zero\n");
2517 abort();
2521 static void dump_constraints(struct isl_basic_map *bmap,
2522 isl_int **c, unsigned n,
2523 const char *op, FILE *out, int indent)
2525 int i;
2527 for (i = 0; i < n; ++i)
2528 dump_constraint(bmap, c[i], op, out, indent);
2531 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2533 int j;
2534 int first = 1;
2535 unsigned total = isl_basic_map_total_dim(bmap);
2537 for (j = 0; j < 1 + total; ++j) {
2538 if (isl_int_is_zero(exp[j]))
2539 continue;
2540 if (!first && isl_int_is_pos(exp[j]))
2541 fprintf(out, "+");
2542 dump_term(bmap, exp[j], j, out);
2543 first = 0;
2547 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2549 int i;
2551 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2552 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2554 for (i = 0; i < bmap->n_div; ++i) {
2555 fprintf(out, "%*s", indent, "");
2556 fprintf(out, "e%d = [(", i);
2557 dump_affine(bmap, bmap->div[i]+1, out);
2558 fprintf(out, ")/");
2559 isl_int_print(out, bmap->div[i][0], 0);
2560 fprintf(out, "]\n");
2564 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2565 FILE *out, int indent)
2567 if (!bset) {
2568 fprintf(out, "null basic set\n");
2569 return;
2572 fprintf(out, "%*s", indent, "");
2573 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2574 bset->ref, bset->dim->nparam, bset->dim->n_out,
2575 bset->extra, bset->flags);
2576 dump((struct isl_basic_map *)bset, out, indent);
2579 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2580 FILE *out, int indent)
2582 if (!bmap) {
2583 fprintf(out, "null basic map\n");
2584 return;
2587 fprintf(out, "%*s", indent, "");
2588 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2589 "flags: %x, n_name: %d\n",
2590 bmap->ref,
2591 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2592 bmap->extra, bmap->flags, bmap->dim->n_id);
2593 dump(bmap, out, indent);
2596 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2598 unsigned total;
2599 if (!bmap)
2600 return -1;
2601 total = isl_basic_map_total_dim(bmap);
2602 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2603 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2604 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2605 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2606 return 0;
2609 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2610 unsigned flags)
2612 if (!space)
2613 return NULL;
2614 if (isl_space_dim(space, isl_dim_in) != 0)
2615 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2616 "set cannot have input dimensions", goto error);
2617 return isl_map_alloc_space(space, n, flags);
2618 error:
2619 isl_space_free(space);
2620 return NULL;
2623 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2624 unsigned nparam, unsigned dim, int n, unsigned flags)
2626 struct isl_set *set;
2627 isl_space *dims;
2629 dims = isl_space_alloc(ctx, nparam, 0, dim);
2630 if (!dims)
2631 return NULL;
2633 set = isl_set_alloc_space(dims, n, flags);
2634 return set;
2637 /* Make sure "map" has room for at least "n" more basic maps.
2639 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2641 int i;
2642 struct isl_map *grown = NULL;
2644 if (!map)
2645 return NULL;
2646 isl_assert(map->ctx, n >= 0, goto error);
2647 if (map->n + n <= map->size)
2648 return map;
2649 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2650 if (!grown)
2651 goto error;
2652 for (i = 0; i < map->n; ++i) {
2653 grown->p[i] = isl_basic_map_copy(map->p[i]);
2654 if (!grown->p[i])
2655 goto error;
2656 grown->n++;
2658 isl_map_free(map);
2659 return grown;
2660 error:
2661 isl_map_free(grown);
2662 isl_map_free(map);
2663 return NULL;
2666 /* Make sure "set" has room for at least "n" more basic sets.
2668 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2670 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2673 struct isl_set *isl_set_dup(struct isl_set *set)
2675 int i;
2676 struct isl_set *dup;
2678 if (!set)
2679 return NULL;
2681 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2682 if (!dup)
2683 return NULL;
2684 for (i = 0; i < set->n; ++i)
2685 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2686 return dup;
2689 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2691 return isl_map_from_basic_map(bset);
2694 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2696 struct isl_map *map;
2698 if (!bmap)
2699 return NULL;
2701 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2702 return isl_map_add_basic_map(map, bmap);
2705 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2706 __isl_take isl_basic_set *bset)
2708 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2709 (struct isl_basic_map *)bset);
2712 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2714 return isl_map_free(set);
2717 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2719 int i;
2721 if (!set) {
2722 fprintf(out, "null set\n");
2723 return;
2726 fprintf(out, "%*s", indent, "");
2727 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2728 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2729 set->flags);
2730 for (i = 0; i < set->n; ++i) {
2731 fprintf(out, "%*s", indent, "");
2732 fprintf(out, "basic set %d:\n", i);
2733 isl_basic_set_print_internal(set->p[i], out, indent+4);
2737 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2739 int i;
2741 if (!map) {
2742 fprintf(out, "null map\n");
2743 return;
2746 fprintf(out, "%*s", indent, "");
2747 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2748 "flags: %x, n_name: %d\n",
2749 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2750 map->dim->n_out, map->flags, map->dim->n_id);
2751 for (i = 0; i < map->n; ++i) {
2752 fprintf(out, "%*s", indent, "");
2753 fprintf(out, "basic map %d:\n", i);
2754 isl_basic_map_print_internal(map->p[i], out, indent+4);
2758 struct isl_basic_map *isl_basic_map_intersect_domain(
2759 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2761 struct isl_basic_map *bmap_domain;
2763 if (!bmap || !bset)
2764 goto error;
2766 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2767 bset->dim, isl_dim_param), goto error);
2769 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2770 isl_assert(bset->ctx,
2771 isl_basic_map_compatible_domain(bmap, bset), goto error);
2773 bmap = isl_basic_map_cow(bmap);
2774 if (!bmap)
2775 goto error;
2776 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2777 bset->n_div, bset->n_eq, bset->n_ineq);
2778 bmap_domain = isl_basic_map_from_domain(bset);
2779 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2781 bmap = isl_basic_map_simplify(bmap);
2782 return isl_basic_map_finalize(bmap);
2783 error:
2784 isl_basic_map_free(bmap);
2785 isl_basic_set_free(bset);
2786 return NULL;
2789 struct isl_basic_map *isl_basic_map_intersect_range(
2790 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2792 struct isl_basic_map *bmap_range;
2794 if (!bmap || !bset)
2795 goto error;
2797 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2798 bset->dim, isl_dim_param), goto error);
2800 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2801 isl_assert(bset->ctx,
2802 isl_basic_map_compatible_range(bmap, bset), goto error);
2804 if (isl_basic_set_plain_is_universe(bset)) {
2805 isl_basic_set_free(bset);
2806 return bmap;
2809 bmap = isl_basic_map_cow(bmap);
2810 if (!bmap)
2811 goto error;
2812 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2813 bset->n_div, bset->n_eq, bset->n_ineq);
2814 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2815 bmap = add_constraints(bmap, bmap_range, 0, 0);
2817 bmap = isl_basic_map_simplify(bmap);
2818 return isl_basic_map_finalize(bmap);
2819 error:
2820 isl_basic_map_free(bmap);
2821 isl_basic_set_free(bset);
2822 return NULL;
2825 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2826 __isl_keep isl_vec *vec)
2828 int i;
2829 unsigned total;
2830 isl_int s;
2832 if (!bmap || !vec)
2833 return isl_bool_error;
2835 total = 1 + isl_basic_map_total_dim(bmap);
2836 if (total != vec->size)
2837 return isl_bool_error;
2839 isl_int_init(s);
2841 for (i = 0; i < bmap->n_eq; ++i) {
2842 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2843 if (!isl_int_is_zero(s)) {
2844 isl_int_clear(s);
2845 return isl_bool_false;
2849 for (i = 0; i < bmap->n_ineq; ++i) {
2850 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2851 if (isl_int_is_neg(s)) {
2852 isl_int_clear(s);
2853 return isl_bool_false;
2857 isl_int_clear(s);
2859 return isl_bool_true;
2862 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2863 __isl_keep isl_vec *vec)
2865 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2868 struct isl_basic_map *isl_basic_map_intersect(
2869 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2871 struct isl_vec *sample = NULL;
2873 if (!bmap1 || !bmap2)
2874 goto error;
2876 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2877 bmap2->dim, isl_dim_param), goto error);
2878 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2879 isl_space_dim(bmap1->dim, isl_dim_param) &&
2880 isl_space_dim(bmap2->dim, isl_dim_all) !=
2881 isl_space_dim(bmap2->dim, isl_dim_param))
2882 return isl_basic_map_intersect(bmap2, bmap1);
2884 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2885 isl_space_dim(bmap2->dim, isl_dim_param))
2886 isl_assert(bmap1->ctx,
2887 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2889 if (isl_basic_map_plain_is_empty(bmap1)) {
2890 isl_basic_map_free(bmap2);
2891 return bmap1;
2893 if (isl_basic_map_plain_is_empty(bmap2)) {
2894 isl_basic_map_free(bmap1);
2895 return bmap2;
2898 if (bmap1->sample &&
2899 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2900 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2901 sample = isl_vec_copy(bmap1->sample);
2902 else if (bmap2->sample &&
2903 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2904 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2905 sample = isl_vec_copy(bmap2->sample);
2907 bmap1 = isl_basic_map_cow(bmap1);
2908 if (!bmap1)
2909 goto error;
2910 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2911 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2912 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2914 if (!bmap1)
2915 isl_vec_free(sample);
2916 else if (sample) {
2917 isl_vec_free(bmap1->sample);
2918 bmap1->sample = sample;
2921 bmap1 = isl_basic_map_simplify(bmap1);
2922 return isl_basic_map_finalize(bmap1);
2923 error:
2924 if (sample)
2925 isl_vec_free(sample);
2926 isl_basic_map_free(bmap1);
2927 isl_basic_map_free(bmap2);
2928 return NULL;
2931 struct isl_basic_set *isl_basic_set_intersect(
2932 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2934 return (struct isl_basic_set *)
2935 isl_basic_map_intersect(
2936 (struct isl_basic_map *)bset1,
2937 (struct isl_basic_map *)bset2);
2940 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2941 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2943 return isl_basic_set_intersect(bset1, bset2);
2946 /* Special case of isl_map_intersect, where both map1 and map2
2947 * are convex, without any divs and such that either map1 or map2
2948 * contains a single constraint. This constraint is then simply
2949 * added to the other map.
2951 static __isl_give isl_map *map_intersect_add_constraint(
2952 __isl_take isl_map *map1, __isl_take isl_map *map2)
2954 isl_assert(map1->ctx, map1->n == 1, goto error);
2955 isl_assert(map2->ctx, map1->n == 1, goto error);
2956 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2957 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2959 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2960 return isl_map_intersect(map2, map1);
2962 isl_assert(map2->ctx,
2963 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2965 map1 = isl_map_cow(map1);
2966 if (!map1)
2967 goto error;
2968 if (isl_map_plain_is_empty(map1)) {
2969 isl_map_free(map2);
2970 return map1;
2972 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2973 if (map2->p[0]->n_eq == 1)
2974 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2975 else
2976 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2977 map2->p[0]->ineq[0]);
2979 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2980 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2981 if (!map1->p[0])
2982 goto error;
2984 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2985 isl_basic_map_free(map1->p[0]);
2986 map1->n = 0;
2989 isl_map_free(map2);
2991 return map1;
2992 error:
2993 isl_map_free(map1);
2994 isl_map_free(map2);
2995 return NULL;
2998 /* map2 may be either a parameter domain or a map living in the same
2999 * space as map1.
3001 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3002 __isl_take isl_map *map2)
3004 unsigned flags = 0;
3005 isl_map *result;
3006 int i, j;
3008 if (!map1 || !map2)
3009 goto error;
3011 if ((isl_map_plain_is_empty(map1) ||
3012 isl_map_plain_is_universe(map2)) &&
3013 isl_space_is_equal(map1->dim, map2->dim)) {
3014 isl_map_free(map2);
3015 return map1;
3017 if ((isl_map_plain_is_empty(map2) ||
3018 isl_map_plain_is_universe(map1)) &&
3019 isl_space_is_equal(map1->dim, map2->dim)) {
3020 isl_map_free(map1);
3021 return map2;
3024 if (map1->n == 1 && map2->n == 1 &&
3025 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3026 isl_space_is_equal(map1->dim, map2->dim) &&
3027 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3028 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3029 return map_intersect_add_constraint(map1, map2);
3031 if (isl_space_dim(map2->dim, isl_dim_all) !=
3032 isl_space_dim(map2->dim, isl_dim_param))
3033 isl_assert(map1->ctx,
3034 isl_space_is_equal(map1->dim, map2->dim), goto error);
3036 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3037 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3038 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3040 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3041 map1->n * map2->n, flags);
3042 if (!result)
3043 goto error;
3044 for (i = 0; i < map1->n; ++i)
3045 for (j = 0; j < map2->n; ++j) {
3046 struct isl_basic_map *part;
3047 part = isl_basic_map_intersect(
3048 isl_basic_map_copy(map1->p[i]),
3049 isl_basic_map_copy(map2->p[j]));
3050 if (isl_basic_map_is_empty(part) < 0)
3051 part = isl_basic_map_free(part);
3052 result = isl_map_add_basic_map(result, part);
3053 if (!result)
3054 goto error;
3056 isl_map_free(map1);
3057 isl_map_free(map2);
3058 return result;
3059 error:
3060 isl_map_free(map1);
3061 isl_map_free(map2);
3062 return NULL;
3065 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3066 __isl_take isl_map *map2)
3068 if (!map1 || !map2)
3069 goto error;
3070 if (!isl_space_is_equal(map1->dim, map2->dim))
3071 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3072 "spaces don't match", goto error);
3073 return map_intersect_internal(map1, map2);
3074 error:
3075 isl_map_free(map1);
3076 isl_map_free(map2);
3077 return NULL;
3080 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3081 __isl_take isl_map *map2)
3083 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3086 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3088 return (struct isl_set *)
3089 isl_map_intersect((struct isl_map *)set1,
3090 (struct isl_map *)set2);
3093 /* map_intersect_internal accepts intersections
3094 * with parameter domains, so we can just call that function.
3096 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3097 __isl_take isl_set *params)
3099 return map_intersect_internal(map, params);
3102 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3103 __isl_take isl_map *map2)
3105 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3108 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3109 __isl_take isl_set *params)
3111 return isl_map_intersect_params(set, params);
3114 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3116 isl_space *space;
3117 unsigned pos, n1, n2;
3119 if (!bmap)
3120 return NULL;
3121 bmap = isl_basic_map_cow(bmap);
3122 if (!bmap)
3123 return NULL;
3124 space = isl_space_reverse(isl_space_copy(bmap->dim));
3125 pos = isl_basic_map_offset(bmap, isl_dim_in);
3126 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3127 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3128 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3129 return isl_basic_map_reset_space(bmap, space);
3132 static __isl_give isl_basic_map *basic_map_space_reset(
3133 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3135 isl_space *space;
3137 if (!bmap)
3138 return NULL;
3139 if (!isl_space_is_named_or_nested(bmap->dim, type))
3140 return bmap;
3142 space = isl_basic_map_get_space(bmap);
3143 space = isl_space_reset(space, type);
3144 bmap = isl_basic_map_reset_space(bmap, space);
3145 return bmap;
3148 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3149 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3150 unsigned pos, unsigned n)
3152 isl_space *res_dim;
3153 struct isl_basic_map *res;
3154 struct isl_dim_map *dim_map;
3155 unsigned total, off;
3156 enum isl_dim_type t;
3158 if (n == 0)
3159 return basic_map_space_reset(bmap, type);
3161 if (!bmap)
3162 return NULL;
3164 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3166 total = isl_basic_map_total_dim(bmap) + n;
3167 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3168 off = 0;
3169 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3170 if (t != type) {
3171 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3172 } else {
3173 unsigned size = isl_basic_map_dim(bmap, t);
3174 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3175 0, pos, off);
3176 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3177 pos, size - pos, off + pos + n);
3179 off += isl_space_dim(res_dim, t);
3181 isl_dim_map_div(dim_map, bmap, off);
3183 res = isl_basic_map_alloc_space(res_dim,
3184 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3185 if (isl_basic_map_is_rational(bmap))
3186 res = isl_basic_map_set_rational(res);
3187 if (isl_basic_map_plain_is_empty(bmap)) {
3188 isl_basic_map_free(bmap);
3189 free(dim_map);
3190 return isl_basic_map_set_to_empty(res);
3192 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3193 return isl_basic_map_finalize(res);
3196 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3197 __isl_take isl_basic_set *bset,
3198 enum isl_dim_type type, unsigned pos, unsigned n)
3200 return isl_basic_map_insert_dims(bset, type, pos, n);
3203 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3204 enum isl_dim_type type, unsigned n)
3206 if (!bmap)
3207 return NULL;
3208 return isl_basic_map_insert_dims(bmap, type,
3209 isl_basic_map_dim(bmap, type), n);
3212 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3213 enum isl_dim_type type, unsigned n)
3215 if (!bset)
3216 return NULL;
3217 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3218 return isl_basic_map_add_dims(bset, type, n);
3219 error:
3220 isl_basic_set_free(bset);
3221 return NULL;
3224 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3225 enum isl_dim_type type)
3227 isl_space *space;
3229 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3230 return map;
3232 space = isl_map_get_space(map);
3233 space = isl_space_reset(space, type);
3234 map = isl_map_reset_space(map, space);
3235 return map;
3238 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3239 enum isl_dim_type type, unsigned pos, unsigned n)
3241 int i;
3243 if (n == 0)
3244 return map_space_reset(map, type);
3246 map = isl_map_cow(map);
3247 if (!map)
3248 return NULL;
3250 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3251 if (!map->dim)
3252 goto error;
3254 for (i = 0; i < map->n; ++i) {
3255 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3256 if (!map->p[i])
3257 goto error;
3260 return map;
3261 error:
3262 isl_map_free(map);
3263 return NULL;
3266 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3267 enum isl_dim_type type, unsigned pos, unsigned n)
3269 return isl_map_insert_dims(set, type, pos, n);
3272 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3273 enum isl_dim_type type, unsigned n)
3275 if (!map)
3276 return NULL;
3277 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3280 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3281 enum isl_dim_type type, unsigned n)
3283 if (!set)
3284 return NULL;
3285 isl_assert(set->ctx, type != isl_dim_in, goto error);
3286 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3287 error:
3288 isl_set_free(set);
3289 return NULL;
3292 __isl_give isl_basic_map *isl_basic_map_move_dims(
3293 __isl_take isl_basic_map *bmap,
3294 enum isl_dim_type dst_type, unsigned dst_pos,
3295 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3297 struct isl_dim_map *dim_map;
3298 struct isl_basic_map *res;
3299 enum isl_dim_type t;
3300 unsigned total, off;
3302 if (!bmap)
3303 return NULL;
3304 if (n == 0)
3305 return bmap;
3307 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3308 goto error);
3310 if (dst_type == src_type && dst_pos == src_pos)
3311 return bmap;
3313 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3315 if (pos(bmap->dim, dst_type) + dst_pos ==
3316 pos(bmap->dim, src_type) + src_pos +
3317 ((src_type < dst_type) ? n : 0)) {
3318 bmap = isl_basic_map_cow(bmap);
3319 if (!bmap)
3320 return NULL;
3322 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3323 src_type, src_pos, n);
3324 if (!bmap->dim)
3325 goto error;
3327 bmap = isl_basic_map_finalize(bmap);
3329 return bmap;
3332 total = isl_basic_map_total_dim(bmap);
3333 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3335 off = 0;
3336 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3337 unsigned size = isl_space_dim(bmap->dim, t);
3338 if (t == dst_type) {
3339 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3340 0, dst_pos, off);
3341 off += dst_pos;
3342 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3343 src_pos, n, off);
3344 off += n;
3345 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3346 dst_pos, size - dst_pos, off);
3347 off += size - dst_pos;
3348 } else if (t == src_type) {
3349 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3350 0, src_pos, off);
3351 off += src_pos;
3352 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3353 src_pos + n, size - src_pos - n, off);
3354 off += size - src_pos - n;
3355 } else {
3356 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3357 off += size;
3360 isl_dim_map_div(dim_map, bmap, off);
3362 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3363 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3364 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3365 if (!bmap)
3366 goto error;
3368 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3369 src_type, src_pos, n);
3370 if (!bmap->dim)
3371 goto error;
3373 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3374 bmap = isl_basic_map_gauss(bmap, NULL);
3375 bmap = isl_basic_map_finalize(bmap);
3377 return bmap;
3378 error:
3379 isl_basic_map_free(bmap);
3380 return NULL;
3383 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3384 enum isl_dim_type dst_type, unsigned dst_pos,
3385 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3387 return (isl_basic_set *)isl_basic_map_move_dims(
3388 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3391 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3392 enum isl_dim_type dst_type, unsigned dst_pos,
3393 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3395 if (!set)
3396 return NULL;
3397 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3398 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3399 src_type, src_pos, n);
3400 error:
3401 isl_set_free(set);
3402 return NULL;
3405 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3406 enum isl_dim_type dst_type, unsigned dst_pos,
3407 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3409 int i;
3411 if (!map)
3412 return NULL;
3413 if (n == 0)
3414 return map;
3416 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3417 goto error);
3419 if (dst_type == src_type && dst_pos == src_pos)
3420 return map;
3422 isl_assert(map->ctx, dst_type != src_type, goto error);
3424 map = isl_map_cow(map);
3425 if (!map)
3426 return NULL;
3428 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3429 if (!map->dim)
3430 goto error;
3432 for (i = 0; i < map->n; ++i) {
3433 map->p[i] = isl_basic_map_move_dims(map->p[i],
3434 dst_type, dst_pos,
3435 src_type, src_pos, n);
3436 if (!map->p[i])
3437 goto error;
3440 return map;
3441 error:
3442 isl_map_free(map);
3443 return NULL;
3446 /* Move the specified dimensions to the last columns right before
3447 * the divs. Don't change the dimension specification of bmap.
3448 * That's the responsibility of the caller.
3450 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3451 enum isl_dim_type type, unsigned first, unsigned n)
3453 struct isl_dim_map *dim_map;
3454 struct isl_basic_map *res;
3455 enum isl_dim_type t;
3456 unsigned total, off;
3458 if (!bmap)
3459 return NULL;
3460 if (pos(bmap->dim, type) + first + n ==
3461 1 + isl_space_dim(bmap->dim, isl_dim_all))
3462 return bmap;
3464 total = isl_basic_map_total_dim(bmap);
3465 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3467 off = 0;
3468 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3469 unsigned size = isl_space_dim(bmap->dim, t);
3470 if (t == type) {
3471 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3472 0, first, off);
3473 off += first;
3474 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3475 first, n, total - bmap->n_div - n);
3476 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3477 first + n, size - (first + n), off);
3478 off += size - (first + n);
3479 } else {
3480 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3481 off += size;
3484 isl_dim_map_div(dim_map, bmap, off + n);
3486 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3487 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3488 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3489 return res;
3492 /* Insert "n" rows in the divs of "bmap".
3494 * The number of columns is not changed, which means that the last
3495 * dimensions of "bmap" are being reintepreted as the new divs.
3496 * The space of "bmap" is not adjusted, however, which means
3497 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3498 * from the space of "bmap" is the responsibility of the caller.
3500 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3501 int n)
3503 int i;
3504 size_t row_size;
3505 isl_int **new_div;
3506 isl_int *old;
3508 bmap = isl_basic_map_cow(bmap);
3509 if (!bmap)
3510 return NULL;
3512 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3513 old = bmap->block2.data;
3514 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3515 (bmap->extra + n) * (1 + row_size));
3516 if (!bmap->block2.data)
3517 return isl_basic_map_free(bmap);
3518 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3519 if (!new_div)
3520 return isl_basic_map_free(bmap);
3521 for (i = 0; i < n; ++i) {
3522 new_div[i] = bmap->block2.data +
3523 (bmap->extra + i) * (1 + row_size);
3524 isl_seq_clr(new_div[i], 1 + row_size);
3526 for (i = 0; i < bmap->extra; ++i)
3527 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3528 free(bmap->div);
3529 bmap->div = new_div;
3530 bmap->n_div += n;
3531 bmap->extra += n;
3533 return bmap;
3536 /* Drop constraints from "bmap" that only involve the variables
3537 * of "type" in the range [first, first + n] that are not related
3538 * to any of the variables outside that interval.
3539 * These constraints cannot influence the values for the variables
3540 * outside the interval, except in case they cause "bmap" to be empty.
3541 * Only drop the constraints if "bmap" is known to be non-empty.
3543 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3544 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3545 unsigned first, unsigned n)
3547 int i;
3548 int *groups;
3549 unsigned dim, n_div;
3550 isl_bool non_empty;
3552 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3553 if (non_empty < 0)
3554 return isl_basic_map_free(bmap);
3555 if (!non_empty)
3556 return bmap;
3558 dim = isl_basic_map_dim(bmap, isl_dim_all);
3559 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3560 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3561 if (!groups)
3562 return isl_basic_map_free(bmap);
3563 first += isl_basic_map_offset(bmap, type) - 1;
3564 for (i = 0; i < first; ++i)
3565 groups[i] = -1;
3566 for (i = first + n; i < dim - n_div; ++i)
3567 groups[i] = -1;
3569 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3571 return bmap;
3574 /* Turn the n dimensions of type type, starting at first
3575 * into existentially quantified variables.
3577 * If a subset of the projected out variables are unrelated
3578 * to any of the variables that remain, then the constraints
3579 * involving this subset are simply dropped first.
3581 __isl_give isl_basic_map *isl_basic_map_project_out(
3582 __isl_take isl_basic_map *bmap,
3583 enum isl_dim_type type, unsigned first, unsigned n)
3585 if (n == 0)
3586 return basic_map_space_reset(bmap, type);
3587 if (type == isl_dim_div)
3588 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3589 "cannot project out existentially quantified variables",
3590 return isl_basic_map_free(bmap));
3592 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3593 if (!bmap)
3594 return NULL;
3596 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3597 return isl_basic_map_remove_dims(bmap, type, first, n);
3599 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3600 goto error);
3602 bmap = move_last(bmap, type, first, n);
3603 bmap = isl_basic_map_cow(bmap);
3604 bmap = insert_div_rows(bmap, n);
3605 if (!bmap)
3606 return NULL;
3608 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3609 if (!bmap->dim)
3610 goto error;
3611 bmap = isl_basic_map_simplify(bmap);
3612 bmap = isl_basic_map_drop_redundant_divs(bmap);
3613 return isl_basic_map_finalize(bmap);
3614 error:
3615 isl_basic_map_free(bmap);
3616 return NULL;
3619 /* Turn the n dimensions of type type, starting at first
3620 * into existentially quantified variables.
3622 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3623 enum isl_dim_type type, unsigned first, unsigned n)
3625 return (isl_basic_set *)isl_basic_map_project_out(
3626 (isl_basic_map *)bset, type, first, n);
3629 /* Turn the n dimensions of type type, starting at first
3630 * into existentially quantified variables.
3632 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3633 enum isl_dim_type type, unsigned first, unsigned n)
3635 int i;
3637 if (!map)
3638 return NULL;
3640 if (n == 0)
3641 return map_space_reset(map, type);
3643 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3645 map = isl_map_cow(map);
3646 if (!map)
3647 return NULL;
3649 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3650 if (!map->dim)
3651 goto error;
3653 for (i = 0; i < map->n; ++i) {
3654 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3655 if (!map->p[i])
3656 goto error;
3659 return map;
3660 error:
3661 isl_map_free(map);
3662 return NULL;
3665 /* Turn the n dimensions of type type, starting at first
3666 * into existentially quantified variables.
3668 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3669 enum isl_dim_type type, unsigned first, unsigned n)
3671 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3674 /* Return a map that projects the elements in "set" onto their
3675 * "n" set dimensions starting at "first".
3676 * "type" should be equal to isl_dim_set.
3678 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3679 enum isl_dim_type type, unsigned first, unsigned n)
3681 int i;
3682 int dim;
3683 isl_map *map;
3685 if (!set)
3686 return NULL;
3687 if (type != isl_dim_set)
3688 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3689 "only set dimensions can be projected out", goto error);
3690 dim = isl_set_dim(set, isl_dim_set);
3691 if (first + n > dim || first + n < first)
3692 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3693 "index out of bounds", goto error);
3695 map = isl_map_from_domain(set);
3696 map = isl_map_add_dims(map, isl_dim_out, n);
3697 for (i = 0; i < n; ++i)
3698 map = isl_map_equate(map, isl_dim_in, first + i,
3699 isl_dim_out, i);
3700 return map;
3701 error:
3702 isl_set_free(set);
3703 return NULL;
3706 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3708 int i, j;
3710 for (i = 0; i < n; ++i) {
3711 j = isl_basic_map_alloc_div(bmap);
3712 if (j < 0)
3713 goto error;
3714 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3716 return bmap;
3717 error:
3718 isl_basic_map_free(bmap);
3719 return NULL;
3722 struct isl_basic_map *isl_basic_map_apply_range(
3723 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3725 isl_space *dim_result = NULL;
3726 struct isl_basic_map *bmap;
3727 unsigned n_in, n_out, n, nparam, total, pos;
3728 struct isl_dim_map *dim_map1, *dim_map2;
3730 if (!bmap1 || !bmap2)
3731 goto error;
3732 if (!isl_space_match(bmap1->dim, isl_dim_param,
3733 bmap2->dim, isl_dim_param))
3734 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3735 "parameters don't match", goto error);
3736 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3737 bmap2->dim, isl_dim_in))
3738 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3739 "spaces don't match", goto error);
3741 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3742 isl_space_copy(bmap2->dim));
3744 n_in = isl_basic_map_n_in(bmap1);
3745 n_out = isl_basic_map_n_out(bmap2);
3746 n = isl_basic_map_n_out(bmap1);
3747 nparam = isl_basic_map_n_param(bmap1);
3749 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3750 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3751 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3752 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3753 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3754 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3755 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3756 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3757 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3758 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3759 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3761 bmap = isl_basic_map_alloc_space(dim_result,
3762 bmap1->n_div + bmap2->n_div + n,
3763 bmap1->n_eq + bmap2->n_eq,
3764 bmap1->n_ineq + bmap2->n_ineq);
3765 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3766 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3767 bmap = add_divs(bmap, n);
3768 bmap = isl_basic_map_simplify(bmap);
3769 bmap = isl_basic_map_drop_redundant_divs(bmap);
3770 return isl_basic_map_finalize(bmap);
3771 error:
3772 isl_basic_map_free(bmap1);
3773 isl_basic_map_free(bmap2);
3774 return NULL;
3777 struct isl_basic_set *isl_basic_set_apply(
3778 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3780 if (!bset || !bmap)
3781 goto error;
3783 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3784 goto error);
3786 return (struct isl_basic_set *)
3787 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3788 error:
3789 isl_basic_set_free(bset);
3790 isl_basic_map_free(bmap);
3791 return NULL;
3794 struct isl_basic_map *isl_basic_map_apply_domain(
3795 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3797 if (!bmap1 || !bmap2)
3798 goto error;
3800 isl_assert(bmap1->ctx,
3801 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3802 isl_assert(bmap1->ctx,
3803 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3804 goto error);
3806 bmap1 = isl_basic_map_reverse(bmap1);
3807 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3808 return isl_basic_map_reverse(bmap1);
3809 error:
3810 isl_basic_map_free(bmap1);
3811 isl_basic_map_free(bmap2);
3812 return NULL;
3815 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3816 * A \cap B -> f(A) + f(B)
3818 struct isl_basic_map *isl_basic_map_sum(
3819 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3821 unsigned n_in, n_out, nparam, total, pos;
3822 struct isl_basic_map *bmap = NULL;
3823 struct isl_dim_map *dim_map1, *dim_map2;
3824 int i;
3826 if (!bmap1 || !bmap2)
3827 goto error;
3829 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3830 goto error);
3832 nparam = isl_basic_map_n_param(bmap1);
3833 n_in = isl_basic_map_n_in(bmap1);
3834 n_out = isl_basic_map_n_out(bmap1);
3836 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3837 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3838 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3839 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3840 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3841 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3842 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3843 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3844 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3845 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3846 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3848 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3849 bmap1->n_div + bmap2->n_div + 2 * n_out,
3850 bmap1->n_eq + bmap2->n_eq + n_out,
3851 bmap1->n_ineq + bmap2->n_ineq);
3852 for (i = 0; i < n_out; ++i) {
3853 int j = isl_basic_map_alloc_equality(bmap);
3854 if (j < 0)
3855 goto error;
3856 isl_seq_clr(bmap->eq[j], 1+total);
3857 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3858 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3859 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3861 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3862 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3863 bmap = add_divs(bmap, 2 * n_out);
3865 bmap = isl_basic_map_simplify(bmap);
3866 return isl_basic_map_finalize(bmap);
3867 error:
3868 isl_basic_map_free(bmap);
3869 isl_basic_map_free(bmap1);
3870 isl_basic_map_free(bmap2);
3871 return NULL;
3874 /* Given two maps A -> f(A) and B -> g(B), construct a map
3875 * A \cap B -> f(A) + f(B)
3877 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3879 struct isl_map *result;
3880 int i, j;
3882 if (!map1 || !map2)
3883 goto error;
3885 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3887 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3888 map1->n * map2->n, 0);
3889 if (!result)
3890 goto error;
3891 for (i = 0; i < map1->n; ++i)
3892 for (j = 0; j < map2->n; ++j) {
3893 struct isl_basic_map *part;
3894 part = isl_basic_map_sum(
3895 isl_basic_map_copy(map1->p[i]),
3896 isl_basic_map_copy(map2->p[j]));
3897 if (isl_basic_map_is_empty(part))
3898 isl_basic_map_free(part);
3899 else
3900 result = isl_map_add_basic_map(result, part);
3901 if (!result)
3902 goto error;
3904 isl_map_free(map1);
3905 isl_map_free(map2);
3906 return result;
3907 error:
3908 isl_map_free(map1);
3909 isl_map_free(map2);
3910 return NULL;
3913 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3914 __isl_take isl_set *set2)
3916 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3919 /* Given a basic map A -> f(A), construct A -> -f(A).
3921 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3923 int i, j;
3924 unsigned off, n;
3926 bmap = isl_basic_map_cow(bmap);
3927 if (!bmap)
3928 return NULL;
3930 n = isl_basic_map_dim(bmap, isl_dim_out);
3931 off = isl_basic_map_offset(bmap, isl_dim_out);
3932 for (i = 0; i < bmap->n_eq; ++i)
3933 for (j = 0; j < n; ++j)
3934 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3935 for (i = 0; i < bmap->n_ineq; ++i)
3936 for (j = 0; j < n; ++j)
3937 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3938 for (i = 0; i < bmap->n_div; ++i)
3939 for (j = 0; j < n; ++j)
3940 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3941 bmap = isl_basic_map_gauss(bmap, NULL);
3942 return isl_basic_map_finalize(bmap);
3945 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3947 return isl_basic_map_neg(bset);
3950 /* Given a map A -> f(A), construct A -> -f(A).
3952 struct isl_map *isl_map_neg(struct isl_map *map)
3954 int i;
3956 map = isl_map_cow(map);
3957 if (!map)
3958 return NULL;
3960 for (i = 0; i < map->n; ++i) {
3961 map->p[i] = isl_basic_map_neg(map->p[i]);
3962 if (!map->p[i])
3963 goto error;
3966 return map;
3967 error:
3968 isl_map_free(map);
3969 return NULL;
3972 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3974 return (isl_set *)isl_map_neg((isl_map *)set);
3977 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3978 * A -> floor(f(A)/d).
3980 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3981 isl_int d)
3983 unsigned n_in, n_out, nparam, total, pos;
3984 struct isl_basic_map *result = NULL;
3985 struct isl_dim_map *dim_map;
3986 int i;
3988 if (!bmap)
3989 return NULL;
3991 nparam = isl_basic_map_n_param(bmap);
3992 n_in = isl_basic_map_n_in(bmap);
3993 n_out = isl_basic_map_n_out(bmap);
3995 total = nparam + n_in + n_out + bmap->n_div + n_out;
3996 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3997 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3998 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3999 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4000 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4002 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4003 bmap->n_div + n_out,
4004 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4005 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4006 result = add_divs(result, n_out);
4007 for (i = 0; i < n_out; ++i) {
4008 int j;
4009 j = isl_basic_map_alloc_inequality(result);
4010 if (j < 0)
4011 goto error;
4012 isl_seq_clr(result->ineq[j], 1+total);
4013 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4014 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4015 j = isl_basic_map_alloc_inequality(result);
4016 if (j < 0)
4017 goto error;
4018 isl_seq_clr(result->ineq[j], 1+total);
4019 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4020 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4021 isl_int_sub_ui(result->ineq[j][0], d, 1);
4024 result = isl_basic_map_simplify(result);
4025 return isl_basic_map_finalize(result);
4026 error:
4027 isl_basic_map_free(result);
4028 return NULL;
4031 /* Given a map A -> f(A) and an integer d, construct a map
4032 * A -> floor(f(A)/d).
4034 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4036 int i;
4038 map = isl_map_cow(map);
4039 if (!map)
4040 return NULL;
4042 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4043 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4044 for (i = 0; i < map->n; ++i) {
4045 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4046 if (!map->p[i])
4047 goto error;
4050 return map;
4051 error:
4052 isl_map_free(map);
4053 return NULL;
4056 /* Given a map A -> f(A) and an integer d, construct a map
4057 * A -> floor(f(A)/d).
4059 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4060 __isl_take isl_val *d)
4062 if (!map || !d)
4063 goto error;
4064 if (!isl_val_is_int(d))
4065 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4066 "expecting integer denominator", goto error);
4067 map = isl_map_floordiv(map, d->n);
4068 isl_val_free(d);
4069 return map;
4070 error:
4071 isl_map_free(map);
4072 isl_val_free(d);
4073 return NULL;
4076 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4078 int i;
4079 unsigned nparam;
4080 unsigned n_in;
4082 i = isl_basic_map_alloc_equality(bmap);
4083 if (i < 0)
4084 goto error;
4085 nparam = isl_basic_map_n_param(bmap);
4086 n_in = isl_basic_map_n_in(bmap);
4087 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4088 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4089 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4090 return isl_basic_map_finalize(bmap);
4091 error:
4092 isl_basic_map_free(bmap);
4093 return NULL;
4096 /* Add a constraints to "bmap" expressing i_pos < o_pos
4098 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4100 int i;
4101 unsigned nparam;
4102 unsigned n_in;
4104 i = isl_basic_map_alloc_inequality(bmap);
4105 if (i < 0)
4106 goto error;
4107 nparam = isl_basic_map_n_param(bmap);
4108 n_in = isl_basic_map_n_in(bmap);
4109 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4110 isl_int_set_si(bmap->ineq[i][0], -1);
4111 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4112 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4113 return isl_basic_map_finalize(bmap);
4114 error:
4115 isl_basic_map_free(bmap);
4116 return NULL;
4119 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4121 static __isl_give isl_basic_map *var_less_or_equal(
4122 __isl_take isl_basic_map *bmap, unsigned pos)
4124 int i;
4125 unsigned nparam;
4126 unsigned n_in;
4128 i = isl_basic_map_alloc_inequality(bmap);
4129 if (i < 0)
4130 goto error;
4131 nparam = isl_basic_map_n_param(bmap);
4132 n_in = isl_basic_map_n_in(bmap);
4133 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4134 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4135 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4136 return isl_basic_map_finalize(bmap);
4137 error:
4138 isl_basic_map_free(bmap);
4139 return NULL;
4142 /* Add a constraints to "bmap" expressing i_pos > o_pos
4144 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4146 int i;
4147 unsigned nparam;
4148 unsigned n_in;
4150 i = isl_basic_map_alloc_inequality(bmap);
4151 if (i < 0)
4152 goto error;
4153 nparam = isl_basic_map_n_param(bmap);
4154 n_in = isl_basic_map_n_in(bmap);
4155 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4156 isl_int_set_si(bmap->ineq[i][0], -1);
4157 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4158 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4159 return isl_basic_map_finalize(bmap);
4160 error:
4161 isl_basic_map_free(bmap);
4162 return NULL;
4165 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4167 static __isl_give isl_basic_map *var_more_or_equal(
4168 __isl_take isl_basic_map *bmap, unsigned pos)
4170 int i;
4171 unsigned nparam;
4172 unsigned n_in;
4174 i = isl_basic_map_alloc_inequality(bmap);
4175 if (i < 0)
4176 goto error;
4177 nparam = isl_basic_map_n_param(bmap);
4178 n_in = isl_basic_map_n_in(bmap);
4179 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4180 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4181 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4182 return isl_basic_map_finalize(bmap);
4183 error:
4184 isl_basic_map_free(bmap);
4185 return NULL;
4188 __isl_give isl_basic_map *isl_basic_map_equal(
4189 __isl_take isl_space *dim, unsigned n_equal)
4191 int i;
4192 struct isl_basic_map *bmap;
4193 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4194 if (!bmap)
4195 return NULL;
4196 for (i = 0; i < n_equal && bmap; ++i)
4197 bmap = var_equal(bmap, i);
4198 return isl_basic_map_finalize(bmap);
4201 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4203 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4204 unsigned pos)
4206 int i;
4207 struct isl_basic_map *bmap;
4208 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4209 if (!bmap)
4210 return NULL;
4211 for (i = 0; i < pos && bmap; ++i)
4212 bmap = var_equal(bmap, i);
4213 if (bmap)
4214 bmap = var_less(bmap, pos);
4215 return isl_basic_map_finalize(bmap);
4218 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4220 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4221 __isl_take isl_space *dim, unsigned pos)
4223 int i;
4224 isl_basic_map *bmap;
4226 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4227 for (i = 0; i < pos; ++i)
4228 bmap = var_equal(bmap, i);
4229 bmap = var_less_or_equal(bmap, pos);
4230 return isl_basic_map_finalize(bmap);
4233 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4235 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4236 unsigned pos)
4238 int i;
4239 struct isl_basic_map *bmap;
4240 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4241 if (!bmap)
4242 return NULL;
4243 for (i = 0; i < pos && bmap; ++i)
4244 bmap = var_equal(bmap, i);
4245 if (bmap)
4246 bmap = var_more(bmap, pos);
4247 return isl_basic_map_finalize(bmap);
4250 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4252 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4253 __isl_take isl_space *dim, unsigned pos)
4255 int i;
4256 isl_basic_map *bmap;
4258 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4259 for (i = 0; i < pos; ++i)
4260 bmap = var_equal(bmap, i);
4261 bmap = var_more_or_equal(bmap, pos);
4262 return isl_basic_map_finalize(bmap);
4265 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4266 unsigned n, int equal)
4268 struct isl_map *map;
4269 int i;
4271 if (n == 0 && equal)
4272 return isl_map_universe(dims);
4274 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4276 for (i = 0; i + 1 < n; ++i)
4277 map = isl_map_add_basic_map(map,
4278 isl_basic_map_less_at(isl_space_copy(dims), i));
4279 if (n > 0) {
4280 if (equal)
4281 map = isl_map_add_basic_map(map,
4282 isl_basic_map_less_or_equal_at(dims, n - 1));
4283 else
4284 map = isl_map_add_basic_map(map,
4285 isl_basic_map_less_at(dims, n - 1));
4286 } else
4287 isl_space_free(dims);
4289 return map;
4292 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4294 if (!dims)
4295 return NULL;
4296 return map_lex_lte_first(dims, dims->n_out, equal);
4299 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4301 return map_lex_lte_first(dim, n, 0);
4304 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4306 return map_lex_lte_first(dim, n, 1);
4309 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4311 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4314 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4316 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4319 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4320 unsigned n, int equal)
4322 struct isl_map *map;
4323 int i;
4325 if (n == 0 && equal)
4326 return isl_map_universe(dims);
4328 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4330 for (i = 0; i + 1 < n; ++i)
4331 map = isl_map_add_basic_map(map,
4332 isl_basic_map_more_at(isl_space_copy(dims), i));
4333 if (n > 0) {
4334 if (equal)
4335 map = isl_map_add_basic_map(map,
4336 isl_basic_map_more_or_equal_at(dims, n - 1));
4337 else
4338 map = isl_map_add_basic_map(map,
4339 isl_basic_map_more_at(dims, n - 1));
4340 } else
4341 isl_space_free(dims);
4343 return map;
4346 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4348 if (!dims)
4349 return NULL;
4350 return map_lex_gte_first(dims, dims->n_out, equal);
4353 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4355 return map_lex_gte_first(dim, n, 0);
4358 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4360 return map_lex_gte_first(dim, n, 1);
4363 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4365 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4368 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4370 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4373 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4374 __isl_take isl_set *set2)
4376 isl_map *map;
4377 map = isl_map_lex_le(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_set_lex_lt_set(__isl_take isl_set *set1,
4384 __isl_take isl_set *set2)
4386 isl_map *map;
4387 map = isl_map_lex_lt(isl_set_get_space(set1));
4388 map = isl_map_intersect_domain(map, set1);
4389 map = isl_map_intersect_range(map, set2);
4390 return map;
4393 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4394 __isl_take isl_set *set2)
4396 isl_map *map;
4397 map = isl_map_lex_ge(isl_set_get_space(set1));
4398 map = isl_map_intersect_domain(map, set1);
4399 map = isl_map_intersect_range(map, set2);
4400 return map;
4403 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4404 __isl_take isl_set *set2)
4406 isl_map *map;
4407 map = isl_map_lex_gt(isl_set_get_space(set1));
4408 map = isl_map_intersect_domain(map, set1);
4409 map = isl_map_intersect_range(map, set2);
4410 return map;
4413 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4414 __isl_take isl_map *map2)
4416 isl_map *map;
4417 map = isl_map_lex_le(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_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4424 __isl_take isl_map *map2)
4426 isl_map *map;
4427 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4428 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4429 map = isl_map_apply_range(map, isl_map_reverse(map2));
4430 return map;
4433 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4434 __isl_take isl_map *map2)
4436 isl_map *map;
4437 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4438 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4439 map = isl_map_apply_range(map, isl_map_reverse(map2));
4440 return map;
4443 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4444 __isl_take isl_map *map2)
4446 isl_map *map;
4447 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4448 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4449 map = isl_map_apply_range(map, isl_map_reverse(map2));
4450 return map;
4453 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4454 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4456 struct isl_basic_map *bmap;
4458 bset = isl_basic_set_cow(bset);
4459 if (!bset || !dim)
4460 goto error;
4462 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4463 isl_space_free(bset->dim);
4464 bmap = (struct isl_basic_map *) bset;
4465 bmap->dim = dim;
4466 return isl_basic_map_finalize(bmap);
4467 error:
4468 isl_basic_set_free(bset);
4469 isl_space_free(dim);
4470 return NULL;
4473 /* For a div d = floor(f/m), add the constraint
4475 * f - m d >= 0
4477 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4478 unsigned pos, isl_int *div)
4480 int i;
4481 unsigned total = isl_basic_map_total_dim(bmap);
4483 i = isl_basic_map_alloc_inequality(bmap);
4484 if (i < 0)
4485 return -1;
4486 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4487 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4489 return 0;
4492 /* For a div d = floor(f/m), add the constraint
4494 * -(f-(m-1)) + m d >= 0
4496 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4497 unsigned pos, isl_int *div)
4499 int i;
4500 unsigned total = isl_basic_map_total_dim(bmap);
4502 i = isl_basic_map_alloc_inequality(bmap);
4503 if (i < 0)
4504 return -1;
4505 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4506 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4507 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4508 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4510 return 0;
4513 /* For a div d = floor(f/m), add the constraints
4515 * f - m d >= 0
4516 * -(f-(m-1)) + m d >= 0
4518 * Note that the second constraint is the negation of
4520 * f - m d >= m
4522 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4523 unsigned pos, isl_int *div)
4525 if (add_upper_div_constraint(bmap, pos, div) < 0)
4526 return -1;
4527 if (add_lower_div_constraint(bmap, pos, div) < 0)
4528 return -1;
4529 return 0;
4532 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4533 unsigned pos, isl_int *div)
4535 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4536 pos, div);
4539 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4541 unsigned total = isl_basic_map_total_dim(bmap);
4542 unsigned div_pos = total - bmap->n_div + div;
4544 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4545 bmap->div[div]);
4548 /* For each known div d = floor(f/m), add the constraints
4550 * f - m d >= 0
4551 * -(f-(m-1)) + m d >= 0
4553 * Remove duplicate constraints in case of some these div constraints
4554 * already appear in "bmap".
4556 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4557 __isl_take isl_basic_map *bmap)
4559 unsigned n_div;
4561 if (!bmap)
4562 return NULL;
4563 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4564 if (n_div == 0)
4565 return bmap;
4567 bmap = add_known_div_constraints(bmap);
4568 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4569 bmap = isl_basic_map_finalize(bmap);
4570 return bmap;
4573 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4575 * In particular, if this div is of the form d = floor(f/m),
4576 * then add the constraint
4578 * f - m d >= 0
4580 * if sign < 0 or the constraint
4582 * -(f-(m-1)) + m d >= 0
4584 * if sign > 0.
4586 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4587 unsigned div, int sign)
4589 unsigned total;
4590 unsigned div_pos;
4592 if (!bmap)
4593 return -1;
4595 total = isl_basic_map_total_dim(bmap);
4596 div_pos = total - bmap->n_div + div;
4598 if (sign < 0)
4599 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4600 else
4601 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4604 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4606 return isl_basic_map_add_div_constraints(bset, div);
4609 struct isl_basic_set *isl_basic_map_underlying_set(
4610 struct isl_basic_map *bmap)
4612 if (!bmap)
4613 goto error;
4614 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4615 bmap->n_div == 0 &&
4616 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4617 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4618 return (struct isl_basic_set *)bmap;
4619 bmap = isl_basic_map_cow(bmap);
4620 if (!bmap)
4621 goto error;
4622 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4623 if (!bmap->dim)
4624 goto error;
4625 bmap->extra -= bmap->n_div;
4626 bmap->n_div = 0;
4627 bmap = isl_basic_map_finalize(bmap);
4628 return (struct isl_basic_set *)bmap;
4629 error:
4630 isl_basic_map_free(bmap);
4631 return NULL;
4634 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4635 __isl_take isl_basic_set *bset)
4637 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4640 /* Replace each element in "list" by the result of applying
4641 * isl_basic_map_underlying_set to the element.
4643 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4644 __isl_take isl_basic_map_list *list)
4646 int i, n;
4648 if (!list)
4649 return NULL;
4651 n = isl_basic_map_list_n_basic_map(list);
4652 for (i = 0; i < n; ++i) {
4653 isl_basic_map *bmap;
4654 isl_basic_set *bset;
4656 bmap = isl_basic_map_list_get_basic_map(list, i);
4657 bset = isl_basic_set_underlying_set(bmap);
4658 list = isl_basic_set_list_set_basic_set(list, i, bset);
4661 return list;
4664 struct isl_basic_map *isl_basic_map_overlying_set(
4665 struct isl_basic_set *bset, struct isl_basic_map *like)
4667 struct isl_basic_map *bmap;
4668 struct isl_ctx *ctx;
4669 unsigned total;
4670 int i;
4672 if (!bset || !like)
4673 goto error;
4674 ctx = bset->ctx;
4675 isl_assert(ctx, bset->n_div == 0, goto error);
4676 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4677 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4678 goto error);
4679 if (like->n_div == 0) {
4680 isl_space *space = isl_basic_map_get_space(like);
4681 isl_basic_map_free(like);
4682 return isl_basic_map_reset_space(bset, space);
4684 bset = isl_basic_set_cow(bset);
4685 if (!bset)
4686 goto error;
4687 total = bset->dim->n_out + bset->extra;
4688 bmap = (struct isl_basic_map *)bset;
4689 isl_space_free(bmap->dim);
4690 bmap->dim = isl_space_copy(like->dim);
4691 if (!bmap->dim)
4692 goto error;
4693 bmap->n_div = like->n_div;
4694 bmap->extra += like->n_div;
4695 if (bmap->extra) {
4696 unsigned ltotal;
4697 isl_int **div;
4698 ltotal = total - bmap->extra + like->extra;
4699 if (ltotal > total)
4700 ltotal = total;
4701 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4702 bmap->extra * (1 + 1 + total));
4703 if (isl_blk_is_error(bmap->block2))
4704 goto error;
4705 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4706 if (!div)
4707 goto error;
4708 bmap->div = div;
4709 for (i = 0; i < bmap->extra; ++i)
4710 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4711 for (i = 0; i < like->n_div; ++i) {
4712 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4713 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4715 bmap = isl_basic_map_add_known_div_constraints(bmap);
4717 isl_basic_map_free(like);
4718 bmap = isl_basic_map_simplify(bmap);
4719 bmap = isl_basic_map_finalize(bmap);
4720 return bmap;
4721 error:
4722 isl_basic_map_free(like);
4723 isl_basic_set_free(bset);
4724 return NULL;
4727 struct isl_basic_set *isl_basic_set_from_underlying_set(
4728 struct isl_basic_set *bset, struct isl_basic_set *like)
4730 return (struct isl_basic_set *)
4731 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4734 struct isl_set *isl_set_from_underlying_set(
4735 struct isl_set *set, struct isl_basic_set *like)
4737 int i;
4739 if (!set || !like)
4740 goto error;
4741 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4742 goto error);
4743 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4744 isl_basic_set_free(like);
4745 return set;
4747 set = isl_set_cow(set);
4748 if (!set)
4749 goto error;
4750 for (i = 0; i < set->n; ++i) {
4751 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4752 isl_basic_set_copy(like));
4753 if (!set->p[i])
4754 goto error;
4756 isl_space_free(set->dim);
4757 set->dim = isl_space_copy(like->dim);
4758 if (!set->dim)
4759 goto error;
4760 isl_basic_set_free(like);
4761 return set;
4762 error:
4763 isl_basic_set_free(like);
4764 isl_set_free(set);
4765 return NULL;
4768 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4770 int i;
4772 map = isl_map_cow(map);
4773 if (!map)
4774 return NULL;
4775 map->dim = isl_space_cow(map->dim);
4776 if (!map->dim)
4777 goto error;
4779 for (i = 1; i < map->n; ++i)
4780 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4781 goto error);
4782 for (i = 0; i < map->n; ++i) {
4783 map->p[i] = (struct isl_basic_map *)
4784 isl_basic_map_underlying_set(map->p[i]);
4785 if (!map->p[i])
4786 goto error;
4788 if (map->n == 0)
4789 map->dim = isl_space_underlying(map->dim, 0);
4790 else {
4791 isl_space_free(map->dim);
4792 map->dim = isl_space_copy(map->p[0]->dim);
4794 if (!map->dim)
4795 goto error;
4796 return (struct isl_set *)map;
4797 error:
4798 isl_map_free(map);
4799 return NULL;
4802 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4804 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4807 /* Replace the space of "bmap" by "space".
4809 * If the space of "bmap" is identical to "space" (including the identifiers
4810 * of the input and output dimensions), then simply return the original input.
4812 __isl_give isl_basic_map *isl_basic_map_reset_space(
4813 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4815 isl_bool equal;
4817 if (!bmap)
4818 goto error;
4819 equal = isl_space_is_equal(bmap->dim, space);
4820 if (equal >= 0 && equal)
4821 equal = isl_space_match(bmap->dim, isl_dim_in,
4822 space, isl_dim_in);
4823 if (equal >= 0 && equal)
4824 equal = isl_space_match(bmap->dim, isl_dim_out,
4825 space, isl_dim_out);
4826 if (equal < 0)
4827 goto error;
4828 if (equal) {
4829 isl_space_free(space);
4830 return bmap;
4832 bmap = isl_basic_map_cow(bmap);
4833 if (!bmap || !space)
4834 goto error;
4836 isl_space_free(bmap->dim);
4837 bmap->dim = space;
4839 bmap = isl_basic_map_finalize(bmap);
4841 return bmap;
4842 error:
4843 isl_basic_map_free(bmap);
4844 isl_space_free(space);
4845 return NULL;
4848 __isl_give isl_basic_set *isl_basic_set_reset_space(
4849 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4851 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4852 dim);
4855 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4856 __isl_take isl_space *dim)
4858 int i;
4860 map = isl_map_cow(map);
4861 if (!map || !dim)
4862 goto error;
4864 for (i = 0; i < map->n; ++i) {
4865 map->p[i] = isl_basic_map_reset_space(map->p[i],
4866 isl_space_copy(dim));
4867 if (!map->p[i])
4868 goto error;
4870 isl_space_free(map->dim);
4871 map->dim = dim;
4873 return map;
4874 error:
4875 isl_map_free(map);
4876 isl_space_free(dim);
4877 return NULL;
4880 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4881 __isl_take isl_space *dim)
4883 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4886 /* Compute the parameter domain of the given basic set.
4888 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4890 isl_space *space;
4891 unsigned n;
4893 if (isl_basic_set_is_params(bset))
4894 return bset;
4896 n = isl_basic_set_dim(bset, isl_dim_set);
4897 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4898 space = isl_basic_set_get_space(bset);
4899 space = isl_space_params(space);
4900 bset = isl_basic_set_reset_space(bset, space);
4901 return bset;
4904 /* Construct a zero-dimensional basic set with the given parameter domain.
4906 __isl_give isl_basic_set *isl_basic_set_from_params(
4907 __isl_take isl_basic_set *bset)
4909 isl_space *space;
4910 space = isl_basic_set_get_space(bset);
4911 space = isl_space_set_from_params(space);
4912 bset = isl_basic_set_reset_space(bset, space);
4913 return bset;
4916 /* Compute the parameter domain of the given set.
4918 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4920 isl_space *space;
4921 unsigned n;
4923 if (isl_set_is_params(set))
4924 return set;
4926 n = isl_set_dim(set, isl_dim_set);
4927 set = isl_set_project_out(set, isl_dim_set, 0, n);
4928 space = isl_set_get_space(set);
4929 space = isl_space_params(space);
4930 set = isl_set_reset_space(set, space);
4931 return set;
4934 /* Construct a zero-dimensional set with the given parameter domain.
4936 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4938 isl_space *space;
4939 space = isl_set_get_space(set);
4940 space = isl_space_set_from_params(space);
4941 set = isl_set_reset_space(set, space);
4942 return set;
4945 /* Compute the parameter domain of the given map.
4947 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4949 isl_space *space;
4950 unsigned n;
4952 n = isl_map_dim(map, isl_dim_in);
4953 map = isl_map_project_out(map, isl_dim_in, 0, n);
4954 n = isl_map_dim(map, isl_dim_out);
4955 map = isl_map_project_out(map, isl_dim_out, 0, n);
4956 space = isl_map_get_space(map);
4957 space = isl_space_params(space);
4958 map = isl_map_reset_space(map, space);
4959 return map;
4962 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4964 isl_space *space;
4965 unsigned n_out;
4967 if (!bmap)
4968 return NULL;
4969 space = isl_space_domain(isl_basic_map_get_space(bmap));
4971 n_out = isl_basic_map_n_out(bmap);
4972 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
4974 return isl_basic_map_reset_space(bmap, space);
4977 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4979 if (!bmap)
4980 return -1;
4981 return isl_space_may_be_set(bmap->dim);
4984 /* Is this basic map actually a set?
4985 * Users should never call this function. Outside of isl,
4986 * the type should indicate whether something is a set or a map.
4988 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4990 if (!bmap)
4991 return -1;
4992 return isl_space_is_set(bmap->dim);
4995 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4997 if (!bmap)
4998 return NULL;
4999 if (isl_basic_map_is_set(bmap))
5000 return bmap;
5001 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5004 __isl_give isl_basic_map *isl_basic_map_domain_map(
5005 __isl_take isl_basic_map *bmap)
5007 int i, k;
5008 isl_space *dim;
5009 isl_basic_map *domain;
5010 int nparam, n_in, n_out;
5011 unsigned total;
5013 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5014 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5015 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5017 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5018 domain = isl_basic_map_universe(dim);
5020 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5021 bmap = isl_basic_map_apply_range(bmap, domain);
5022 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5024 total = isl_basic_map_total_dim(bmap);
5026 for (i = 0; i < n_in; ++i) {
5027 k = isl_basic_map_alloc_equality(bmap);
5028 if (k < 0)
5029 goto error;
5030 isl_seq_clr(bmap->eq[k], 1 + total);
5031 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5032 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5035 bmap = isl_basic_map_gauss(bmap, NULL);
5036 return isl_basic_map_finalize(bmap);
5037 error:
5038 isl_basic_map_free(bmap);
5039 return NULL;
5042 __isl_give isl_basic_map *isl_basic_map_range_map(
5043 __isl_take isl_basic_map *bmap)
5045 int i, k;
5046 isl_space *dim;
5047 isl_basic_map *range;
5048 int nparam, n_in, n_out;
5049 unsigned total;
5051 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5052 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5053 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5055 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5056 range = isl_basic_map_universe(dim);
5058 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5059 bmap = isl_basic_map_apply_range(bmap, range);
5060 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5062 total = isl_basic_map_total_dim(bmap);
5064 for (i = 0; i < n_out; ++i) {
5065 k = isl_basic_map_alloc_equality(bmap);
5066 if (k < 0)
5067 goto error;
5068 isl_seq_clr(bmap->eq[k], 1 + total);
5069 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5070 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5073 bmap = isl_basic_map_gauss(bmap, NULL);
5074 return isl_basic_map_finalize(bmap);
5075 error:
5076 isl_basic_map_free(bmap);
5077 return NULL;
5080 int isl_map_may_be_set(__isl_keep isl_map *map)
5082 if (!map)
5083 return -1;
5084 return isl_space_may_be_set(map->dim);
5087 /* Is this map actually a set?
5088 * Users should never call this function. Outside of isl,
5089 * the type should indicate whether something is a set or a map.
5091 int isl_map_is_set(__isl_keep isl_map *map)
5093 if (!map)
5094 return -1;
5095 return isl_space_is_set(map->dim);
5098 struct isl_set *isl_map_range(struct isl_map *map)
5100 int i;
5101 struct isl_set *set;
5103 if (!map)
5104 goto error;
5105 if (isl_map_is_set(map))
5106 return (isl_set *)map;
5108 map = isl_map_cow(map);
5109 if (!map)
5110 goto error;
5112 set = (struct isl_set *) map;
5113 set->dim = isl_space_range(set->dim);
5114 if (!set->dim)
5115 goto error;
5116 for (i = 0; i < map->n; ++i) {
5117 set->p[i] = isl_basic_map_range(map->p[i]);
5118 if (!set->p[i])
5119 goto error;
5121 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5122 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5123 return set;
5124 error:
5125 isl_map_free(map);
5126 return NULL;
5129 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5131 int i;
5133 map = isl_map_cow(map);
5134 if (!map)
5135 return NULL;
5137 map->dim = isl_space_domain_map(map->dim);
5138 if (!map->dim)
5139 goto error;
5140 for (i = 0; i < map->n; ++i) {
5141 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5142 if (!map->p[i])
5143 goto error;
5145 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5146 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5147 return map;
5148 error:
5149 isl_map_free(map);
5150 return NULL;
5153 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5155 int i;
5156 isl_space *range_dim;
5158 map = isl_map_cow(map);
5159 if (!map)
5160 return NULL;
5162 range_dim = isl_space_range(isl_map_get_space(map));
5163 range_dim = isl_space_from_range(range_dim);
5164 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5165 map->dim = isl_space_join(map->dim, range_dim);
5166 if (!map->dim)
5167 goto error;
5168 for (i = 0; i < map->n; ++i) {
5169 map->p[i] = isl_basic_map_range_map(map->p[i]);
5170 if (!map->p[i])
5171 goto error;
5173 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5174 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5175 return map;
5176 error:
5177 isl_map_free(map);
5178 return NULL;
5181 /* Given a wrapped map of the form A[B -> C],
5182 * return the map A[B -> C] -> B.
5184 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5186 isl_id *id;
5187 isl_map *map;
5189 if (!set)
5190 return NULL;
5191 if (!isl_set_has_tuple_id(set))
5192 return isl_map_domain_map(isl_set_unwrap(set));
5194 id = isl_set_get_tuple_id(set);
5195 map = isl_map_domain_map(isl_set_unwrap(set));
5196 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5198 return map;
5201 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5202 __isl_take isl_space *dim)
5204 int i;
5205 struct isl_map *map = NULL;
5207 set = isl_set_cow(set);
5208 if (!set || !dim)
5209 goto error;
5210 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5211 map = (struct isl_map *)set;
5212 for (i = 0; i < set->n; ++i) {
5213 map->p[i] = isl_basic_map_from_basic_set(
5214 set->p[i], isl_space_copy(dim));
5215 if (!map->p[i])
5216 goto error;
5218 isl_space_free(map->dim);
5219 map->dim = dim;
5220 return map;
5221 error:
5222 isl_space_free(dim);
5223 isl_set_free(set);
5224 return NULL;
5227 __isl_give isl_basic_map *isl_basic_map_from_domain(
5228 __isl_take isl_basic_set *bset)
5230 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5233 __isl_give isl_basic_map *isl_basic_map_from_range(
5234 __isl_take isl_basic_set *bset)
5236 isl_space *space;
5237 space = isl_basic_set_get_space(bset);
5238 space = isl_space_from_range(space);
5239 bset = isl_basic_set_reset_space(bset, space);
5240 return (isl_basic_map *)bset;
5243 /* Create a relation with the given set as range.
5244 * The domain of the created relation is a zero-dimensional
5245 * flat anonymous space.
5247 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5249 isl_space *space;
5250 space = isl_set_get_space(set);
5251 space = isl_space_from_range(space);
5252 set = isl_set_reset_space(set, space);
5253 return (struct isl_map *)set;
5256 /* Create a relation with the given set as domain.
5257 * The range of the created relation is a zero-dimensional
5258 * flat anonymous space.
5260 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5262 return isl_map_reverse(isl_map_from_range(set));
5265 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5266 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5268 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5271 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5272 __isl_take isl_set *range)
5274 return isl_map_apply_range(isl_map_reverse(domain), range);
5277 /* Return a newly allocated isl_map with given space and flags and
5278 * room for "n" basic maps.
5279 * Make sure that all cached information is cleared.
5281 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5282 unsigned flags)
5284 struct isl_map *map;
5286 if (!space)
5287 return NULL;
5288 if (n < 0)
5289 isl_die(space->ctx, isl_error_internal,
5290 "negative number of basic maps", goto error);
5291 map = isl_calloc(space->ctx, struct isl_map,
5292 sizeof(struct isl_map) +
5293 (n - 1) * sizeof(struct isl_basic_map *));
5294 if (!map)
5295 goto error;
5297 map->ctx = space->ctx;
5298 isl_ctx_ref(map->ctx);
5299 map->ref = 1;
5300 map->size = n;
5301 map->n = 0;
5302 map->dim = space;
5303 map->flags = flags;
5304 return map;
5305 error:
5306 isl_space_free(space);
5307 return NULL;
5310 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5311 unsigned nparam, unsigned in, unsigned out, int n,
5312 unsigned flags)
5314 struct isl_map *map;
5315 isl_space *dims;
5317 dims = isl_space_alloc(ctx, nparam, in, out);
5318 if (!dims)
5319 return NULL;
5321 map = isl_map_alloc_space(dims, n, flags);
5322 return map;
5325 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5327 struct isl_basic_map *bmap;
5328 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5329 bmap = isl_basic_map_set_to_empty(bmap);
5330 return bmap;
5333 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5335 struct isl_basic_set *bset;
5336 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5337 bset = isl_basic_set_set_to_empty(bset);
5338 return bset;
5341 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5343 struct isl_basic_map *bmap;
5344 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5345 bmap = isl_basic_map_finalize(bmap);
5346 return bmap;
5349 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5351 struct isl_basic_set *bset;
5352 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5353 bset = isl_basic_set_finalize(bset);
5354 return bset;
5357 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5359 int i;
5360 unsigned total = isl_space_dim(dim, isl_dim_all);
5361 isl_basic_map *bmap;
5363 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5364 for (i = 0; i < total; ++i) {
5365 int k = isl_basic_map_alloc_inequality(bmap);
5366 if (k < 0)
5367 goto error;
5368 isl_seq_clr(bmap->ineq[k], 1 + total);
5369 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5371 return bmap;
5372 error:
5373 isl_basic_map_free(bmap);
5374 return NULL;
5377 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5379 return isl_basic_map_nat_universe(dim);
5382 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5384 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5387 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5389 return isl_map_nat_universe(dim);
5392 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5394 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5397 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5399 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5402 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5404 struct isl_map *map;
5405 if (!dim)
5406 return NULL;
5407 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5408 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5409 return map;
5412 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5414 struct isl_set *set;
5415 if (!dim)
5416 return NULL;
5417 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5418 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5419 return set;
5422 struct isl_map *isl_map_dup(struct isl_map *map)
5424 int i;
5425 struct isl_map *dup;
5427 if (!map)
5428 return NULL;
5429 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5430 for (i = 0; i < map->n; ++i)
5431 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5432 return dup;
5435 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5436 __isl_take isl_basic_map *bmap)
5438 if (!bmap || !map)
5439 goto error;
5440 if (isl_basic_map_plain_is_empty(bmap)) {
5441 isl_basic_map_free(bmap);
5442 return map;
5444 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5445 isl_assert(map->ctx, map->n < map->size, goto error);
5446 map->p[map->n] = bmap;
5447 map->n++;
5448 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5449 return map;
5450 error:
5451 if (map)
5452 isl_map_free(map);
5453 if (bmap)
5454 isl_basic_map_free(bmap);
5455 return NULL;
5458 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5460 int i;
5462 if (!map)
5463 return NULL;
5465 if (--map->ref > 0)
5466 return NULL;
5468 clear_caches(map);
5469 isl_ctx_deref(map->ctx);
5470 for (i = 0; i < map->n; ++i)
5471 isl_basic_map_free(map->p[i]);
5472 isl_space_free(map->dim);
5473 free(map);
5475 return NULL;
5478 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5479 struct isl_basic_map *bmap, unsigned pos, int value)
5481 int j;
5483 bmap = isl_basic_map_cow(bmap);
5484 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5485 j = isl_basic_map_alloc_equality(bmap);
5486 if (j < 0)
5487 goto error;
5488 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5489 isl_int_set_si(bmap->eq[j][pos], -1);
5490 isl_int_set_si(bmap->eq[j][0], value);
5491 bmap = isl_basic_map_simplify(bmap);
5492 return isl_basic_map_finalize(bmap);
5493 error:
5494 isl_basic_map_free(bmap);
5495 return NULL;
5498 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5499 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5501 int j;
5503 bmap = isl_basic_map_cow(bmap);
5504 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5505 j = isl_basic_map_alloc_equality(bmap);
5506 if (j < 0)
5507 goto error;
5508 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5509 isl_int_set_si(bmap->eq[j][pos], -1);
5510 isl_int_set(bmap->eq[j][0], value);
5511 bmap = isl_basic_map_simplify(bmap);
5512 return isl_basic_map_finalize(bmap);
5513 error:
5514 isl_basic_map_free(bmap);
5515 return NULL;
5518 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5519 enum isl_dim_type type, unsigned pos, int value)
5521 if (!bmap)
5522 return NULL;
5523 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5524 return isl_basic_map_fix_pos_si(bmap,
5525 isl_basic_map_offset(bmap, type) + pos, value);
5526 error:
5527 isl_basic_map_free(bmap);
5528 return NULL;
5531 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5532 enum isl_dim_type type, unsigned pos, isl_int value)
5534 if (!bmap)
5535 return NULL;
5536 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5537 return isl_basic_map_fix_pos(bmap,
5538 isl_basic_map_offset(bmap, type) + pos, value);
5539 error:
5540 isl_basic_map_free(bmap);
5541 return NULL;
5544 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5545 * to be equal to "v".
5547 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5548 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5550 if (!bmap || !v)
5551 goto error;
5552 if (!isl_val_is_int(v))
5553 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5554 "expecting integer value", goto error);
5555 if (pos >= isl_basic_map_dim(bmap, type))
5556 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5557 "index out of bounds", goto error);
5558 pos += isl_basic_map_offset(bmap, type);
5559 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5560 isl_val_free(v);
5561 return bmap;
5562 error:
5563 isl_basic_map_free(bmap);
5564 isl_val_free(v);
5565 return NULL;
5568 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5569 * to be equal to "v".
5571 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5572 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5574 return isl_basic_map_fix_val(bset, type, pos, v);
5577 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5578 enum isl_dim_type type, unsigned pos, int value)
5580 return (struct isl_basic_set *)
5581 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5582 type, pos, value);
5585 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5586 enum isl_dim_type type, unsigned pos, isl_int value)
5588 return (struct isl_basic_set *)
5589 isl_basic_map_fix((struct isl_basic_map *)bset,
5590 type, pos, value);
5593 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5594 unsigned input, int value)
5596 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5599 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5600 unsigned dim, int value)
5602 return (struct isl_basic_set *)
5603 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5604 isl_dim_set, dim, value);
5607 static int remove_if_empty(__isl_keep isl_map *map, int i)
5609 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5611 if (empty < 0)
5612 return -1;
5613 if (!empty)
5614 return 0;
5616 isl_basic_map_free(map->p[i]);
5617 if (i != map->n - 1) {
5618 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5619 map->p[i] = map->p[map->n - 1];
5621 map->n--;
5623 return 0;
5626 /* Perform "fn" on each basic map of "map", where we may not be holding
5627 * the only reference to "map".
5628 * In particular, "fn" should be a semantics preserving operation
5629 * that we want to apply to all copies of "map". We therefore need
5630 * to be careful not to modify "map" in a way that breaks "map"
5631 * in case anything goes wrong.
5633 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5634 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5636 struct isl_basic_map *bmap;
5637 int i;
5639 if (!map)
5640 return NULL;
5642 for (i = map->n - 1; i >= 0; --i) {
5643 bmap = isl_basic_map_copy(map->p[i]);
5644 bmap = fn(bmap);
5645 if (!bmap)
5646 goto error;
5647 isl_basic_map_free(map->p[i]);
5648 map->p[i] = bmap;
5649 if (remove_if_empty(map, i) < 0)
5650 goto error;
5653 return map;
5654 error:
5655 isl_map_free(map);
5656 return NULL;
5659 struct isl_map *isl_map_fix_si(struct isl_map *map,
5660 enum isl_dim_type type, unsigned pos, int value)
5662 int i;
5664 map = isl_map_cow(map);
5665 if (!map)
5666 return NULL;
5668 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5669 for (i = map->n - 1; i >= 0; --i) {
5670 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5671 if (remove_if_empty(map, i) < 0)
5672 goto error;
5674 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5675 return map;
5676 error:
5677 isl_map_free(map);
5678 return NULL;
5681 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5682 enum isl_dim_type type, unsigned pos, int value)
5684 return (struct isl_set *)
5685 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5688 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5689 enum isl_dim_type type, unsigned pos, isl_int value)
5691 int i;
5693 map = isl_map_cow(map);
5694 if (!map)
5695 return NULL;
5697 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5698 for (i = 0; i < map->n; ++i) {
5699 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5700 if (!map->p[i])
5701 goto error;
5703 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5704 return map;
5705 error:
5706 isl_map_free(map);
5707 return NULL;
5710 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5711 enum isl_dim_type type, unsigned pos, isl_int value)
5713 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5716 /* Fix the value of the variable at position "pos" of type "type" of "map"
5717 * to be equal to "v".
5719 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5720 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5722 int i;
5724 map = isl_map_cow(map);
5725 if (!map || !v)
5726 goto error;
5728 if (!isl_val_is_int(v))
5729 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5730 "expecting integer value", goto error);
5731 if (pos >= isl_map_dim(map, type))
5732 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5733 "index out of bounds", goto error);
5734 for (i = map->n - 1; i >= 0; --i) {
5735 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5736 isl_val_copy(v));
5737 if (remove_if_empty(map, i) < 0)
5738 goto error;
5740 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5741 isl_val_free(v);
5742 return map;
5743 error:
5744 isl_map_free(map);
5745 isl_val_free(v);
5746 return NULL;
5749 /* Fix the value of the variable at position "pos" of type "type" of "set"
5750 * to be equal to "v".
5752 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5753 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5755 return isl_map_fix_val(set, type, pos, v);
5758 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5759 unsigned input, int value)
5761 return isl_map_fix_si(map, isl_dim_in, input, value);
5764 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5766 return (struct isl_set *)
5767 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5770 static __isl_give isl_basic_map *basic_map_bound_si(
5771 __isl_take isl_basic_map *bmap,
5772 enum isl_dim_type type, unsigned pos, int value, int upper)
5774 int j;
5776 if (!bmap)
5777 return NULL;
5778 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5779 pos += isl_basic_map_offset(bmap, type);
5780 bmap = isl_basic_map_cow(bmap);
5781 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5782 j = isl_basic_map_alloc_inequality(bmap);
5783 if (j < 0)
5784 goto error;
5785 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5786 if (upper) {
5787 isl_int_set_si(bmap->ineq[j][pos], -1);
5788 isl_int_set_si(bmap->ineq[j][0], value);
5789 } else {
5790 isl_int_set_si(bmap->ineq[j][pos], 1);
5791 isl_int_set_si(bmap->ineq[j][0], -value);
5793 bmap = isl_basic_map_simplify(bmap);
5794 return isl_basic_map_finalize(bmap);
5795 error:
5796 isl_basic_map_free(bmap);
5797 return NULL;
5800 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5801 __isl_take isl_basic_map *bmap,
5802 enum isl_dim_type type, unsigned pos, int value)
5804 return basic_map_bound_si(bmap, type, pos, value, 0);
5807 /* Constrain the values of the given dimension to be no greater than "value".
5809 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5810 __isl_take isl_basic_map *bmap,
5811 enum isl_dim_type type, unsigned pos, int value)
5813 return basic_map_bound_si(bmap, type, pos, value, 1);
5816 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5817 unsigned dim, isl_int value)
5819 int j;
5821 bset = isl_basic_set_cow(bset);
5822 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5823 j = isl_basic_set_alloc_inequality(bset);
5824 if (j < 0)
5825 goto error;
5826 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5827 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5828 isl_int_neg(bset->ineq[j][0], value);
5829 bset = isl_basic_set_simplify(bset);
5830 return isl_basic_set_finalize(bset);
5831 error:
5832 isl_basic_set_free(bset);
5833 return NULL;
5836 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5837 enum isl_dim_type type, unsigned pos, int value, int upper)
5839 int i;
5841 map = isl_map_cow(map);
5842 if (!map)
5843 return NULL;
5845 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5846 for (i = 0; i < map->n; ++i) {
5847 map->p[i] = basic_map_bound_si(map->p[i],
5848 type, pos, value, upper);
5849 if (!map->p[i])
5850 goto error;
5852 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5853 return map;
5854 error:
5855 isl_map_free(map);
5856 return NULL;
5859 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5860 enum isl_dim_type type, unsigned pos, int value)
5862 return map_bound_si(map, type, pos, value, 0);
5865 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5866 enum isl_dim_type type, unsigned pos, int value)
5868 return map_bound_si(map, type, pos, value, 1);
5871 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5872 enum isl_dim_type type, unsigned pos, int value)
5874 return (struct isl_set *)
5875 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5878 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5879 enum isl_dim_type type, unsigned pos, int value)
5881 return isl_map_upper_bound_si(set, type, pos, value);
5884 /* Bound the given variable of "bmap" from below (or above is "upper"
5885 * is set) to "value".
5887 static __isl_give isl_basic_map *basic_map_bound(
5888 __isl_take isl_basic_map *bmap,
5889 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5891 int j;
5893 if (!bmap)
5894 return NULL;
5895 if (pos >= isl_basic_map_dim(bmap, type))
5896 isl_die(bmap->ctx, isl_error_invalid,
5897 "index out of bounds", goto error);
5898 pos += isl_basic_map_offset(bmap, type);
5899 bmap = isl_basic_map_cow(bmap);
5900 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5901 j = isl_basic_map_alloc_inequality(bmap);
5902 if (j < 0)
5903 goto error;
5904 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5905 if (upper) {
5906 isl_int_set_si(bmap->ineq[j][pos], -1);
5907 isl_int_set(bmap->ineq[j][0], value);
5908 } else {
5909 isl_int_set_si(bmap->ineq[j][pos], 1);
5910 isl_int_neg(bmap->ineq[j][0], value);
5912 bmap = isl_basic_map_simplify(bmap);
5913 return isl_basic_map_finalize(bmap);
5914 error:
5915 isl_basic_map_free(bmap);
5916 return NULL;
5919 /* Bound the given variable of "map" from below (or above is "upper"
5920 * is set) to "value".
5922 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5923 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5925 int i;
5927 map = isl_map_cow(map);
5928 if (!map)
5929 return NULL;
5931 if (pos >= isl_map_dim(map, type))
5932 isl_die(map->ctx, isl_error_invalid,
5933 "index out of bounds", goto error);
5934 for (i = map->n - 1; i >= 0; --i) {
5935 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5936 if (remove_if_empty(map, i) < 0)
5937 goto error;
5939 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5940 return map;
5941 error:
5942 isl_map_free(map);
5943 return NULL;
5946 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5947 enum isl_dim_type type, unsigned pos, isl_int value)
5949 return map_bound(map, type, pos, value, 0);
5952 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5953 enum isl_dim_type type, unsigned pos, isl_int value)
5955 return map_bound(map, type, pos, value, 1);
5958 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5959 enum isl_dim_type type, unsigned pos, isl_int value)
5961 return isl_map_lower_bound(set, type, pos, value);
5964 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5965 enum isl_dim_type type, unsigned pos, isl_int value)
5967 return isl_map_upper_bound(set, type, pos, value);
5970 /* Force the values of the variable at position "pos" of type "type" of "set"
5971 * to be no smaller than "value".
5973 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5974 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5976 if (!value)
5977 goto error;
5978 if (!isl_val_is_int(value))
5979 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5980 "expecting integer value", goto error);
5981 set = isl_set_lower_bound(set, type, pos, value->n);
5982 isl_val_free(value);
5983 return set;
5984 error:
5985 isl_val_free(value);
5986 isl_set_free(set);
5987 return NULL;
5990 /* Force the values of the variable at position "pos" of type "type" of "set"
5991 * to be no greater than "value".
5993 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5994 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5996 if (!value)
5997 goto error;
5998 if (!isl_val_is_int(value))
5999 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6000 "expecting integer value", goto error);
6001 set = isl_set_upper_bound(set, type, pos, value->n);
6002 isl_val_free(value);
6003 return set;
6004 error:
6005 isl_val_free(value);
6006 isl_set_free(set);
6007 return NULL;
6010 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6011 isl_int value)
6013 int i;
6015 set = isl_set_cow(set);
6016 if (!set)
6017 return NULL;
6019 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6020 for (i = 0; i < set->n; ++i) {
6021 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6022 if (!set->p[i])
6023 goto error;
6025 return set;
6026 error:
6027 isl_set_free(set);
6028 return NULL;
6031 struct isl_map *isl_map_reverse(struct isl_map *map)
6033 int i;
6035 map = isl_map_cow(map);
6036 if (!map)
6037 return NULL;
6039 map->dim = isl_space_reverse(map->dim);
6040 if (!map->dim)
6041 goto error;
6042 for (i = 0; i < map->n; ++i) {
6043 map->p[i] = isl_basic_map_reverse(map->p[i]);
6044 if (!map->p[i])
6045 goto error;
6047 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6048 return map;
6049 error:
6050 isl_map_free(map);
6051 return NULL;
6054 static struct isl_map *isl_basic_map_partial_lexopt(
6055 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6056 struct isl_set **empty, int max)
6058 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6061 struct isl_map *isl_basic_map_partial_lexmax(
6062 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6063 struct isl_set **empty)
6065 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6068 struct isl_map *isl_basic_map_partial_lexmin(
6069 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6070 struct isl_set **empty)
6072 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6075 struct isl_set *isl_basic_set_partial_lexmin(
6076 struct isl_basic_set *bset, struct isl_basic_set *dom,
6077 struct isl_set **empty)
6079 return (struct isl_set *)
6080 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6081 dom, empty);
6084 struct isl_set *isl_basic_set_partial_lexmax(
6085 struct isl_basic_set *bset, struct isl_basic_set *dom,
6086 struct isl_set **empty)
6088 return (struct isl_set *)
6089 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6090 dom, empty);
6093 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6094 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6095 __isl_give isl_set **empty)
6097 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6100 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6101 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6102 __isl_give isl_set **empty)
6104 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6107 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6108 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6109 __isl_give isl_set **empty)
6111 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6114 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6115 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6116 __isl_give isl_set **empty)
6118 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6121 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6122 __isl_take isl_basic_map *bmap, int max)
6124 isl_basic_set *dom = NULL;
6125 isl_space *dom_space;
6127 if (!bmap)
6128 goto error;
6129 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6130 dom = isl_basic_set_universe(dom_space);
6131 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6132 error:
6133 isl_basic_map_free(bmap);
6134 return NULL;
6137 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6138 __isl_take isl_basic_map *bmap)
6140 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6143 #undef TYPE
6144 #define TYPE isl_pw_multi_aff
6145 #undef SUFFIX
6146 #define SUFFIX _pw_multi_aff
6147 #undef EMPTY
6148 #define EMPTY isl_pw_multi_aff_empty
6149 #undef ADD
6150 #define ADD isl_pw_multi_aff_union_add
6151 #include "isl_map_lexopt_templ.c"
6153 /* Given a map "map", compute the lexicographically minimal
6154 * (or maximal) image element for each domain element in dom,
6155 * in the form of an isl_pw_multi_aff.
6156 * If "empty" is not NULL, then set *empty to those elements in dom that
6157 * do not have an image element.
6159 * We first compute the lexicographically minimal or maximal element
6160 * in the first basic map. This results in a partial solution "res"
6161 * and a subset "todo" of dom that still need to be handled.
6162 * We then consider each of the remaining maps in "map" and successively
6163 * update both "res" and "todo".
6164 * If "empty" is NULL, then the todo sets are not needed and therefore
6165 * also not computed.
6167 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6168 __isl_take isl_map *map, __isl_take isl_set *dom,
6169 __isl_give isl_set **empty, int max)
6171 int i;
6172 isl_pw_multi_aff *res;
6173 isl_set *todo;
6175 if (!map || !dom)
6176 goto error;
6178 if (isl_map_plain_is_empty(map)) {
6179 if (empty)
6180 *empty = dom;
6181 else
6182 isl_set_free(dom);
6183 return isl_pw_multi_aff_from_map(map);
6186 res = basic_map_partial_lexopt_pw_multi_aff(
6187 isl_basic_map_copy(map->p[0]),
6188 isl_set_copy(dom), empty, max);
6190 if (empty)
6191 todo = *empty;
6192 for (i = 1; i < map->n; ++i) {
6193 isl_pw_multi_aff *res_i;
6195 res_i = basic_map_partial_lexopt_pw_multi_aff(
6196 isl_basic_map_copy(map->p[i]),
6197 isl_set_copy(dom), empty, max);
6199 if (max)
6200 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6201 else
6202 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6204 if (empty)
6205 todo = isl_set_intersect(todo, *empty);
6208 isl_set_free(dom);
6209 isl_map_free(map);
6211 if (empty)
6212 *empty = todo;
6214 return res;
6215 error:
6216 if (empty)
6217 *empty = NULL;
6218 isl_set_free(dom);
6219 isl_map_free(map);
6220 return NULL;
6223 #undef TYPE
6224 #define TYPE isl_map
6225 #undef SUFFIX
6226 #define SUFFIX
6227 #undef EMPTY
6228 #define EMPTY isl_map_empty
6229 #undef ADD
6230 #define ADD isl_map_union_disjoint
6231 #include "isl_map_lexopt_templ.c"
6233 /* Given a map "map", compute the lexicographically minimal
6234 * (or maximal) image element for each domain element in "dom",
6235 * in the form of an isl_map.
6236 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6237 * do not have an image element.
6239 * If the input consists of more than one disjunct, then first
6240 * compute the desired result in the form of an isl_pw_multi_aff and
6241 * then convert that into an isl_map.
6243 * This function used to have an explicit implementation in terms
6244 * of isl_maps, but it would continually intersect the domains of
6245 * partial results with the complement of the domain of the next
6246 * partial solution, potentially leading to an explosion in the number
6247 * of disjuncts if there are several disjuncts in the input.
6248 * An even earlier implementation of this function would look for
6249 * better results in the domain of the partial result and for extra
6250 * results in the complement of this domain, which would lead to
6251 * even more splintering.
6253 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6254 __isl_take isl_map *map, __isl_take isl_set *dom,
6255 __isl_give isl_set **empty, int max)
6257 struct isl_map *res;
6258 isl_pw_multi_aff *pma;
6260 if (!map || !dom)
6261 goto error;
6263 if (isl_map_plain_is_empty(map)) {
6264 if (empty)
6265 *empty = dom;
6266 else
6267 isl_set_free(dom);
6268 return map;
6271 if (map->n == 1) {
6272 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6273 dom, empty, max);
6274 isl_map_free(map);
6275 return res;
6278 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty, max);
6279 return isl_map_from_pw_multi_aff(pma);
6280 error:
6281 if (empty)
6282 *empty = NULL;
6283 isl_set_free(dom);
6284 isl_map_free(map);
6285 return NULL;
6288 __isl_give isl_map *isl_map_partial_lexmax(
6289 __isl_take isl_map *map, __isl_take isl_set *dom,
6290 __isl_give isl_set **empty)
6292 return isl_map_partial_lexopt(map, dom, empty, 1);
6295 __isl_give isl_map *isl_map_partial_lexmin(
6296 __isl_take isl_map *map, __isl_take isl_set *dom,
6297 __isl_give isl_set **empty)
6299 return isl_map_partial_lexopt(map, dom, empty, 0);
6302 __isl_give isl_set *isl_set_partial_lexmin(
6303 __isl_take isl_set *set, __isl_take isl_set *dom,
6304 __isl_give isl_set **empty)
6306 return (struct isl_set *)
6307 isl_map_partial_lexmin((struct isl_map *)set,
6308 dom, empty);
6311 __isl_give isl_set *isl_set_partial_lexmax(
6312 __isl_take isl_set *set, __isl_take isl_set *dom,
6313 __isl_give isl_set **empty)
6315 return (struct isl_set *)
6316 isl_map_partial_lexmax((struct isl_map *)set,
6317 dom, empty);
6320 /* Compute the lexicographic minimum (or maximum if "max" is set)
6321 * of "bmap" over its domain.
6323 * Since we are not interested in the part of the domain space where
6324 * there is no solution, we initialize the domain to those constraints
6325 * of "bmap" that only involve the parameters and the input dimensions.
6326 * This relieves the parametric programming engine from detecting those
6327 * inequalities and transferring them to the context. More importantly,
6328 * it ensures that those inequalities are transferred first and not
6329 * intermixed with inequalities that actually split the domain.
6331 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6333 int n_div;
6334 int n_out;
6335 isl_basic_map *copy;
6336 isl_basic_set *dom;
6338 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6339 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6340 copy = isl_basic_map_copy(bmap);
6341 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6342 isl_dim_div, 0, n_div);
6343 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6344 isl_dim_out, 0, n_out);
6345 dom = isl_basic_map_domain(copy);
6346 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6349 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6351 return isl_basic_map_lexopt(bmap, 0);
6354 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6356 return isl_basic_map_lexopt(bmap, 1);
6359 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6361 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6364 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6366 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6369 /* Extract the first and only affine expression from list
6370 * and then add it to *pwaff with the given dom.
6371 * This domain is known to be disjoint from other domains
6372 * because of the way isl_basic_map_foreach_lexmax works.
6374 static int update_dim_opt(__isl_take isl_basic_set *dom,
6375 __isl_take isl_aff_list *list, void *user)
6377 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6378 isl_aff *aff;
6379 isl_pw_aff **pwaff = user;
6380 isl_pw_aff *pwaff_i;
6382 if (!list)
6383 goto error;
6384 if (isl_aff_list_n_aff(list) != 1)
6385 isl_die(ctx, isl_error_internal,
6386 "expecting single element list", goto error);
6388 aff = isl_aff_list_get_aff(list, 0);
6389 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6391 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6393 isl_aff_list_free(list);
6395 return 0;
6396 error:
6397 isl_basic_set_free(dom);
6398 isl_aff_list_free(list);
6399 return -1;
6402 /* Given a basic map with one output dimension, compute the minimum or
6403 * maximum of that dimension as an isl_pw_aff.
6405 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6406 * call update_dim_opt on each leaf of the result.
6408 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6409 int max)
6411 isl_space *dim = isl_basic_map_get_space(bmap);
6412 isl_pw_aff *pwaff;
6413 int r;
6415 dim = isl_space_from_domain(isl_space_domain(dim));
6416 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6417 pwaff = isl_pw_aff_empty(dim);
6419 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6420 if (r < 0)
6421 return isl_pw_aff_free(pwaff);
6423 return pwaff;
6426 /* Compute the minimum or maximum of the given output dimension
6427 * as a function of the parameters and the input dimensions,
6428 * but independently of the other output dimensions.
6430 * We first project out the other output dimension and then compute
6431 * the "lexicographic" maximum in each basic map, combining the results
6432 * using isl_pw_aff_union_max.
6434 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6435 int max)
6437 int i;
6438 isl_pw_aff *pwaff;
6439 unsigned n_out;
6441 n_out = isl_map_dim(map, isl_dim_out);
6442 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6443 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6444 if (!map)
6445 return NULL;
6447 if (map->n == 0) {
6448 isl_space *dim = isl_map_get_space(map);
6449 isl_map_free(map);
6450 return isl_pw_aff_empty(dim);
6453 pwaff = basic_map_dim_opt(map->p[0], max);
6454 for (i = 1; i < map->n; ++i) {
6455 isl_pw_aff *pwaff_i;
6457 pwaff_i = basic_map_dim_opt(map->p[i], max);
6458 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6461 isl_map_free(map);
6463 return pwaff;
6466 /* Compute the maximum of the given output dimension as a function of the
6467 * parameters and input dimensions, but independently of
6468 * the other output dimensions.
6470 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6472 return map_dim_opt(map, pos, 1);
6475 /* Compute the minimum or maximum of the given set dimension
6476 * as a function of the parameters,
6477 * but independently of the other set dimensions.
6479 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6480 int max)
6482 return map_dim_opt(set, pos, max);
6485 /* Compute the maximum of the given set dimension as a function of the
6486 * parameters, but independently of the other set dimensions.
6488 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6490 return set_dim_opt(set, pos, 1);
6493 /* Compute the minimum of the given set dimension as a function of the
6494 * parameters, but independently of the other set dimensions.
6496 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6498 return set_dim_opt(set, pos, 0);
6501 /* Apply a preimage specified by "mat" on the parameters of "bset".
6502 * bset is assumed to have only parameters and divs.
6504 static struct isl_basic_set *basic_set_parameter_preimage(
6505 struct isl_basic_set *bset, struct isl_mat *mat)
6507 unsigned nparam;
6509 if (!bset || !mat)
6510 goto error;
6512 bset->dim = isl_space_cow(bset->dim);
6513 if (!bset->dim)
6514 goto error;
6516 nparam = isl_basic_set_dim(bset, isl_dim_param);
6518 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6520 bset->dim->nparam = 0;
6521 bset->dim->n_out = nparam;
6522 bset = isl_basic_set_preimage(bset, mat);
6523 if (bset) {
6524 bset->dim->nparam = bset->dim->n_out;
6525 bset->dim->n_out = 0;
6527 return bset;
6528 error:
6529 isl_mat_free(mat);
6530 isl_basic_set_free(bset);
6531 return NULL;
6534 /* Apply a preimage specified by "mat" on the parameters of "set".
6535 * set is assumed to have only parameters and divs.
6537 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6538 __isl_take isl_mat *mat)
6540 isl_space *space;
6541 unsigned nparam;
6543 if (!set || !mat)
6544 goto error;
6546 nparam = isl_set_dim(set, isl_dim_param);
6548 if (mat->n_row != 1 + nparam)
6549 isl_die(isl_set_get_ctx(set), isl_error_internal,
6550 "unexpected number of rows", goto error);
6552 space = isl_set_get_space(set);
6553 space = isl_space_move_dims(space, isl_dim_set, 0,
6554 isl_dim_param, 0, nparam);
6555 set = isl_set_reset_space(set, space);
6556 set = isl_set_preimage(set, mat);
6557 nparam = isl_set_dim(set, isl_dim_out);
6558 space = isl_set_get_space(set);
6559 space = isl_space_move_dims(space, isl_dim_param, 0,
6560 isl_dim_out, 0, nparam);
6561 set = isl_set_reset_space(set, space);
6562 return set;
6563 error:
6564 isl_mat_free(mat);
6565 isl_set_free(set);
6566 return NULL;
6569 /* Intersect the basic set "bset" with the affine space specified by the
6570 * equalities in "eq".
6572 static struct isl_basic_set *basic_set_append_equalities(
6573 struct isl_basic_set *bset, struct isl_mat *eq)
6575 int i, k;
6576 unsigned len;
6578 if (!bset || !eq)
6579 goto error;
6581 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6582 eq->n_row, 0);
6583 if (!bset)
6584 goto error;
6586 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6587 for (i = 0; i < eq->n_row; ++i) {
6588 k = isl_basic_set_alloc_equality(bset);
6589 if (k < 0)
6590 goto error;
6591 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6592 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6594 isl_mat_free(eq);
6596 bset = isl_basic_set_gauss(bset, NULL);
6597 bset = isl_basic_set_finalize(bset);
6599 return bset;
6600 error:
6601 isl_mat_free(eq);
6602 isl_basic_set_free(bset);
6603 return NULL;
6606 /* Intersect the set "set" with the affine space specified by the
6607 * equalities in "eq".
6609 static struct isl_set *set_append_equalities(struct isl_set *set,
6610 struct isl_mat *eq)
6612 int i;
6614 if (!set || !eq)
6615 goto error;
6617 for (i = 0; i < set->n; ++i) {
6618 set->p[i] = basic_set_append_equalities(set->p[i],
6619 isl_mat_copy(eq));
6620 if (!set->p[i])
6621 goto error;
6623 isl_mat_free(eq);
6624 return set;
6625 error:
6626 isl_mat_free(eq);
6627 isl_set_free(set);
6628 return NULL;
6631 /* Given a basic set "bset" that only involves parameters and existentially
6632 * quantified variables, return the index of the first equality
6633 * that only involves parameters. If there is no such equality then
6634 * return bset->n_eq.
6636 * This function assumes that isl_basic_set_gauss has been called on "bset".
6638 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6640 int i, j;
6641 unsigned nparam, n_div;
6643 if (!bset)
6644 return -1;
6646 nparam = isl_basic_set_dim(bset, isl_dim_param);
6647 n_div = isl_basic_set_dim(bset, isl_dim_div);
6649 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6650 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6651 ++i;
6654 return i;
6657 /* Compute an explicit representation for the existentially quantified
6658 * variables in "bset" by computing the "minimal value" of the set
6659 * variables. Since there are no set variables, the computation of
6660 * the minimal value essentially computes an explicit representation
6661 * of the non-empty part(s) of "bset".
6663 * The input only involves parameters and existentially quantified variables.
6664 * All equalities among parameters have been removed.
6666 * Since the existentially quantified variables in the result are in general
6667 * going to be different from those in the input, we first replace
6668 * them by the minimal number of variables based on their equalities.
6669 * This should simplify the parametric integer programming.
6671 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6673 isl_morph *morph1, *morph2;
6674 isl_set *set;
6675 unsigned n;
6677 if (!bset)
6678 return NULL;
6679 if (bset->n_eq == 0)
6680 return isl_basic_set_lexmin(bset);
6682 morph1 = isl_basic_set_parameter_compression(bset);
6683 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6684 bset = isl_basic_set_lift(bset);
6685 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6686 bset = isl_morph_basic_set(morph2, bset);
6687 n = isl_basic_set_dim(bset, isl_dim_set);
6688 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6690 set = isl_basic_set_lexmin(bset);
6692 set = isl_morph_set(isl_morph_inverse(morph1), set);
6694 return set;
6697 /* Project the given basic set onto its parameter domain, possibly introducing
6698 * new, explicit, existential variables in the constraints.
6699 * The input has parameters and (possibly implicit) existential variables.
6700 * The output has the same parameters, but only
6701 * explicit existentially quantified variables.
6703 * The actual projection is performed by pip, but pip doesn't seem
6704 * to like equalities very much, so we first remove the equalities
6705 * among the parameters by performing a variable compression on
6706 * the parameters. Afterward, an inverse transformation is performed
6707 * and the equalities among the parameters are inserted back in.
6709 * The variable compression on the parameters may uncover additional
6710 * equalities that were only implicit before. We therefore check
6711 * if there are any new parameter equalities in the result and
6712 * if so recurse. The removal of parameter equalities is required
6713 * for the parameter compression performed by base_compute_divs.
6715 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6717 int i;
6718 struct isl_mat *eq;
6719 struct isl_mat *T, *T2;
6720 struct isl_set *set;
6721 unsigned nparam;
6723 bset = isl_basic_set_cow(bset);
6724 if (!bset)
6725 return NULL;
6727 if (bset->n_eq == 0)
6728 return base_compute_divs(bset);
6730 bset = isl_basic_set_gauss(bset, NULL);
6731 if (!bset)
6732 return NULL;
6733 if (isl_basic_set_plain_is_empty(bset))
6734 return isl_set_from_basic_set(bset);
6736 i = first_parameter_equality(bset);
6737 if (i == bset->n_eq)
6738 return base_compute_divs(bset);
6740 nparam = isl_basic_set_dim(bset, isl_dim_param);
6741 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6742 0, 1 + nparam);
6743 eq = isl_mat_cow(eq);
6744 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6745 if (T && T->n_col == 0) {
6746 isl_mat_free(T);
6747 isl_mat_free(T2);
6748 isl_mat_free(eq);
6749 bset = isl_basic_set_set_to_empty(bset);
6750 return isl_set_from_basic_set(bset);
6752 bset = basic_set_parameter_preimage(bset, T);
6754 i = first_parameter_equality(bset);
6755 if (!bset)
6756 set = NULL;
6757 else if (i == bset->n_eq)
6758 set = base_compute_divs(bset);
6759 else
6760 set = parameter_compute_divs(bset);
6761 set = set_parameter_preimage(set, T2);
6762 set = set_append_equalities(set, eq);
6763 return set;
6766 /* Insert the divs from "ls" before those of "bmap".
6768 * The number of columns is not changed, which means that the last
6769 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6770 * The caller is responsible for removing the same number of dimensions
6771 * from the space of "bmap".
6773 static __isl_give isl_basic_map *insert_divs_from_local_space(
6774 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6776 int i;
6777 int n_div;
6778 int old_n_div;
6780 n_div = isl_local_space_dim(ls, isl_dim_div);
6781 if (n_div == 0)
6782 return bmap;
6784 old_n_div = bmap->n_div;
6785 bmap = insert_div_rows(bmap, n_div);
6786 if (!bmap)
6787 return NULL;
6789 for (i = 0; i < n_div; ++i) {
6790 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6791 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6794 return bmap;
6797 /* Replace the space of "bmap" by the space and divs of "ls".
6799 * If "ls" has any divs, then we simplify the result since we may
6800 * have discovered some additional equalities that could simplify
6801 * the div expressions.
6803 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6804 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6806 int n_div;
6808 bmap = isl_basic_map_cow(bmap);
6809 if (!bmap || !ls)
6810 goto error;
6812 n_div = isl_local_space_dim(ls, isl_dim_div);
6813 bmap = insert_divs_from_local_space(bmap, ls);
6814 if (!bmap)
6815 goto error;
6817 isl_space_free(bmap->dim);
6818 bmap->dim = isl_local_space_get_space(ls);
6819 if (!bmap->dim)
6820 goto error;
6822 isl_local_space_free(ls);
6823 if (n_div > 0)
6824 bmap = isl_basic_map_simplify(bmap);
6825 bmap = isl_basic_map_finalize(bmap);
6826 return bmap;
6827 error:
6828 isl_basic_map_free(bmap);
6829 isl_local_space_free(ls);
6830 return NULL;
6833 /* Replace the space of "map" by the space and divs of "ls".
6835 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6836 __isl_take isl_local_space *ls)
6838 int i;
6840 map = isl_map_cow(map);
6841 if (!map || !ls)
6842 goto error;
6844 for (i = 0; i < map->n; ++i) {
6845 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6846 isl_local_space_copy(ls));
6847 if (!map->p[i])
6848 goto error;
6850 isl_space_free(map->dim);
6851 map->dim = isl_local_space_get_space(ls);
6852 if (!map->dim)
6853 goto error;
6855 isl_local_space_free(ls);
6856 return map;
6857 error:
6858 isl_local_space_free(ls);
6859 isl_map_free(map);
6860 return NULL;
6863 /* Compute an explicit representation for the existentially
6864 * quantified variables for which do not know any explicit representation yet.
6866 * We first sort the existentially quantified variables so that the
6867 * existentially quantified variables for which we already have an explicit
6868 * representation are placed before those for which we do not.
6869 * The input dimensions, the output dimensions and the existentially
6870 * quantified variables for which we already have an explicit
6871 * representation are then turned into parameters.
6872 * compute_divs returns a map with the same parameters and
6873 * no input or output dimensions and the dimension specification
6874 * is reset to that of the input, including the existentially quantified
6875 * variables for which we already had an explicit representation.
6877 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6879 struct isl_basic_set *bset;
6880 struct isl_set *set;
6881 struct isl_map *map;
6882 isl_space *dim;
6883 isl_local_space *ls;
6884 unsigned nparam;
6885 unsigned n_in;
6886 unsigned n_out;
6887 int n_known;
6888 int i;
6890 bmap = isl_basic_map_sort_divs(bmap);
6891 bmap = isl_basic_map_cow(bmap);
6892 if (!bmap)
6893 return NULL;
6895 n_known = isl_basic_map_first_unknown_div(bmap);
6896 if (n_known < 0)
6897 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6899 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6900 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6901 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6902 dim = isl_space_set_alloc(bmap->ctx,
6903 nparam + n_in + n_out + n_known, 0);
6904 if (!dim)
6905 goto error;
6907 ls = isl_basic_map_get_local_space(bmap);
6908 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6909 n_known, bmap->n_div - n_known);
6910 if (n_known > 0) {
6911 for (i = n_known; i < bmap->n_div; ++i)
6912 swap_div(bmap, i - n_known, i);
6913 bmap->n_div -= n_known;
6914 bmap->extra -= n_known;
6916 bmap = isl_basic_map_reset_space(bmap, dim);
6917 bset = (struct isl_basic_set *)bmap;
6919 set = parameter_compute_divs(bset);
6920 map = (struct isl_map *)set;
6921 map = replace_space_by_local_space(map, ls);
6923 return map;
6924 error:
6925 isl_basic_map_free(bmap);
6926 return NULL;
6929 /* Remove the explicit representation of local variable "div",
6930 * if there is any.
6932 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6933 __isl_take isl_basic_map *bmap, int div)
6935 isl_bool unknown;
6937 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6938 if (unknown < 0)
6939 return isl_basic_map_free(bmap);
6940 if (unknown)
6941 return bmap;
6943 bmap = isl_basic_map_cow(bmap);
6944 if (!bmap)
6945 return NULL;
6946 isl_int_set_si(bmap->div[div][0], 0);
6947 return bmap;
6950 /* Is local variable "div" of "bmap" marked as not having an explicit
6951 * representation?
6952 * Note that even if "div" is not marked in this way and therefore
6953 * has an explicit representation, this representation may still
6954 * depend (indirectly) on other local variables that do not
6955 * have an explicit representation.
6957 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
6958 int div)
6960 if (!bmap)
6961 return isl_bool_error;
6962 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6963 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6964 "position out of bounds", return isl_bool_error);
6965 return isl_int_is_zero(bmap->div[div][0]);
6968 /* Return the position of the first local variable that does not
6969 * have an explicit representation.
6970 * Return the total number of local variables if they all have
6971 * an explicit representation.
6972 * Return -1 on error.
6974 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
6976 int i;
6978 if (!bmap)
6979 return -1;
6981 for (i = 0; i < bmap->n_div; ++i) {
6982 if (!isl_basic_map_div_is_known(bmap, i))
6983 return i;
6985 return bmap->n_div;
6988 /* Does "bmap" have an explicit representation for all local variables?
6990 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6992 int first, n;
6994 n = isl_basic_map_dim(bmap, isl_dim_div);
6995 first = isl_basic_map_first_unknown_div(bmap);
6996 if (first < 0)
6997 return isl_bool_error;
6998 return first == n;
7001 /* Do all basic maps in "map" have an explicit representation
7002 * for all local variables?
7004 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7006 int i;
7008 if (!map)
7009 return isl_bool_error;
7011 for (i = 0; i < map->n; ++i) {
7012 int known = isl_basic_map_divs_known(map->p[i]);
7013 if (known <= 0)
7014 return known;
7017 return isl_bool_true;
7020 /* If bmap contains any unknown divs, then compute explicit
7021 * expressions for them. However, this computation may be
7022 * quite expensive, so first try to remove divs that aren't
7023 * strictly needed.
7025 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7027 int known;
7028 struct isl_map *map;
7030 known = isl_basic_map_divs_known(bmap);
7031 if (known < 0)
7032 goto error;
7033 if (known)
7034 return isl_map_from_basic_map(bmap);
7036 bmap = isl_basic_map_drop_redundant_divs(bmap);
7038 known = isl_basic_map_divs_known(bmap);
7039 if (known < 0)
7040 goto error;
7041 if (known)
7042 return isl_map_from_basic_map(bmap);
7044 map = compute_divs(bmap);
7045 return map;
7046 error:
7047 isl_basic_map_free(bmap);
7048 return NULL;
7051 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7053 int i;
7054 int known;
7055 struct isl_map *res;
7057 if (!map)
7058 return NULL;
7059 if (map->n == 0)
7060 return map;
7062 known = isl_map_divs_known(map);
7063 if (known < 0) {
7064 isl_map_free(map);
7065 return NULL;
7067 if (known)
7068 return map;
7070 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7071 for (i = 1 ; i < map->n; ++i) {
7072 struct isl_map *r2;
7073 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7074 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7075 res = isl_map_union_disjoint(res, r2);
7076 else
7077 res = isl_map_union(res, r2);
7079 isl_map_free(map);
7081 return res;
7084 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7086 return (struct isl_set *)
7087 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7090 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7092 return (struct isl_set *)
7093 isl_map_compute_divs((struct isl_map *)set);
7096 struct isl_set *isl_map_domain(struct isl_map *map)
7098 int i;
7099 struct isl_set *set;
7101 if (!map)
7102 goto error;
7104 map = isl_map_cow(map);
7105 if (!map)
7106 return NULL;
7108 set = (struct isl_set *)map;
7109 set->dim = isl_space_domain(set->dim);
7110 if (!set->dim)
7111 goto error;
7112 for (i = 0; i < map->n; ++i) {
7113 set->p[i] = isl_basic_map_domain(map->p[i]);
7114 if (!set->p[i])
7115 goto error;
7117 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7118 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7119 return set;
7120 error:
7121 isl_map_free(map);
7122 return NULL;
7125 /* Return the union of "map1" and "map2", where we assume for now that
7126 * "map1" and "map2" are disjoint. Note that the basic maps inside
7127 * "map1" or "map2" may not be disjoint from each other.
7128 * Also note that this function is also called from isl_map_union,
7129 * which takes care of handling the situation where "map1" and "map2"
7130 * may not be disjoint.
7132 * If one of the inputs is empty, we can simply return the other input.
7133 * Similarly, if one of the inputs is universal, then it is equal to the union.
7135 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7136 __isl_take isl_map *map2)
7138 int i;
7139 unsigned flags = 0;
7140 struct isl_map *map = NULL;
7141 int is_universe;
7143 if (!map1 || !map2)
7144 goto error;
7146 if (!isl_space_is_equal(map1->dim, map2->dim))
7147 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7148 "spaces don't match", goto error);
7150 if (map1->n == 0) {
7151 isl_map_free(map1);
7152 return map2;
7154 if (map2->n == 0) {
7155 isl_map_free(map2);
7156 return map1;
7159 is_universe = isl_map_plain_is_universe(map1);
7160 if (is_universe < 0)
7161 goto error;
7162 if (is_universe) {
7163 isl_map_free(map2);
7164 return map1;
7167 is_universe = isl_map_plain_is_universe(map2);
7168 if (is_universe < 0)
7169 goto error;
7170 if (is_universe) {
7171 isl_map_free(map1);
7172 return map2;
7175 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7176 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7177 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7179 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7180 map1->n + map2->n, flags);
7181 if (!map)
7182 goto error;
7183 for (i = 0; i < map1->n; ++i) {
7184 map = isl_map_add_basic_map(map,
7185 isl_basic_map_copy(map1->p[i]));
7186 if (!map)
7187 goto error;
7189 for (i = 0; i < map2->n; ++i) {
7190 map = isl_map_add_basic_map(map,
7191 isl_basic_map_copy(map2->p[i]));
7192 if (!map)
7193 goto error;
7195 isl_map_free(map1);
7196 isl_map_free(map2);
7197 return map;
7198 error:
7199 isl_map_free(map);
7200 isl_map_free(map1);
7201 isl_map_free(map2);
7202 return NULL;
7205 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7206 * guaranteed to be disjoint by the caller.
7208 * Note that this functions is called from within isl_map_make_disjoint,
7209 * so we have to be careful not to touch the constraints of the inputs
7210 * in any way.
7212 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7213 __isl_take isl_map *map2)
7215 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7218 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7219 * not be disjoint. The parameters are assumed to have been aligned.
7221 * We currently simply call map_union_disjoint, the internal operation
7222 * of which does not really depend on the inputs being disjoint.
7223 * If the result contains more than one basic map, then we clear
7224 * the disjoint flag since the result may contain basic maps from
7225 * both inputs and these are not guaranteed to be disjoint.
7227 * As a special case, if "map1" and "map2" are obviously equal,
7228 * then we simply return "map1".
7230 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7231 __isl_take isl_map *map2)
7233 int equal;
7235 if (!map1 || !map2)
7236 goto error;
7238 equal = isl_map_plain_is_equal(map1, map2);
7239 if (equal < 0)
7240 goto error;
7241 if (equal) {
7242 isl_map_free(map2);
7243 return map1;
7246 map1 = map_union_disjoint(map1, map2);
7247 if (!map1)
7248 return NULL;
7249 if (map1->n > 1)
7250 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7251 return map1;
7252 error:
7253 isl_map_free(map1);
7254 isl_map_free(map2);
7255 return NULL;
7258 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7259 * not be disjoint.
7261 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7262 __isl_take isl_map *map2)
7264 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7267 struct isl_set *isl_set_union_disjoint(
7268 struct isl_set *set1, struct isl_set *set2)
7270 return (struct isl_set *)
7271 isl_map_union_disjoint(
7272 (struct isl_map *)set1, (struct isl_map *)set2);
7275 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7277 return (struct isl_set *)
7278 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7281 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7282 * the results.
7284 * "map" and "set" are assumed to be compatible and non-NULL.
7286 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7287 __isl_take isl_set *set,
7288 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7289 __isl_take isl_basic_set *bset))
7291 unsigned flags = 0;
7292 struct isl_map *result;
7293 int i, j;
7295 if (isl_set_plain_is_universe(set)) {
7296 isl_set_free(set);
7297 return map;
7300 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7301 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7302 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7304 result = isl_map_alloc_space(isl_space_copy(map->dim),
7305 map->n * set->n, flags);
7306 for (i = 0; result && i < map->n; ++i)
7307 for (j = 0; j < set->n; ++j) {
7308 result = isl_map_add_basic_map(result,
7309 fn(isl_basic_map_copy(map->p[i]),
7310 isl_basic_set_copy(set->p[j])));
7311 if (!result)
7312 break;
7315 isl_map_free(map);
7316 isl_set_free(set);
7317 return result;
7320 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7321 __isl_take isl_set *set)
7323 if (!map || !set)
7324 goto error;
7326 if (!isl_map_compatible_range(map, set))
7327 isl_die(set->ctx, isl_error_invalid,
7328 "incompatible spaces", goto error);
7330 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7331 error:
7332 isl_map_free(map);
7333 isl_set_free(set);
7334 return NULL;
7337 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7338 __isl_take isl_set *set)
7340 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7343 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7344 __isl_take isl_set *set)
7346 if (!map || !set)
7347 goto error;
7349 if (!isl_map_compatible_domain(map, set))
7350 isl_die(set->ctx, isl_error_invalid,
7351 "incompatible spaces", goto error);
7353 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7354 error:
7355 isl_map_free(map);
7356 isl_set_free(set);
7357 return NULL;
7360 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7361 __isl_take isl_set *set)
7363 return isl_map_align_params_map_map_and(map, set,
7364 &map_intersect_domain);
7367 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7368 __isl_take isl_map *map2)
7370 if (!map1 || !map2)
7371 goto error;
7372 map1 = isl_map_reverse(map1);
7373 map1 = isl_map_apply_range(map1, map2);
7374 return isl_map_reverse(map1);
7375 error:
7376 isl_map_free(map1);
7377 isl_map_free(map2);
7378 return NULL;
7381 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7382 __isl_take isl_map *map2)
7384 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7387 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7388 __isl_take isl_map *map2)
7390 isl_space *dim_result;
7391 struct isl_map *result;
7392 int i, j;
7394 if (!map1 || !map2)
7395 goto error;
7397 dim_result = isl_space_join(isl_space_copy(map1->dim),
7398 isl_space_copy(map2->dim));
7400 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7401 if (!result)
7402 goto error;
7403 for (i = 0; i < map1->n; ++i)
7404 for (j = 0; j < map2->n; ++j) {
7405 result = isl_map_add_basic_map(result,
7406 isl_basic_map_apply_range(
7407 isl_basic_map_copy(map1->p[i]),
7408 isl_basic_map_copy(map2->p[j])));
7409 if (!result)
7410 goto error;
7412 isl_map_free(map1);
7413 isl_map_free(map2);
7414 if (result && result->n <= 1)
7415 ISL_F_SET(result, ISL_MAP_DISJOINT);
7416 return result;
7417 error:
7418 isl_map_free(map1);
7419 isl_map_free(map2);
7420 return NULL;
7423 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7424 __isl_take isl_map *map2)
7426 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7430 * returns range - domain
7432 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7434 isl_space *target_space;
7435 struct isl_basic_set *bset;
7436 unsigned dim;
7437 unsigned nparam;
7438 int i;
7440 if (!bmap)
7441 goto error;
7442 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7443 bmap->dim, isl_dim_out),
7444 goto error);
7445 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7446 dim = isl_basic_map_n_in(bmap);
7447 nparam = isl_basic_map_n_param(bmap);
7448 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7449 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7450 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7451 for (i = 0; i < dim; ++i) {
7452 int j = isl_basic_map_alloc_equality(bmap);
7453 if (j < 0) {
7454 bmap = isl_basic_map_free(bmap);
7455 break;
7457 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7458 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7459 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7460 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7462 bset = isl_basic_map_domain(bmap);
7463 bset = isl_basic_set_reset_space(bset, target_space);
7464 return bset;
7465 error:
7466 isl_basic_map_free(bmap);
7467 return NULL;
7471 * returns range - domain
7473 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7475 int i;
7476 isl_space *dim;
7477 struct isl_set *result;
7479 if (!map)
7480 return NULL;
7482 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7483 map->dim, isl_dim_out),
7484 goto error);
7485 dim = isl_map_get_space(map);
7486 dim = isl_space_domain(dim);
7487 result = isl_set_alloc_space(dim, map->n, 0);
7488 if (!result)
7489 goto error;
7490 for (i = 0; i < map->n; ++i)
7491 result = isl_set_add_basic_set(result,
7492 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7493 isl_map_free(map);
7494 return result;
7495 error:
7496 isl_map_free(map);
7497 return NULL;
7501 * returns [domain -> range] -> range - domain
7503 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7504 __isl_take isl_basic_map *bmap)
7506 int i, k;
7507 isl_space *dim;
7508 isl_basic_map *domain;
7509 int nparam, n;
7510 unsigned total;
7512 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7513 bmap->dim, isl_dim_out))
7514 isl_die(bmap->ctx, isl_error_invalid,
7515 "domain and range don't match", goto error);
7517 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7518 n = isl_basic_map_dim(bmap, isl_dim_in);
7520 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7521 domain = isl_basic_map_universe(dim);
7523 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7524 bmap = isl_basic_map_apply_range(bmap, domain);
7525 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7527 total = isl_basic_map_total_dim(bmap);
7529 for (i = 0; i < n; ++i) {
7530 k = isl_basic_map_alloc_equality(bmap);
7531 if (k < 0)
7532 goto error;
7533 isl_seq_clr(bmap->eq[k], 1 + total);
7534 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7535 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7536 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7539 bmap = isl_basic_map_gauss(bmap, NULL);
7540 return isl_basic_map_finalize(bmap);
7541 error:
7542 isl_basic_map_free(bmap);
7543 return NULL;
7547 * returns [domain -> range] -> range - domain
7549 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7551 int i;
7552 isl_space *domain_dim;
7554 if (!map)
7555 return NULL;
7557 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7558 map->dim, isl_dim_out))
7559 isl_die(map->ctx, isl_error_invalid,
7560 "domain and range don't match", goto error);
7562 map = isl_map_cow(map);
7563 if (!map)
7564 return NULL;
7566 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7567 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7568 map->dim = isl_space_join(map->dim, domain_dim);
7569 if (!map->dim)
7570 goto error;
7571 for (i = 0; i < map->n; ++i) {
7572 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7573 if (!map->p[i])
7574 goto error;
7576 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7577 return map;
7578 error:
7579 isl_map_free(map);
7580 return NULL;
7583 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7585 struct isl_basic_map *bmap;
7586 unsigned nparam;
7587 unsigned dim;
7588 int i;
7590 if (!dims)
7591 return NULL;
7593 nparam = dims->nparam;
7594 dim = dims->n_out;
7595 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7596 if (!bmap)
7597 goto error;
7599 for (i = 0; i < dim; ++i) {
7600 int j = isl_basic_map_alloc_equality(bmap);
7601 if (j < 0)
7602 goto error;
7603 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7604 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7605 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7607 return isl_basic_map_finalize(bmap);
7608 error:
7609 isl_basic_map_free(bmap);
7610 return NULL;
7613 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7615 if (!dim)
7616 return NULL;
7617 if (dim->n_in != dim->n_out)
7618 isl_die(dim->ctx, isl_error_invalid,
7619 "number of input and output dimensions needs to be "
7620 "the same", goto error);
7621 return basic_map_identity(dim);
7622 error:
7623 isl_space_free(dim);
7624 return NULL;
7627 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7629 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7632 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7634 isl_space *dim = isl_set_get_space(set);
7635 isl_map *id;
7636 id = isl_map_identity(isl_space_map_from_set(dim));
7637 return isl_map_intersect_range(id, set);
7640 /* Construct a basic set with all set dimensions having only non-negative
7641 * values.
7643 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7644 __isl_take isl_space *space)
7646 int i;
7647 unsigned nparam;
7648 unsigned dim;
7649 struct isl_basic_set *bset;
7651 if (!space)
7652 return NULL;
7653 nparam = space->nparam;
7654 dim = space->n_out;
7655 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7656 if (!bset)
7657 return NULL;
7658 for (i = 0; i < dim; ++i) {
7659 int k = isl_basic_set_alloc_inequality(bset);
7660 if (k < 0)
7661 goto error;
7662 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7663 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7665 return bset;
7666 error:
7667 isl_basic_set_free(bset);
7668 return NULL;
7671 /* Construct the half-space x_pos >= 0.
7673 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7674 int pos)
7676 int k;
7677 isl_basic_set *nonneg;
7679 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7680 k = isl_basic_set_alloc_inequality(nonneg);
7681 if (k < 0)
7682 goto error;
7683 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7684 isl_int_set_si(nonneg->ineq[k][pos], 1);
7686 return isl_basic_set_finalize(nonneg);
7687 error:
7688 isl_basic_set_free(nonneg);
7689 return NULL;
7692 /* Construct the half-space x_pos <= -1.
7694 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7696 int k;
7697 isl_basic_set *neg;
7699 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7700 k = isl_basic_set_alloc_inequality(neg);
7701 if (k < 0)
7702 goto error;
7703 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7704 isl_int_set_si(neg->ineq[k][0], -1);
7705 isl_int_set_si(neg->ineq[k][pos], -1);
7707 return isl_basic_set_finalize(neg);
7708 error:
7709 isl_basic_set_free(neg);
7710 return NULL;
7713 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7714 enum isl_dim_type type, unsigned first, unsigned n)
7716 int i;
7717 unsigned offset;
7718 isl_basic_set *nonneg;
7719 isl_basic_set *neg;
7721 if (!set)
7722 return NULL;
7723 if (n == 0)
7724 return set;
7726 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7728 offset = pos(set->dim, type);
7729 for (i = 0; i < n; ++i) {
7730 nonneg = nonneg_halfspace(isl_set_get_space(set),
7731 offset + first + i);
7732 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7734 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7737 return set;
7738 error:
7739 isl_set_free(set);
7740 return NULL;
7743 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7744 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7745 void *user)
7747 isl_set *half;
7749 if (!set)
7750 return -1;
7751 if (isl_set_plain_is_empty(set)) {
7752 isl_set_free(set);
7753 return 0;
7755 if (first == len)
7756 return fn(set, signs, user);
7758 signs[first] = 1;
7759 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7760 1 + first));
7761 half = isl_set_intersect(half, isl_set_copy(set));
7762 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7763 goto error;
7765 signs[first] = -1;
7766 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7767 1 + first));
7768 half = isl_set_intersect(half, set);
7769 return foreach_orthant(half, signs, first + 1, len, fn, user);
7770 error:
7771 isl_set_free(set);
7772 return -1;
7775 /* Call "fn" on the intersections of "set" with each of the orthants
7776 * (except for obviously empty intersections). The orthant is identified
7777 * by the signs array, with each entry having value 1 or -1 according
7778 * to the sign of the corresponding variable.
7780 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7781 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7782 void *user)
7784 unsigned nparam;
7785 unsigned nvar;
7786 int *signs;
7787 int r;
7789 if (!set)
7790 return -1;
7791 if (isl_set_plain_is_empty(set))
7792 return 0;
7794 nparam = isl_set_dim(set, isl_dim_param);
7795 nvar = isl_set_dim(set, isl_dim_set);
7797 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7799 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7800 fn, user);
7802 free(signs);
7804 return r;
7807 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7809 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7812 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7813 __isl_keep isl_basic_map *bmap2)
7815 int is_subset;
7816 struct isl_map *map1;
7817 struct isl_map *map2;
7819 if (!bmap1 || !bmap2)
7820 return isl_bool_error;
7822 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7823 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7825 is_subset = isl_map_is_subset(map1, map2);
7827 isl_map_free(map1);
7828 isl_map_free(map2);
7830 return is_subset;
7833 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7834 __isl_keep isl_basic_set *bset2)
7836 return isl_basic_map_is_subset(bset1, bset2);
7839 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7840 __isl_keep isl_basic_map *bmap2)
7842 isl_bool is_subset;
7844 if (!bmap1 || !bmap2)
7845 return isl_bool_error;
7846 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7847 if (is_subset != isl_bool_true)
7848 return is_subset;
7849 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7850 return is_subset;
7853 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7854 __isl_keep isl_basic_set *bset2)
7856 return isl_basic_map_is_equal(
7857 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7860 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7862 int i;
7863 int is_empty;
7865 if (!map)
7866 return isl_bool_error;
7867 for (i = 0; i < map->n; ++i) {
7868 is_empty = isl_basic_map_is_empty(map->p[i]);
7869 if (is_empty < 0)
7870 return isl_bool_error;
7871 if (!is_empty)
7872 return isl_bool_false;
7874 return isl_bool_true;
7877 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7879 return map ? map->n == 0 : isl_bool_error;
7882 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7884 return set ? set->n == 0 : isl_bool_error;
7887 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7889 return isl_map_is_empty((struct isl_map *)set);
7892 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7894 if (!map1 || !map2)
7895 return -1;
7897 return isl_space_is_equal(map1->dim, map2->dim);
7900 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7902 if (!set1 || !set2)
7903 return -1;
7905 return isl_space_is_equal(set1->dim, set2->dim);
7908 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7910 isl_bool is_subset;
7912 if (!map1 || !map2)
7913 return isl_bool_error;
7914 is_subset = isl_map_is_subset(map1, map2);
7915 if (is_subset != isl_bool_true)
7916 return is_subset;
7917 is_subset = isl_map_is_subset(map2, map1);
7918 return is_subset;
7921 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7923 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7926 isl_bool isl_basic_map_is_strict_subset(
7927 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7929 isl_bool is_subset;
7931 if (!bmap1 || !bmap2)
7932 return isl_bool_error;
7933 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7934 if (is_subset != isl_bool_true)
7935 return is_subset;
7936 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7937 if (is_subset == isl_bool_error)
7938 return is_subset;
7939 return !is_subset;
7942 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7943 __isl_keep isl_map *map2)
7945 isl_bool is_subset;
7947 if (!map1 || !map2)
7948 return isl_bool_error;
7949 is_subset = isl_map_is_subset(map1, map2);
7950 if (is_subset != isl_bool_true)
7951 return is_subset;
7952 is_subset = isl_map_is_subset(map2, map1);
7953 if (is_subset == isl_bool_error)
7954 return is_subset;
7955 return !is_subset;
7958 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7959 __isl_keep isl_set *set2)
7961 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7964 /* Is "bmap" obviously equal to the universe with the same space?
7966 * That is, does it not have any constraints?
7968 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
7970 if (!bmap)
7971 return isl_bool_error;
7972 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7975 /* Is "bset" obviously equal to the universe with the same space?
7977 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
7979 return isl_basic_map_plain_is_universe(bset);
7982 /* If "c" does not involve any existentially quantified variables,
7983 * then set *univ to false and abort
7985 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
7987 isl_bool *univ = user;
7988 unsigned n;
7990 n = isl_constraint_dim(c, isl_dim_div);
7991 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
7992 isl_constraint_free(c);
7993 if (*univ < 0 || !*univ)
7994 return isl_stat_error;
7995 return isl_stat_ok;
7998 /* Is "bmap" equal to the universe with the same space?
8000 * First check if it is obviously equal to the universe.
8001 * If not and if there are any constraints not involving
8002 * existentially quantified variables, then it is certainly
8003 * not equal to the universe.
8004 * Otherwise, check if the universe is a subset of "bmap".
8006 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8008 isl_bool univ;
8009 isl_basic_map *test;
8011 univ = isl_basic_map_plain_is_universe(bmap);
8012 if (univ < 0 || univ)
8013 return univ;
8014 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8015 return isl_bool_false;
8016 univ = isl_bool_true;
8017 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8018 univ)
8019 return isl_bool_error;
8020 if (univ < 0 || !univ)
8021 return univ;
8022 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8023 univ = isl_basic_map_is_subset(test, bmap);
8024 isl_basic_map_free(test);
8025 return univ;
8028 /* Is "bset" equal to the universe with the same space?
8030 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8032 return isl_basic_map_is_universe(bset);
8035 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8037 int i;
8039 if (!map)
8040 return isl_bool_error;
8042 for (i = 0; i < map->n; ++i) {
8043 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8044 if (r < 0 || r)
8045 return r;
8048 return isl_bool_false;
8051 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8053 return isl_map_plain_is_universe((isl_map *) set);
8056 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8058 struct isl_basic_set *bset = NULL;
8059 struct isl_vec *sample = NULL;
8060 isl_bool empty, non_empty;
8062 if (!bmap)
8063 return isl_bool_error;
8065 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8066 return isl_bool_true;
8068 if (isl_basic_map_plain_is_universe(bmap))
8069 return isl_bool_false;
8071 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8072 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8073 copy = isl_basic_map_remove_redundancies(copy);
8074 empty = isl_basic_map_plain_is_empty(copy);
8075 isl_basic_map_free(copy);
8076 return empty;
8079 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8080 if (non_empty < 0)
8081 return isl_bool_error;
8082 if (non_empty)
8083 return isl_bool_false;
8084 isl_vec_free(bmap->sample);
8085 bmap->sample = NULL;
8086 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8087 if (!bset)
8088 return isl_bool_error;
8089 sample = isl_basic_set_sample_vec(bset);
8090 if (!sample)
8091 return isl_bool_error;
8092 empty = sample->size == 0;
8093 isl_vec_free(bmap->sample);
8094 bmap->sample = sample;
8095 if (empty)
8096 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8098 return empty;
8101 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8103 if (!bmap)
8104 return isl_bool_error;
8105 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8108 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8110 if (!bset)
8111 return isl_bool_error;
8112 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8115 /* Is "bmap" known to be non-empty?
8117 * That is, is the cached sample still valid?
8119 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8121 unsigned total;
8123 if (!bmap)
8124 return isl_bool_error;
8125 if (!bmap->sample)
8126 return isl_bool_false;
8127 total = 1 + isl_basic_map_total_dim(bmap);
8128 if (bmap->sample->size != total)
8129 return isl_bool_false;
8130 return isl_basic_map_contains(bmap, bmap->sample);
8133 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8135 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8138 struct isl_map *isl_basic_map_union(
8139 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8141 struct isl_map *map;
8142 if (!bmap1 || !bmap2)
8143 goto error;
8145 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8147 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8148 if (!map)
8149 goto error;
8150 map = isl_map_add_basic_map(map, bmap1);
8151 map = isl_map_add_basic_map(map, bmap2);
8152 return map;
8153 error:
8154 isl_basic_map_free(bmap1);
8155 isl_basic_map_free(bmap2);
8156 return NULL;
8159 struct isl_set *isl_basic_set_union(
8160 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8162 return (struct isl_set *)isl_basic_map_union(
8163 (struct isl_basic_map *)bset1,
8164 (struct isl_basic_map *)bset2);
8167 /* Order divs such that any div only depends on previous divs */
8168 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8170 int i;
8171 unsigned off;
8173 if (!bmap)
8174 return NULL;
8176 off = isl_space_dim(bmap->dim, isl_dim_all);
8178 for (i = 0; i < bmap->n_div; ++i) {
8179 int pos;
8180 if (isl_int_is_zero(bmap->div[i][0]))
8181 continue;
8182 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8183 bmap->n_div-i);
8184 if (pos == -1)
8185 continue;
8186 if (pos == 0)
8187 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8188 "integer division depends on itself",
8189 return isl_basic_map_free(bmap));
8190 isl_basic_map_swap_div(bmap, i, i + pos);
8191 --i;
8193 return bmap;
8196 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8198 return (struct isl_basic_set *)
8199 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8202 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8204 int i;
8206 if (!map)
8207 return 0;
8209 for (i = 0; i < map->n; ++i) {
8210 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8211 if (!map->p[i])
8212 goto error;
8215 return map;
8216 error:
8217 isl_map_free(map);
8218 return NULL;
8221 /* Apply the expansion computed by isl_merge_divs.
8222 * The expansion itself is given by "exp" while the resulting
8223 * list of divs is given by "div".
8225 * Move the integer divisions of "bmap" into the right position
8226 * according to "exp" and then introduce the additional integer
8227 * divisions, adding div constraints.
8228 * The moving should be done first to avoid moving coefficients
8229 * in the definitions of the extra integer divisions.
8231 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8232 __isl_take isl_basic_set *bmap, __isl_take isl_mat *div, int *exp)
8234 int i, j;
8235 int n_div;
8237 bmap = isl_basic_map_cow(bmap);
8238 if (!bmap || !div)
8239 goto error;
8241 if (div->n_row < bmap->n_div)
8242 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8243 "not an expansion", goto error);
8245 n_div = bmap->n_div;
8246 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8247 div->n_row - n_div, 0,
8248 2 * (div->n_row - n_div));
8250 for (i = n_div; i < div->n_row; ++i)
8251 if (isl_basic_map_alloc_div(bmap) < 0)
8252 goto error;
8254 for (j = n_div - 1; j >= 0; --j) {
8255 if (exp[j] == j)
8256 break;
8257 isl_basic_map_swap_div(bmap, j, exp[j]);
8259 j = 0;
8260 for (i = 0; i < div->n_row; ++i) {
8261 if (j < n_div && exp[j] == i) {
8262 j++;
8263 } else {
8264 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8265 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8266 continue;
8267 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8268 goto error;
8272 isl_mat_free(div);
8273 return bmap;
8274 error:
8275 isl_basic_map_free(bmap);
8276 isl_mat_free(div);
8277 return NULL;
8280 /* Apply the expansion computed by isl_merge_divs.
8281 * The expansion itself is given by "exp" while the resulting
8282 * list of divs is given by "div".
8284 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8285 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8287 return isl_basic_map_expand_divs(bset, div, exp);
8290 /* Look for a div in dst that corresponds to the div "div" in src.
8291 * The divs before "div" in src and dst are assumed to be the same.
8293 * Returns -1 if no corresponding div was found and the position
8294 * of the corresponding div in dst otherwise.
8296 static int find_div(struct isl_basic_map *dst,
8297 struct isl_basic_map *src, unsigned div)
8299 int i;
8301 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8303 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8304 for (i = div; i < dst->n_div; ++i)
8305 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8306 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8307 dst->n_div - div) == -1)
8308 return i;
8309 return -1;
8312 /* Align the divs of "dst" to those of "src", adding divs from "src"
8313 * if needed. That is, make sure that the first src->n_div divs
8314 * of the result are equal to those of src.
8316 * The result is not finalized as by design it will have redundant
8317 * divs if any divs from "src" were copied.
8319 __isl_give isl_basic_map *isl_basic_map_align_divs(
8320 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8322 int i;
8323 int known, extended;
8324 unsigned total;
8326 if (!dst || !src)
8327 return isl_basic_map_free(dst);
8329 if (src->n_div == 0)
8330 return dst;
8332 known = isl_basic_map_divs_known(src);
8333 if (known < 0)
8334 return isl_basic_map_free(dst);
8335 if (!known)
8336 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8337 "some src divs are unknown",
8338 return isl_basic_map_free(dst));
8340 src = isl_basic_map_order_divs(src);
8342 extended = 0;
8343 total = isl_space_dim(src->dim, isl_dim_all);
8344 for (i = 0; i < src->n_div; ++i) {
8345 int j = find_div(dst, src, i);
8346 if (j < 0) {
8347 if (!extended) {
8348 int extra = src->n_div - i;
8349 dst = isl_basic_map_cow(dst);
8350 if (!dst)
8351 return NULL;
8352 dst = isl_basic_map_extend_space(dst,
8353 isl_space_copy(dst->dim),
8354 extra, 0, 2 * extra);
8355 extended = 1;
8357 j = isl_basic_map_alloc_div(dst);
8358 if (j < 0)
8359 return isl_basic_map_free(dst);
8360 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8361 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8362 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8363 return isl_basic_map_free(dst);
8365 if (j != i)
8366 isl_basic_map_swap_div(dst, i, j);
8368 return dst;
8371 struct isl_basic_set *isl_basic_set_align_divs(
8372 struct isl_basic_set *dst, struct isl_basic_set *src)
8374 return (struct isl_basic_set *)isl_basic_map_align_divs(
8375 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8378 struct isl_map *isl_map_align_divs(struct isl_map *map)
8380 int i;
8382 if (!map)
8383 return NULL;
8384 if (map->n == 0)
8385 return map;
8386 map = isl_map_compute_divs(map);
8387 map = isl_map_cow(map);
8388 if (!map)
8389 return NULL;
8391 for (i = 1; i < map->n; ++i)
8392 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8393 for (i = 1; i < map->n; ++i) {
8394 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8395 if (!map->p[i])
8396 return isl_map_free(map);
8399 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8400 return map;
8403 struct isl_set *isl_set_align_divs(struct isl_set *set)
8405 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8408 /* Align the divs of the basic maps in "map" to those
8409 * of the basic maps in "list", as well as to the other basic maps in "map".
8410 * The elements in "list" are assumed to have known divs.
8412 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8413 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8415 int i, n;
8417 map = isl_map_compute_divs(map);
8418 map = isl_map_cow(map);
8419 if (!map || !list)
8420 return isl_map_free(map);
8421 if (map->n == 0)
8422 return map;
8424 n = isl_basic_map_list_n_basic_map(list);
8425 for (i = 0; i < n; ++i) {
8426 isl_basic_map *bmap;
8428 bmap = isl_basic_map_list_get_basic_map(list, i);
8429 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8430 isl_basic_map_free(bmap);
8432 if (!map->p[0])
8433 return isl_map_free(map);
8435 return isl_map_align_divs(map);
8438 /* Align the divs of each element of "list" to those of "bmap".
8439 * Both "bmap" and the elements of "list" are assumed to have known divs.
8441 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8442 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8444 int i, n;
8446 if (!list || !bmap)
8447 return isl_basic_map_list_free(list);
8449 n = isl_basic_map_list_n_basic_map(list);
8450 for (i = 0; i < n; ++i) {
8451 isl_basic_map *bmap_i;
8453 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8454 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8455 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8458 return list;
8461 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8462 __isl_take isl_map *map)
8464 if (!set || !map)
8465 goto error;
8466 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8467 map = isl_map_intersect_domain(map, set);
8468 set = isl_map_range(map);
8469 return set;
8470 error:
8471 isl_set_free(set);
8472 isl_map_free(map);
8473 return NULL;
8476 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8477 __isl_take isl_map *map)
8479 return isl_map_align_params_map_map_and(set, map, &set_apply);
8482 /* There is no need to cow as removing empty parts doesn't change
8483 * the meaning of the set.
8485 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8487 int i;
8489 if (!map)
8490 return NULL;
8492 for (i = map->n - 1; i >= 0; --i)
8493 remove_if_empty(map, i);
8495 return map;
8498 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8500 return (struct isl_set *)
8501 isl_map_remove_empty_parts((struct isl_map *)set);
8504 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8506 struct isl_basic_map *bmap;
8507 if (!map || map->n == 0)
8508 return NULL;
8509 bmap = map->p[map->n-1];
8510 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8511 return isl_basic_map_copy(bmap);
8514 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8516 return (struct isl_basic_set *)
8517 isl_map_copy_basic_map((struct isl_map *)set);
8520 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8521 __isl_keep isl_basic_map *bmap)
8523 int i;
8525 if (!map || !bmap)
8526 goto error;
8527 for (i = map->n-1; i >= 0; --i) {
8528 if (map->p[i] != bmap)
8529 continue;
8530 map = isl_map_cow(map);
8531 if (!map)
8532 goto error;
8533 isl_basic_map_free(map->p[i]);
8534 if (i != map->n-1) {
8535 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8536 map->p[i] = map->p[map->n-1];
8538 map->n--;
8539 return map;
8541 return map;
8542 error:
8543 isl_map_free(map);
8544 return NULL;
8547 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8548 struct isl_basic_set *bset)
8550 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8551 (struct isl_basic_map *)bset);
8554 /* Given two basic sets bset1 and bset2, compute the maximal difference
8555 * between the values of dimension pos in bset1 and those in bset2
8556 * for any common value of the parameters and dimensions preceding pos.
8558 static enum isl_lp_result basic_set_maximal_difference_at(
8559 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8560 int pos, isl_int *opt)
8562 isl_space *dims;
8563 struct isl_basic_map *bmap1 = NULL;
8564 struct isl_basic_map *bmap2 = NULL;
8565 struct isl_ctx *ctx;
8566 struct isl_vec *obj;
8567 unsigned total;
8568 unsigned nparam;
8569 unsigned dim1, dim2;
8570 enum isl_lp_result res;
8572 if (!bset1 || !bset2)
8573 return isl_lp_error;
8575 nparam = isl_basic_set_n_param(bset1);
8576 dim1 = isl_basic_set_n_dim(bset1);
8577 dim2 = isl_basic_set_n_dim(bset2);
8578 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8579 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8580 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8581 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8582 if (!bmap1 || !bmap2)
8583 goto error;
8584 bmap1 = isl_basic_map_cow(bmap1);
8585 bmap1 = isl_basic_map_extend(bmap1, nparam,
8586 pos, (dim1 - pos) + (dim2 - pos),
8587 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8588 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8589 if (!bmap1)
8590 goto error2;
8591 total = isl_basic_map_total_dim(bmap1);
8592 ctx = bmap1->ctx;
8593 obj = isl_vec_alloc(ctx, 1 + total);
8594 if (!obj)
8595 goto error2;
8596 isl_seq_clr(obj->block.data, 1 + total);
8597 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8598 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8599 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8600 opt, NULL, NULL);
8601 isl_basic_map_free(bmap1);
8602 isl_vec_free(obj);
8603 return res;
8604 error:
8605 isl_basic_map_free(bmap2);
8606 error2:
8607 isl_basic_map_free(bmap1);
8608 return isl_lp_error;
8611 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8612 * for any common value of the parameters and dimensions preceding pos
8613 * in both basic sets, the values of dimension pos in bset1 are
8614 * smaller or larger than those in bset2.
8616 * Returns
8617 * 1 if bset1 follows bset2
8618 * -1 if bset1 precedes bset2
8619 * 0 if bset1 and bset2 are incomparable
8620 * -2 if some error occurred.
8622 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8623 struct isl_basic_set *bset2, int pos)
8625 isl_int opt;
8626 enum isl_lp_result res;
8627 int cmp;
8629 isl_int_init(opt);
8631 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8633 if (res == isl_lp_empty)
8634 cmp = 0;
8635 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8636 res == isl_lp_unbounded)
8637 cmp = 1;
8638 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8639 cmp = -1;
8640 else
8641 cmp = -2;
8643 isl_int_clear(opt);
8644 return cmp;
8647 /* Given two basic sets bset1 and bset2, check whether
8648 * for any common value of the parameters and dimensions preceding pos
8649 * there is a value of dimension pos in bset1 that is larger
8650 * than a value of the same dimension in bset2.
8652 * Return
8653 * 1 if there exists such a pair
8654 * 0 if there is no such pair, but there is a pair of equal values
8655 * -1 otherwise
8656 * -2 if some error occurred.
8658 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8659 __isl_keep isl_basic_set *bset2, int pos)
8661 isl_int opt;
8662 enum isl_lp_result res;
8663 int cmp;
8665 isl_int_init(opt);
8667 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8669 if (res == isl_lp_empty)
8670 cmp = -1;
8671 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8672 res == isl_lp_unbounded)
8673 cmp = 1;
8674 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8675 cmp = -1;
8676 else if (res == isl_lp_ok)
8677 cmp = 0;
8678 else
8679 cmp = -2;
8681 isl_int_clear(opt);
8682 return cmp;
8685 /* Given two sets set1 and set2, check whether
8686 * for any common value of the parameters and dimensions preceding pos
8687 * there is a value of dimension pos in set1 that is larger
8688 * than a value of the same dimension in set2.
8690 * Return
8691 * 1 if there exists such a pair
8692 * 0 if there is no such pair, but there is a pair of equal values
8693 * -1 otherwise
8694 * -2 if some error occurred.
8696 int isl_set_follows_at(__isl_keep isl_set *set1,
8697 __isl_keep isl_set *set2, int pos)
8699 int i, j;
8700 int follows = -1;
8702 if (!set1 || !set2)
8703 return -2;
8705 for (i = 0; i < set1->n; ++i)
8706 for (j = 0; j < set2->n; ++j) {
8707 int f;
8708 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8709 if (f == 1 || f == -2)
8710 return f;
8711 if (f > follows)
8712 follows = f;
8715 return follows;
8718 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8719 unsigned pos, isl_int *val)
8721 int i;
8722 int d;
8723 unsigned total;
8725 if (!bmap)
8726 return -1;
8727 total = isl_basic_map_total_dim(bmap);
8728 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8729 for (; d+1 > pos; --d)
8730 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8731 break;
8732 if (d != pos)
8733 continue;
8734 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8735 return 0;
8736 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8737 return 0;
8738 if (!isl_int_is_one(bmap->eq[i][1+d]))
8739 return 0;
8740 if (val)
8741 isl_int_neg(*val, bmap->eq[i][0]);
8742 return 1;
8744 return 0;
8747 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8748 unsigned pos, isl_int *val)
8750 int i;
8751 isl_int v;
8752 isl_int tmp;
8753 int fixed;
8755 if (!map)
8756 return -1;
8757 if (map->n == 0)
8758 return 0;
8759 if (map->n == 1)
8760 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8761 isl_int_init(v);
8762 isl_int_init(tmp);
8763 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8764 for (i = 1; fixed == 1 && i < map->n; ++i) {
8765 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8766 if (fixed == 1 && isl_int_ne(tmp, v))
8767 fixed = 0;
8769 if (val)
8770 isl_int_set(*val, v);
8771 isl_int_clear(tmp);
8772 isl_int_clear(v);
8773 return fixed;
8776 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8777 unsigned pos, isl_int *val)
8779 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8780 pos, val);
8783 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8784 isl_int *val)
8786 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8789 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8790 enum isl_dim_type type, unsigned pos, isl_int *val)
8792 if (pos >= isl_basic_map_dim(bmap, type))
8793 return -1;
8794 return isl_basic_map_plain_has_fixed_var(bmap,
8795 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8798 /* If "bmap" obviously lies on a hyperplane where the given dimension
8799 * has a fixed value, then return that value.
8800 * Otherwise return NaN.
8802 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8803 __isl_keep isl_basic_map *bmap,
8804 enum isl_dim_type type, unsigned pos)
8806 isl_ctx *ctx;
8807 isl_val *v;
8808 int fixed;
8810 if (!bmap)
8811 return NULL;
8812 ctx = isl_basic_map_get_ctx(bmap);
8813 v = isl_val_alloc(ctx);
8814 if (!v)
8815 return NULL;
8816 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8817 if (fixed < 0)
8818 return isl_val_free(v);
8819 if (fixed) {
8820 isl_int_set_si(v->d, 1);
8821 return v;
8823 isl_val_free(v);
8824 return isl_val_nan(ctx);
8827 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8828 enum isl_dim_type type, unsigned pos, isl_int *val)
8830 if (pos >= isl_map_dim(map, type))
8831 return -1;
8832 return isl_map_plain_has_fixed_var(map,
8833 map_offset(map, type) - 1 + pos, val);
8836 /* If "map" obviously lies on a hyperplane where the given dimension
8837 * has a fixed value, then return that value.
8838 * Otherwise return NaN.
8840 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8841 enum isl_dim_type type, unsigned pos)
8843 isl_ctx *ctx;
8844 isl_val *v;
8845 int fixed;
8847 if (!map)
8848 return NULL;
8849 ctx = isl_map_get_ctx(map);
8850 v = isl_val_alloc(ctx);
8851 if (!v)
8852 return NULL;
8853 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8854 if (fixed < 0)
8855 return isl_val_free(v);
8856 if (fixed) {
8857 isl_int_set_si(v->d, 1);
8858 return v;
8860 isl_val_free(v);
8861 return isl_val_nan(ctx);
8864 /* If "set" obviously lies on a hyperplane where the given dimension
8865 * has a fixed value, then return that value.
8866 * Otherwise return NaN.
8868 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8869 enum isl_dim_type type, unsigned pos)
8871 return isl_map_plain_get_val_if_fixed(set, type, pos);
8874 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8875 enum isl_dim_type type, unsigned pos, isl_int *val)
8877 return isl_map_plain_is_fixed(set, type, pos, val);
8880 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8881 * then return this fixed value in *val.
8883 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8884 unsigned dim, isl_int *val)
8886 return isl_basic_set_plain_has_fixed_var(bset,
8887 isl_basic_set_n_param(bset) + dim, val);
8890 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8891 * then return this fixed value in *val.
8893 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8894 unsigned dim, isl_int *val)
8896 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8899 /* Check if input variable in has fixed value and if so and if val is not NULL,
8900 * then return this fixed value in *val.
8902 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8903 unsigned in, isl_int *val)
8905 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8908 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8909 * and if val is not NULL, then return this lower bound in *val.
8911 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8912 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8914 int i, i_eq = -1, i_ineq = -1;
8915 isl_int *c;
8916 unsigned total;
8917 unsigned nparam;
8919 if (!bset)
8920 return -1;
8921 total = isl_basic_set_total_dim(bset);
8922 nparam = isl_basic_set_n_param(bset);
8923 for (i = 0; i < bset->n_eq; ++i) {
8924 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8925 continue;
8926 if (i_eq != -1)
8927 return 0;
8928 i_eq = i;
8930 for (i = 0; i < bset->n_ineq; ++i) {
8931 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8932 continue;
8933 if (i_eq != -1 || i_ineq != -1)
8934 return 0;
8935 i_ineq = i;
8937 if (i_eq == -1 && i_ineq == -1)
8938 return 0;
8939 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8940 /* The coefficient should always be one due to normalization. */
8941 if (!isl_int_is_one(c[1+nparam+dim]))
8942 return 0;
8943 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8944 return 0;
8945 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8946 total - nparam - dim - 1) != -1)
8947 return 0;
8948 if (val)
8949 isl_int_neg(*val, c[0]);
8950 return 1;
8953 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8954 unsigned dim, isl_int *val)
8956 int i;
8957 isl_int v;
8958 isl_int tmp;
8959 int fixed;
8961 if (!set)
8962 return -1;
8963 if (set->n == 0)
8964 return 0;
8965 if (set->n == 1)
8966 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8967 dim, val);
8968 isl_int_init(v);
8969 isl_int_init(tmp);
8970 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8971 dim, &v);
8972 for (i = 1; fixed == 1 && i < set->n; ++i) {
8973 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8974 dim, &tmp);
8975 if (fixed == 1 && isl_int_ne(tmp, v))
8976 fixed = 0;
8978 if (val)
8979 isl_int_set(*val, v);
8980 isl_int_clear(tmp);
8981 isl_int_clear(v);
8982 return fixed;
8985 /* Return -1 if the constraint "c1" should be sorted before "c2"
8986 * and 1 if it should be sorted after "c2".
8987 * Return 0 if the two constraints are the same (up to the constant term).
8989 * In particular, if a constraint involves later variables than another
8990 * then it is sorted after this other constraint.
8991 * uset_gist depends on constraints without existentially quantified
8992 * variables sorting first.
8994 * For constraints that have the same latest variable, those
8995 * with the same coefficient for this latest variable (first in absolute value
8996 * and then in actual value) are grouped together.
8997 * This is useful for detecting pairs of constraints that can
8998 * be chained in their printed representation.
9000 * Finally, within a group, constraints are sorted according to
9001 * their coefficients (excluding the constant term).
9003 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9005 isl_int **c1 = (isl_int **) p1;
9006 isl_int **c2 = (isl_int **) p2;
9007 int l1, l2;
9008 unsigned size = *(unsigned *) arg;
9009 int cmp;
9011 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9012 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9014 if (l1 != l2)
9015 return l1 - l2;
9017 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9018 if (cmp != 0)
9019 return cmp;
9020 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9021 if (cmp != 0)
9022 return -cmp;
9024 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9027 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9028 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9029 * and 0 if the two constraints are the same (up to the constant term).
9031 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9032 isl_int *c1, isl_int *c2)
9034 unsigned total;
9036 if (!bmap)
9037 return -2;
9038 total = isl_basic_map_total_dim(bmap);
9039 return sort_constraint_cmp(&c1, &c2, &total);
9042 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9043 __isl_take isl_basic_map *bmap)
9045 unsigned total;
9047 if (!bmap)
9048 return NULL;
9049 if (bmap->n_ineq == 0)
9050 return bmap;
9051 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9052 return bmap;
9053 total = isl_basic_map_total_dim(bmap);
9054 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9055 &sort_constraint_cmp, &total) < 0)
9056 return isl_basic_map_free(bmap);
9057 return bmap;
9060 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9061 __isl_take isl_basic_set *bset)
9063 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9064 (struct isl_basic_map *)bset);
9067 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9069 if (!bmap)
9070 return NULL;
9071 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9072 return bmap;
9073 bmap = isl_basic_map_remove_redundancies(bmap);
9074 bmap = isl_basic_map_sort_constraints(bmap);
9075 if (bmap)
9076 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9077 return bmap;
9080 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9082 return (struct isl_basic_set *)isl_basic_map_normalize(
9083 (struct isl_basic_map *)bset);
9086 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9087 const __isl_keep isl_basic_map *bmap2)
9089 int i, cmp;
9090 unsigned total;
9092 if (!bmap1 || !bmap2)
9093 return -1;
9095 if (bmap1 == bmap2)
9096 return 0;
9097 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9098 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9099 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9100 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9101 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9102 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9103 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9104 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9105 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9106 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9107 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9108 return 0;
9109 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9110 return 1;
9111 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9112 return -1;
9113 if (bmap1->n_eq != bmap2->n_eq)
9114 return bmap1->n_eq - bmap2->n_eq;
9115 if (bmap1->n_ineq != bmap2->n_ineq)
9116 return bmap1->n_ineq - bmap2->n_ineq;
9117 if (bmap1->n_div != bmap2->n_div)
9118 return bmap1->n_div - bmap2->n_div;
9119 total = isl_basic_map_total_dim(bmap1);
9120 for (i = 0; i < bmap1->n_eq; ++i) {
9121 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9122 if (cmp)
9123 return cmp;
9125 for (i = 0; i < bmap1->n_ineq; ++i) {
9126 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9127 if (cmp)
9128 return cmp;
9130 for (i = 0; i < bmap1->n_div; ++i) {
9131 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9132 if (cmp)
9133 return cmp;
9135 return 0;
9138 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9139 const __isl_keep isl_basic_set *bset2)
9141 return isl_basic_map_plain_cmp(bset1, bset2);
9144 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9146 int i, cmp;
9148 if (set1 == set2)
9149 return 0;
9150 if (set1->n != set2->n)
9151 return set1->n - set2->n;
9153 for (i = 0; i < set1->n; ++i) {
9154 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9155 if (cmp)
9156 return cmp;
9159 return 0;
9162 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9163 __isl_keep isl_basic_map *bmap2)
9165 if (!bmap1 || !bmap2)
9166 return isl_bool_error;
9167 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9170 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9171 __isl_keep isl_basic_set *bset2)
9173 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9174 (isl_basic_map *)bset2);
9177 static int qsort_bmap_cmp(const void *p1, const void *p2)
9179 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9180 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9182 return isl_basic_map_plain_cmp(bmap1, bmap2);
9185 /* Sort the basic maps of "map" and remove duplicate basic maps.
9187 * While removing basic maps, we make sure that the basic maps remain
9188 * sorted because isl_map_normalize expects the basic maps of the result
9189 * to be sorted.
9191 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9193 int i, j;
9195 map = isl_map_remove_empty_parts(map);
9196 if (!map)
9197 return NULL;
9198 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9199 for (i = map->n - 1; i >= 1; --i) {
9200 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9201 continue;
9202 isl_basic_map_free(map->p[i-1]);
9203 for (j = i; j < map->n; ++j)
9204 map->p[j - 1] = map->p[j];
9205 map->n--;
9208 return map;
9211 /* Remove obvious duplicates among the basic maps of "map".
9213 * Unlike isl_map_normalize, this function does not remove redundant
9214 * constraints and only removes duplicates that have exactly the same
9215 * constraints in the input. It does sort the constraints and
9216 * the basic maps to ease the detection of duplicates.
9218 * If "map" has already been normalized or if the basic maps are
9219 * disjoint, then there can be no duplicates.
9221 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9223 int i;
9224 isl_basic_map *bmap;
9226 if (!map)
9227 return NULL;
9228 if (map->n <= 1)
9229 return map;
9230 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9231 return map;
9232 for (i = 0; i < map->n; ++i) {
9233 bmap = isl_basic_map_copy(map->p[i]);
9234 bmap = isl_basic_map_sort_constraints(bmap);
9235 if (!bmap)
9236 return isl_map_free(map);
9237 isl_basic_map_free(map->p[i]);
9238 map->p[i] = bmap;
9241 map = sort_and_remove_duplicates(map);
9242 return map;
9245 /* We normalize in place, but if anything goes wrong we need
9246 * to return NULL, so we need to make sure we don't change the
9247 * meaning of any possible other copies of map.
9249 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9251 int i;
9252 struct isl_basic_map *bmap;
9254 if (!map)
9255 return NULL;
9256 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9257 return map;
9258 for (i = 0; i < map->n; ++i) {
9259 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9260 if (!bmap)
9261 goto error;
9262 isl_basic_map_free(map->p[i]);
9263 map->p[i] = bmap;
9266 map = sort_and_remove_duplicates(map);
9267 if (map)
9268 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9269 return map;
9270 error:
9271 isl_map_free(map);
9272 return NULL;
9275 struct isl_set *isl_set_normalize(struct isl_set *set)
9277 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9280 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9281 __isl_keep isl_map *map2)
9283 int i;
9284 isl_bool equal;
9286 if (!map1 || !map2)
9287 return isl_bool_error;
9289 if (map1 == map2)
9290 return isl_bool_true;
9291 if (!isl_space_is_equal(map1->dim, map2->dim))
9292 return isl_bool_false;
9294 map1 = isl_map_copy(map1);
9295 map2 = isl_map_copy(map2);
9296 map1 = isl_map_normalize(map1);
9297 map2 = isl_map_normalize(map2);
9298 if (!map1 || !map2)
9299 goto error;
9300 equal = map1->n == map2->n;
9301 for (i = 0; equal && i < map1->n; ++i) {
9302 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9303 if (equal < 0)
9304 goto error;
9306 isl_map_free(map1);
9307 isl_map_free(map2);
9308 return equal;
9309 error:
9310 isl_map_free(map1);
9311 isl_map_free(map2);
9312 return isl_bool_error;
9315 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9316 __isl_keep isl_set *set2)
9318 return isl_map_plain_is_equal((struct isl_map *)set1,
9319 (struct isl_map *)set2);
9322 /* Return an interval that ranges from min to max (inclusive)
9324 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9325 isl_int min, isl_int max)
9327 int k;
9328 struct isl_basic_set *bset = NULL;
9330 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9331 if (!bset)
9332 goto error;
9334 k = isl_basic_set_alloc_inequality(bset);
9335 if (k < 0)
9336 goto error;
9337 isl_int_set_si(bset->ineq[k][1], 1);
9338 isl_int_neg(bset->ineq[k][0], min);
9340 k = isl_basic_set_alloc_inequality(bset);
9341 if (k < 0)
9342 goto error;
9343 isl_int_set_si(bset->ineq[k][1], -1);
9344 isl_int_set(bset->ineq[k][0], max);
9346 return bset;
9347 error:
9348 isl_basic_set_free(bset);
9349 return NULL;
9352 /* Return the basic maps in "map" as a list.
9354 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9355 __isl_keep isl_map *map)
9357 int i;
9358 isl_ctx *ctx;
9359 isl_basic_map_list *list;
9361 if (!map)
9362 return NULL;
9363 ctx = isl_map_get_ctx(map);
9364 list = isl_basic_map_list_alloc(ctx, map->n);
9366 for (i = 0; i < map->n; ++i) {
9367 isl_basic_map *bmap;
9369 bmap = isl_basic_map_copy(map->p[i]);
9370 list = isl_basic_map_list_add(list, bmap);
9373 return list;
9376 /* Return the intersection of the elements in the non-empty list "list".
9377 * All elements are assumed to live in the same space.
9379 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9380 __isl_take isl_basic_map_list *list)
9382 int i, n;
9383 isl_basic_map *bmap;
9385 if (!list)
9386 return NULL;
9387 n = isl_basic_map_list_n_basic_map(list);
9388 if (n < 1)
9389 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9390 "expecting non-empty list", goto error);
9392 bmap = isl_basic_map_list_get_basic_map(list, 0);
9393 for (i = 1; i < n; ++i) {
9394 isl_basic_map *bmap_i;
9396 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9397 bmap = isl_basic_map_intersect(bmap, bmap_i);
9400 isl_basic_map_list_free(list);
9401 return bmap;
9402 error:
9403 isl_basic_map_list_free(list);
9404 return NULL;
9407 /* Return the intersection of the elements in the non-empty list "list".
9408 * All elements are assumed to live in the same space.
9410 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9411 __isl_take isl_basic_set_list *list)
9413 return isl_basic_map_list_intersect(list);
9416 /* Return the union of the elements in the non-empty list "list".
9417 * All elements are assumed to live in the same space.
9419 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9421 int i, n;
9422 isl_set *set;
9424 if (!list)
9425 return NULL;
9426 n = isl_set_list_n_set(list);
9427 if (n < 1)
9428 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9429 "expecting non-empty list", goto error);
9431 set = isl_set_list_get_set(list, 0);
9432 for (i = 1; i < n; ++i) {
9433 isl_set *set_i;
9435 set_i = isl_set_list_get_set(list, i);
9436 set = isl_set_union(set, set_i);
9439 isl_set_list_free(list);
9440 return set;
9441 error:
9442 isl_set_list_free(list);
9443 return NULL;
9446 /* Return the Cartesian product of the basic sets in list (in the given order).
9448 __isl_give isl_basic_set *isl_basic_set_list_product(
9449 __isl_take struct isl_basic_set_list *list)
9451 int i;
9452 unsigned dim;
9453 unsigned nparam;
9454 unsigned extra;
9455 unsigned n_eq;
9456 unsigned n_ineq;
9457 struct isl_basic_set *product = NULL;
9459 if (!list)
9460 goto error;
9461 isl_assert(list->ctx, list->n > 0, goto error);
9462 isl_assert(list->ctx, list->p[0], goto error);
9463 nparam = isl_basic_set_n_param(list->p[0]);
9464 dim = isl_basic_set_n_dim(list->p[0]);
9465 extra = list->p[0]->n_div;
9466 n_eq = list->p[0]->n_eq;
9467 n_ineq = list->p[0]->n_ineq;
9468 for (i = 1; i < list->n; ++i) {
9469 isl_assert(list->ctx, list->p[i], goto error);
9470 isl_assert(list->ctx,
9471 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9472 dim += isl_basic_set_n_dim(list->p[i]);
9473 extra += list->p[i]->n_div;
9474 n_eq += list->p[i]->n_eq;
9475 n_ineq += list->p[i]->n_ineq;
9477 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9478 n_eq, n_ineq);
9479 if (!product)
9480 goto error;
9481 dim = 0;
9482 for (i = 0; i < list->n; ++i) {
9483 isl_basic_set_add_constraints(product,
9484 isl_basic_set_copy(list->p[i]), dim);
9485 dim += isl_basic_set_n_dim(list->p[i]);
9487 isl_basic_set_list_free(list);
9488 return product;
9489 error:
9490 isl_basic_set_free(product);
9491 isl_basic_set_list_free(list);
9492 return NULL;
9495 struct isl_basic_map *isl_basic_map_product(
9496 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9498 isl_space *dim_result = NULL;
9499 struct isl_basic_map *bmap;
9500 unsigned in1, in2, out1, out2, nparam, total, pos;
9501 struct isl_dim_map *dim_map1, *dim_map2;
9503 if (!bmap1 || !bmap2)
9504 goto error;
9506 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9507 bmap2->dim, isl_dim_param), goto error);
9508 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9509 isl_space_copy(bmap2->dim));
9511 in1 = isl_basic_map_n_in(bmap1);
9512 in2 = isl_basic_map_n_in(bmap2);
9513 out1 = isl_basic_map_n_out(bmap1);
9514 out2 = isl_basic_map_n_out(bmap2);
9515 nparam = isl_basic_map_n_param(bmap1);
9517 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9518 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9519 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9520 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9521 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9522 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9523 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9524 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9525 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9526 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9527 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9529 bmap = isl_basic_map_alloc_space(dim_result,
9530 bmap1->n_div + bmap2->n_div,
9531 bmap1->n_eq + bmap2->n_eq,
9532 bmap1->n_ineq + bmap2->n_ineq);
9533 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9534 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9535 bmap = isl_basic_map_simplify(bmap);
9536 return isl_basic_map_finalize(bmap);
9537 error:
9538 isl_basic_map_free(bmap1);
9539 isl_basic_map_free(bmap2);
9540 return NULL;
9543 __isl_give isl_basic_map *isl_basic_map_flat_product(
9544 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9546 isl_basic_map *prod;
9548 prod = isl_basic_map_product(bmap1, bmap2);
9549 prod = isl_basic_map_flatten(prod);
9550 return prod;
9553 __isl_give isl_basic_set *isl_basic_set_flat_product(
9554 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9556 return isl_basic_map_flat_range_product(bset1, bset2);
9559 __isl_give isl_basic_map *isl_basic_map_domain_product(
9560 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9562 isl_space *space_result = NULL;
9563 isl_basic_map *bmap;
9564 unsigned in1, in2, out, nparam, total, pos;
9565 struct isl_dim_map *dim_map1, *dim_map2;
9567 if (!bmap1 || !bmap2)
9568 goto error;
9570 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9571 isl_space_copy(bmap2->dim));
9573 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9574 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9575 out = isl_basic_map_dim(bmap1, isl_dim_out);
9576 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9578 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9579 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9580 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9581 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9582 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9583 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9584 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9585 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9586 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9587 isl_dim_map_div(dim_map1, bmap1, pos += out);
9588 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9590 bmap = isl_basic_map_alloc_space(space_result,
9591 bmap1->n_div + bmap2->n_div,
9592 bmap1->n_eq + bmap2->n_eq,
9593 bmap1->n_ineq + bmap2->n_ineq);
9594 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9595 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9596 bmap = isl_basic_map_simplify(bmap);
9597 return isl_basic_map_finalize(bmap);
9598 error:
9599 isl_basic_map_free(bmap1);
9600 isl_basic_map_free(bmap2);
9601 return NULL;
9604 __isl_give isl_basic_map *isl_basic_map_range_product(
9605 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9607 isl_space *dim_result = NULL;
9608 isl_basic_map *bmap;
9609 unsigned in, out1, out2, nparam, total, pos;
9610 struct isl_dim_map *dim_map1, *dim_map2;
9612 if (!bmap1 || !bmap2)
9613 goto error;
9615 if (!isl_space_match(bmap1->dim, isl_dim_param,
9616 bmap2->dim, isl_dim_param))
9617 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9618 "parameters don't match", goto error);
9620 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9621 isl_space_copy(bmap2->dim));
9623 in = isl_basic_map_dim(bmap1, isl_dim_in);
9624 out1 = isl_basic_map_n_out(bmap1);
9625 out2 = isl_basic_map_n_out(bmap2);
9626 nparam = isl_basic_map_n_param(bmap1);
9628 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9629 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9630 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9631 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9632 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9633 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9634 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9635 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9636 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9637 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9638 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9640 bmap = isl_basic_map_alloc_space(dim_result,
9641 bmap1->n_div + bmap2->n_div,
9642 bmap1->n_eq + bmap2->n_eq,
9643 bmap1->n_ineq + bmap2->n_ineq);
9644 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9645 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9646 bmap = isl_basic_map_simplify(bmap);
9647 return isl_basic_map_finalize(bmap);
9648 error:
9649 isl_basic_map_free(bmap1);
9650 isl_basic_map_free(bmap2);
9651 return NULL;
9654 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9655 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9657 isl_basic_map *prod;
9659 prod = isl_basic_map_range_product(bmap1, bmap2);
9660 prod = isl_basic_map_flatten_range(prod);
9661 return prod;
9664 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9665 * and collect the results.
9666 * The result live in the space obtained by calling "space_product"
9667 * on the spaces of "map1" and "map2".
9668 * If "remove_duplicates" is set then the result may contain duplicates
9669 * (even if the inputs do not) and so we try and remove the obvious
9670 * duplicates.
9672 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9673 __isl_take isl_map *map2,
9674 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9675 __isl_take isl_space *right),
9676 __isl_give isl_basic_map *(*basic_map_product)(
9677 __isl_take isl_basic_map *left,
9678 __isl_take isl_basic_map *right),
9679 int remove_duplicates)
9681 unsigned flags = 0;
9682 struct isl_map *result;
9683 int i, j;
9685 if (!map1 || !map2)
9686 goto error;
9688 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9689 map2->dim, isl_dim_param), goto error);
9691 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9692 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9693 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9695 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9696 isl_space_copy(map2->dim)),
9697 map1->n * map2->n, flags);
9698 if (!result)
9699 goto error;
9700 for (i = 0; i < map1->n; ++i)
9701 for (j = 0; j < map2->n; ++j) {
9702 struct isl_basic_map *part;
9703 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9704 isl_basic_map_copy(map2->p[j]));
9705 if (isl_basic_map_is_empty(part))
9706 isl_basic_map_free(part);
9707 else
9708 result = isl_map_add_basic_map(result, part);
9709 if (!result)
9710 goto error;
9712 if (remove_duplicates)
9713 result = isl_map_remove_obvious_duplicates(result);
9714 isl_map_free(map1);
9715 isl_map_free(map2);
9716 return result;
9717 error:
9718 isl_map_free(map1);
9719 isl_map_free(map2);
9720 return NULL;
9723 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9725 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9726 __isl_take isl_map *map2)
9728 return map_product(map1, map2, &isl_space_product,
9729 &isl_basic_map_product, 0);
9732 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9733 __isl_take isl_map *map2)
9735 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9738 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9740 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9741 __isl_take isl_map *map2)
9743 isl_map *prod;
9745 prod = isl_map_product(map1, map2);
9746 prod = isl_map_flatten(prod);
9747 return prod;
9750 /* Given two set A and B, construct its Cartesian product A x B.
9752 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9754 return isl_map_range_product(set1, set2);
9757 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9758 __isl_take isl_set *set2)
9760 return isl_map_flat_range_product(set1, set2);
9763 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9765 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9766 __isl_take isl_map *map2)
9768 return map_product(map1, map2, &isl_space_domain_product,
9769 &isl_basic_map_domain_product, 1);
9772 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9774 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9775 __isl_take isl_map *map2)
9777 return map_product(map1, map2, &isl_space_range_product,
9778 &isl_basic_map_range_product, 1);
9781 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9782 __isl_take isl_map *map2)
9784 return isl_map_align_params_map_map_and(map1, map2,
9785 &map_domain_product_aligned);
9788 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9789 __isl_take isl_map *map2)
9791 return isl_map_align_params_map_map_and(map1, map2,
9792 &map_range_product_aligned);
9795 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9797 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9799 isl_space *space;
9800 int total1, keep1, total2, keep2;
9802 if (!map)
9803 return NULL;
9804 if (!isl_space_domain_is_wrapping(map->dim) ||
9805 !isl_space_range_is_wrapping(map->dim))
9806 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9807 "not a product", return isl_map_free(map));
9809 space = isl_map_get_space(map);
9810 total1 = isl_space_dim(space, isl_dim_in);
9811 total2 = isl_space_dim(space, isl_dim_out);
9812 space = isl_space_factor_domain(space);
9813 keep1 = isl_space_dim(space, isl_dim_in);
9814 keep2 = isl_space_dim(space, isl_dim_out);
9815 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9816 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9817 map = isl_map_reset_space(map, space);
9819 return map;
9822 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9824 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9826 isl_space *space;
9827 int total1, keep1, total2, keep2;
9829 if (!map)
9830 return NULL;
9831 if (!isl_space_domain_is_wrapping(map->dim) ||
9832 !isl_space_range_is_wrapping(map->dim))
9833 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9834 "not a product", return isl_map_free(map));
9836 space = isl_map_get_space(map);
9837 total1 = isl_space_dim(space, isl_dim_in);
9838 total2 = isl_space_dim(space, isl_dim_out);
9839 space = isl_space_factor_range(space);
9840 keep1 = isl_space_dim(space, isl_dim_in);
9841 keep2 = isl_space_dim(space, isl_dim_out);
9842 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9843 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9844 map = isl_map_reset_space(map, space);
9846 return map;
9849 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9851 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9853 isl_space *space;
9854 int total, keep;
9856 if (!map)
9857 return NULL;
9858 if (!isl_space_domain_is_wrapping(map->dim))
9859 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9860 "domain is not a product", return isl_map_free(map));
9862 space = isl_map_get_space(map);
9863 total = isl_space_dim(space, isl_dim_in);
9864 space = isl_space_domain_factor_domain(space);
9865 keep = isl_space_dim(space, isl_dim_in);
9866 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9867 map = isl_map_reset_space(map, space);
9869 return map;
9872 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9874 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9876 isl_space *space;
9877 int total, keep;
9879 if (!map)
9880 return NULL;
9881 if (!isl_space_domain_is_wrapping(map->dim))
9882 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9883 "domain is not a product", return isl_map_free(map));
9885 space = isl_map_get_space(map);
9886 total = isl_space_dim(space, isl_dim_in);
9887 space = isl_space_domain_factor_range(space);
9888 keep = isl_space_dim(space, isl_dim_in);
9889 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9890 map = isl_map_reset_space(map, space);
9892 return map;
9895 /* Given a map A -> [B -> C], extract the map A -> B.
9897 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9899 isl_space *space;
9900 int total, keep;
9902 if (!map)
9903 return NULL;
9904 if (!isl_space_range_is_wrapping(map->dim))
9905 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9906 "range is not a product", return isl_map_free(map));
9908 space = isl_map_get_space(map);
9909 total = isl_space_dim(space, isl_dim_out);
9910 space = isl_space_range_factor_domain(space);
9911 keep = isl_space_dim(space, isl_dim_out);
9912 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9913 map = isl_map_reset_space(map, space);
9915 return map;
9918 /* Given a map A -> [B -> C], extract the map A -> C.
9920 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9922 isl_space *space;
9923 int total, keep;
9925 if (!map)
9926 return NULL;
9927 if (!isl_space_range_is_wrapping(map->dim))
9928 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9929 "range is not a product", return isl_map_free(map));
9931 space = isl_map_get_space(map);
9932 total = isl_space_dim(space, isl_dim_out);
9933 space = isl_space_range_factor_range(space);
9934 keep = isl_space_dim(space, isl_dim_out);
9935 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9936 map = isl_map_reset_space(map, space);
9938 return map;
9941 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9943 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9944 __isl_take isl_map *map2)
9946 isl_map *prod;
9948 prod = isl_map_domain_product(map1, map2);
9949 prod = isl_map_flatten_domain(prod);
9950 return prod;
9953 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9955 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9956 __isl_take isl_map *map2)
9958 isl_map *prod;
9960 prod = isl_map_range_product(map1, map2);
9961 prod = isl_map_flatten_range(prod);
9962 return prod;
9965 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9967 int i;
9968 uint32_t hash = isl_hash_init();
9969 unsigned total;
9971 if (!bmap)
9972 return 0;
9973 bmap = isl_basic_map_copy(bmap);
9974 bmap = isl_basic_map_normalize(bmap);
9975 if (!bmap)
9976 return 0;
9977 total = isl_basic_map_total_dim(bmap);
9978 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9979 for (i = 0; i < bmap->n_eq; ++i) {
9980 uint32_t c_hash;
9981 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9982 isl_hash_hash(hash, c_hash);
9984 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9985 for (i = 0; i < bmap->n_ineq; ++i) {
9986 uint32_t c_hash;
9987 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9988 isl_hash_hash(hash, c_hash);
9990 isl_hash_byte(hash, bmap->n_div & 0xFF);
9991 for (i = 0; i < bmap->n_div; ++i) {
9992 uint32_t c_hash;
9993 if (isl_int_is_zero(bmap->div[i][0]))
9994 continue;
9995 isl_hash_byte(hash, i & 0xFF);
9996 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9997 isl_hash_hash(hash, c_hash);
9999 isl_basic_map_free(bmap);
10000 return hash;
10003 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10005 return isl_basic_map_get_hash((isl_basic_map *)bset);
10008 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10010 int i;
10011 uint32_t hash;
10013 if (!map)
10014 return 0;
10015 map = isl_map_copy(map);
10016 map = isl_map_normalize(map);
10017 if (!map)
10018 return 0;
10020 hash = isl_hash_init();
10021 for (i = 0; i < map->n; ++i) {
10022 uint32_t bmap_hash;
10023 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10024 isl_hash_hash(hash, bmap_hash);
10027 isl_map_free(map);
10029 return hash;
10032 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10034 return isl_map_get_hash((isl_map *)set);
10037 /* Check if the value for dimension dim is completely determined
10038 * by the values of the other parameters and variables.
10039 * That is, check if dimension dim is involved in an equality.
10041 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10043 int i;
10044 unsigned nparam;
10046 if (!bset)
10047 return -1;
10048 nparam = isl_basic_set_n_param(bset);
10049 for (i = 0; i < bset->n_eq; ++i)
10050 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10051 return 1;
10052 return 0;
10055 /* Check if the value for dimension dim is completely determined
10056 * by the values of the other parameters and variables.
10057 * That is, check if dimension dim is involved in an equality
10058 * for each of the subsets.
10060 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10062 int i;
10064 if (!set)
10065 return -1;
10066 for (i = 0; i < set->n; ++i) {
10067 int unique;
10068 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10069 if (unique != 1)
10070 return unique;
10072 return 1;
10075 /* Return the number of basic maps in the (current) representation of "map".
10077 int isl_map_n_basic_map(__isl_keep isl_map *map)
10079 return map ? map->n : 0;
10082 int isl_set_n_basic_set(__isl_keep isl_set *set)
10084 return set ? set->n : 0;
10087 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10088 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10090 int i;
10092 if (!map)
10093 return isl_stat_error;
10095 for (i = 0; i < map->n; ++i)
10096 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10097 return isl_stat_error;
10099 return isl_stat_ok;
10102 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10103 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10105 int i;
10107 if (!set)
10108 return isl_stat_error;
10110 for (i = 0; i < set->n; ++i)
10111 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10112 return isl_stat_error;
10114 return isl_stat_ok;
10117 /* Return a list of basic sets, the union of which is equal to "set".
10119 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10120 __isl_keep isl_set *set)
10122 int i;
10123 isl_basic_set_list *list;
10125 if (!set)
10126 return NULL;
10128 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10129 for (i = 0; i < set->n; ++i) {
10130 isl_basic_set *bset;
10132 bset = isl_basic_set_copy(set->p[i]);
10133 list = isl_basic_set_list_add(list, bset);
10136 return list;
10139 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10141 isl_space *dim;
10143 if (!bset)
10144 return NULL;
10146 bset = isl_basic_set_cow(bset);
10147 if (!bset)
10148 return NULL;
10150 dim = isl_basic_set_get_space(bset);
10151 dim = isl_space_lift(dim, bset->n_div);
10152 if (!dim)
10153 goto error;
10154 isl_space_free(bset->dim);
10155 bset->dim = dim;
10156 bset->extra -= bset->n_div;
10157 bset->n_div = 0;
10159 bset = isl_basic_set_finalize(bset);
10161 return bset;
10162 error:
10163 isl_basic_set_free(bset);
10164 return NULL;
10167 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10169 int i;
10170 isl_space *dim;
10171 unsigned n_div;
10173 set = isl_set_align_divs(set);
10175 if (!set)
10176 return NULL;
10178 set = isl_set_cow(set);
10179 if (!set)
10180 return NULL;
10182 n_div = set->p[0]->n_div;
10183 dim = isl_set_get_space(set);
10184 dim = isl_space_lift(dim, n_div);
10185 if (!dim)
10186 goto error;
10187 isl_space_free(set->dim);
10188 set->dim = dim;
10190 for (i = 0; i < set->n; ++i) {
10191 set->p[i] = isl_basic_set_lift(set->p[i]);
10192 if (!set->p[i])
10193 goto error;
10196 return set;
10197 error:
10198 isl_set_free(set);
10199 return NULL;
10202 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10204 isl_space *dim;
10205 struct isl_basic_map *bmap;
10206 unsigned n_set;
10207 unsigned n_div;
10208 unsigned n_param;
10209 unsigned total;
10210 int i, k, l;
10212 set = isl_set_align_divs(set);
10214 if (!set)
10215 return NULL;
10217 dim = isl_set_get_space(set);
10218 if (set->n == 0 || set->p[0]->n_div == 0) {
10219 isl_set_free(set);
10220 return isl_map_identity(isl_space_map_from_set(dim));
10223 n_div = set->p[0]->n_div;
10224 dim = isl_space_map_from_set(dim);
10225 n_param = isl_space_dim(dim, isl_dim_param);
10226 n_set = isl_space_dim(dim, isl_dim_in);
10227 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10228 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10229 for (i = 0; i < n_set; ++i)
10230 bmap = var_equal(bmap, i);
10232 total = n_param + n_set + n_set + n_div;
10233 for (i = 0; i < n_div; ++i) {
10234 k = isl_basic_map_alloc_inequality(bmap);
10235 if (k < 0)
10236 goto error;
10237 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10238 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10239 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10240 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10241 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10242 set->p[0]->div[i][0]);
10244 l = isl_basic_map_alloc_inequality(bmap);
10245 if (l < 0)
10246 goto error;
10247 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10248 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10249 set->p[0]->div[i][0]);
10250 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10253 isl_set_free(set);
10254 bmap = isl_basic_map_simplify(bmap);
10255 bmap = isl_basic_map_finalize(bmap);
10256 return isl_map_from_basic_map(bmap);
10257 error:
10258 isl_set_free(set);
10259 isl_basic_map_free(bmap);
10260 return NULL;
10263 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10265 unsigned dim;
10266 int size = 0;
10268 if (!bset)
10269 return -1;
10271 dim = isl_basic_set_total_dim(bset);
10272 size += bset->n_eq * (1 + dim);
10273 size += bset->n_ineq * (1 + dim);
10274 size += bset->n_div * (2 + dim);
10276 return size;
10279 int isl_set_size(__isl_keep isl_set *set)
10281 int i;
10282 int size = 0;
10284 if (!set)
10285 return -1;
10287 for (i = 0; i < set->n; ++i)
10288 size += isl_basic_set_size(set->p[i]);
10290 return size;
10293 /* Check if there is any lower bound (if lower == 0) and/or upper
10294 * bound (if upper == 0) on the specified dim.
10296 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10297 enum isl_dim_type type, unsigned pos, int lower, int upper)
10299 int i;
10301 if (!bmap)
10302 return isl_bool_error;
10304 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10305 return isl_bool_error);
10307 pos += isl_basic_map_offset(bmap, type);
10309 for (i = 0; i < bmap->n_div; ++i) {
10310 if (isl_int_is_zero(bmap->div[i][0]))
10311 continue;
10312 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10313 return isl_bool_true;
10316 for (i = 0; i < bmap->n_eq; ++i)
10317 if (!isl_int_is_zero(bmap->eq[i][pos]))
10318 return isl_bool_true;
10320 for (i = 0; i < bmap->n_ineq; ++i) {
10321 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10322 if (sgn > 0)
10323 lower = 1;
10324 if (sgn < 0)
10325 upper = 1;
10328 return lower && upper;
10331 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10332 enum isl_dim_type type, unsigned pos)
10334 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10337 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10338 enum isl_dim_type type, unsigned pos)
10340 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10343 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10344 enum isl_dim_type type, unsigned pos)
10346 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10349 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10350 enum isl_dim_type type, unsigned pos)
10352 int i;
10354 if (!map)
10355 return -1;
10357 for (i = 0; i < map->n; ++i) {
10358 int bounded;
10359 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10360 if (bounded < 0 || !bounded)
10361 return bounded;
10364 return 1;
10367 /* Return 1 if the specified dim is involved in both an upper bound
10368 * and a lower bound.
10370 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10371 enum isl_dim_type type, unsigned pos)
10373 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10376 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10378 static isl_bool has_any_bound(__isl_keep isl_map *map,
10379 enum isl_dim_type type, unsigned pos,
10380 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10381 enum isl_dim_type type, unsigned pos))
10383 int i;
10385 if (!map)
10386 return isl_bool_error;
10388 for (i = 0; i < map->n; ++i) {
10389 isl_bool bounded;
10390 bounded = fn(map->p[i], type, pos);
10391 if (bounded < 0 || bounded)
10392 return bounded;
10395 return isl_bool_false;
10398 /* Return 1 if the specified dim is involved in any lower bound.
10400 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10401 enum isl_dim_type type, unsigned pos)
10403 return has_any_bound(set, type, pos,
10404 &isl_basic_map_dim_has_lower_bound);
10407 /* Return 1 if the specified dim is involved in any upper bound.
10409 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10410 enum isl_dim_type type, unsigned pos)
10412 return has_any_bound(set, type, pos,
10413 &isl_basic_map_dim_has_upper_bound);
10416 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10418 static isl_bool has_bound(__isl_keep isl_map *map,
10419 enum isl_dim_type type, unsigned pos,
10420 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10421 enum isl_dim_type type, unsigned pos))
10423 int i;
10425 if (!map)
10426 return isl_bool_error;
10428 for (i = 0; i < map->n; ++i) {
10429 isl_bool bounded;
10430 bounded = fn(map->p[i], type, pos);
10431 if (bounded < 0 || !bounded)
10432 return bounded;
10435 return isl_bool_true;
10438 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10440 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10441 enum isl_dim_type type, unsigned pos)
10443 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10446 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10448 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10449 enum isl_dim_type type, unsigned pos)
10451 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10454 /* For each of the "n" variables starting at "first", determine
10455 * the sign of the variable and put the results in the first "n"
10456 * elements of the array "signs".
10457 * Sign
10458 * 1 means that the variable is non-negative
10459 * -1 means that the variable is non-positive
10460 * 0 means the variable attains both positive and negative values.
10462 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10463 unsigned first, unsigned n, int *signs)
10465 isl_vec *bound = NULL;
10466 struct isl_tab *tab = NULL;
10467 struct isl_tab_undo *snap;
10468 int i;
10470 if (!bset || !signs)
10471 return -1;
10473 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10474 tab = isl_tab_from_basic_set(bset, 0);
10475 if (!bound || !tab)
10476 goto error;
10478 isl_seq_clr(bound->el, bound->size);
10479 isl_int_set_si(bound->el[0], -1);
10481 snap = isl_tab_snap(tab);
10482 for (i = 0; i < n; ++i) {
10483 int empty;
10485 isl_int_set_si(bound->el[1 + first + i], -1);
10486 if (isl_tab_add_ineq(tab, bound->el) < 0)
10487 goto error;
10488 empty = tab->empty;
10489 isl_int_set_si(bound->el[1 + first + i], 0);
10490 if (isl_tab_rollback(tab, snap) < 0)
10491 goto error;
10493 if (empty) {
10494 signs[i] = 1;
10495 continue;
10498 isl_int_set_si(bound->el[1 + first + i], 1);
10499 if (isl_tab_add_ineq(tab, bound->el) < 0)
10500 goto error;
10501 empty = tab->empty;
10502 isl_int_set_si(bound->el[1 + first + i], 0);
10503 if (isl_tab_rollback(tab, snap) < 0)
10504 goto error;
10506 signs[i] = empty ? -1 : 0;
10509 isl_tab_free(tab);
10510 isl_vec_free(bound);
10511 return 0;
10512 error:
10513 isl_tab_free(tab);
10514 isl_vec_free(bound);
10515 return -1;
10518 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10519 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10521 if (!bset || !signs)
10522 return -1;
10523 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10524 return -1);
10526 first += pos(bset->dim, type) - 1;
10527 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10530 /* Is it possible for the integer division "div" to depend (possibly
10531 * indirectly) on any output dimensions?
10533 * If the div is undefined, then we conservatively assume that it
10534 * may depend on them.
10535 * Otherwise, we check if it actually depends on them or on any integer
10536 * divisions that may depend on them.
10538 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10540 int i;
10541 unsigned n_out, o_out;
10542 unsigned n_div, o_div;
10544 if (isl_int_is_zero(bmap->div[div][0]))
10545 return 1;
10547 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10548 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10550 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10551 return 1;
10553 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10554 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10556 for (i = 0; i < n_div; ++i) {
10557 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10558 continue;
10559 if (div_may_involve_output(bmap, i))
10560 return 1;
10563 return 0;
10566 /* Return the first integer division of "bmap" in the range
10567 * [first, first + n[ that may depend on any output dimensions and
10568 * that has a non-zero coefficient in "c" (where the first coefficient
10569 * in "c" corresponds to integer division "first").
10571 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10572 isl_int *c, int first, int n)
10574 int k;
10576 if (!bmap)
10577 return -1;
10579 for (k = first; k < first + n; ++k) {
10580 if (isl_int_is_zero(c[k]))
10581 continue;
10582 if (div_may_involve_output(bmap, k))
10583 return k;
10586 return first + n;
10589 /* Look for a pair of inequality constraints in "bmap" of the form
10591 * -l + i >= 0 or i >= l
10592 * and
10593 * n + l - i >= 0 or i <= l + n
10595 * with n < "m" and i the output dimension at position "pos".
10596 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10597 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10598 * and earlier output dimensions, as well as integer divisions that do
10599 * not involve any of the output dimensions.
10601 * Return the index of the first inequality constraint or bmap->n_ineq
10602 * if no such pair can be found.
10604 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10605 int pos, isl_int m)
10607 int i, j;
10608 isl_ctx *ctx;
10609 unsigned total;
10610 unsigned n_div, o_div;
10611 unsigned n_out, o_out;
10612 int less;
10614 if (!bmap)
10615 return -1;
10617 ctx = isl_basic_map_get_ctx(bmap);
10618 total = isl_basic_map_total_dim(bmap);
10619 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10620 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10621 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10622 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10623 for (i = 0; i < bmap->n_ineq; ++i) {
10624 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10625 continue;
10626 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10627 n_out - (pos + 1)) != -1)
10628 continue;
10629 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10630 0, n_div) < n_div)
10631 continue;
10632 for (j = i + 1; j < bmap->n_ineq; ++j) {
10633 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10634 ctx->one))
10635 continue;
10636 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10637 bmap->ineq[j] + 1, total))
10638 continue;
10639 break;
10641 if (j >= bmap->n_ineq)
10642 continue;
10643 isl_int_add(bmap->ineq[i][0],
10644 bmap->ineq[i][0], bmap->ineq[j][0]);
10645 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10646 isl_int_sub(bmap->ineq[i][0],
10647 bmap->ineq[i][0], bmap->ineq[j][0]);
10648 if (!less)
10649 continue;
10650 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10651 return i;
10652 else
10653 return j;
10656 return bmap->n_ineq;
10659 /* Return the index of the equality of "bmap" that defines
10660 * the output dimension "pos" in terms of earlier dimensions.
10661 * The equality may also involve integer divisions, as long
10662 * as those integer divisions are defined in terms of
10663 * parameters or input dimensions.
10664 * In this case, *div is set to the number of integer divisions and
10665 * *ineq is set to the number of inequality constraints (provided
10666 * div and ineq are not NULL).
10668 * The equality may also involve a single integer division involving
10669 * the output dimensions (typically only output dimension "pos") as
10670 * long as the coefficient of output dimension "pos" is 1 or -1 and
10671 * there is a pair of constraints i >= l and i <= l + n, with i referring
10672 * to output dimension "pos", l an expression involving only earlier
10673 * dimensions and n smaller than the coefficient of the integer division
10674 * in the equality. In this case, the output dimension can be defined
10675 * in terms of a modulo expression that does not involve the integer division.
10676 * *div is then set to this single integer division and
10677 * *ineq is set to the index of constraint i >= l.
10679 * Return bmap->n_eq if there is no such equality.
10680 * Return -1 on error.
10682 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10683 int pos, int *div, int *ineq)
10685 int j, k, l;
10686 unsigned n_out, o_out;
10687 unsigned n_div, o_div;
10689 if (!bmap)
10690 return -1;
10692 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10693 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10694 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10695 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10697 if (ineq)
10698 *ineq = bmap->n_ineq;
10699 if (div)
10700 *div = n_div;
10701 for (j = 0; j < bmap->n_eq; ++j) {
10702 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10703 continue;
10704 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10705 n_out - (pos + 1)) != -1)
10706 continue;
10707 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10708 0, n_div);
10709 if (k >= n_div)
10710 return j;
10711 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10712 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10713 continue;
10714 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10715 k + 1, n_div - (k+1)) < n_div)
10716 continue;
10717 l = find_modulo_constraint_pair(bmap, pos,
10718 bmap->eq[j][o_div + k]);
10719 if (l < 0)
10720 return -1;
10721 if (l >= bmap->n_ineq)
10722 continue;
10723 if (div)
10724 *div = k;
10725 if (ineq)
10726 *ineq = l;
10727 return j;
10730 return bmap->n_eq;
10733 /* Check if the given basic map is obviously single-valued.
10734 * In particular, for each output dimension, check that there is
10735 * an equality that defines the output dimension in terms of
10736 * earlier dimensions.
10738 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10740 int i;
10741 unsigned n_out;
10743 if (!bmap)
10744 return isl_bool_error;
10746 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10748 for (i = 0; i < n_out; ++i) {
10749 int eq;
10751 eq = isl_basic_map_output_defining_equality(bmap, i,
10752 NULL, NULL);
10753 if (eq < 0)
10754 return isl_bool_error;
10755 if (eq >= bmap->n_eq)
10756 return isl_bool_false;
10759 return isl_bool_true;
10762 /* Check if the given basic map is single-valued.
10763 * We simply compute
10765 * M \circ M^-1
10767 * and check if the result is a subset of the identity mapping.
10769 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10771 isl_space *space;
10772 isl_basic_map *test;
10773 isl_basic_map *id;
10774 isl_bool sv;
10776 sv = isl_basic_map_plain_is_single_valued(bmap);
10777 if (sv < 0 || sv)
10778 return sv;
10780 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10781 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10783 space = isl_basic_map_get_space(bmap);
10784 space = isl_space_map_from_set(isl_space_range(space));
10785 id = isl_basic_map_identity(space);
10787 sv = isl_basic_map_is_subset(test, id);
10789 isl_basic_map_free(test);
10790 isl_basic_map_free(id);
10792 return sv;
10795 /* Check if the given map is obviously single-valued.
10797 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10799 if (!map)
10800 return isl_bool_error;
10801 if (map->n == 0)
10802 return isl_bool_true;
10803 if (map->n >= 2)
10804 return isl_bool_false;
10806 return isl_basic_map_plain_is_single_valued(map->p[0]);
10809 /* Check if the given map is single-valued.
10810 * We simply compute
10812 * M \circ M^-1
10814 * and check if the result is a subset of the identity mapping.
10816 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10818 isl_space *dim;
10819 isl_map *test;
10820 isl_map *id;
10821 isl_bool sv;
10823 sv = isl_map_plain_is_single_valued(map);
10824 if (sv < 0 || sv)
10825 return sv;
10827 test = isl_map_reverse(isl_map_copy(map));
10828 test = isl_map_apply_range(test, isl_map_copy(map));
10830 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10831 id = isl_map_identity(dim);
10833 sv = isl_map_is_subset(test, id);
10835 isl_map_free(test);
10836 isl_map_free(id);
10838 return sv;
10841 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10843 isl_bool in;
10845 map = isl_map_copy(map);
10846 map = isl_map_reverse(map);
10847 in = isl_map_is_single_valued(map);
10848 isl_map_free(map);
10850 return in;
10853 /* Check if the given map is obviously injective.
10855 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10857 isl_bool in;
10859 map = isl_map_copy(map);
10860 map = isl_map_reverse(map);
10861 in = isl_map_plain_is_single_valued(map);
10862 isl_map_free(map);
10864 return in;
10867 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10869 isl_bool sv;
10871 sv = isl_map_is_single_valued(map);
10872 if (sv < 0 || !sv)
10873 return sv;
10875 return isl_map_is_injective(map);
10878 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10880 return isl_map_is_single_valued((isl_map *)set);
10883 /* Does "map" only map elements to themselves?
10885 * If the domain and range spaces are different, then "map"
10886 * is considered not to be an identity relation, even if it is empty.
10887 * Otherwise, construct the maximal identity relation and
10888 * check whether "map" is a subset of this relation.
10890 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10892 isl_space *space;
10893 isl_map *id;
10894 isl_bool equal, is_identity;
10896 space = isl_map_get_space(map);
10897 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10898 isl_space_free(space);
10899 if (equal < 0 || !equal)
10900 return equal;
10902 id = isl_map_identity(isl_map_get_space(map));
10903 is_identity = isl_map_is_subset(map, id);
10904 isl_map_free(id);
10906 return is_identity;
10909 int isl_map_is_translation(__isl_keep isl_map *map)
10911 int ok;
10912 isl_set *delta;
10914 delta = isl_map_deltas(isl_map_copy(map));
10915 ok = isl_set_is_singleton(delta);
10916 isl_set_free(delta);
10918 return ok;
10921 static int unique(isl_int *p, unsigned pos, unsigned len)
10923 if (isl_seq_first_non_zero(p, pos) != -1)
10924 return 0;
10925 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10926 return 0;
10927 return 1;
10930 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10932 int i, j;
10933 unsigned nvar;
10934 unsigned ovar;
10936 if (!bset)
10937 return -1;
10939 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10940 return 0;
10942 nvar = isl_basic_set_dim(bset, isl_dim_set);
10943 ovar = isl_space_offset(bset->dim, isl_dim_set);
10944 for (j = 0; j < nvar; ++j) {
10945 int lower = 0, upper = 0;
10946 for (i = 0; i < bset->n_eq; ++i) {
10947 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10948 continue;
10949 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10950 return 0;
10951 break;
10953 if (i < bset->n_eq)
10954 continue;
10955 for (i = 0; i < bset->n_ineq; ++i) {
10956 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10957 continue;
10958 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10959 return 0;
10960 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10961 lower = 1;
10962 else
10963 upper = 1;
10965 if (!lower || !upper)
10966 return 0;
10969 return 1;
10972 int isl_set_is_box(__isl_keep isl_set *set)
10974 if (!set)
10975 return -1;
10976 if (set->n != 1)
10977 return 0;
10979 return isl_basic_set_is_box(set->p[0]);
10982 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10984 if (!bset)
10985 return isl_bool_error;
10987 return isl_space_is_wrapping(bset->dim);
10990 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10992 if (!set)
10993 return isl_bool_error;
10995 return isl_space_is_wrapping(set->dim);
10998 /* Modify the space of "map" through a call to "change".
10999 * If "can_change" is set (not NULL), then first call it to check
11000 * if the modification is allowed, printing the error message "cannot_change"
11001 * if it is not.
11003 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11004 isl_bool (*can_change)(__isl_keep isl_map *map),
11005 const char *cannot_change,
11006 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11008 isl_bool ok;
11009 isl_space *space;
11011 if (!map)
11012 return NULL;
11014 ok = can_change ? can_change(map) : isl_bool_true;
11015 if (ok < 0)
11016 return isl_map_free(map);
11017 if (!ok)
11018 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11019 return isl_map_free(map));
11021 space = change(isl_map_get_space(map));
11022 map = isl_map_reset_space(map, space);
11024 return map;
11027 /* Is the domain of "map" a wrapped relation?
11029 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11031 if (!map)
11032 return isl_bool_error;
11034 return isl_space_domain_is_wrapping(map->dim);
11037 /* Is the range of "map" a wrapped relation?
11039 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11041 if (!map)
11042 return isl_bool_error;
11044 return isl_space_range_is_wrapping(map->dim);
11047 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11049 bmap = isl_basic_map_cow(bmap);
11050 if (!bmap)
11051 return NULL;
11053 bmap->dim = isl_space_wrap(bmap->dim);
11054 if (!bmap->dim)
11055 goto error;
11057 bmap = isl_basic_map_finalize(bmap);
11059 return (isl_basic_set *)bmap;
11060 error:
11061 isl_basic_map_free(bmap);
11062 return NULL;
11065 /* Given a map A -> B, return the set (A -> B).
11067 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11069 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11072 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11074 bset = isl_basic_set_cow(bset);
11075 if (!bset)
11076 return NULL;
11078 bset->dim = isl_space_unwrap(bset->dim);
11079 if (!bset->dim)
11080 goto error;
11082 bset = isl_basic_set_finalize(bset);
11084 return (isl_basic_map *)bset;
11085 error:
11086 isl_basic_set_free(bset);
11087 return NULL;
11090 /* Given a set (A -> B), return the map A -> B.
11091 * Error out if "set" is not of the form (A -> B).
11093 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11095 return isl_map_change_space(set, &isl_set_is_wrapping,
11096 "not a wrapping set", &isl_space_unwrap);
11099 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11100 enum isl_dim_type type)
11102 if (!bmap)
11103 return NULL;
11105 if (!isl_space_is_named_or_nested(bmap->dim, type))
11106 return bmap;
11108 bmap = isl_basic_map_cow(bmap);
11109 if (!bmap)
11110 return NULL;
11112 bmap->dim = isl_space_reset(bmap->dim, type);
11113 if (!bmap->dim)
11114 goto error;
11116 bmap = isl_basic_map_finalize(bmap);
11118 return bmap;
11119 error:
11120 isl_basic_map_free(bmap);
11121 return NULL;
11124 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11125 enum isl_dim_type type)
11127 int i;
11129 if (!map)
11130 return NULL;
11132 if (!isl_space_is_named_or_nested(map->dim, type))
11133 return map;
11135 map = isl_map_cow(map);
11136 if (!map)
11137 return NULL;
11139 for (i = 0; i < map->n; ++i) {
11140 map->p[i] = isl_basic_map_reset(map->p[i], type);
11141 if (!map->p[i])
11142 goto error;
11144 map->dim = isl_space_reset(map->dim, type);
11145 if (!map->dim)
11146 goto error;
11148 return map;
11149 error:
11150 isl_map_free(map);
11151 return NULL;
11154 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11156 if (!bmap)
11157 return NULL;
11159 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11160 return bmap;
11162 bmap = isl_basic_map_cow(bmap);
11163 if (!bmap)
11164 return NULL;
11166 bmap->dim = isl_space_flatten(bmap->dim);
11167 if (!bmap->dim)
11168 goto error;
11170 bmap = isl_basic_map_finalize(bmap);
11172 return bmap;
11173 error:
11174 isl_basic_map_free(bmap);
11175 return NULL;
11178 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11180 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
11183 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11184 __isl_take isl_basic_map *bmap)
11186 if (!bmap)
11187 return NULL;
11189 if (!bmap->dim->nested[0])
11190 return bmap;
11192 bmap = isl_basic_map_cow(bmap);
11193 if (!bmap)
11194 return NULL;
11196 bmap->dim = isl_space_flatten_domain(bmap->dim);
11197 if (!bmap->dim)
11198 goto error;
11200 bmap = isl_basic_map_finalize(bmap);
11202 return bmap;
11203 error:
11204 isl_basic_map_free(bmap);
11205 return NULL;
11208 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11209 __isl_take isl_basic_map *bmap)
11211 if (!bmap)
11212 return NULL;
11214 if (!bmap->dim->nested[1])
11215 return bmap;
11217 bmap = isl_basic_map_cow(bmap);
11218 if (!bmap)
11219 return NULL;
11221 bmap->dim = isl_space_flatten_range(bmap->dim);
11222 if (!bmap->dim)
11223 goto error;
11225 bmap = isl_basic_map_finalize(bmap);
11227 return bmap;
11228 error:
11229 isl_basic_map_free(bmap);
11230 return NULL;
11233 /* Remove any internal structure from the spaces of domain and range of "map".
11235 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11237 if (!map)
11238 return NULL;
11240 if (!map->dim->nested[0] && !map->dim->nested[1])
11241 return map;
11243 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11246 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11248 return (isl_set *)isl_map_flatten((isl_map *)set);
11251 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11253 isl_space *dim, *flat_dim;
11254 isl_map *map;
11256 dim = isl_set_get_space(set);
11257 flat_dim = isl_space_flatten(isl_space_copy(dim));
11258 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11259 map = isl_map_intersect_domain(map, set);
11261 return map;
11264 /* Remove any internal structure from the space of the domain of "map".
11266 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11268 if (!map)
11269 return NULL;
11271 if (!map->dim->nested[0])
11272 return map;
11274 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11277 /* Remove any internal structure from the space of the range of "map".
11279 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11281 if (!map)
11282 return NULL;
11284 if (!map->dim->nested[1])
11285 return map;
11287 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11290 /* Reorder the dimensions of "bmap" according to the given dim_map
11291 * and set the dimension specification to "dim" and
11292 * perform Gaussian elimination on the result.
11294 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11295 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11297 isl_basic_map *res;
11298 unsigned flags;
11300 bmap = isl_basic_map_cow(bmap);
11301 if (!bmap || !dim || !dim_map)
11302 goto error;
11304 flags = bmap->flags;
11305 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11306 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11307 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11308 res = isl_basic_map_alloc_space(dim,
11309 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11310 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11311 if (res)
11312 res->flags = flags;
11313 res = isl_basic_map_gauss(res, NULL);
11314 res = isl_basic_map_finalize(res);
11315 return res;
11316 error:
11317 free(dim_map);
11318 isl_basic_map_free(bmap);
11319 isl_space_free(dim);
11320 return NULL;
11323 /* Reorder the dimensions of "map" according to given reordering.
11325 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11326 __isl_take isl_reordering *r)
11328 int i;
11329 struct isl_dim_map *dim_map;
11331 map = isl_map_cow(map);
11332 dim_map = isl_dim_map_from_reordering(r);
11333 if (!map || !r || !dim_map)
11334 goto error;
11336 for (i = 0; i < map->n; ++i) {
11337 struct isl_dim_map *dim_map_i;
11339 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11341 map->p[i] = isl_basic_map_realign(map->p[i],
11342 isl_space_copy(r->dim), dim_map_i);
11344 if (!map->p[i])
11345 goto error;
11348 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11350 isl_reordering_free(r);
11351 free(dim_map);
11352 return map;
11353 error:
11354 free(dim_map);
11355 isl_map_free(map);
11356 isl_reordering_free(r);
11357 return NULL;
11360 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11361 __isl_take isl_reordering *r)
11363 return (isl_set *)isl_map_realign((isl_map *)set, r);
11366 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11367 __isl_take isl_space *model)
11369 isl_ctx *ctx;
11371 if (!map || !model)
11372 goto error;
11374 ctx = isl_space_get_ctx(model);
11375 if (!isl_space_has_named_params(model))
11376 isl_die(ctx, isl_error_invalid,
11377 "model has unnamed parameters", goto error);
11378 if (!isl_space_has_named_params(map->dim))
11379 isl_die(ctx, isl_error_invalid,
11380 "relation has unnamed parameters", goto error);
11381 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11382 isl_reordering *exp;
11384 model = isl_space_drop_dims(model, isl_dim_in,
11385 0, isl_space_dim(model, isl_dim_in));
11386 model = isl_space_drop_dims(model, isl_dim_out,
11387 0, isl_space_dim(model, isl_dim_out));
11388 exp = isl_parameter_alignment_reordering(map->dim, model);
11389 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11390 map = isl_map_realign(map, exp);
11393 isl_space_free(model);
11394 return map;
11395 error:
11396 isl_space_free(model);
11397 isl_map_free(map);
11398 return NULL;
11401 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11402 __isl_take isl_space *model)
11404 return isl_map_align_params(set, model);
11407 /* Align the parameters of "bmap" to those of "model", introducing
11408 * additional parameters if needed.
11410 __isl_give isl_basic_map *isl_basic_map_align_params(
11411 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11413 isl_ctx *ctx;
11415 if (!bmap || !model)
11416 goto error;
11418 ctx = isl_space_get_ctx(model);
11419 if (!isl_space_has_named_params(model))
11420 isl_die(ctx, isl_error_invalid,
11421 "model has unnamed parameters", goto error);
11422 if (!isl_space_has_named_params(bmap->dim))
11423 isl_die(ctx, isl_error_invalid,
11424 "relation has unnamed parameters", goto error);
11425 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11426 isl_reordering *exp;
11427 struct isl_dim_map *dim_map;
11429 model = isl_space_drop_dims(model, isl_dim_in,
11430 0, isl_space_dim(model, isl_dim_in));
11431 model = isl_space_drop_dims(model, isl_dim_out,
11432 0, isl_space_dim(model, isl_dim_out));
11433 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11434 exp = isl_reordering_extend_space(exp,
11435 isl_basic_map_get_space(bmap));
11436 dim_map = isl_dim_map_from_reordering(exp);
11437 bmap = isl_basic_map_realign(bmap,
11438 exp ? isl_space_copy(exp->dim) : NULL,
11439 isl_dim_map_extend(dim_map, bmap));
11440 isl_reordering_free(exp);
11441 free(dim_map);
11444 isl_space_free(model);
11445 return bmap;
11446 error:
11447 isl_space_free(model);
11448 isl_basic_map_free(bmap);
11449 return NULL;
11452 /* Align the parameters of "bset" to those of "model", introducing
11453 * additional parameters if needed.
11455 __isl_give isl_basic_set *isl_basic_set_align_params(
11456 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11458 return isl_basic_map_align_params(bset, model);
11461 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11462 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11463 enum isl_dim_type c2, enum isl_dim_type c3,
11464 enum isl_dim_type c4, enum isl_dim_type c5)
11466 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11467 struct isl_mat *mat;
11468 int i, j, k;
11469 int pos;
11471 if (!bmap)
11472 return NULL;
11473 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11474 isl_basic_map_total_dim(bmap) + 1);
11475 if (!mat)
11476 return NULL;
11477 for (i = 0; i < bmap->n_eq; ++i)
11478 for (j = 0, pos = 0; j < 5; ++j) {
11479 int off = isl_basic_map_offset(bmap, c[j]);
11480 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11481 isl_int_set(mat->row[i][pos],
11482 bmap->eq[i][off + k]);
11483 ++pos;
11487 return mat;
11490 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11491 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11492 enum isl_dim_type c2, enum isl_dim_type c3,
11493 enum isl_dim_type c4, enum isl_dim_type c5)
11495 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11496 struct isl_mat *mat;
11497 int i, j, k;
11498 int pos;
11500 if (!bmap)
11501 return NULL;
11502 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11503 isl_basic_map_total_dim(bmap) + 1);
11504 if (!mat)
11505 return NULL;
11506 for (i = 0; i < bmap->n_ineq; ++i)
11507 for (j = 0, pos = 0; j < 5; ++j) {
11508 int off = isl_basic_map_offset(bmap, c[j]);
11509 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11510 isl_int_set(mat->row[i][pos],
11511 bmap->ineq[i][off + k]);
11512 ++pos;
11516 return mat;
11519 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11520 __isl_take isl_space *dim,
11521 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11522 enum isl_dim_type c2, enum isl_dim_type c3,
11523 enum isl_dim_type c4, enum isl_dim_type c5)
11525 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11526 isl_basic_map *bmap;
11527 unsigned total;
11528 unsigned extra;
11529 int i, j, k, l;
11530 int pos;
11532 if (!dim || !eq || !ineq)
11533 goto error;
11535 if (eq->n_col != ineq->n_col)
11536 isl_die(dim->ctx, isl_error_invalid,
11537 "equalities and inequalities matrices should have "
11538 "same number of columns", goto error);
11540 total = 1 + isl_space_dim(dim, isl_dim_all);
11542 if (eq->n_col < total)
11543 isl_die(dim->ctx, isl_error_invalid,
11544 "number of columns too small", goto error);
11546 extra = eq->n_col - total;
11548 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11549 eq->n_row, ineq->n_row);
11550 if (!bmap)
11551 goto error;
11552 for (i = 0; i < extra; ++i) {
11553 k = isl_basic_map_alloc_div(bmap);
11554 if (k < 0)
11555 goto error;
11556 isl_int_set_si(bmap->div[k][0], 0);
11558 for (i = 0; i < eq->n_row; ++i) {
11559 l = isl_basic_map_alloc_equality(bmap);
11560 if (l < 0)
11561 goto error;
11562 for (j = 0, pos = 0; j < 5; ++j) {
11563 int off = isl_basic_map_offset(bmap, c[j]);
11564 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11565 isl_int_set(bmap->eq[l][off + k],
11566 eq->row[i][pos]);
11567 ++pos;
11571 for (i = 0; i < ineq->n_row; ++i) {
11572 l = isl_basic_map_alloc_inequality(bmap);
11573 if (l < 0)
11574 goto error;
11575 for (j = 0, pos = 0; j < 5; ++j) {
11576 int off = isl_basic_map_offset(bmap, c[j]);
11577 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11578 isl_int_set(bmap->ineq[l][off + k],
11579 ineq->row[i][pos]);
11580 ++pos;
11585 isl_space_free(dim);
11586 isl_mat_free(eq);
11587 isl_mat_free(ineq);
11589 bmap = isl_basic_map_simplify(bmap);
11590 return isl_basic_map_finalize(bmap);
11591 error:
11592 isl_space_free(dim);
11593 isl_mat_free(eq);
11594 isl_mat_free(ineq);
11595 return NULL;
11598 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11599 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11600 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11602 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11603 c1, c2, c3, c4, isl_dim_in);
11606 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11607 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11608 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11610 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11611 c1, c2, c3, c4, isl_dim_in);
11614 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11615 __isl_take isl_space *dim,
11616 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11617 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11619 return (isl_basic_set*)
11620 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11621 c1, c2, c3, c4, isl_dim_in);
11624 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11626 if (!bmap)
11627 return isl_bool_error;
11629 return isl_space_can_zip(bmap->dim);
11632 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11634 if (!map)
11635 return isl_bool_error;
11637 return isl_space_can_zip(map->dim);
11640 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11641 * (A -> C) -> (B -> D).
11643 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11645 unsigned pos;
11646 unsigned n1;
11647 unsigned n2;
11649 if (!bmap)
11650 return NULL;
11652 if (!isl_basic_map_can_zip(bmap))
11653 isl_die(bmap->ctx, isl_error_invalid,
11654 "basic map cannot be zipped", goto error);
11655 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11656 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11657 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11658 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11659 bmap = isl_basic_map_cow(bmap);
11660 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11661 if (!bmap)
11662 return NULL;
11663 bmap->dim = isl_space_zip(bmap->dim);
11664 if (!bmap->dim)
11665 goto error;
11666 bmap = isl_basic_map_mark_final(bmap);
11667 return bmap;
11668 error:
11669 isl_basic_map_free(bmap);
11670 return NULL;
11673 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11674 * (A -> C) -> (B -> D).
11676 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11678 int i;
11680 if (!map)
11681 return NULL;
11683 if (!isl_map_can_zip(map))
11684 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11685 goto error);
11687 map = isl_map_cow(map);
11688 if (!map)
11689 return NULL;
11691 for (i = 0; i < map->n; ++i) {
11692 map->p[i] = isl_basic_map_zip(map->p[i]);
11693 if (!map->p[i])
11694 goto error;
11697 map->dim = isl_space_zip(map->dim);
11698 if (!map->dim)
11699 goto error;
11701 return map;
11702 error:
11703 isl_map_free(map);
11704 return NULL;
11707 /* Can we apply isl_basic_map_curry to "bmap"?
11708 * That is, does it have a nested relation in its domain?
11710 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11712 if (!bmap)
11713 return isl_bool_error;
11715 return isl_space_can_curry(bmap->dim);
11718 /* Can we apply isl_map_curry to "map"?
11719 * That is, does it have a nested relation in its domain?
11721 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11723 if (!map)
11724 return isl_bool_error;
11726 return isl_space_can_curry(map->dim);
11729 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11730 * A -> (B -> C).
11732 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11735 if (!bmap)
11736 return NULL;
11738 if (!isl_basic_map_can_curry(bmap))
11739 isl_die(bmap->ctx, isl_error_invalid,
11740 "basic map cannot be curried", goto error);
11741 bmap = isl_basic_map_cow(bmap);
11742 if (!bmap)
11743 return NULL;
11744 bmap->dim = isl_space_curry(bmap->dim);
11745 if (!bmap->dim)
11746 goto error;
11747 bmap = isl_basic_map_mark_final(bmap);
11748 return bmap;
11749 error:
11750 isl_basic_map_free(bmap);
11751 return NULL;
11754 /* Given a map (A -> B) -> C, return the corresponding map
11755 * A -> (B -> C).
11757 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11759 return isl_map_change_space(map, &isl_map_can_curry,
11760 "map cannot be curried", &isl_space_curry);
11763 /* Can isl_map_range_curry be applied to "map"?
11764 * That is, does it have a nested relation in its range,
11765 * the domain of which is itself a nested relation?
11767 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11769 if (!map)
11770 return isl_bool_error;
11772 return isl_space_can_range_curry(map->dim);
11775 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11776 * A -> (B -> (C -> D)).
11778 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11780 return isl_map_change_space(map, &isl_map_can_range_curry,
11781 "map range cannot be curried",
11782 &isl_space_range_curry);
11785 /* Can we apply isl_basic_map_uncurry to "bmap"?
11786 * That is, does it have a nested relation in its domain?
11788 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11790 if (!bmap)
11791 return isl_bool_error;
11793 return isl_space_can_uncurry(bmap->dim);
11796 /* Can we apply isl_map_uncurry to "map"?
11797 * That is, does it have a nested relation in its domain?
11799 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11801 if (!map)
11802 return isl_bool_error;
11804 return isl_space_can_uncurry(map->dim);
11807 /* Given a basic map A -> (B -> C), return the corresponding basic map
11808 * (A -> B) -> C.
11810 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11813 if (!bmap)
11814 return NULL;
11816 if (!isl_basic_map_can_uncurry(bmap))
11817 isl_die(bmap->ctx, isl_error_invalid,
11818 "basic map cannot be uncurried",
11819 return isl_basic_map_free(bmap));
11820 bmap = isl_basic_map_cow(bmap);
11821 if (!bmap)
11822 return NULL;
11823 bmap->dim = isl_space_uncurry(bmap->dim);
11824 if (!bmap->dim)
11825 return isl_basic_map_free(bmap);
11826 bmap = isl_basic_map_mark_final(bmap);
11827 return bmap;
11830 /* Given a map A -> (B -> C), return the corresponding map
11831 * (A -> B) -> C.
11833 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11835 return isl_map_change_space(map, &isl_map_can_uncurry,
11836 "map cannot be uncurried", &isl_space_uncurry);
11839 /* Construct a basic map mapping the domain of the affine expression
11840 * to a one-dimensional range prescribed by the affine expression.
11842 * A NaN affine expression cannot be converted to a basic map.
11844 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11846 int k;
11847 int pos;
11848 isl_bool is_nan;
11849 isl_local_space *ls;
11850 isl_basic_map *bmap = NULL;
11852 if (!aff)
11853 return NULL;
11854 is_nan = isl_aff_is_nan(aff);
11855 if (is_nan < 0)
11856 goto error;
11857 if (is_nan)
11858 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11859 "cannot convert NaN", goto error);
11861 ls = isl_aff_get_local_space(aff);
11862 bmap = isl_basic_map_from_local_space(ls);
11863 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11864 k = isl_basic_map_alloc_equality(bmap);
11865 if (k < 0)
11866 goto error;
11868 pos = isl_basic_map_offset(bmap, isl_dim_out);
11869 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11870 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11871 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11872 aff->v->size - (pos + 1));
11874 isl_aff_free(aff);
11875 bmap = isl_basic_map_finalize(bmap);
11876 return bmap;
11877 error:
11878 isl_aff_free(aff);
11879 isl_basic_map_free(bmap);
11880 return NULL;
11883 /* Construct a map mapping the domain of the affine expression
11884 * to a one-dimensional range prescribed by the affine expression.
11886 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11888 isl_basic_map *bmap;
11890 bmap = isl_basic_map_from_aff(aff);
11891 return isl_map_from_basic_map(bmap);
11894 /* Construct a basic map mapping the domain the multi-affine expression
11895 * to its range, with each dimension in the range equated to the
11896 * corresponding affine expression.
11898 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11899 __isl_take isl_multi_aff *maff)
11901 int i;
11902 isl_space *space;
11903 isl_basic_map *bmap;
11905 if (!maff)
11906 return NULL;
11908 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11909 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11910 "invalid space", goto error);
11912 space = isl_space_domain(isl_multi_aff_get_space(maff));
11913 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11915 for (i = 0; i < maff->n; ++i) {
11916 isl_aff *aff;
11917 isl_basic_map *bmap_i;
11919 aff = isl_aff_copy(maff->p[i]);
11920 bmap_i = isl_basic_map_from_aff(aff);
11922 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11925 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11927 isl_multi_aff_free(maff);
11928 return bmap;
11929 error:
11930 isl_multi_aff_free(maff);
11931 return NULL;
11934 /* Construct a map mapping the domain the multi-affine expression
11935 * to its range, with each dimension in the range equated to the
11936 * corresponding affine expression.
11938 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11940 isl_basic_map *bmap;
11942 bmap = isl_basic_map_from_multi_aff(maff);
11943 return isl_map_from_basic_map(bmap);
11946 /* Construct a basic map mapping a domain in the given space to
11947 * to an n-dimensional range, with n the number of elements in the list,
11948 * where each coordinate in the range is prescribed by the
11949 * corresponding affine expression.
11950 * The domains of all affine expressions in the list are assumed to match
11951 * domain_dim.
11953 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11954 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11956 int i;
11957 isl_space *dim;
11958 isl_basic_map *bmap;
11960 if (!list)
11961 return NULL;
11963 dim = isl_space_from_domain(domain_dim);
11964 bmap = isl_basic_map_universe(dim);
11966 for (i = 0; i < list->n; ++i) {
11967 isl_aff *aff;
11968 isl_basic_map *bmap_i;
11970 aff = isl_aff_copy(list->p[i]);
11971 bmap_i = isl_basic_map_from_aff(aff);
11973 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11976 isl_aff_list_free(list);
11977 return bmap;
11980 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11981 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11983 return isl_map_equate(set, type1, pos1, type2, pos2);
11986 /* Construct a basic map where the given dimensions are equal to each other.
11988 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11989 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11991 isl_basic_map *bmap = NULL;
11992 int i;
11994 if (!space)
11995 return NULL;
11997 if (pos1 >= isl_space_dim(space, type1))
11998 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11999 "index out of bounds", goto error);
12000 if (pos2 >= isl_space_dim(space, type2))
12001 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12002 "index out of bounds", goto error);
12004 if (type1 == type2 && pos1 == pos2)
12005 return isl_basic_map_universe(space);
12007 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12008 i = isl_basic_map_alloc_equality(bmap);
12009 if (i < 0)
12010 goto error;
12011 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12012 pos1 += isl_basic_map_offset(bmap, type1);
12013 pos2 += isl_basic_map_offset(bmap, type2);
12014 isl_int_set_si(bmap->eq[i][pos1], -1);
12015 isl_int_set_si(bmap->eq[i][pos2], 1);
12016 bmap = isl_basic_map_finalize(bmap);
12017 isl_space_free(space);
12018 return bmap;
12019 error:
12020 isl_space_free(space);
12021 isl_basic_map_free(bmap);
12022 return NULL;
12025 /* Add a constraint imposing that the given two dimensions are equal.
12027 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12028 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12030 isl_basic_map *eq;
12032 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12034 bmap = isl_basic_map_intersect(bmap, eq);
12036 return bmap;
12039 /* Add a constraint imposing that the given two dimensions are equal.
12041 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12042 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12044 isl_basic_map *bmap;
12046 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12048 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12050 return map;
12053 /* Add a constraint imposing that the given two dimensions have opposite values.
12055 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12056 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12058 isl_basic_map *bmap = NULL;
12059 int i;
12061 if (!map)
12062 return NULL;
12064 if (pos1 >= isl_map_dim(map, type1))
12065 isl_die(map->ctx, isl_error_invalid,
12066 "index out of bounds", goto error);
12067 if (pos2 >= isl_map_dim(map, type2))
12068 isl_die(map->ctx, isl_error_invalid,
12069 "index out of bounds", goto error);
12071 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12072 i = isl_basic_map_alloc_equality(bmap);
12073 if (i < 0)
12074 goto error;
12075 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12076 pos1 += isl_basic_map_offset(bmap, type1);
12077 pos2 += isl_basic_map_offset(bmap, type2);
12078 isl_int_set_si(bmap->eq[i][pos1], 1);
12079 isl_int_set_si(bmap->eq[i][pos2], 1);
12080 bmap = isl_basic_map_finalize(bmap);
12082 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12084 return map;
12085 error:
12086 isl_basic_map_free(bmap);
12087 isl_map_free(map);
12088 return NULL;
12091 /* Construct a constraint imposing that the value of the first dimension is
12092 * greater than or equal to that of the second.
12094 static __isl_give isl_constraint *constraint_order_ge(
12095 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12096 enum isl_dim_type type2, int pos2)
12098 isl_constraint *c;
12100 if (!space)
12101 return NULL;
12103 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12105 if (pos1 >= isl_constraint_dim(c, type1))
12106 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12107 "index out of bounds", return isl_constraint_free(c));
12108 if (pos2 >= isl_constraint_dim(c, type2))
12109 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12110 "index out of bounds", return isl_constraint_free(c));
12112 if (type1 == type2 && pos1 == pos2)
12113 return c;
12115 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12116 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12118 return c;
12121 /* Add a constraint imposing that the value of the first dimension is
12122 * greater than or equal to that of the second.
12124 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12125 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12127 isl_constraint *c;
12128 isl_space *space;
12130 if (type1 == type2 && pos1 == pos2)
12131 return bmap;
12132 space = isl_basic_map_get_space(bmap);
12133 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12134 bmap = isl_basic_map_add_constraint(bmap, c);
12136 return bmap;
12139 /* Add a constraint imposing that the value of the first dimension is
12140 * greater than or equal to that of the second.
12142 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12143 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12145 isl_constraint *c;
12146 isl_space *space;
12148 if (type1 == type2 && pos1 == pos2)
12149 return map;
12150 space = isl_map_get_space(map);
12151 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12152 map = isl_map_add_constraint(map, c);
12154 return map;
12157 /* Add a constraint imposing that the value of the first dimension is
12158 * less than or equal to that of the second.
12160 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12161 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12163 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12166 /* Construct a basic map where the value of the first dimension is
12167 * greater than that of the second.
12169 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12170 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12172 isl_basic_map *bmap = NULL;
12173 int i;
12175 if (!space)
12176 return NULL;
12178 if (pos1 >= isl_space_dim(space, type1))
12179 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12180 "index out of bounds", goto error);
12181 if (pos2 >= isl_space_dim(space, type2))
12182 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12183 "index out of bounds", goto error);
12185 if (type1 == type2 && pos1 == pos2)
12186 return isl_basic_map_empty(space);
12188 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12189 i = isl_basic_map_alloc_inequality(bmap);
12190 if (i < 0)
12191 return isl_basic_map_free(bmap);
12192 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12193 pos1 += isl_basic_map_offset(bmap, type1);
12194 pos2 += isl_basic_map_offset(bmap, type2);
12195 isl_int_set_si(bmap->ineq[i][pos1], 1);
12196 isl_int_set_si(bmap->ineq[i][pos2], -1);
12197 isl_int_set_si(bmap->ineq[i][0], -1);
12198 bmap = isl_basic_map_finalize(bmap);
12200 return bmap;
12201 error:
12202 isl_space_free(space);
12203 isl_basic_map_free(bmap);
12204 return NULL;
12207 /* Add a constraint imposing that the value of the first dimension is
12208 * greater than that of the second.
12210 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12211 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12213 isl_basic_map *gt;
12215 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12217 bmap = isl_basic_map_intersect(bmap, gt);
12219 return bmap;
12222 /* Add a constraint imposing that the value of the first dimension is
12223 * greater than that of the second.
12225 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12226 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12228 isl_basic_map *bmap;
12230 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12232 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12234 return map;
12237 /* Add a constraint imposing that the value of the first dimension is
12238 * smaller than that of the second.
12240 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12241 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12243 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12246 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12247 int pos)
12249 isl_aff *div;
12250 isl_local_space *ls;
12252 if (!bmap)
12253 return NULL;
12255 if (!isl_basic_map_divs_known(bmap))
12256 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12257 "some divs are unknown", return NULL);
12259 ls = isl_basic_map_get_local_space(bmap);
12260 div = isl_local_space_get_div(ls, pos);
12261 isl_local_space_free(ls);
12263 return div;
12266 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12267 int pos)
12269 return isl_basic_map_get_div(bset, pos);
12272 /* Plug in "subs" for dimension "type", "pos" of "bset".
12274 * Let i be the dimension to replace and let "subs" be of the form
12276 * f/d
12278 * Any integer division with a non-zero coefficient for i,
12280 * floor((a i + g)/m)
12282 * is replaced by
12284 * floor((a f + d g)/(m d))
12286 * Constraints of the form
12288 * a i + g
12290 * are replaced by
12292 * a f + d g
12294 * We currently require that "subs" is an integral expression.
12295 * Handling rational expressions may require us to add stride constraints
12296 * as we do in isl_basic_set_preimage_multi_aff.
12298 __isl_give isl_basic_set *isl_basic_set_substitute(
12299 __isl_take isl_basic_set *bset,
12300 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12302 int i;
12303 isl_int v;
12304 isl_ctx *ctx;
12306 if (bset && isl_basic_set_plain_is_empty(bset))
12307 return bset;
12309 bset = isl_basic_set_cow(bset);
12310 if (!bset || !subs)
12311 goto error;
12313 ctx = isl_basic_set_get_ctx(bset);
12314 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12315 isl_die(ctx, isl_error_invalid,
12316 "spaces don't match", goto error);
12317 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12318 isl_die(ctx, isl_error_unsupported,
12319 "cannot handle divs yet", goto error);
12320 if (!isl_int_is_one(subs->v->el[0]))
12321 isl_die(ctx, isl_error_invalid,
12322 "can only substitute integer expressions", goto error);
12324 pos += isl_basic_set_offset(bset, type);
12326 isl_int_init(v);
12328 for (i = 0; i < bset->n_eq; ++i) {
12329 if (isl_int_is_zero(bset->eq[i][pos]))
12330 continue;
12331 isl_int_set(v, bset->eq[i][pos]);
12332 isl_int_set_si(bset->eq[i][pos], 0);
12333 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12334 v, subs->v->el + 1, subs->v->size - 1);
12337 for (i = 0; i < bset->n_ineq; ++i) {
12338 if (isl_int_is_zero(bset->ineq[i][pos]))
12339 continue;
12340 isl_int_set(v, bset->ineq[i][pos]);
12341 isl_int_set_si(bset->ineq[i][pos], 0);
12342 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12343 v, subs->v->el + 1, subs->v->size - 1);
12346 for (i = 0; i < bset->n_div; ++i) {
12347 if (isl_int_is_zero(bset->div[i][1 + pos]))
12348 continue;
12349 isl_int_set(v, bset->div[i][1 + pos]);
12350 isl_int_set_si(bset->div[i][1 + pos], 0);
12351 isl_seq_combine(bset->div[i] + 1,
12352 subs->v->el[0], bset->div[i] + 1,
12353 v, subs->v->el + 1, subs->v->size - 1);
12354 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12357 isl_int_clear(v);
12359 bset = isl_basic_set_simplify(bset);
12360 return isl_basic_set_finalize(bset);
12361 error:
12362 isl_basic_set_free(bset);
12363 return NULL;
12366 /* Plug in "subs" for dimension "type", "pos" of "set".
12368 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12369 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12371 int i;
12373 if (set && isl_set_plain_is_empty(set))
12374 return set;
12376 set = isl_set_cow(set);
12377 if (!set || !subs)
12378 goto error;
12380 for (i = set->n - 1; i >= 0; --i) {
12381 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12382 if (remove_if_empty(set, i) < 0)
12383 goto error;
12386 return set;
12387 error:
12388 isl_set_free(set);
12389 return NULL;
12392 /* Check if the range of "ma" is compatible with the domain or range
12393 * (depending on "type") of "bmap".
12394 * Return -1 if anything is wrong.
12396 static int check_basic_map_compatible_range_multi_aff(
12397 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12398 __isl_keep isl_multi_aff *ma)
12400 int m;
12401 isl_space *ma_space;
12403 ma_space = isl_multi_aff_get_space(ma);
12405 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12406 if (m < 0)
12407 goto error;
12408 if (!m)
12409 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12410 "parameters don't match", goto error);
12411 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12412 if (m < 0)
12413 goto error;
12414 if (!m)
12415 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12416 "spaces don't match", goto error);
12418 isl_space_free(ma_space);
12419 return m;
12420 error:
12421 isl_space_free(ma_space);
12422 return -1;
12425 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12426 * coefficients before the transformed range of dimensions,
12427 * the "n_after" coefficients after the transformed range of dimensions
12428 * and the coefficients of the other divs in "bmap".
12430 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12431 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12433 int i;
12434 int n_param;
12435 int n_set;
12436 isl_local_space *ls;
12438 if (n_div == 0)
12439 return 0;
12441 ls = isl_aff_get_domain_local_space(ma->p[0]);
12442 if (!ls)
12443 return -1;
12445 n_param = isl_local_space_dim(ls, isl_dim_param);
12446 n_set = isl_local_space_dim(ls, isl_dim_set);
12447 for (i = 0; i < n_div; ++i) {
12448 int o_bmap = 0, o_ls = 0;
12450 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12451 o_bmap += 1 + 1 + n_param;
12452 o_ls += 1 + 1 + n_param;
12453 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12454 o_bmap += n_before;
12455 isl_seq_cpy(bmap->div[i] + o_bmap,
12456 ls->div->row[i] + o_ls, n_set);
12457 o_bmap += n_set;
12458 o_ls += n_set;
12459 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12460 o_bmap += n_after;
12461 isl_seq_cpy(bmap->div[i] + o_bmap,
12462 ls->div->row[i] + o_ls, n_div);
12463 o_bmap += n_div;
12464 o_ls += n_div;
12465 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12466 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12467 goto error;
12470 isl_local_space_free(ls);
12471 return 0;
12472 error:
12473 isl_local_space_free(ls);
12474 return -1;
12477 /* How many stride constraints does "ma" enforce?
12478 * That is, how many of the affine expressions have a denominator
12479 * different from one?
12481 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12483 int i;
12484 int strides = 0;
12486 for (i = 0; i < ma->n; ++i)
12487 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12488 strides++;
12490 return strides;
12493 /* For each affine expression in ma of the form
12495 * x_i = (f_i y + h_i)/m_i
12497 * with m_i different from one, add a constraint to "bmap"
12498 * of the form
12500 * f_i y + h_i = m_i alpha_i
12502 * with alpha_i an additional existentially quantified variable.
12504 * The input variables of "ma" correspond to a subset of the variables
12505 * of "bmap". There are "n_before" variables in "bmap" before this
12506 * subset and "n_after" variables after this subset.
12507 * The integer divisions of the affine expressions in "ma" are assumed
12508 * to have been aligned. There are "n_div_ma" of them and
12509 * they appear first in "bmap", straight after the "n_after" variables.
12511 static __isl_give isl_basic_map *add_ma_strides(
12512 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12513 int n_before, int n_after, int n_div_ma)
12515 int i, k;
12516 int div;
12517 int total;
12518 int n_param;
12519 int n_in;
12521 total = isl_basic_map_total_dim(bmap);
12522 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12523 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12524 for (i = 0; i < ma->n; ++i) {
12525 int o_bmap = 0, o_ma = 1;
12527 if (isl_int_is_one(ma->p[i]->v->el[0]))
12528 continue;
12529 div = isl_basic_map_alloc_div(bmap);
12530 k = isl_basic_map_alloc_equality(bmap);
12531 if (div < 0 || k < 0)
12532 goto error;
12533 isl_int_set_si(bmap->div[div][0], 0);
12534 isl_seq_cpy(bmap->eq[k] + o_bmap,
12535 ma->p[i]->v->el + o_ma, 1 + n_param);
12536 o_bmap += 1 + n_param;
12537 o_ma += 1 + n_param;
12538 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12539 o_bmap += n_before;
12540 isl_seq_cpy(bmap->eq[k] + o_bmap,
12541 ma->p[i]->v->el + o_ma, n_in);
12542 o_bmap += n_in;
12543 o_ma += n_in;
12544 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12545 o_bmap += n_after;
12546 isl_seq_cpy(bmap->eq[k] + o_bmap,
12547 ma->p[i]->v->el + o_ma, n_div_ma);
12548 o_bmap += n_div_ma;
12549 o_ma += n_div_ma;
12550 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12551 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12552 total++;
12555 return bmap;
12556 error:
12557 isl_basic_map_free(bmap);
12558 return NULL;
12561 /* Replace the domain or range space (depending on "type) of "space" by "set".
12563 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12564 enum isl_dim_type type, __isl_take isl_space *set)
12566 if (type == isl_dim_in) {
12567 space = isl_space_range(space);
12568 space = isl_space_map_from_domain_and_range(set, space);
12569 } else {
12570 space = isl_space_domain(space);
12571 space = isl_space_map_from_domain_and_range(space, set);
12574 return space;
12577 /* Compute the preimage of the domain or range (depending on "type")
12578 * of "bmap" under the function represented by "ma".
12579 * In other words, plug in "ma" in the domain or range of "bmap".
12580 * The result is a basic map that lives in the same space as "bmap"
12581 * except that the domain or range has been replaced by
12582 * the domain space of "ma".
12584 * If bmap is represented by
12586 * A(p) + S u + B x + T v + C(divs) >= 0,
12588 * where u and x are input and output dimensions if type == isl_dim_out
12589 * while x and v are input and output dimensions if type == isl_dim_in,
12590 * and ma is represented by
12592 * x = D(p) + F(y) + G(divs')
12594 * then the result is
12596 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12598 * The divs in the input set are similarly adjusted.
12599 * In particular
12601 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12603 * becomes
12605 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12606 * B_i G(divs') + c_i(divs))/n_i)
12608 * If bmap is not a rational map and if F(y) involves any denominators
12610 * x_i = (f_i y + h_i)/m_i
12612 * then additional constraints are added to ensure that we only
12613 * map back integer points. That is we enforce
12615 * f_i y + h_i = m_i alpha_i
12617 * with alpha_i an additional existentially quantified variable.
12619 * We first copy over the divs from "ma".
12620 * Then we add the modified constraints and divs from "bmap".
12621 * Finally, we add the stride constraints, if needed.
12623 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12624 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12625 __isl_take isl_multi_aff *ma)
12627 int i, k;
12628 isl_space *space;
12629 isl_basic_map *res = NULL;
12630 int n_before, n_after, n_div_bmap, n_div_ma;
12631 isl_int f, c1, c2, g;
12632 int rational, strides;
12634 isl_int_init(f);
12635 isl_int_init(c1);
12636 isl_int_init(c2);
12637 isl_int_init(g);
12639 ma = isl_multi_aff_align_divs(ma);
12640 if (!bmap || !ma)
12641 goto error;
12642 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12643 goto error;
12645 if (type == isl_dim_in) {
12646 n_before = 0;
12647 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12648 } else {
12649 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12650 n_after = 0;
12652 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12653 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12655 space = isl_multi_aff_get_domain_space(ma);
12656 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12657 rational = isl_basic_map_is_rational(bmap);
12658 strides = rational ? 0 : multi_aff_strides(ma);
12659 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12660 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12661 if (rational)
12662 res = isl_basic_map_set_rational(res);
12664 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12665 if (isl_basic_map_alloc_div(res) < 0)
12666 goto error;
12668 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12669 goto error;
12671 for (i = 0; i < bmap->n_eq; ++i) {
12672 k = isl_basic_map_alloc_equality(res);
12673 if (k < 0)
12674 goto error;
12675 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12676 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12679 for (i = 0; i < bmap->n_ineq; ++i) {
12680 k = isl_basic_map_alloc_inequality(res);
12681 if (k < 0)
12682 goto error;
12683 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12684 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12687 for (i = 0; i < bmap->n_div; ++i) {
12688 if (isl_int_is_zero(bmap->div[i][0])) {
12689 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12690 continue;
12692 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12693 n_before, n_after, n_div_ma, n_div_bmap,
12694 f, c1, c2, g, 1);
12697 if (strides)
12698 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12700 isl_int_clear(f);
12701 isl_int_clear(c1);
12702 isl_int_clear(c2);
12703 isl_int_clear(g);
12704 isl_basic_map_free(bmap);
12705 isl_multi_aff_free(ma);
12706 res = isl_basic_set_simplify(res);
12707 return isl_basic_map_finalize(res);
12708 error:
12709 isl_int_clear(f);
12710 isl_int_clear(c1);
12711 isl_int_clear(c2);
12712 isl_int_clear(g);
12713 isl_basic_map_free(bmap);
12714 isl_multi_aff_free(ma);
12715 isl_basic_map_free(res);
12716 return NULL;
12719 /* Compute the preimage of "bset" under the function represented by "ma".
12720 * In other words, plug in "ma" in "bset". The result is a basic set
12721 * that lives in the domain space of "ma".
12723 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12724 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12726 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12729 /* Compute the preimage of the domain of "bmap" under the function
12730 * represented by "ma".
12731 * In other words, plug in "ma" in the domain of "bmap".
12732 * The result is a basic map that lives in the same space as "bmap"
12733 * except that the domain has been replaced by the domain space of "ma".
12735 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12736 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12738 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12741 /* Compute the preimage of the range of "bmap" under the function
12742 * represented by "ma".
12743 * In other words, plug in "ma" in the range of "bmap".
12744 * The result is a basic map that lives in the same space as "bmap"
12745 * except that the range has been replaced by the domain space of "ma".
12747 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12748 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12750 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12753 /* Check if the range of "ma" is compatible with the domain or range
12754 * (depending on "type") of "map".
12755 * Return -1 if anything is wrong.
12757 static int check_map_compatible_range_multi_aff(
12758 __isl_keep isl_map *map, enum isl_dim_type type,
12759 __isl_keep isl_multi_aff *ma)
12761 int m;
12762 isl_space *ma_space;
12764 ma_space = isl_multi_aff_get_space(ma);
12765 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12766 isl_space_free(ma_space);
12767 if (m >= 0 && !m)
12768 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12769 "spaces don't match", return -1);
12770 return m;
12773 /* Compute the preimage of the domain or range (depending on "type")
12774 * of "map" under the function represented by "ma".
12775 * In other words, plug in "ma" in the domain or range of "map".
12776 * The result is a map that lives in the same space as "map"
12777 * except that the domain or range has been replaced by
12778 * the domain space of "ma".
12780 * The parameters are assumed to have been aligned.
12782 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12783 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12785 int i;
12786 isl_space *space;
12788 map = isl_map_cow(map);
12789 ma = isl_multi_aff_align_divs(ma);
12790 if (!map || !ma)
12791 goto error;
12792 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12793 goto error;
12795 for (i = 0; i < map->n; ++i) {
12796 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12797 isl_multi_aff_copy(ma));
12798 if (!map->p[i])
12799 goto error;
12802 space = isl_multi_aff_get_domain_space(ma);
12803 space = isl_space_set(isl_map_get_space(map), type, space);
12805 isl_space_free(map->dim);
12806 map->dim = space;
12807 if (!map->dim)
12808 goto error;
12810 isl_multi_aff_free(ma);
12811 if (map->n > 1)
12812 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12813 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12814 return map;
12815 error:
12816 isl_multi_aff_free(ma);
12817 isl_map_free(map);
12818 return NULL;
12821 /* Compute the preimage of the domain or range (depending on "type")
12822 * of "map" under the function represented by "ma".
12823 * In other words, plug in "ma" in the domain or range of "map".
12824 * The result is a map that lives in the same space as "map"
12825 * except that the domain or range has been replaced by
12826 * the domain space of "ma".
12828 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12829 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12831 if (!map || !ma)
12832 goto error;
12834 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12835 return map_preimage_multi_aff(map, type, ma);
12837 if (!isl_space_has_named_params(map->dim) ||
12838 !isl_space_has_named_params(ma->space))
12839 isl_die(map->ctx, isl_error_invalid,
12840 "unaligned unnamed parameters", goto error);
12841 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12842 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12844 return map_preimage_multi_aff(map, type, ma);
12845 error:
12846 isl_multi_aff_free(ma);
12847 return isl_map_free(map);
12850 /* Compute the preimage of "set" under the function represented by "ma".
12851 * In other words, plug in "ma" in "set". The result is a set
12852 * that lives in the domain space of "ma".
12854 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12855 __isl_take isl_multi_aff *ma)
12857 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12860 /* Compute the preimage of the domain of "map" under the function
12861 * represented by "ma".
12862 * In other words, plug in "ma" in the domain of "map".
12863 * The result is a map that lives in the same space as "map"
12864 * except that the domain has been replaced by the domain space of "ma".
12866 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12867 __isl_take isl_multi_aff *ma)
12869 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12872 /* Compute the preimage of the range of "map" under the function
12873 * represented by "ma".
12874 * In other words, plug in "ma" in the range of "map".
12875 * The result is a map that lives in the same space as "map"
12876 * except that the range has been replaced by the domain space of "ma".
12878 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12879 __isl_take isl_multi_aff *ma)
12881 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12884 /* Compute the preimage of "map" under the function represented by "pma".
12885 * In other words, plug in "pma" in the domain or range of "map".
12886 * The result is a map that lives in the same space as "map",
12887 * except that the space of type "type" has been replaced by
12888 * the domain space of "pma".
12890 * The parameters of "map" and "pma" are assumed to have been aligned.
12892 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12893 __isl_take isl_map *map, enum isl_dim_type type,
12894 __isl_take isl_pw_multi_aff *pma)
12896 int i;
12897 isl_map *res;
12899 if (!pma)
12900 goto error;
12902 if (pma->n == 0) {
12903 isl_pw_multi_aff_free(pma);
12904 res = isl_map_empty(isl_map_get_space(map));
12905 isl_map_free(map);
12906 return res;
12909 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12910 isl_multi_aff_copy(pma->p[0].maff));
12911 if (type == isl_dim_in)
12912 res = isl_map_intersect_domain(res,
12913 isl_map_copy(pma->p[0].set));
12914 else
12915 res = isl_map_intersect_range(res,
12916 isl_map_copy(pma->p[0].set));
12918 for (i = 1; i < pma->n; ++i) {
12919 isl_map *res_i;
12921 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12922 isl_multi_aff_copy(pma->p[i].maff));
12923 if (type == isl_dim_in)
12924 res_i = isl_map_intersect_domain(res_i,
12925 isl_map_copy(pma->p[i].set));
12926 else
12927 res_i = isl_map_intersect_range(res_i,
12928 isl_map_copy(pma->p[i].set));
12929 res = isl_map_union(res, res_i);
12932 isl_pw_multi_aff_free(pma);
12933 isl_map_free(map);
12934 return res;
12935 error:
12936 isl_pw_multi_aff_free(pma);
12937 isl_map_free(map);
12938 return NULL;
12941 /* Compute the preimage of "map" under the function represented by "pma".
12942 * In other words, plug in "pma" in the domain or range of "map".
12943 * The result is a map that lives in the same space as "map",
12944 * except that the space of type "type" has been replaced by
12945 * the domain space of "pma".
12947 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12948 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12950 if (!map || !pma)
12951 goto error;
12953 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12954 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12956 if (!isl_space_has_named_params(map->dim) ||
12957 !isl_space_has_named_params(pma->dim))
12958 isl_die(map->ctx, isl_error_invalid,
12959 "unaligned unnamed parameters", goto error);
12960 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12961 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12963 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12964 error:
12965 isl_pw_multi_aff_free(pma);
12966 return isl_map_free(map);
12969 /* Compute the preimage of "set" under the function represented by "pma".
12970 * In other words, plug in "pma" in "set". The result is a set
12971 * that lives in the domain space of "pma".
12973 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12974 __isl_take isl_pw_multi_aff *pma)
12976 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12979 /* Compute the preimage of the domain of "map" under the function
12980 * represented by "pma".
12981 * In other words, plug in "pma" in the domain of "map".
12982 * The result is a map that lives in the same space as "map",
12983 * except that domain space has been replaced by the domain space of "pma".
12985 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12986 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12988 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12991 /* Compute the preimage of the range of "map" under the function
12992 * represented by "pma".
12993 * In other words, plug in "pma" in the range of "map".
12994 * The result is a map that lives in the same space as "map",
12995 * except that range space has been replaced by the domain space of "pma".
12997 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12998 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13000 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13003 /* Compute the preimage of "map" under the function represented by "mpa".
13004 * In other words, plug in "mpa" in the domain or range of "map".
13005 * The result is a map that lives in the same space as "map",
13006 * except that the space of type "type" has been replaced by
13007 * the domain space of "mpa".
13009 * If the map does not involve any constraints that refer to the
13010 * dimensions of the substituted space, then the only possible
13011 * effect of "mpa" on the map is to map the space to a different space.
13012 * We create a separate isl_multi_aff to effectuate this change
13013 * in order to avoid spurious splitting of the map along the pieces
13014 * of "mpa".
13016 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13017 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13019 int n;
13020 isl_pw_multi_aff *pma;
13022 if (!map || !mpa)
13023 goto error;
13025 n = isl_map_dim(map, type);
13026 if (!isl_map_involves_dims(map, type, 0, n)) {
13027 isl_space *space;
13028 isl_multi_aff *ma;
13030 space = isl_multi_pw_aff_get_space(mpa);
13031 isl_multi_pw_aff_free(mpa);
13032 ma = isl_multi_aff_zero(space);
13033 return isl_map_preimage_multi_aff(map, type, ma);
13036 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13037 return isl_map_preimage_pw_multi_aff(map, type, pma);
13038 error:
13039 isl_map_free(map);
13040 isl_multi_pw_aff_free(mpa);
13041 return NULL;
13044 /* Compute the preimage of "map" under the function represented by "mpa".
13045 * In other words, plug in "mpa" in the domain "map".
13046 * The result is a map that lives in the same space as "map",
13047 * except that domain space has been replaced by the domain space of "mpa".
13049 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13050 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13052 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13055 /* Compute the preimage of "set" by the function represented by "mpa".
13056 * In other words, plug in "mpa" in "set".
13058 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13059 __isl_take isl_multi_pw_aff *mpa)
13061 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);