isl_*_partial_lex{min,max}_pw_multi_aff: remove existentials fix-up code
[isl.git] / isl_map.c
blobddff7c6f898388daca918185653afd9dae42c61b
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 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
204 __isl_keep isl_basic_set *bset)
206 isl_bool m;
207 if (!bmap || !bset)
208 return isl_bool_error;
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 /* Insert an extra integer division, prescribed by "div", to "bmap"
1429 * at (integer division) position "pos".
1431 * The integer division is first added at the end and then moved
1432 * into the right position.
1434 __isl_give isl_basic_map *isl_basic_map_insert_div(
1435 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1437 int i, k, n_div;
1439 bmap = isl_basic_map_cow(bmap);
1440 if (!bmap || !div)
1441 return isl_basic_map_free(bmap);
1443 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1444 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1445 "unexpected size", return isl_basic_map_free(bmap));
1446 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1447 if (pos < 0 || pos > n_div)
1448 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1449 "invalid position", return isl_basic_map_free(bmap));
1451 bmap = isl_basic_map_extend_space(bmap,
1452 isl_basic_map_get_space(bmap), 1, 0, 2);
1453 k = isl_basic_map_alloc_div(bmap);
1454 if (k < 0)
1455 return isl_basic_map_free(bmap);
1456 isl_seq_cpy(bmap->div[k], div->el, div->size);
1457 isl_int_set_si(bmap->div[k][div->size], 0);
1459 for (i = k; i > pos; --i)
1460 isl_basic_map_swap_div(bmap, i, i - 1);
1462 return bmap;
1465 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1467 if (!bmap)
1468 return -1;
1469 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1470 bmap->n_div -= n;
1471 return 0;
1474 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1476 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1479 /* Copy constraint from src to dst, putting the vars of src at offset
1480 * dim_off in dst and the divs of src at offset div_off in dst.
1481 * If both sets are actually map, then dim_off applies to the input
1482 * variables.
1484 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1485 struct isl_basic_map *src_map, isl_int *src,
1486 unsigned in_off, unsigned out_off, unsigned div_off)
1488 unsigned src_nparam = isl_basic_map_n_param(src_map);
1489 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1490 unsigned src_in = isl_basic_map_n_in(src_map);
1491 unsigned dst_in = isl_basic_map_n_in(dst_map);
1492 unsigned src_out = isl_basic_map_n_out(src_map);
1493 unsigned dst_out = isl_basic_map_n_out(dst_map);
1494 isl_int_set(dst[0], src[0]);
1495 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1496 if (dst_nparam > src_nparam)
1497 isl_seq_clr(dst+1+src_nparam,
1498 dst_nparam - src_nparam);
1499 isl_seq_clr(dst+1+dst_nparam, in_off);
1500 isl_seq_cpy(dst+1+dst_nparam+in_off,
1501 src+1+src_nparam,
1502 isl_min(dst_in-in_off, src_in));
1503 if (dst_in-in_off > src_in)
1504 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1505 dst_in - in_off - src_in);
1506 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1507 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1508 src+1+src_nparam+src_in,
1509 isl_min(dst_out-out_off, src_out));
1510 if (dst_out-out_off > src_out)
1511 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1512 dst_out - out_off - src_out);
1513 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1514 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1515 src+1+src_nparam+src_in+src_out,
1516 isl_min(dst_map->extra-div_off, src_map->n_div));
1517 if (dst_map->n_div-div_off > src_map->n_div)
1518 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1519 div_off+src_map->n_div,
1520 dst_map->n_div - div_off - src_map->n_div);
1523 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1524 struct isl_basic_map *src_map, isl_int *src,
1525 unsigned in_off, unsigned out_off, unsigned div_off)
1527 isl_int_set(dst[0], src[0]);
1528 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1531 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1532 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1534 int i;
1535 unsigned div_off;
1537 if (!bmap1 || !bmap2)
1538 goto error;
1540 div_off = bmap1->n_div;
1542 for (i = 0; i < bmap2->n_eq; ++i) {
1543 int i1 = isl_basic_map_alloc_equality(bmap1);
1544 if (i1 < 0)
1545 goto error;
1546 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1547 i_pos, o_pos, div_off);
1550 for (i = 0; i < bmap2->n_ineq; ++i) {
1551 int i1 = isl_basic_map_alloc_inequality(bmap1);
1552 if (i1 < 0)
1553 goto error;
1554 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1555 i_pos, o_pos, div_off);
1558 for (i = 0; i < bmap2->n_div; ++i) {
1559 int i1 = isl_basic_map_alloc_div(bmap1);
1560 if (i1 < 0)
1561 goto error;
1562 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1563 i_pos, o_pos, div_off);
1566 isl_basic_map_free(bmap2);
1568 return bmap1;
1570 error:
1571 isl_basic_map_free(bmap1);
1572 isl_basic_map_free(bmap2);
1573 return NULL;
1576 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1577 struct isl_basic_set *bset2, unsigned pos)
1579 return (struct isl_basic_set *)
1580 add_constraints((struct isl_basic_map *)bset1,
1581 (struct isl_basic_map *)bset2, 0, pos);
1584 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1585 __isl_take isl_space *dim, unsigned extra,
1586 unsigned n_eq, unsigned n_ineq)
1588 struct isl_basic_map *ext;
1589 unsigned flags;
1590 int dims_ok;
1592 if (!dim)
1593 goto error;
1595 if (!base)
1596 goto error;
1598 dims_ok = isl_space_is_equal(base->dim, dim) &&
1599 base->extra >= base->n_div + extra;
1601 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1602 room_for_ineq(base, n_ineq)) {
1603 isl_space_free(dim);
1604 return base;
1607 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1608 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1609 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1610 extra += base->extra;
1611 n_eq += base->n_eq;
1612 n_ineq += base->n_ineq;
1614 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1615 dim = NULL;
1616 if (!ext)
1617 goto error;
1619 if (dims_ok)
1620 ext->sample = isl_vec_copy(base->sample);
1621 flags = base->flags;
1622 ext = add_constraints(ext, base, 0, 0);
1623 if (ext) {
1624 ext->flags = flags;
1625 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1628 return ext;
1630 error:
1631 isl_space_free(dim);
1632 isl_basic_map_free(base);
1633 return NULL;
1636 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1637 __isl_take isl_space *dim, unsigned extra,
1638 unsigned n_eq, unsigned n_ineq)
1640 return (struct isl_basic_set *)
1641 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1642 extra, n_eq, n_ineq);
1645 struct isl_basic_map *isl_basic_map_extend_constraints(
1646 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1648 if (!base)
1649 return NULL;
1650 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1651 0, n_eq, n_ineq);
1654 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1655 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1656 unsigned n_eq, unsigned n_ineq)
1658 struct isl_basic_map *bmap;
1659 isl_space *dim;
1661 if (!base)
1662 return NULL;
1663 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1664 if (!dim)
1665 goto error;
1667 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1668 return bmap;
1669 error:
1670 isl_basic_map_free(base);
1671 return NULL;
1674 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1675 unsigned nparam, unsigned dim, unsigned extra,
1676 unsigned n_eq, unsigned n_ineq)
1678 return (struct isl_basic_set *)
1679 isl_basic_map_extend((struct isl_basic_map *)base,
1680 nparam, 0, dim, extra, n_eq, n_ineq);
1683 struct isl_basic_set *isl_basic_set_extend_constraints(
1684 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1686 return (struct isl_basic_set *)
1687 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1688 n_eq, n_ineq);
1691 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1693 return (struct isl_basic_set *)
1694 isl_basic_map_cow((struct isl_basic_map *)bset);
1697 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1699 if (!bmap)
1700 return NULL;
1702 if (bmap->ref > 1) {
1703 bmap->ref--;
1704 bmap = isl_basic_map_dup(bmap);
1706 if (bmap) {
1707 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1708 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1710 return bmap;
1713 /* Clear all cached information in "map", either because it is about
1714 * to be modified or because it is being freed.
1715 * Always return the same pointer that is passed in.
1716 * This is needed for the use in isl_map_free.
1718 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1720 isl_basic_map_free(map->cached_simple_hull[0]);
1721 isl_basic_map_free(map->cached_simple_hull[1]);
1722 map->cached_simple_hull[0] = NULL;
1723 map->cached_simple_hull[1] = NULL;
1724 return map;
1727 struct isl_set *isl_set_cow(struct isl_set *set)
1729 return isl_map_cow(set);
1732 /* Return an isl_map that is equal to "map" and that has only
1733 * a single reference.
1735 * If the original input already has only one reference, then
1736 * simply return it, but clear all cached information, since
1737 * it may be rendered invalid by the operations that will be
1738 * performed on the result.
1740 * Otherwise, create a duplicate (without any cached information).
1742 struct isl_map *isl_map_cow(struct isl_map *map)
1744 if (!map)
1745 return NULL;
1747 if (map->ref == 1)
1748 return clear_caches(map);
1749 map->ref--;
1750 return isl_map_dup(map);
1753 static void swap_vars(struct isl_blk blk, isl_int *a,
1754 unsigned a_len, unsigned b_len)
1756 isl_seq_cpy(blk.data, a+a_len, b_len);
1757 isl_seq_cpy(blk.data+b_len, a, a_len);
1758 isl_seq_cpy(a, blk.data, b_len+a_len);
1761 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1762 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1764 int i;
1765 struct isl_blk blk;
1767 if (!bmap)
1768 goto error;
1770 isl_assert(bmap->ctx,
1771 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1773 if (n1 == 0 || n2 == 0)
1774 return bmap;
1776 bmap = isl_basic_map_cow(bmap);
1777 if (!bmap)
1778 return NULL;
1780 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1781 if (isl_blk_is_error(blk))
1782 goto error;
1784 for (i = 0; i < bmap->n_eq; ++i)
1785 swap_vars(blk,
1786 bmap->eq[i] + pos, n1, n2);
1788 for (i = 0; i < bmap->n_ineq; ++i)
1789 swap_vars(blk,
1790 bmap->ineq[i] + pos, n1, n2);
1792 for (i = 0; i < bmap->n_div; ++i)
1793 swap_vars(blk,
1794 bmap->div[i]+1 + pos, n1, n2);
1796 isl_blk_free(bmap->ctx, blk);
1798 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1799 bmap = isl_basic_map_gauss(bmap, NULL);
1800 return isl_basic_map_finalize(bmap);
1801 error:
1802 isl_basic_map_free(bmap);
1803 return NULL;
1806 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1808 int i = 0;
1809 unsigned total;
1810 if (!bmap)
1811 goto error;
1812 total = isl_basic_map_total_dim(bmap);
1813 isl_basic_map_free_div(bmap, bmap->n_div);
1814 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1815 if (bmap->n_eq > 0)
1816 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1817 else {
1818 i = isl_basic_map_alloc_equality(bmap);
1819 if (i < 0)
1820 goto error;
1822 isl_int_set_si(bmap->eq[i][0], 1);
1823 isl_seq_clr(bmap->eq[i]+1, total);
1824 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1825 isl_vec_free(bmap->sample);
1826 bmap->sample = NULL;
1827 return isl_basic_map_finalize(bmap);
1828 error:
1829 isl_basic_map_free(bmap);
1830 return NULL;
1833 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1835 return (struct isl_basic_set *)
1836 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1839 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1840 * of "bmap").
1842 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1844 isl_int *t = bmap->div[a];
1845 bmap->div[a] = bmap->div[b];
1846 bmap->div[b] = t;
1849 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1850 * div definitions accordingly.
1852 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1854 int i;
1855 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1857 swap_div(bmap, a, b);
1859 for (i = 0; i < bmap->n_eq; ++i)
1860 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1862 for (i = 0; i < bmap->n_ineq; ++i)
1863 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1865 for (i = 0; i < bmap->n_div; ++i)
1866 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1867 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1870 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1871 * div definitions accordingly.
1873 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1875 isl_basic_map_swap_div(bset, a, b);
1878 /* Eliminate the specified n dimensions starting at first from the
1879 * constraints, without removing the dimensions from the space.
1880 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1882 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1883 enum isl_dim_type type, unsigned first, unsigned n)
1885 int i;
1887 if (!map)
1888 return NULL;
1889 if (n == 0)
1890 return map;
1892 if (first + n > isl_map_dim(map, type) || first + n < first)
1893 isl_die(map->ctx, isl_error_invalid,
1894 "index out of bounds", goto error);
1896 map = isl_map_cow(map);
1897 if (!map)
1898 return NULL;
1900 for (i = 0; i < map->n; ++i) {
1901 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1902 if (!map->p[i])
1903 goto error;
1905 return map;
1906 error:
1907 isl_map_free(map);
1908 return NULL;
1911 /* Eliminate the specified n dimensions starting at first from the
1912 * constraints, without removing the dimensions from the space.
1913 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1915 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1916 enum isl_dim_type type, unsigned first, unsigned n)
1918 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1921 /* Eliminate the specified n dimensions starting at first from the
1922 * constraints, without removing the dimensions from the space.
1923 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1925 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1926 unsigned first, unsigned n)
1928 return isl_set_eliminate(set, isl_dim_set, first, n);
1931 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1932 __isl_take isl_basic_map *bmap)
1934 if (!bmap)
1935 return NULL;
1936 bmap = isl_basic_map_eliminate_vars(bmap,
1937 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1938 if (!bmap)
1939 return NULL;
1940 bmap->n_div = 0;
1941 return isl_basic_map_finalize(bmap);
1944 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1945 __isl_take isl_basic_set *bset)
1947 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1948 (struct isl_basic_map *)bset);
1951 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1953 int i;
1955 if (!map)
1956 return NULL;
1957 if (map->n == 0)
1958 return map;
1960 map = isl_map_cow(map);
1961 if (!map)
1962 return NULL;
1964 for (i = 0; i < map->n; ++i) {
1965 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1966 if (!map->p[i])
1967 goto error;
1969 return map;
1970 error:
1971 isl_map_free(map);
1972 return NULL;
1975 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1977 return isl_map_remove_divs(set);
1980 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1981 enum isl_dim_type type, unsigned first, unsigned n)
1983 if (!bmap)
1984 return NULL;
1985 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1986 goto error);
1987 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1988 return bmap;
1989 bmap = isl_basic_map_eliminate_vars(bmap,
1990 isl_basic_map_offset(bmap, type) - 1 + first, n);
1991 if (!bmap)
1992 return bmap;
1993 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1994 return bmap;
1995 bmap = isl_basic_map_drop(bmap, type, first, n);
1996 return bmap;
1997 error:
1998 isl_basic_map_free(bmap);
1999 return NULL;
2002 /* Return true if the definition of the given div (recursively) involves
2003 * any of the given variables.
2005 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2006 unsigned first, unsigned n)
2008 int i;
2009 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2011 if (isl_int_is_zero(bmap->div[div][0]))
2012 return 0;
2013 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2014 return 1;
2016 for (i = bmap->n_div - 1; i >= 0; --i) {
2017 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2018 continue;
2019 if (div_involves_vars(bmap, i, first, n))
2020 return 1;
2023 return 0;
2026 /* Try and add a lower and/or upper bound on "div" to "bmap"
2027 * based on inequality "i".
2028 * "total" is the total number of variables (excluding the divs).
2029 * "v" is a temporary object that can be used during the calculations.
2030 * If "lb" is set, then a lower bound should be constructed.
2031 * If "ub" is set, then an upper bound should be constructed.
2033 * The calling function has already checked that the inequality does not
2034 * reference "div", but we still need to check that the inequality is
2035 * of the right form. We'll consider the case where we want to construct
2036 * a lower bound. The construction of upper bounds is similar.
2038 * Let "div" be of the form
2040 * q = floor((a + f(x))/d)
2042 * We essentially check if constraint "i" is of the form
2044 * b + f(x) >= 0
2046 * so that we can use it to derive a lower bound on "div".
2047 * However, we allow a slightly more general form
2049 * b + g(x) >= 0
2051 * with the condition that the coefficients of g(x) - f(x) are all
2052 * divisible by d.
2053 * Rewriting this constraint as
2055 * 0 >= -b - g(x)
2057 * adding a + f(x) to both sides and dividing by d, we obtain
2059 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2061 * Taking the floor on both sides, we obtain
2063 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2065 * or
2067 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2069 * In the case of an upper bound, we construct the constraint
2071 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2074 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2075 __isl_take isl_basic_map *bmap, int div, int i,
2076 unsigned total, isl_int v, int lb, int ub)
2078 int j;
2080 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2081 if (lb) {
2082 isl_int_sub(v, bmap->ineq[i][1 + j],
2083 bmap->div[div][1 + 1 + j]);
2084 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2086 if (ub) {
2087 isl_int_add(v, bmap->ineq[i][1 + j],
2088 bmap->div[div][1 + 1 + j]);
2089 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2092 if (!lb && !ub)
2093 return bmap;
2095 bmap = isl_basic_map_cow(bmap);
2096 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2097 if (lb) {
2098 int k = isl_basic_map_alloc_inequality(bmap);
2099 if (k < 0)
2100 goto error;
2101 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2102 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2103 bmap->div[div][1 + j]);
2104 isl_int_cdiv_q(bmap->ineq[k][j],
2105 bmap->ineq[k][j], bmap->div[div][0]);
2107 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2109 if (ub) {
2110 int k = isl_basic_map_alloc_inequality(bmap);
2111 if (k < 0)
2112 goto error;
2113 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2114 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2115 bmap->div[div][1 + j]);
2116 isl_int_fdiv_q(bmap->ineq[k][j],
2117 bmap->ineq[k][j], bmap->div[div][0]);
2119 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2122 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2123 return bmap;
2124 error:
2125 isl_basic_map_free(bmap);
2126 return NULL;
2129 /* This function is called right before "div" is eliminated from "bmap"
2130 * using Fourier-Motzkin.
2131 * Look through the constraints of "bmap" for constraints on the argument
2132 * of the integer division and use them to construct constraints on the
2133 * integer division itself. These constraints can then be combined
2134 * during the Fourier-Motzkin elimination.
2135 * Note that it is only useful to introduce lower bounds on "div"
2136 * if "bmap" already contains upper bounds on "div" as the newly
2137 * introduce lower bounds can then be combined with the pre-existing
2138 * upper bounds. Similarly for upper bounds.
2139 * We therefore first check if "bmap" contains any lower and/or upper bounds
2140 * on "div".
2142 * It is interesting to note that the introduction of these constraints
2143 * can indeed lead to more accurate results, even when compared to
2144 * deriving constraints on the argument of "div" from constraints on "div".
2145 * Consider, for example, the set
2147 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2149 * The second constraint can be rewritten as
2151 * 2 * [(-i-2j+3)/4] + k >= 0
2153 * from which we can derive
2155 * -i - 2j + 3 >= -2k
2157 * or
2159 * i + 2j <= 3 + 2k
2161 * Combined with the first constraint, we obtain
2163 * -3 <= 3 + 2k or k >= -3
2165 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2166 * the first constraint, we obtain
2168 * [(i + 2j)/4] >= [-3/4] = -1
2170 * Combining this constraint with the second constraint, we obtain
2172 * k >= -2
2174 static __isl_give isl_basic_map *insert_bounds_on_div(
2175 __isl_take isl_basic_map *bmap, int div)
2177 int i;
2178 int check_lb, check_ub;
2179 isl_int v;
2180 unsigned total;
2182 if (!bmap)
2183 return NULL;
2185 if (isl_int_is_zero(bmap->div[div][0]))
2186 return bmap;
2188 total = isl_space_dim(bmap->dim, isl_dim_all);
2190 check_lb = 0;
2191 check_ub = 0;
2192 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2193 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2194 if (s > 0)
2195 check_ub = 1;
2196 if (s < 0)
2197 check_lb = 1;
2200 if (!check_lb && !check_ub)
2201 return bmap;
2203 isl_int_init(v);
2205 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2206 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2207 continue;
2209 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2210 check_lb, check_ub);
2213 isl_int_clear(v);
2215 return bmap;
2218 /* Remove all divs (recursively) involving any of the given dimensions
2219 * in their definitions.
2221 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2222 __isl_take isl_basic_map *bmap,
2223 enum isl_dim_type type, unsigned first, unsigned n)
2225 int i;
2227 if (!bmap)
2228 return NULL;
2229 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2230 goto error);
2231 first += isl_basic_map_offset(bmap, type);
2233 for (i = bmap->n_div - 1; i >= 0; --i) {
2234 if (!div_involves_vars(bmap, i, first, n))
2235 continue;
2236 bmap = insert_bounds_on_div(bmap, i);
2237 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2238 if (!bmap)
2239 return NULL;
2240 i = bmap->n_div;
2243 return bmap;
2244 error:
2245 isl_basic_map_free(bmap);
2246 return NULL;
2249 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2250 __isl_take isl_basic_set *bset,
2251 enum isl_dim_type type, unsigned first, unsigned n)
2253 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2256 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2257 enum isl_dim_type type, unsigned first, unsigned n)
2259 int i;
2261 if (!map)
2262 return NULL;
2263 if (map->n == 0)
2264 return map;
2266 map = isl_map_cow(map);
2267 if (!map)
2268 return NULL;
2270 for (i = 0; i < map->n; ++i) {
2271 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2272 type, first, n);
2273 if (!map->p[i])
2274 goto error;
2276 return map;
2277 error:
2278 isl_map_free(map);
2279 return NULL;
2282 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2283 enum isl_dim_type type, unsigned first, unsigned n)
2285 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2286 type, first, n);
2289 /* Does the desciption of "bmap" depend on the specified dimensions?
2290 * We also check whether the dimensions appear in any of the div definitions.
2291 * In principle there is no need for this check. If the dimensions appear
2292 * in a div definition, they also appear in the defining constraints of that
2293 * div.
2295 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2296 enum isl_dim_type type, unsigned first, unsigned n)
2298 int i;
2300 if (!bmap)
2301 return isl_bool_error;
2303 if (first + n > isl_basic_map_dim(bmap, type))
2304 isl_die(bmap->ctx, isl_error_invalid,
2305 "index out of bounds", return isl_bool_error);
2307 first += isl_basic_map_offset(bmap, type);
2308 for (i = 0; i < bmap->n_eq; ++i)
2309 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2310 return isl_bool_true;
2311 for (i = 0; i < bmap->n_ineq; ++i)
2312 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2313 return isl_bool_true;
2314 for (i = 0; i < bmap->n_div; ++i) {
2315 if (isl_int_is_zero(bmap->div[i][0]))
2316 continue;
2317 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2318 return isl_bool_true;
2321 return isl_bool_false;
2324 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2325 enum isl_dim_type type, unsigned first, unsigned n)
2327 int i;
2329 if (!map)
2330 return isl_bool_error;
2332 if (first + n > isl_map_dim(map, type))
2333 isl_die(map->ctx, isl_error_invalid,
2334 "index out of bounds", return isl_bool_error);
2336 for (i = 0; i < map->n; ++i) {
2337 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2338 type, first, n);
2339 if (involves < 0 || involves)
2340 return involves;
2343 return isl_bool_false;
2346 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2347 enum isl_dim_type type, unsigned first, unsigned n)
2349 return isl_basic_map_involves_dims(bset, type, first, n);
2352 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2353 enum isl_dim_type type, unsigned first, unsigned n)
2355 return isl_map_involves_dims(set, type, first, n);
2358 /* Does local variable "div" of "bmap" have a complete explicit representation?
2359 * Having a complete explicit representation requires not only
2360 * an explicit representation, but also that all local variables
2361 * that appear in this explicit representation in turn have
2362 * a complete explicit representation.
2364 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2366 int i;
2367 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2368 isl_bool marked;
2370 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2371 if (marked < 0 || marked)
2372 return isl_bool_not(marked);
2374 for (i = bmap->n_div - 1; i >= 0; --i) {
2375 isl_bool known;
2377 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2378 continue;
2379 known = isl_basic_map_div_is_known(bmap, i);
2380 if (known < 0 || !known)
2381 return known;
2384 return isl_bool_true;
2387 /* Does local variable "div" of "bset" have a complete explicit representation?
2389 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2391 return isl_basic_map_div_is_known(bset, div);
2394 /* Remove all divs that are unknown or defined in terms of unknown divs.
2396 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2397 __isl_take isl_basic_map *bmap)
2399 int i;
2401 if (!bmap)
2402 return NULL;
2404 for (i = bmap->n_div - 1; i >= 0; --i) {
2405 if (isl_basic_map_div_is_known(bmap, i))
2406 continue;
2407 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2408 if (!bmap)
2409 return NULL;
2410 i = bmap->n_div;
2413 return bmap;
2416 /* Remove all divs that are unknown or defined in terms of unknown divs.
2418 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2419 __isl_take isl_basic_set *bset)
2421 return isl_basic_map_remove_unknown_divs(bset);
2424 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2426 int i;
2428 if (!map)
2429 return NULL;
2430 if (map->n == 0)
2431 return map;
2433 map = isl_map_cow(map);
2434 if (!map)
2435 return NULL;
2437 for (i = 0; i < map->n; ++i) {
2438 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2439 if (!map->p[i])
2440 goto error;
2442 return map;
2443 error:
2444 isl_map_free(map);
2445 return NULL;
2448 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2450 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2453 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2454 __isl_take isl_basic_set *bset,
2455 enum isl_dim_type type, unsigned first, unsigned n)
2457 return (isl_basic_set *)
2458 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2461 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2462 enum isl_dim_type type, unsigned first, unsigned n)
2464 int i;
2466 if (n == 0)
2467 return map;
2469 map = isl_map_cow(map);
2470 if (!map)
2471 return NULL;
2472 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2474 for (i = 0; i < map->n; ++i) {
2475 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2476 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2477 if (!map->p[i])
2478 goto error;
2480 map = isl_map_drop(map, type, first, n);
2481 return map;
2482 error:
2483 isl_map_free(map);
2484 return NULL;
2487 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2488 enum isl_dim_type type, unsigned first, unsigned n)
2490 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2493 /* Project out n inputs starting at first using Fourier-Motzkin */
2494 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2495 unsigned first, unsigned n)
2497 return isl_map_remove_dims(map, isl_dim_in, first, n);
2500 static void dump_term(struct isl_basic_map *bmap,
2501 isl_int c, int pos, FILE *out)
2503 const char *name;
2504 unsigned in = isl_basic_map_n_in(bmap);
2505 unsigned dim = in + isl_basic_map_n_out(bmap);
2506 unsigned nparam = isl_basic_map_n_param(bmap);
2507 if (!pos)
2508 isl_int_print(out, c, 0);
2509 else {
2510 if (!isl_int_is_one(c))
2511 isl_int_print(out, c, 0);
2512 if (pos < 1 + nparam) {
2513 name = isl_space_get_dim_name(bmap->dim,
2514 isl_dim_param, pos - 1);
2515 if (name)
2516 fprintf(out, "%s", name);
2517 else
2518 fprintf(out, "p%d", pos - 1);
2519 } else if (pos < 1 + nparam + in)
2520 fprintf(out, "i%d", pos - 1 - nparam);
2521 else if (pos < 1 + nparam + dim)
2522 fprintf(out, "o%d", pos - 1 - nparam - in);
2523 else
2524 fprintf(out, "e%d", pos - 1 - nparam - dim);
2528 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2529 int sign, FILE *out)
2531 int i;
2532 int first;
2533 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2534 isl_int v;
2536 isl_int_init(v);
2537 for (i = 0, first = 1; i < len; ++i) {
2538 if (isl_int_sgn(c[i]) * sign <= 0)
2539 continue;
2540 if (!first)
2541 fprintf(out, " + ");
2542 first = 0;
2543 isl_int_abs(v, c[i]);
2544 dump_term(bmap, v, i, out);
2546 isl_int_clear(v);
2547 if (first)
2548 fprintf(out, "0");
2551 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2552 const char *op, FILE *out, int indent)
2554 int i;
2556 fprintf(out, "%*s", indent, "");
2558 dump_constraint_sign(bmap, c, 1, out);
2559 fprintf(out, " %s ", op);
2560 dump_constraint_sign(bmap, c, -1, out);
2562 fprintf(out, "\n");
2564 for (i = bmap->n_div; i < bmap->extra; ++i) {
2565 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2566 continue;
2567 fprintf(out, "%*s", indent, "");
2568 fprintf(out, "ERROR: unused div coefficient not zero\n");
2569 abort();
2573 static void dump_constraints(struct isl_basic_map *bmap,
2574 isl_int **c, unsigned n,
2575 const char *op, FILE *out, int indent)
2577 int i;
2579 for (i = 0; i < n; ++i)
2580 dump_constraint(bmap, c[i], op, out, indent);
2583 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2585 int j;
2586 int first = 1;
2587 unsigned total = isl_basic_map_total_dim(bmap);
2589 for (j = 0; j < 1 + total; ++j) {
2590 if (isl_int_is_zero(exp[j]))
2591 continue;
2592 if (!first && isl_int_is_pos(exp[j]))
2593 fprintf(out, "+");
2594 dump_term(bmap, exp[j], j, out);
2595 first = 0;
2599 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2601 int i;
2603 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2604 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2606 for (i = 0; i < bmap->n_div; ++i) {
2607 fprintf(out, "%*s", indent, "");
2608 fprintf(out, "e%d = [(", i);
2609 dump_affine(bmap, bmap->div[i]+1, out);
2610 fprintf(out, ")/");
2611 isl_int_print(out, bmap->div[i][0], 0);
2612 fprintf(out, "]\n");
2616 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2617 FILE *out, int indent)
2619 if (!bset) {
2620 fprintf(out, "null basic set\n");
2621 return;
2624 fprintf(out, "%*s", indent, "");
2625 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2626 bset->ref, bset->dim->nparam, bset->dim->n_out,
2627 bset->extra, bset->flags);
2628 dump((struct isl_basic_map *)bset, out, indent);
2631 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2632 FILE *out, int indent)
2634 if (!bmap) {
2635 fprintf(out, "null basic map\n");
2636 return;
2639 fprintf(out, "%*s", indent, "");
2640 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2641 "flags: %x, n_name: %d\n",
2642 bmap->ref,
2643 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2644 bmap->extra, bmap->flags, bmap->dim->n_id);
2645 dump(bmap, out, indent);
2648 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2650 unsigned total;
2651 if (!bmap)
2652 return -1;
2653 total = isl_basic_map_total_dim(bmap);
2654 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2655 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2656 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2657 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2658 return 0;
2661 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2662 unsigned flags)
2664 if (!space)
2665 return NULL;
2666 if (isl_space_dim(space, isl_dim_in) != 0)
2667 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2668 "set cannot have input dimensions", goto error);
2669 return isl_map_alloc_space(space, n, flags);
2670 error:
2671 isl_space_free(space);
2672 return NULL;
2675 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2676 unsigned nparam, unsigned dim, int n, unsigned flags)
2678 struct isl_set *set;
2679 isl_space *dims;
2681 dims = isl_space_alloc(ctx, nparam, 0, dim);
2682 if (!dims)
2683 return NULL;
2685 set = isl_set_alloc_space(dims, n, flags);
2686 return set;
2689 /* Make sure "map" has room for at least "n" more basic maps.
2691 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2693 int i;
2694 struct isl_map *grown = NULL;
2696 if (!map)
2697 return NULL;
2698 isl_assert(map->ctx, n >= 0, goto error);
2699 if (map->n + n <= map->size)
2700 return map;
2701 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2702 if (!grown)
2703 goto error;
2704 for (i = 0; i < map->n; ++i) {
2705 grown->p[i] = isl_basic_map_copy(map->p[i]);
2706 if (!grown->p[i])
2707 goto error;
2708 grown->n++;
2710 isl_map_free(map);
2711 return grown;
2712 error:
2713 isl_map_free(grown);
2714 isl_map_free(map);
2715 return NULL;
2718 /* Make sure "set" has room for at least "n" more basic sets.
2720 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2722 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2725 struct isl_set *isl_set_dup(struct isl_set *set)
2727 int i;
2728 struct isl_set *dup;
2730 if (!set)
2731 return NULL;
2733 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2734 if (!dup)
2735 return NULL;
2736 for (i = 0; i < set->n; ++i)
2737 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2738 return dup;
2741 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2743 return isl_map_from_basic_map(bset);
2746 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2748 struct isl_map *map;
2750 if (!bmap)
2751 return NULL;
2753 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2754 return isl_map_add_basic_map(map, bmap);
2757 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2758 __isl_take isl_basic_set *bset)
2760 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2761 (struct isl_basic_map *)bset);
2764 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2766 return isl_map_free(set);
2769 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2771 int i;
2773 if (!set) {
2774 fprintf(out, "null set\n");
2775 return;
2778 fprintf(out, "%*s", indent, "");
2779 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2780 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2781 set->flags);
2782 for (i = 0; i < set->n; ++i) {
2783 fprintf(out, "%*s", indent, "");
2784 fprintf(out, "basic set %d:\n", i);
2785 isl_basic_set_print_internal(set->p[i], out, indent+4);
2789 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2791 int i;
2793 if (!map) {
2794 fprintf(out, "null map\n");
2795 return;
2798 fprintf(out, "%*s", indent, "");
2799 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2800 "flags: %x, n_name: %d\n",
2801 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2802 map->dim->n_out, map->flags, map->dim->n_id);
2803 for (i = 0; i < map->n; ++i) {
2804 fprintf(out, "%*s", indent, "");
2805 fprintf(out, "basic map %d:\n", i);
2806 isl_basic_map_print_internal(map->p[i], out, indent+4);
2810 struct isl_basic_map *isl_basic_map_intersect_domain(
2811 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2813 struct isl_basic_map *bmap_domain;
2815 if (!bmap || !bset)
2816 goto error;
2818 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2819 bset->dim, isl_dim_param), goto error);
2821 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2822 isl_assert(bset->ctx,
2823 isl_basic_map_compatible_domain(bmap, bset), goto error);
2825 bmap = isl_basic_map_cow(bmap);
2826 if (!bmap)
2827 goto error;
2828 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2829 bset->n_div, bset->n_eq, bset->n_ineq);
2830 bmap_domain = isl_basic_map_from_domain(bset);
2831 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2833 bmap = isl_basic_map_simplify(bmap);
2834 return isl_basic_map_finalize(bmap);
2835 error:
2836 isl_basic_map_free(bmap);
2837 isl_basic_set_free(bset);
2838 return NULL;
2841 struct isl_basic_map *isl_basic_map_intersect_range(
2842 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2844 struct isl_basic_map *bmap_range;
2846 if (!bmap || !bset)
2847 goto error;
2849 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2850 bset->dim, isl_dim_param), goto error);
2852 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2853 isl_assert(bset->ctx,
2854 isl_basic_map_compatible_range(bmap, bset), goto error);
2856 if (isl_basic_set_plain_is_universe(bset)) {
2857 isl_basic_set_free(bset);
2858 return bmap;
2861 bmap = isl_basic_map_cow(bmap);
2862 if (!bmap)
2863 goto error;
2864 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2865 bset->n_div, bset->n_eq, bset->n_ineq);
2866 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2867 bmap = add_constraints(bmap, bmap_range, 0, 0);
2869 bmap = isl_basic_map_simplify(bmap);
2870 return isl_basic_map_finalize(bmap);
2871 error:
2872 isl_basic_map_free(bmap);
2873 isl_basic_set_free(bset);
2874 return NULL;
2877 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2878 __isl_keep isl_vec *vec)
2880 int i;
2881 unsigned total;
2882 isl_int s;
2884 if (!bmap || !vec)
2885 return isl_bool_error;
2887 total = 1 + isl_basic_map_total_dim(bmap);
2888 if (total != vec->size)
2889 return isl_bool_error;
2891 isl_int_init(s);
2893 for (i = 0; i < bmap->n_eq; ++i) {
2894 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2895 if (!isl_int_is_zero(s)) {
2896 isl_int_clear(s);
2897 return isl_bool_false;
2901 for (i = 0; i < bmap->n_ineq; ++i) {
2902 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2903 if (isl_int_is_neg(s)) {
2904 isl_int_clear(s);
2905 return isl_bool_false;
2909 isl_int_clear(s);
2911 return isl_bool_true;
2914 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2915 __isl_keep isl_vec *vec)
2917 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2920 struct isl_basic_map *isl_basic_map_intersect(
2921 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2923 struct isl_vec *sample = NULL;
2925 if (!bmap1 || !bmap2)
2926 goto error;
2928 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2929 bmap2->dim, isl_dim_param), goto error);
2930 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2931 isl_space_dim(bmap1->dim, isl_dim_param) &&
2932 isl_space_dim(bmap2->dim, isl_dim_all) !=
2933 isl_space_dim(bmap2->dim, isl_dim_param))
2934 return isl_basic_map_intersect(bmap2, bmap1);
2936 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2937 isl_space_dim(bmap2->dim, isl_dim_param))
2938 isl_assert(bmap1->ctx,
2939 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2941 if (isl_basic_map_plain_is_empty(bmap1)) {
2942 isl_basic_map_free(bmap2);
2943 return bmap1;
2945 if (isl_basic_map_plain_is_empty(bmap2)) {
2946 isl_basic_map_free(bmap1);
2947 return bmap2;
2950 if (bmap1->sample &&
2951 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2952 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2953 sample = isl_vec_copy(bmap1->sample);
2954 else if (bmap2->sample &&
2955 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2956 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2957 sample = isl_vec_copy(bmap2->sample);
2959 bmap1 = isl_basic_map_cow(bmap1);
2960 if (!bmap1)
2961 goto error;
2962 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2963 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2964 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2966 if (!bmap1)
2967 isl_vec_free(sample);
2968 else if (sample) {
2969 isl_vec_free(bmap1->sample);
2970 bmap1->sample = sample;
2973 bmap1 = isl_basic_map_simplify(bmap1);
2974 return isl_basic_map_finalize(bmap1);
2975 error:
2976 if (sample)
2977 isl_vec_free(sample);
2978 isl_basic_map_free(bmap1);
2979 isl_basic_map_free(bmap2);
2980 return NULL;
2983 struct isl_basic_set *isl_basic_set_intersect(
2984 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2986 return (struct isl_basic_set *)
2987 isl_basic_map_intersect(
2988 (struct isl_basic_map *)bset1,
2989 (struct isl_basic_map *)bset2);
2992 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2993 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2995 return isl_basic_set_intersect(bset1, bset2);
2998 /* Special case of isl_map_intersect, where both map1 and map2
2999 * are convex, without any divs and such that either map1 or map2
3000 * contains a single constraint. This constraint is then simply
3001 * added to the other map.
3003 static __isl_give isl_map *map_intersect_add_constraint(
3004 __isl_take isl_map *map1, __isl_take isl_map *map2)
3006 isl_assert(map1->ctx, map1->n == 1, goto error);
3007 isl_assert(map2->ctx, map1->n == 1, goto error);
3008 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3009 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3011 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3012 return isl_map_intersect(map2, map1);
3014 isl_assert(map2->ctx,
3015 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3017 map1 = isl_map_cow(map1);
3018 if (!map1)
3019 goto error;
3020 if (isl_map_plain_is_empty(map1)) {
3021 isl_map_free(map2);
3022 return map1;
3024 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3025 if (map2->p[0]->n_eq == 1)
3026 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3027 else
3028 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3029 map2->p[0]->ineq[0]);
3031 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3032 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3033 if (!map1->p[0])
3034 goto error;
3036 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3037 isl_basic_map_free(map1->p[0]);
3038 map1->n = 0;
3041 isl_map_free(map2);
3043 return map1;
3044 error:
3045 isl_map_free(map1);
3046 isl_map_free(map2);
3047 return NULL;
3050 /* map2 may be either a parameter domain or a map living in the same
3051 * space as map1.
3053 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3054 __isl_take isl_map *map2)
3056 unsigned flags = 0;
3057 isl_map *result;
3058 int i, j;
3060 if (!map1 || !map2)
3061 goto error;
3063 if ((isl_map_plain_is_empty(map1) ||
3064 isl_map_plain_is_universe(map2)) &&
3065 isl_space_is_equal(map1->dim, map2->dim)) {
3066 isl_map_free(map2);
3067 return map1;
3069 if ((isl_map_plain_is_empty(map2) ||
3070 isl_map_plain_is_universe(map1)) &&
3071 isl_space_is_equal(map1->dim, map2->dim)) {
3072 isl_map_free(map1);
3073 return map2;
3076 if (map1->n == 1 && map2->n == 1 &&
3077 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3078 isl_space_is_equal(map1->dim, map2->dim) &&
3079 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3080 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3081 return map_intersect_add_constraint(map1, map2);
3083 if (isl_space_dim(map2->dim, isl_dim_all) !=
3084 isl_space_dim(map2->dim, isl_dim_param))
3085 isl_assert(map1->ctx,
3086 isl_space_is_equal(map1->dim, map2->dim), goto error);
3088 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3089 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3090 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3092 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3093 map1->n * map2->n, flags);
3094 if (!result)
3095 goto error;
3096 for (i = 0; i < map1->n; ++i)
3097 for (j = 0; j < map2->n; ++j) {
3098 struct isl_basic_map *part;
3099 part = isl_basic_map_intersect(
3100 isl_basic_map_copy(map1->p[i]),
3101 isl_basic_map_copy(map2->p[j]));
3102 if (isl_basic_map_is_empty(part) < 0)
3103 part = isl_basic_map_free(part);
3104 result = isl_map_add_basic_map(result, part);
3105 if (!result)
3106 goto error;
3108 isl_map_free(map1);
3109 isl_map_free(map2);
3110 return result;
3111 error:
3112 isl_map_free(map1);
3113 isl_map_free(map2);
3114 return NULL;
3117 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3118 __isl_take isl_map *map2)
3120 if (!map1 || !map2)
3121 goto error;
3122 if (!isl_space_is_equal(map1->dim, map2->dim))
3123 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3124 "spaces don't match", goto error);
3125 return map_intersect_internal(map1, map2);
3126 error:
3127 isl_map_free(map1);
3128 isl_map_free(map2);
3129 return NULL;
3132 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3133 __isl_take isl_map *map2)
3135 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3138 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3140 return (struct isl_set *)
3141 isl_map_intersect((struct isl_map *)set1,
3142 (struct isl_map *)set2);
3145 /* map_intersect_internal accepts intersections
3146 * with parameter domains, so we can just call that function.
3148 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3149 __isl_take isl_set *params)
3151 return map_intersect_internal(map, params);
3154 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3155 __isl_take isl_map *map2)
3157 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3160 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3161 __isl_take isl_set *params)
3163 return isl_map_intersect_params(set, params);
3166 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3168 isl_space *space;
3169 unsigned pos, n1, n2;
3171 if (!bmap)
3172 return NULL;
3173 bmap = isl_basic_map_cow(bmap);
3174 if (!bmap)
3175 return NULL;
3176 space = isl_space_reverse(isl_space_copy(bmap->dim));
3177 pos = isl_basic_map_offset(bmap, isl_dim_in);
3178 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3179 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3180 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3181 return isl_basic_map_reset_space(bmap, space);
3184 static __isl_give isl_basic_map *basic_map_space_reset(
3185 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3187 isl_space *space;
3189 if (!bmap)
3190 return NULL;
3191 if (!isl_space_is_named_or_nested(bmap->dim, type))
3192 return bmap;
3194 space = isl_basic_map_get_space(bmap);
3195 space = isl_space_reset(space, type);
3196 bmap = isl_basic_map_reset_space(bmap, space);
3197 return bmap;
3200 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3201 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3202 unsigned pos, unsigned n)
3204 isl_space *res_dim;
3205 struct isl_basic_map *res;
3206 struct isl_dim_map *dim_map;
3207 unsigned total, off;
3208 enum isl_dim_type t;
3210 if (n == 0)
3211 return basic_map_space_reset(bmap, type);
3213 if (!bmap)
3214 return NULL;
3216 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3218 total = isl_basic_map_total_dim(bmap) + n;
3219 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3220 off = 0;
3221 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3222 if (t != type) {
3223 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3224 } else {
3225 unsigned size = isl_basic_map_dim(bmap, t);
3226 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3227 0, pos, off);
3228 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3229 pos, size - pos, off + pos + n);
3231 off += isl_space_dim(res_dim, t);
3233 isl_dim_map_div(dim_map, bmap, off);
3235 res = isl_basic_map_alloc_space(res_dim,
3236 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3237 if (isl_basic_map_is_rational(bmap))
3238 res = isl_basic_map_set_rational(res);
3239 if (isl_basic_map_plain_is_empty(bmap)) {
3240 isl_basic_map_free(bmap);
3241 free(dim_map);
3242 return isl_basic_map_set_to_empty(res);
3244 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3245 return isl_basic_map_finalize(res);
3248 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3249 __isl_take isl_basic_set *bset,
3250 enum isl_dim_type type, unsigned pos, unsigned n)
3252 return isl_basic_map_insert_dims(bset, type, pos, n);
3255 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3256 enum isl_dim_type type, unsigned n)
3258 if (!bmap)
3259 return NULL;
3260 return isl_basic_map_insert_dims(bmap, type,
3261 isl_basic_map_dim(bmap, type), n);
3264 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3265 enum isl_dim_type type, unsigned n)
3267 if (!bset)
3268 return NULL;
3269 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3270 return isl_basic_map_add_dims(bset, type, n);
3271 error:
3272 isl_basic_set_free(bset);
3273 return NULL;
3276 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3277 enum isl_dim_type type)
3279 isl_space *space;
3281 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3282 return map;
3284 space = isl_map_get_space(map);
3285 space = isl_space_reset(space, type);
3286 map = isl_map_reset_space(map, space);
3287 return map;
3290 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3291 enum isl_dim_type type, unsigned pos, unsigned n)
3293 int i;
3295 if (n == 0)
3296 return map_space_reset(map, type);
3298 map = isl_map_cow(map);
3299 if (!map)
3300 return NULL;
3302 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3303 if (!map->dim)
3304 goto error;
3306 for (i = 0; i < map->n; ++i) {
3307 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3308 if (!map->p[i])
3309 goto error;
3312 return map;
3313 error:
3314 isl_map_free(map);
3315 return NULL;
3318 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3319 enum isl_dim_type type, unsigned pos, unsigned n)
3321 return isl_map_insert_dims(set, type, pos, n);
3324 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3325 enum isl_dim_type type, unsigned n)
3327 if (!map)
3328 return NULL;
3329 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3332 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3333 enum isl_dim_type type, unsigned n)
3335 if (!set)
3336 return NULL;
3337 isl_assert(set->ctx, type != isl_dim_in, goto error);
3338 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3339 error:
3340 isl_set_free(set);
3341 return NULL;
3344 __isl_give isl_basic_map *isl_basic_map_move_dims(
3345 __isl_take isl_basic_map *bmap,
3346 enum isl_dim_type dst_type, unsigned dst_pos,
3347 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3349 struct isl_dim_map *dim_map;
3350 struct isl_basic_map *res;
3351 enum isl_dim_type t;
3352 unsigned total, off;
3354 if (!bmap)
3355 return NULL;
3356 if (n == 0)
3357 return bmap;
3359 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3360 goto error);
3362 if (dst_type == src_type && dst_pos == src_pos)
3363 return bmap;
3365 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3367 if (pos(bmap->dim, dst_type) + dst_pos ==
3368 pos(bmap->dim, src_type) + src_pos +
3369 ((src_type < dst_type) ? n : 0)) {
3370 bmap = isl_basic_map_cow(bmap);
3371 if (!bmap)
3372 return NULL;
3374 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3375 src_type, src_pos, n);
3376 if (!bmap->dim)
3377 goto error;
3379 bmap = isl_basic_map_finalize(bmap);
3381 return bmap;
3384 total = isl_basic_map_total_dim(bmap);
3385 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3387 off = 0;
3388 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3389 unsigned size = isl_space_dim(bmap->dim, t);
3390 if (t == dst_type) {
3391 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3392 0, dst_pos, off);
3393 off += dst_pos;
3394 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3395 src_pos, n, off);
3396 off += n;
3397 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3398 dst_pos, size - dst_pos, off);
3399 off += size - dst_pos;
3400 } else if (t == src_type) {
3401 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3402 0, src_pos, off);
3403 off += src_pos;
3404 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3405 src_pos + n, size - src_pos - n, off);
3406 off += size - src_pos - n;
3407 } else {
3408 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3409 off += size;
3412 isl_dim_map_div(dim_map, bmap, off);
3414 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3415 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3416 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3417 if (!bmap)
3418 goto error;
3420 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3421 src_type, src_pos, n);
3422 if (!bmap->dim)
3423 goto error;
3425 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3426 bmap = isl_basic_map_gauss(bmap, NULL);
3427 bmap = isl_basic_map_finalize(bmap);
3429 return bmap;
3430 error:
3431 isl_basic_map_free(bmap);
3432 return NULL;
3435 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3436 enum isl_dim_type dst_type, unsigned dst_pos,
3437 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3439 return (isl_basic_set *)isl_basic_map_move_dims(
3440 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3443 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3444 enum isl_dim_type dst_type, unsigned dst_pos,
3445 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3447 if (!set)
3448 return NULL;
3449 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3450 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3451 src_type, src_pos, n);
3452 error:
3453 isl_set_free(set);
3454 return NULL;
3457 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3458 enum isl_dim_type dst_type, unsigned dst_pos,
3459 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3461 int i;
3463 if (!map)
3464 return NULL;
3465 if (n == 0)
3466 return map;
3468 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3469 goto error);
3471 if (dst_type == src_type && dst_pos == src_pos)
3472 return map;
3474 isl_assert(map->ctx, dst_type != src_type, goto error);
3476 map = isl_map_cow(map);
3477 if (!map)
3478 return NULL;
3480 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3481 if (!map->dim)
3482 goto error;
3484 for (i = 0; i < map->n; ++i) {
3485 map->p[i] = isl_basic_map_move_dims(map->p[i],
3486 dst_type, dst_pos,
3487 src_type, src_pos, n);
3488 if (!map->p[i])
3489 goto error;
3492 return map;
3493 error:
3494 isl_map_free(map);
3495 return NULL;
3498 /* Move the specified dimensions to the last columns right before
3499 * the divs. Don't change the dimension specification of bmap.
3500 * That's the responsibility of the caller.
3502 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3503 enum isl_dim_type type, unsigned first, unsigned n)
3505 struct isl_dim_map *dim_map;
3506 struct isl_basic_map *res;
3507 enum isl_dim_type t;
3508 unsigned total, off;
3510 if (!bmap)
3511 return NULL;
3512 if (pos(bmap->dim, type) + first + n ==
3513 1 + isl_space_dim(bmap->dim, isl_dim_all))
3514 return bmap;
3516 total = isl_basic_map_total_dim(bmap);
3517 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3519 off = 0;
3520 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3521 unsigned size = isl_space_dim(bmap->dim, t);
3522 if (t == type) {
3523 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3524 0, first, off);
3525 off += first;
3526 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3527 first, n, total - bmap->n_div - n);
3528 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3529 first + n, size - (first + n), off);
3530 off += size - (first + n);
3531 } else {
3532 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3533 off += size;
3536 isl_dim_map_div(dim_map, bmap, off + n);
3538 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3539 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3540 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3541 return res;
3544 /* Insert "n" rows in the divs of "bmap".
3546 * The number of columns is not changed, which means that the last
3547 * dimensions of "bmap" are being reintepreted as the new divs.
3548 * The space of "bmap" is not adjusted, however, which means
3549 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3550 * from the space of "bmap" is the responsibility of the caller.
3552 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3553 int n)
3555 int i;
3556 size_t row_size;
3557 isl_int **new_div;
3558 isl_int *old;
3560 bmap = isl_basic_map_cow(bmap);
3561 if (!bmap)
3562 return NULL;
3564 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3565 old = bmap->block2.data;
3566 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3567 (bmap->extra + n) * (1 + row_size));
3568 if (!bmap->block2.data)
3569 return isl_basic_map_free(bmap);
3570 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3571 if (!new_div)
3572 return isl_basic_map_free(bmap);
3573 for (i = 0; i < n; ++i) {
3574 new_div[i] = bmap->block2.data +
3575 (bmap->extra + i) * (1 + row_size);
3576 isl_seq_clr(new_div[i], 1 + row_size);
3578 for (i = 0; i < bmap->extra; ++i)
3579 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3580 free(bmap->div);
3581 bmap->div = new_div;
3582 bmap->n_div += n;
3583 bmap->extra += n;
3585 return bmap;
3588 /* Drop constraints from "bmap" that only involve the variables
3589 * of "type" in the range [first, first + n] that are not related
3590 * to any of the variables outside that interval.
3591 * These constraints cannot influence the values for the variables
3592 * outside the interval, except in case they cause "bmap" to be empty.
3593 * Only drop the constraints if "bmap" is known to be non-empty.
3595 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3596 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3597 unsigned first, unsigned n)
3599 int i;
3600 int *groups;
3601 unsigned dim, n_div;
3602 isl_bool non_empty;
3604 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3605 if (non_empty < 0)
3606 return isl_basic_map_free(bmap);
3607 if (!non_empty)
3608 return bmap;
3610 dim = isl_basic_map_dim(bmap, isl_dim_all);
3611 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3612 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3613 if (!groups)
3614 return isl_basic_map_free(bmap);
3615 first += isl_basic_map_offset(bmap, type) - 1;
3616 for (i = 0; i < first; ++i)
3617 groups[i] = -1;
3618 for (i = first + n; i < dim - n_div; ++i)
3619 groups[i] = -1;
3621 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3623 return bmap;
3626 /* Turn the n dimensions of type type, starting at first
3627 * into existentially quantified variables.
3629 * If a subset of the projected out variables are unrelated
3630 * to any of the variables that remain, then the constraints
3631 * involving this subset are simply dropped first.
3633 __isl_give isl_basic_map *isl_basic_map_project_out(
3634 __isl_take isl_basic_map *bmap,
3635 enum isl_dim_type type, unsigned first, unsigned n)
3637 if (n == 0)
3638 return basic_map_space_reset(bmap, type);
3639 if (type == isl_dim_div)
3640 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3641 "cannot project out existentially quantified variables",
3642 return isl_basic_map_free(bmap));
3644 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3645 if (!bmap)
3646 return NULL;
3648 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3649 return isl_basic_map_remove_dims(bmap, type, first, n);
3651 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3652 goto error);
3654 bmap = move_last(bmap, type, first, n);
3655 bmap = isl_basic_map_cow(bmap);
3656 bmap = insert_div_rows(bmap, n);
3657 if (!bmap)
3658 return NULL;
3660 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3661 if (!bmap->dim)
3662 goto error;
3663 bmap = isl_basic_map_simplify(bmap);
3664 bmap = isl_basic_map_drop_redundant_divs(bmap);
3665 return isl_basic_map_finalize(bmap);
3666 error:
3667 isl_basic_map_free(bmap);
3668 return NULL;
3671 /* Turn the n dimensions of type type, starting at first
3672 * into existentially quantified variables.
3674 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3675 enum isl_dim_type type, unsigned first, unsigned n)
3677 return (isl_basic_set *)isl_basic_map_project_out(
3678 (isl_basic_map *)bset, type, first, n);
3681 /* Turn the n dimensions of type type, starting at first
3682 * into existentially quantified variables.
3684 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3685 enum isl_dim_type type, unsigned first, unsigned n)
3687 int i;
3689 if (!map)
3690 return NULL;
3692 if (n == 0)
3693 return map_space_reset(map, type);
3695 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3697 map = isl_map_cow(map);
3698 if (!map)
3699 return NULL;
3701 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3702 if (!map->dim)
3703 goto error;
3705 for (i = 0; i < map->n; ++i) {
3706 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3707 if (!map->p[i])
3708 goto error;
3711 return map;
3712 error:
3713 isl_map_free(map);
3714 return NULL;
3717 /* Turn the n dimensions of type type, starting at first
3718 * into existentially quantified variables.
3720 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3721 enum isl_dim_type type, unsigned first, unsigned n)
3723 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3726 /* Return a map that projects the elements in "set" onto their
3727 * "n" set dimensions starting at "first".
3728 * "type" should be equal to isl_dim_set.
3730 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3731 enum isl_dim_type type, unsigned first, unsigned n)
3733 int i;
3734 int dim;
3735 isl_map *map;
3737 if (!set)
3738 return NULL;
3739 if (type != isl_dim_set)
3740 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3741 "only set dimensions can be projected out", goto error);
3742 dim = isl_set_dim(set, isl_dim_set);
3743 if (first + n > dim || first + n < first)
3744 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3745 "index out of bounds", goto error);
3747 map = isl_map_from_domain(set);
3748 map = isl_map_add_dims(map, isl_dim_out, n);
3749 for (i = 0; i < n; ++i)
3750 map = isl_map_equate(map, isl_dim_in, first + i,
3751 isl_dim_out, i);
3752 return map;
3753 error:
3754 isl_set_free(set);
3755 return NULL;
3758 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3760 int i, j;
3762 for (i = 0; i < n; ++i) {
3763 j = isl_basic_map_alloc_div(bmap);
3764 if (j < 0)
3765 goto error;
3766 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3768 return bmap;
3769 error:
3770 isl_basic_map_free(bmap);
3771 return NULL;
3774 struct isl_basic_map *isl_basic_map_apply_range(
3775 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3777 isl_space *dim_result = NULL;
3778 struct isl_basic_map *bmap;
3779 unsigned n_in, n_out, n, nparam, total, pos;
3780 struct isl_dim_map *dim_map1, *dim_map2;
3782 if (!bmap1 || !bmap2)
3783 goto error;
3784 if (!isl_space_match(bmap1->dim, isl_dim_param,
3785 bmap2->dim, isl_dim_param))
3786 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3787 "parameters don't match", goto error);
3788 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3789 bmap2->dim, isl_dim_in))
3790 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3791 "spaces don't match", goto error);
3793 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3794 isl_space_copy(bmap2->dim));
3796 n_in = isl_basic_map_n_in(bmap1);
3797 n_out = isl_basic_map_n_out(bmap2);
3798 n = isl_basic_map_n_out(bmap1);
3799 nparam = isl_basic_map_n_param(bmap1);
3801 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3802 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3803 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3804 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3805 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3806 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3807 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3808 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3809 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3810 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3811 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3813 bmap = isl_basic_map_alloc_space(dim_result,
3814 bmap1->n_div + bmap2->n_div + n,
3815 bmap1->n_eq + bmap2->n_eq,
3816 bmap1->n_ineq + bmap2->n_ineq);
3817 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3818 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3819 bmap = add_divs(bmap, n);
3820 bmap = isl_basic_map_simplify(bmap);
3821 bmap = isl_basic_map_drop_redundant_divs(bmap);
3822 return isl_basic_map_finalize(bmap);
3823 error:
3824 isl_basic_map_free(bmap1);
3825 isl_basic_map_free(bmap2);
3826 return NULL;
3829 struct isl_basic_set *isl_basic_set_apply(
3830 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3832 if (!bset || !bmap)
3833 goto error;
3835 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3836 goto error);
3838 return (struct isl_basic_set *)
3839 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3840 error:
3841 isl_basic_set_free(bset);
3842 isl_basic_map_free(bmap);
3843 return NULL;
3846 struct isl_basic_map *isl_basic_map_apply_domain(
3847 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3849 if (!bmap1 || !bmap2)
3850 goto error;
3852 isl_assert(bmap1->ctx,
3853 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3854 isl_assert(bmap1->ctx,
3855 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3856 goto error);
3858 bmap1 = isl_basic_map_reverse(bmap1);
3859 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3860 return isl_basic_map_reverse(bmap1);
3861 error:
3862 isl_basic_map_free(bmap1);
3863 isl_basic_map_free(bmap2);
3864 return NULL;
3867 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3868 * A \cap B -> f(A) + f(B)
3870 struct isl_basic_map *isl_basic_map_sum(
3871 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3873 unsigned n_in, n_out, nparam, total, pos;
3874 struct isl_basic_map *bmap = NULL;
3875 struct isl_dim_map *dim_map1, *dim_map2;
3876 int i;
3878 if (!bmap1 || !bmap2)
3879 goto error;
3881 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3882 goto error);
3884 nparam = isl_basic_map_n_param(bmap1);
3885 n_in = isl_basic_map_n_in(bmap1);
3886 n_out = isl_basic_map_n_out(bmap1);
3888 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3889 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3890 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3891 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3892 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3893 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3894 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3895 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3896 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3897 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3898 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3900 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3901 bmap1->n_div + bmap2->n_div + 2 * n_out,
3902 bmap1->n_eq + bmap2->n_eq + n_out,
3903 bmap1->n_ineq + bmap2->n_ineq);
3904 for (i = 0; i < n_out; ++i) {
3905 int j = isl_basic_map_alloc_equality(bmap);
3906 if (j < 0)
3907 goto error;
3908 isl_seq_clr(bmap->eq[j], 1+total);
3909 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3910 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3911 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3913 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3914 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3915 bmap = add_divs(bmap, 2 * n_out);
3917 bmap = isl_basic_map_simplify(bmap);
3918 return isl_basic_map_finalize(bmap);
3919 error:
3920 isl_basic_map_free(bmap);
3921 isl_basic_map_free(bmap1);
3922 isl_basic_map_free(bmap2);
3923 return NULL;
3926 /* Given two maps A -> f(A) and B -> g(B), construct a map
3927 * A \cap B -> f(A) + f(B)
3929 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3931 struct isl_map *result;
3932 int i, j;
3934 if (!map1 || !map2)
3935 goto error;
3937 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3939 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3940 map1->n * map2->n, 0);
3941 if (!result)
3942 goto error;
3943 for (i = 0; i < map1->n; ++i)
3944 for (j = 0; j < map2->n; ++j) {
3945 struct isl_basic_map *part;
3946 part = isl_basic_map_sum(
3947 isl_basic_map_copy(map1->p[i]),
3948 isl_basic_map_copy(map2->p[j]));
3949 if (isl_basic_map_is_empty(part))
3950 isl_basic_map_free(part);
3951 else
3952 result = isl_map_add_basic_map(result, part);
3953 if (!result)
3954 goto error;
3956 isl_map_free(map1);
3957 isl_map_free(map2);
3958 return result;
3959 error:
3960 isl_map_free(map1);
3961 isl_map_free(map2);
3962 return NULL;
3965 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3966 __isl_take isl_set *set2)
3968 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3971 /* Given a basic map A -> f(A), construct A -> -f(A).
3973 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3975 int i, j;
3976 unsigned off, n;
3978 bmap = isl_basic_map_cow(bmap);
3979 if (!bmap)
3980 return NULL;
3982 n = isl_basic_map_dim(bmap, isl_dim_out);
3983 off = isl_basic_map_offset(bmap, isl_dim_out);
3984 for (i = 0; i < bmap->n_eq; ++i)
3985 for (j = 0; j < n; ++j)
3986 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3987 for (i = 0; i < bmap->n_ineq; ++i)
3988 for (j = 0; j < n; ++j)
3989 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3990 for (i = 0; i < bmap->n_div; ++i)
3991 for (j = 0; j < n; ++j)
3992 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3993 bmap = isl_basic_map_gauss(bmap, NULL);
3994 return isl_basic_map_finalize(bmap);
3997 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3999 return isl_basic_map_neg(bset);
4002 /* Given a map A -> f(A), construct A -> -f(A).
4004 struct isl_map *isl_map_neg(struct isl_map *map)
4006 int i;
4008 map = isl_map_cow(map);
4009 if (!map)
4010 return NULL;
4012 for (i = 0; i < map->n; ++i) {
4013 map->p[i] = isl_basic_map_neg(map->p[i]);
4014 if (!map->p[i])
4015 goto error;
4018 return map;
4019 error:
4020 isl_map_free(map);
4021 return NULL;
4024 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4026 return (isl_set *)isl_map_neg((isl_map *)set);
4029 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4030 * A -> floor(f(A)/d).
4032 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4033 isl_int d)
4035 unsigned n_in, n_out, nparam, total, pos;
4036 struct isl_basic_map *result = NULL;
4037 struct isl_dim_map *dim_map;
4038 int i;
4040 if (!bmap)
4041 return NULL;
4043 nparam = isl_basic_map_n_param(bmap);
4044 n_in = isl_basic_map_n_in(bmap);
4045 n_out = isl_basic_map_n_out(bmap);
4047 total = nparam + n_in + n_out + bmap->n_div + n_out;
4048 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4049 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4050 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4051 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4052 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4054 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4055 bmap->n_div + n_out,
4056 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4057 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4058 result = add_divs(result, n_out);
4059 for (i = 0; i < n_out; ++i) {
4060 int j;
4061 j = isl_basic_map_alloc_inequality(result);
4062 if (j < 0)
4063 goto error;
4064 isl_seq_clr(result->ineq[j], 1+total);
4065 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4066 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4067 j = isl_basic_map_alloc_inequality(result);
4068 if (j < 0)
4069 goto error;
4070 isl_seq_clr(result->ineq[j], 1+total);
4071 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4072 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4073 isl_int_sub_ui(result->ineq[j][0], d, 1);
4076 result = isl_basic_map_simplify(result);
4077 return isl_basic_map_finalize(result);
4078 error:
4079 isl_basic_map_free(result);
4080 return NULL;
4083 /* Given a map A -> f(A) and an integer d, construct a map
4084 * A -> floor(f(A)/d).
4086 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4088 int i;
4090 map = isl_map_cow(map);
4091 if (!map)
4092 return NULL;
4094 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4095 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4096 for (i = 0; i < map->n; ++i) {
4097 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4098 if (!map->p[i])
4099 goto error;
4102 return map;
4103 error:
4104 isl_map_free(map);
4105 return NULL;
4108 /* Given a map A -> f(A) and an integer d, construct a map
4109 * A -> floor(f(A)/d).
4111 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4112 __isl_take isl_val *d)
4114 if (!map || !d)
4115 goto error;
4116 if (!isl_val_is_int(d))
4117 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4118 "expecting integer denominator", goto error);
4119 map = isl_map_floordiv(map, d->n);
4120 isl_val_free(d);
4121 return map;
4122 error:
4123 isl_map_free(map);
4124 isl_val_free(d);
4125 return NULL;
4128 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4130 int i;
4131 unsigned nparam;
4132 unsigned n_in;
4134 i = isl_basic_map_alloc_equality(bmap);
4135 if (i < 0)
4136 goto error;
4137 nparam = isl_basic_map_n_param(bmap);
4138 n_in = isl_basic_map_n_in(bmap);
4139 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4140 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4141 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4142 return isl_basic_map_finalize(bmap);
4143 error:
4144 isl_basic_map_free(bmap);
4145 return NULL;
4148 /* Add a constraints to "bmap" expressing i_pos < o_pos
4150 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4152 int i;
4153 unsigned nparam;
4154 unsigned n_in;
4156 i = isl_basic_map_alloc_inequality(bmap);
4157 if (i < 0)
4158 goto error;
4159 nparam = isl_basic_map_n_param(bmap);
4160 n_in = isl_basic_map_n_in(bmap);
4161 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4162 isl_int_set_si(bmap->ineq[i][0], -1);
4163 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4164 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4165 return isl_basic_map_finalize(bmap);
4166 error:
4167 isl_basic_map_free(bmap);
4168 return NULL;
4171 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4173 static __isl_give isl_basic_map *var_less_or_equal(
4174 __isl_take isl_basic_map *bmap, unsigned pos)
4176 int i;
4177 unsigned nparam;
4178 unsigned n_in;
4180 i = isl_basic_map_alloc_inequality(bmap);
4181 if (i < 0)
4182 goto error;
4183 nparam = isl_basic_map_n_param(bmap);
4184 n_in = isl_basic_map_n_in(bmap);
4185 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4186 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4187 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4188 return isl_basic_map_finalize(bmap);
4189 error:
4190 isl_basic_map_free(bmap);
4191 return NULL;
4194 /* Add a constraints to "bmap" expressing i_pos > o_pos
4196 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4198 int i;
4199 unsigned nparam;
4200 unsigned n_in;
4202 i = isl_basic_map_alloc_inequality(bmap);
4203 if (i < 0)
4204 goto error;
4205 nparam = isl_basic_map_n_param(bmap);
4206 n_in = isl_basic_map_n_in(bmap);
4207 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4208 isl_int_set_si(bmap->ineq[i][0], -1);
4209 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4210 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4211 return isl_basic_map_finalize(bmap);
4212 error:
4213 isl_basic_map_free(bmap);
4214 return NULL;
4217 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4219 static __isl_give isl_basic_map *var_more_or_equal(
4220 __isl_take isl_basic_map *bmap, unsigned pos)
4222 int i;
4223 unsigned nparam;
4224 unsigned n_in;
4226 i = isl_basic_map_alloc_inequality(bmap);
4227 if (i < 0)
4228 goto error;
4229 nparam = isl_basic_map_n_param(bmap);
4230 n_in = isl_basic_map_n_in(bmap);
4231 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4232 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4233 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4234 return isl_basic_map_finalize(bmap);
4235 error:
4236 isl_basic_map_free(bmap);
4237 return NULL;
4240 __isl_give isl_basic_map *isl_basic_map_equal(
4241 __isl_take isl_space *dim, unsigned n_equal)
4243 int i;
4244 struct isl_basic_map *bmap;
4245 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4246 if (!bmap)
4247 return NULL;
4248 for (i = 0; i < n_equal && bmap; ++i)
4249 bmap = var_equal(bmap, i);
4250 return isl_basic_map_finalize(bmap);
4253 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4255 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4256 unsigned pos)
4258 int i;
4259 struct isl_basic_map *bmap;
4260 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4261 if (!bmap)
4262 return NULL;
4263 for (i = 0; i < pos && bmap; ++i)
4264 bmap = var_equal(bmap, i);
4265 if (bmap)
4266 bmap = var_less(bmap, pos);
4267 return isl_basic_map_finalize(bmap);
4270 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4272 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4273 __isl_take isl_space *dim, unsigned pos)
4275 int i;
4276 isl_basic_map *bmap;
4278 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4279 for (i = 0; i < pos; ++i)
4280 bmap = var_equal(bmap, i);
4281 bmap = var_less_or_equal(bmap, pos);
4282 return isl_basic_map_finalize(bmap);
4285 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4287 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4288 unsigned pos)
4290 int i;
4291 struct isl_basic_map *bmap;
4292 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4293 if (!bmap)
4294 return NULL;
4295 for (i = 0; i < pos && bmap; ++i)
4296 bmap = var_equal(bmap, i);
4297 if (bmap)
4298 bmap = var_more(bmap, pos);
4299 return isl_basic_map_finalize(bmap);
4302 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4304 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4305 __isl_take isl_space *dim, unsigned pos)
4307 int i;
4308 isl_basic_map *bmap;
4310 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4311 for (i = 0; i < pos; ++i)
4312 bmap = var_equal(bmap, i);
4313 bmap = var_more_or_equal(bmap, pos);
4314 return isl_basic_map_finalize(bmap);
4317 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4318 unsigned n, int equal)
4320 struct isl_map *map;
4321 int i;
4323 if (n == 0 && equal)
4324 return isl_map_universe(dims);
4326 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4328 for (i = 0; i + 1 < n; ++i)
4329 map = isl_map_add_basic_map(map,
4330 isl_basic_map_less_at(isl_space_copy(dims), i));
4331 if (n > 0) {
4332 if (equal)
4333 map = isl_map_add_basic_map(map,
4334 isl_basic_map_less_or_equal_at(dims, n - 1));
4335 else
4336 map = isl_map_add_basic_map(map,
4337 isl_basic_map_less_at(dims, n - 1));
4338 } else
4339 isl_space_free(dims);
4341 return map;
4344 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4346 if (!dims)
4347 return NULL;
4348 return map_lex_lte_first(dims, dims->n_out, equal);
4351 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4353 return map_lex_lte_first(dim, n, 0);
4356 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4358 return map_lex_lte_first(dim, n, 1);
4361 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4363 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4366 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4368 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4371 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4372 unsigned n, int equal)
4374 struct isl_map *map;
4375 int i;
4377 if (n == 0 && equal)
4378 return isl_map_universe(dims);
4380 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4382 for (i = 0; i + 1 < n; ++i)
4383 map = isl_map_add_basic_map(map,
4384 isl_basic_map_more_at(isl_space_copy(dims), i));
4385 if (n > 0) {
4386 if (equal)
4387 map = isl_map_add_basic_map(map,
4388 isl_basic_map_more_or_equal_at(dims, n - 1));
4389 else
4390 map = isl_map_add_basic_map(map,
4391 isl_basic_map_more_at(dims, n - 1));
4392 } else
4393 isl_space_free(dims);
4395 return map;
4398 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4400 if (!dims)
4401 return NULL;
4402 return map_lex_gte_first(dims, dims->n_out, equal);
4405 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4407 return map_lex_gte_first(dim, n, 0);
4410 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4412 return map_lex_gte_first(dim, n, 1);
4415 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4417 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4420 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4422 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4425 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4426 __isl_take isl_set *set2)
4428 isl_map *map;
4429 map = isl_map_lex_le(isl_set_get_space(set1));
4430 map = isl_map_intersect_domain(map, set1);
4431 map = isl_map_intersect_range(map, set2);
4432 return map;
4435 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4436 __isl_take isl_set *set2)
4438 isl_map *map;
4439 map = isl_map_lex_lt(isl_set_get_space(set1));
4440 map = isl_map_intersect_domain(map, set1);
4441 map = isl_map_intersect_range(map, set2);
4442 return map;
4445 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4446 __isl_take isl_set *set2)
4448 isl_map *map;
4449 map = isl_map_lex_ge(isl_set_get_space(set1));
4450 map = isl_map_intersect_domain(map, set1);
4451 map = isl_map_intersect_range(map, set2);
4452 return map;
4455 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4456 __isl_take isl_set *set2)
4458 isl_map *map;
4459 map = isl_map_lex_gt(isl_set_get_space(set1));
4460 map = isl_map_intersect_domain(map, set1);
4461 map = isl_map_intersect_range(map, set2);
4462 return map;
4465 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4466 __isl_take isl_map *map2)
4468 isl_map *map;
4469 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4470 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4471 map = isl_map_apply_range(map, isl_map_reverse(map2));
4472 return map;
4475 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4476 __isl_take isl_map *map2)
4478 isl_map *map;
4479 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4480 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4481 map = isl_map_apply_range(map, isl_map_reverse(map2));
4482 return map;
4485 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4486 __isl_take isl_map *map2)
4488 isl_map *map;
4489 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4490 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4491 map = isl_map_apply_range(map, isl_map_reverse(map2));
4492 return map;
4495 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4496 __isl_take isl_map *map2)
4498 isl_map *map;
4499 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4500 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4501 map = isl_map_apply_range(map, isl_map_reverse(map2));
4502 return map;
4505 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4506 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4508 struct isl_basic_map *bmap;
4510 bset = isl_basic_set_cow(bset);
4511 if (!bset || !dim)
4512 goto error;
4514 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4515 isl_space_free(bset->dim);
4516 bmap = (struct isl_basic_map *) bset;
4517 bmap->dim = dim;
4518 return isl_basic_map_finalize(bmap);
4519 error:
4520 isl_basic_set_free(bset);
4521 isl_space_free(dim);
4522 return NULL;
4525 /* For a div d = floor(f/m), add the constraint
4527 * f - m d >= 0
4529 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4530 unsigned pos, isl_int *div)
4532 int i;
4533 unsigned total = isl_basic_map_total_dim(bmap);
4535 i = isl_basic_map_alloc_inequality(bmap);
4536 if (i < 0)
4537 return -1;
4538 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4539 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4541 return 0;
4544 /* For a div d = floor(f/m), add the constraint
4546 * -(f-(m-1)) + m d >= 0
4548 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4549 unsigned pos, isl_int *div)
4551 int i;
4552 unsigned total = isl_basic_map_total_dim(bmap);
4554 i = isl_basic_map_alloc_inequality(bmap);
4555 if (i < 0)
4556 return -1;
4557 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4558 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4559 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4560 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4562 return 0;
4565 /* For a div d = floor(f/m), add the constraints
4567 * f - m d >= 0
4568 * -(f-(m-1)) + m d >= 0
4570 * Note that the second constraint is the negation of
4572 * f - m d >= m
4574 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4575 unsigned pos, isl_int *div)
4577 if (add_upper_div_constraint(bmap, pos, div) < 0)
4578 return -1;
4579 if (add_lower_div_constraint(bmap, pos, div) < 0)
4580 return -1;
4581 return 0;
4584 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4585 unsigned pos, isl_int *div)
4587 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4588 pos, div);
4591 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4593 unsigned total = isl_basic_map_total_dim(bmap);
4594 unsigned div_pos = total - bmap->n_div + div;
4596 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4597 bmap->div[div]);
4600 /* For each known div d = floor(f/m), add the constraints
4602 * f - m d >= 0
4603 * -(f-(m-1)) + m d >= 0
4605 * Remove duplicate constraints in case of some these div constraints
4606 * already appear in "bmap".
4608 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4609 __isl_take isl_basic_map *bmap)
4611 unsigned n_div;
4613 if (!bmap)
4614 return NULL;
4615 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4616 if (n_div == 0)
4617 return bmap;
4619 bmap = add_known_div_constraints(bmap);
4620 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4621 bmap = isl_basic_map_finalize(bmap);
4622 return bmap;
4625 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4627 * In particular, if this div is of the form d = floor(f/m),
4628 * then add the constraint
4630 * f - m d >= 0
4632 * if sign < 0 or the constraint
4634 * -(f-(m-1)) + m d >= 0
4636 * if sign > 0.
4638 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4639 unsigned div, int sign)
4641 unsigned total;
4642 unsigned div_pos;
4644 if (!bmap)
4645 return -1;
4647 total = isl_basic_map_total_dim(bmap);
4648 div_pos = total - bmap->n_div + div;
4650 if (sign < 0)
4651 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4652 else
4653 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4656 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4658 return isl_basic_map_add_div_constraints(bset, div);
4661 struct isl_basic_set *isl_basic_map_underlying_set(
4662 struct isl_basic_map *bmap)
4664 if (!bmap)
4665 goto error;
4666 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4667 bmap->n_div == 0 &&
4668 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4669 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4670 return (struct isl_basic_set *)bmap;
4671 bmap = isl_basic_map_cow(bmap);
4672 if (!bmap)
4673 goto error;
4674 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4675 if (!bmap->dim)
4676 goto error;
4677 bmap->extra -= bmap->n_div;
4678 bmap->n_div = 0;
4679 bmap = isl_basic_map_finalize(bmap);
4680 return (struct isl_basic_set *)bmap;
4681 error:
4682 isl_basic_map_free(bmap);
4683 return NULL;
4686 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4687 __isl_take isl_basic_set *bset)
4689 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4692 /* Replace each element in "list" by the result of applying
4693 * isl_basic_map_underlying_set to the element.
4695 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4696 __isl_take isl_basic_map_list *list)
4698 int i, n;
4700 if (!list)
4701 return NULL;
4703 n = isl_basic_map_list_n_basic_map(list);
4704 for (i = 0; i < n; ++i) {
4705 isl_basic_map *bmap;
4706 isl_basic_set *bset;
4708 bmap = isl_basic_map_list_get_basic_map(list, i);
4709 bset = isl_basic_set_underlying_set(bmap);
4710 list = isl_basic_set_list_set_basic_set(list, i, bset);
4713 return list;
4716 struct isl_basic_map *isl_basic_map_overlying_set(
4717 struct isl_basic_set *bset, struct isl_basic_map *like)
4719 struct isl_basic_map *bmap;
4720 struct isl_ctx *ctx;
4721 unsigned total;
4722 int i;
4724 if (!bset || !like)
4725 goto error;
4726 ctx = bset->ctx;
4727 isl_assert(ctx, bset->n_div == 0, goto error);
4728 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4729 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4730 goto error);
4731 if (like->n_div == 0) {
4732 isl_space *space = isl_basic_map_get_space(like);
4733 isl_basic_map_free(like);
4734 return isl_basic_map_reset_space(bset, space);
4736 bset = isl_basic_set_cow(bset);
4737 if (!bset)
4738 goto error;
4739 total = bset->dim->n_out + bset->extra;
4740 bmap = (struct isl_basic_map *)bset;
4741 isl_space_free(bmap->dim);
4742 bmap->dim = isl_space_copy(like->dim);
4743 if (!bmap->dim)
4744 goto error;
4745 bmap->n_div = like->n_div;
4746 bmap->extra += like->n_div;
4747 if (bmap->extra) {
4748 unsigned ltotal;
4749 isl_int **div;
4750 ltotal = total - bmap->extra + like->extra;
4751 if (ltotal > total)
4752 ltotal = total;
4753 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4754 bmap->extra * (1 + 1 + total));
4755 if (isl_blk_is_error(bmap->block2))
4756 goto error;
4757 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4758 if (!div)
4759 goto error;
4760 bmap->div = div;
4761 for (i = 0; i < bmap->extra; ++i)
4762 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4763 for (i = 0; i < like->n_div; ++i) {
4764 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4765 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4767 bmap = isl_basic_map_add_known_div_constraints(bmap);
4769 isl_basic_map_free(like);
4770 bmap = isl_basic_map_simplify(bmap);
4771 bmap = isl_basic_map_finalize(bmap);
4772 return bmap;
4773 error:
4774 isl_basic_map_free(like);
4775 isl_basic_set_free(bset);
4776 return NULL;
4779 struct isl_basic_set *isl_basic_set_from_underlying_set(
4780 struct isl_basic_set *bset, struct isl_basic_set *like)
4782 return (struct isl_basic_set *)
4783 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4786 struct isl_set *isl_set_from_underlying_set(
4787 struct isl_set *set, struct isl_basic_set *like)
4789 int i;
4791 if (!set || !like)
4792 goto error;
4793 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4794 goto error);
4795 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4796 isl_basic_set_free(like);
4797 return set;
4799 set = isl_set_cow(set);
4800 if (!set)
4801 goto error;
4802 for (i = 0; i < set->n; ++i) {
4803 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4804 isl_basic_set_copy(like));
4805 if (!set->p[i])
4806 goto error;
4808 isl_space_free(set->dim);
4809 set->dim = isl_space_copy(like->dim);
4810 if (!set->dim)
4811 goto error;
4812 isl_basic_set_free(like);
4813 return set;
4814 error:
4815 isl_basic_set_free(like);
4816 isl_set_free(set);
4817 return NULL;
4820 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4822 int i;
4824 map = isl_map_cow(map);
4825 if (!map)
4826 return NULL;
4827 map->dim = isl_space_cow(map->dim);
4828 if (!map->dim)
4829 goto error;
4831 for (i = 1; i < map->n; ++i)
4832 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4833 goto error);
4834 for (i = 0; i < map->n; ++i) {
4835 map->p[i] = (struct isl_basic_map *)
4836 isl_basic_map_underlying_set(map->p[i]);
4837 if (!map->p[i])
4838 goto error;
4840 if (map->n == 0)
4841 map->dim = isl_space_underlying(map->dim, 0);
4842 else {
4843 isl_space_free(map->dim);
4844 map->dim = isl_space_copy(map->p[0]->dim);
4846 if (!map->dim)
4847 goto error;
4848 return (struct isl_set *)map;
4849 error:
4850 isl_map_free(map);
4851 return NULL;
4854 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4856 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4859 /* Replace the space of "bmap" by "space".
4861 * If the space of "bmap" is identical to "space" (including the identifiers
4862 * of the input and output dimensions), then simply return the original input.
4864 __isl_give isl_basic_map *isl_basic_map_reset_space(
4865 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4867 isl_bool equal;
4869 if (!bmap)
4870 goto error;
4871 equal = isl_space_is_equal(bmap->dim, space);
4872 if (equal >= 0 && equal)
4873 equal = isl_space_match(bmap->dim, isl_dim_in,
4874 space, isl_dim_in);
4875 if (equal >= 0 && equal)
4876 equal = isl_space_match(bmap->dim, isl_dim_out,
4877 space, isl_dim_out);
4878 if (equal < 0)
4879 goto error;
4880 if (equal) {
4881 isl_space_free(space);
4882 return bmap;
4884 bmap = isl_basic_map_cow(bmap);
4885 if (!bmap || !space)
4886 goto error;
4888 isl_space_free(bmap->dim);
4889 bmap->dim = space;
4891 bmap = isl_basic_map_finalize(bmap);
4893 return bmap;
4894 error:
4895 isl_basic_map_free(bmap);
4896 isl_space_free(space);
4897 return NULL;
4900 __isl_give isl_basic_set *isl_basic_set_reset_space(
4901 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4903 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4904 dim);
4907 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4908 __isl_take isl_space *dim)
4910 int i;
4912 map = isl_map_cow(map);
4913 if (!map || !dim)
4914 goto error;
4916 for (i = 0; i < map->n; ++i) {
4917 map->p[i] = isl_basic_map_reset_space(map->p[i],
4918 isl_space_copy(dim));
4919 if (!map->p[i])
4920 goto error;
4922 isl_space_free(map->dim);
4923 map->dim = dim;
4925 return map;
4926 error:
4927 isl_map_free(map);
4928 isl_space_free(dim);
4929 return NULL;
4932 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4933 __isl_take isl_space *dim)
4935 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4938 /* Compute the parameter domain of the given basic set.
4940 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4942 isl_space *space;
4943 unsigned n;
4945 if (isl_basic_set_is_params(bset))
4946 return bset;
4948 n = isl_basic_set_dim(bset, isl_dim_set);
4949 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4950 space = isl_basic_set_get_space(bset);
4951 space = isl_space_params(space);
4952 bset = isl_basic_set_reset_space(bset, space);
4953 return bset;
4956 /* Construct a zero-dimensional basic set with the given parameter domain.
4958 __isl_give isl_basic_set *isl_basic_set_from_params(
4959 __isl_take isl_basic_set *bset)
4961 isl_space *space;
4962 space = isl_basic_set_get_space(bset);
4963 space = isl_space_set_from_params(space);
4964 bset = isl_basic_set_reset_space(bset, space);
4965 return bset;
4968 /* Compute the parameter domain of the given set.
4970 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4972 isl_space *space;
4973 unsigned n;
4975 if (isl_set_is_params(set))
4976 return set;
4978 n = isl_set_dim(set, isl_dim_set);
4979 set = isl_set_project_out(set, isl_dim_set, 0, n);
4980 space = isl_set_get_space(set);
4981 space = isl_space_params(space);
4982 set = isl_set_reset_space(set, space);
4983 return set;
4986 /* Construct a zero-dimensional set with the given parameter domain.
4988 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4990 isl_space *space;
4991 space = isl_set_get_space(set);
4992 space = isl_space_set_from_params(space);
4993 set = isl_set_reset_space(set, space);
4994 return set;
4997 /* Compute the parameter domain of the given map.
4999 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5001 isl_space *space;
5002 unsigned n;
5004 n = isl_map_dim(map, isl_dim_in);
5005 map = isl_map_project_out(map, isl_dim_in, 0, n);
5006 n = isl_map_dim(map, isl_dim_out);
5007 map = isl_map_project_out(map, isl_dim_out, 0, n);
5008 space = isl_map_get_space(map);
5009 space = isl_space_params(space);
5010 map = isl_map_reset_space(map, space);
5011 return map;
5014 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5016 isl_space *space;
5017 unsigned n_out;
5019 if (!bmap)
5020 return NULL;
5021 space = isl_space_domain(isl_basic_map_get_space(bmap));
5023 n_out = isl_basic_map_n_out(bmap);
5024 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5026 return isl_basic_map_reset_space(bmap, space);
5029 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5031 if (!bmap)
5032 return -1;
5033 return isl_space_may_be_set(bmap->dim);
5036 /* Is this basic map actually a set?
5037 * Users should never call this function. Outside of isl,
5038 * the type should indicate whether something is a set or a map.
5040 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5042 if (!bmap)
5043 return -1;
5044 return isl_space_is_set(bmap->dim);
5047 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5049 if (!bmap)
5050 return NULL;
5051 if (isl_basic_map_is_set(bmap))
5052 return bmap;
5053 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5056 __isl_give isl_basic_map *isl_basic_map_domain_map(
5057 __isl_take isl_basic_map *bmap)
5059 int i, k;
5060 isl_space *dim;
5061 isl_basic_map *domain;
5062 int nparam, n_in, n_out;
5063 unsigned total;
5065 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5066 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5067 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5069 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5070 domain = isl_basic_map_universe(dim);
5072 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5073 bmap = isl_basic_map_apply_range(bmap, domain);
5074 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5076 total = isl_basic_map_total_dim(bmap);
5078 for (i = 0; i < n_in; ++i) {
5079 k = isl_basic_map_alloc_equality(bmap);
5080 if (k < 0)
5081 goto error;
5082 isl_seq_clr(bmap->eq[k], 1 + total);
5083 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5084 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5087 bmap = isl_basic_map_gauss(bmap, NULL);
5088 return isl_basic_map_finalize(bmap);
5089 error:
5090 isl_basic_map_free(bmap);
5091 return NULL;
5094 __isl_give isl_basic_map *isl_basic_map_range_map(
5095 __isl_take isl_basic_map *bmap)
5097 int i, k;
5098 isl_space *dim;
5099 isl_basic_map *range;
5100 int nparam, n_in, n_out;
5101 unsigned total;
5103 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5104 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5105 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5107 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5108 range = isl_basic_map_universe(dim);
5110 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5111 bmap = isl_basic_map_apply_range(bmap, range);
5112 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5114 total = isl_basic_map_total_dim(bmap);
5116 for (i = 0; i < n_out; ++i) {
5117 k = isl_basic_map_alloc_equality(bmap);
5118 if (k < 0)
5119 goto error;
5120 isl_seq_clr(bmap->eq[k], 1 + total);
5121 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5122 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5125 bmap = isl_basic_map_gauss(bmap, NULL);
5126 return isl_basic_map_finalize(bmap);
5127 error:
5128 isl_basic_map_free(bmap);
5129 return NULL;
5132 int isl_map_may_be_set(__isl_keep isl_map *map)
5134 if (!map)
5135 return -1;
5136 return isl_space_may_be_set(map->dim);
5139 /* Is this map actually a set?
5140 * Users should never call this function. Outside of isl,
5141 * the type should indicate whether something is a set or a map.
5143 int isl_map_is_set(__isl_keep isl_map *map)
5145 if (!map)
5146 return -1;
5147 return isl_space_is_set(map->dim);
5150 struct isl_set *isl_map_range(struct isl_map *map)
5152 int i;
5153 struct isl_set *set;
5155 if (!map)
5156 goto error;
5157 if (isl_map_is_set(map))
5158 return (isl_set *)map;
5160 map = isl_map_cow(map);
5161 if (!map)
5162 goto error;
5164 set = (struct isl_set *) map;
5165 set->dim = isl_space_range(set->dim);
5166 if (!set->dim)
5167 goto error;
5168 for (i = 0; i < map->n; ++i) {
5169 set->p[i] = isl_basic_map_range(map->p[i]);
5170 if (!set->p[i])
5171 goto error;
5173 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5174 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5175 return set;
5176 error:
5177 isl_map_free(map);
5178 return NULL;
5181 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5183 int i;
5185 map = isl_map_cow(map);
5186 if (!map)
5187 return NULL;
5189 map->dim = isl_space_domain_map(map->dim);
5190 if (!map->dim)
5191 goto error;
5192 for (i = 0; i < map->n; ++i) {
5193 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5194 if (!map->p[i])
5195 goto error;
5197 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5198 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5199 return map;
5200 error:
5201 isl_map_free(map);
5202 return NULL;
5205 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5207 int i;
5208 isl_space *range_dim;
5210 map = isl_map_cow(map);
5211 if (!map)
5212 return NULL;
5214 range_dim = isl_space_range(isl_map_get_space(map));
5215 range_dim = isl_space_from_range(range_dim);
5216 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5217 map->dim = isl_space_join(map->dim, range_dim);
5218 if (!map->dim)
5219 goto error;
5220 for (i = 0; i < map->n; ++i) {
5221 map->p[i] = isl_basic_map_range_map(map->p[i]);
5222 if (!map->p[i])
5223 goto error;
5225 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5226 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5227 return map;
5228 error:
5229 isl_map_free(map);
5230 return NULL;
5233 /* Given a wrapped map of the form A[B -> C],
5234 * return the map A[B -> C] -> B.
5236 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5238 isl_id *id;
5239 isl_map *map;
5241 if (!set)
5242 return NULL;
5243 if (!isl_set_has_tuple_id(set))
5244 return isl_map_domain_map(isl_set_unwrap(set));
5246 id = isl_set_get_tuple_id(set);
5247 map = isl_map_domain_map(isl_set_unwrap(set));
5248 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5250 return map;
5253 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5254 __isl_take isl_space *dim)
5256 int i;
5257 struct isl_map *map = NULL;
5259 set = isl_set_cow(set);
5260 if (!set || !dim)
5261 goto error;
5262 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5263 map = (struct isl_map *)set;
5264 for (i = 0; i < set->n; ++i) {
5265 map->p[i] = isl_basic_map_from_basic_set(
5266 set->p[i], isl_space_copy(dim));
5267 if (!map->p[i])
5268 goto error;
5270 isl_space_free(map->dim);
5271 map->dim = dim;
5272 return map;
5273 error:
5274 isl_space_free(dim);
5275 isl_set_free(set);
5276 return NULL;
5279 __isl_give isl_basic_map *isl_basic_map_from_domain(
5280 __isl_take isl_basic_set *bset)
5282 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5285 __isl_give isl_basic_map *isl_basic_map_from_range(
5286 __isl_take isl_basic_set *bset)
5288 isl_space *space;
5289 space = isl_basic_set_get_space(bset);
5290 space = isl_space_from_range(space);
5291 bset = isl_basic_set_reset_space(bset, space);
5292 return (isl_basic_map *)bset;
5295 /* Create a relation with the given set as range.
5296 * The domain of the created relation is a zero-dimensional
5297 * flat anonymous space.
5299 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5301 isl_space *space;
5302 space = isl_set_get_space(set);
5303 space = isl_space_from_range(space);
5304 set = isl_set_reset_space(set, space);
5305 return (struct isl_map *)set;
5308 /* Create a relation with the given set as domain.
5309 * The range of the created relation is a zero-dimensional
5310 * flat anonymous space.
5312 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5314 return isl_map_reverse(isl_map_from_range(set));
5317 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5318 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5320 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5323 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5324 __isl_take isl_set *range)
5326 return isl_map_apply_range(isl_map_reverse(domain), range);
5329 /* Return a newly allocated isl_map with given space and flags and
5330 * room for "n" basic maps.
5331 * Make sure that all cached information is cleared.
5333 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5334 unsigned flags)
5336 struct isl_map *map;
5338 if (!space)
5339 return NULL;
5340 if (n < 0)
5341 isl_die(space->ctx, isl_error_internal,
5342 "negative number of basic maps", goto error);
5343 map = isl_calloc(space->ctx, struct isl_map,
5344 sizeof(struct isl_map) +
5345 (n - 1) * sizeof(struct isl_basic_map *));
5346 if (!map)
5347 goto error;
5349 map->ctx = space->ctx;
5350 isl_ctx_ref(map->ctx);
5351 map->ref = 1;
5352 map->size = n;
5353 map->n = 0;
5354 map->dim = space;
5355 map->flags = flags;
5356 return map;
5357 error:
5358 isl_space_free(space);
5359 return NULL;
5362 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5363 unsigned nparam, unsigned in, unsigned out, int n,
5364 unsigned flags)
5366 struct isl_map *map;
5367 isl_space *dims;
5369 dims = isl_space_alloc(ctx, nparam, in, out);
5370 if (!dims)
5371 return NULL;
5373 map = isl_map_alloc_space(dims, n, flags);
5374 return map;
5377 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5379 struct isl_basic_map *bmap;
5380 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5381 bmap = isl_basic_map_set_to_empty(bmap);
5382 return bmap;
5385 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5387 struct isl_basic_set *bset;
5388 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5389 bset = isl_basic_set_set_to_empty(bset);
5390 return bset;
5393 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5395 struct isl_basic_map *bmap;
5396 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5397 bmap = isl_basic_map_finalize(bmap);
5398 return bmap;
5401 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5403 struct isl_basic_set *bset;
5404 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5405 bset = isl_basic_set_finalize(bset);
5406 return bset;
5409 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5411 int i;
5412 unsigned total = isl_space_dim(dim, isl_dim_all);
5413 isl_basic_map *bmap;
5415 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5416 for (i = 0; i < total; ++i) {
5417 int k = isl_basic_map_alloc_inequality(bmap);
5418 if (k < 0)
5419 goto error;
5420 isl_seq_clr(bmap->ineq[k], 1 + total);
5421 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5423 return bmap;
5424 error:
5425 isl_basic_map_free(bmap);
5426 return NULL;
5429 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5431 return isl_basic_map_nat_universe(dim);
5434 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5436 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5439 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5441 return isl_map_nat_universe(dim);
5444 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5446 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5449 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5451 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5454 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5456 struct isl_map *map;
5457 if (!dim)
5458 return NULL;
5459 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5460 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5461 return map;
5464 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5466 struct isl_set *set;
5467 if (!dim)
5468 return NULL;
5469 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5470 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5471 return set;
5474 struct isl_map *isl_map_dup(struct isl_map *map)
5476 int i;
5477 struct isl_map *dup;
5479 if (!map)
5480 return NULL;
5481 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5482 for (i = 0; i < map->n; ++i)
5483 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5484 return dup;
5487 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5488 __isl_take isl_basic_map *bmap)
5490 if (!bmap || !map)
5491 goto error;
5492 if (isl_basic_map_plain_is_empty(bmap)) {
5493 isl_basic_map_free(bmap);
5494 return map;
5496 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5497 isl_assert(map->ctx, map->n < map->size, goto error);
5498 map->p[map->n] = bmap;
5499 map->n++;
5500 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5501 return map;
5502 error:
5503 if (map)
5504 isl_map_free(map);
5505 if (bmap)
5506 isl_basic_map_free(bmap);
5507 return NULL;
5510 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5512 int i;
5514 if (!map)
5515 return NULL;
5517 if (--map->ref > 0)
5518 return NULL;
5520 clear_caches(map);
5521 isl_ctx_deref(map->ctx);
5522 for (i = 0; i < map->n; ++i)
5523 isl_basic_map_free(map->p[i]);
5524 isl_space_free(map->dim);
5525 free(map);
5527 return NULL;
5530 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5531 struct isl_basic_map *bmap, unsigned pos, int value)
5533 int j;
5535 bmap = isl_basic_map_cow(bmap);
5536 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5537 j = isl_basic_map_alloc_equality(bmap);
5538 if (j < 0)
5539 goto error;
5540 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5541 isl_int_set_si(bmap->eq[j][pos], -1);
5542 isl_int_set_si(bmap->eq[j][0], value);
5543 bmap = isl_basic_map_simplify(bmap);
5544 return isl_basic_map_finalize(bmap);
5545 error:
5546 isl_basic_map_free(bmap);
5547 return NULL;
5550 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5551 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5553 int j;
5555 bmap = isl_basic_map_cow(bmap);
5556 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5557 j = isl_basic_map_alloc_equality(bmap);
5558 if (j < 0)
5559 goto error;
5560 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5561 isl_int_set_si(bmap->eq[j][pos], -1);
5562 isl_int_set(bmap->eq[j][0], value);
5563 bmap = isl_basic_map_simplify(bmap);
5564 return isl_basic_map_finalize(bmap);
5565 error:
5566 isl_basic_map_free(bmap);
5567 return NULL;
5570 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5571 enum isl_dim_type type, unsigned pos, int value)
5573 if (!bmap)
5574 return NULL;
5575 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5576 return isl_basic_map_fix_pos_si(bmap,
5577 isl_basic_map_offset(bmap, type) + pos, value);
5578 error:
5579 isl_basic_map_free(bmap);
5580 return NULL;
5583 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5584 enum isl_dim_type type, unsigned pos, isl_int value)
5586 if (!bmap)
5587 return NULL;
5588 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5589 return isl_basic_map_fix_pos(bmap,
5590 isl_basic_map_offset(bmap, type) + pos, value);
5591 error:
5592 isl_basic_map_free(bmap);
5593 return NULL;
5596 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5597 * to be equal to "v".
5599 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5600 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5602 if (!bmap || !v)
5603 goto error;
5604 if (!isl_val_is_int(v))
5605 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5606 "expecting integer value", goto error);
5607 if (pos >= isl_basic_map_dim(bmap, type))
5608 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5609 "index out of bounds", goto error);
5610 pos += isl_basic_map_offset(bmap, type);
5611 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5612 isl_val_free(v);
5613 return bmap;
5614 error:
5615 isl_basic_map_free(bmap);
5616 isl_val_free(v);
5617 return NULL;
5620 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5621 * to be equal to "v".
5623 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5624 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5626 return isl_basic_map_fix_val(bset, type, pos, v);
5629 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5630 enum isl_dim_type type, unsigned pos, int value)
5632 return (struct isl_basic_set *)
5633 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5634 type, pos, value);
5637 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5638 enum isl_dim_type type, unsigned pos, isl_int value)
5640 return (struct isl_basic_set *)
5641 isl_basic_map_fix((struct isl_basic_map *)bset,
5642 type, pos, value);
5645 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5646 unsigned input, int value)
5648 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5651 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5652 unsigned dim, int value)
5654 return (struct isl_basic_set *)
5655 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5656 isl_dim_set, dim, value);
5659 static int remove_if_empty(__isl_keep isl_map *map, int i)
5661 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5663 if (empty < 0)
5664 return -1;
5665 if (!empty)
5666 return 0;
5668 isl_basic_map_free(map->p[i]);
5669 if (i != map->n - 1) {
5670 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5671 map->p[i] = map->p[map->n - 1];
5673 map->n--;
5675 return 0;
5678 /* Perform "fn" on each basic map of "map", where we may not be holding
5679 * the only reference to "map".
5680 * In particular, "fn" should be a semantics preserving operation
5681 * that we want to apply to all copies of "map". We therefore need
5682 * to be careful not to modify "map" in a way that breaks "map"
5683 * in case anything goes wrong.
5685 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5686 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5688 struct isl_basic_map *bmap;
5689 int i;
5691 if (!map)
5692 return NULL;
5694 for (i = map->n - 1; i >= 0; --i) {
5695 bmap = isl_basic_map_copy(map->p[i]);
5696 bmap = fn(bmap);
5697 if (!bmap)
5698 goto error;
5699 isl_basic_map_free(map->p[i]);
5700 map->p[i] = bmap;
5701 if (remove_if_empty(map, i) < 0)
5702 goto error;
5705 return map;
5706 error:
5707 isl_map_free(map);
5708 return NULL;
5711 struct isl_map *isl_map_fix_si(struct isl_map *map,
5712 enum isl_dim_type type, unsigned pos, int value)
5714 int i;
5716 map = isl_map_cow(map);
5717 if (!map)
5718 return NULL;
5720 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5721 for (i = map->n - 1; i >= 0; --i) {
5722 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5723 if (remove_if_empty(map, i) < 0)
5724 goto error;
5726 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5727 return map;
5728 error:
5729 isl_map_free(map);
5730 return NULL;
5733 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5734 enum isl_dim_type type, unsigned pos, int value)
5736 return (struct isl_set *)
5737 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5740 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5741 enum isl_dim_type type, unsigned pos, isl_int value)
5743 int i;
5745 map = isl_map_cow(map);
5746 if (!map)
5747 return NULL;
5749 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5750 for (i = 0; i < map->n; ++i) {
5751 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5752 if (!map->p[i])
5753 goto error;
5755 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5756 return map;
5757 error:
5758 isl_map_free(map);
5759 return NULL;
5762 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5763 enum isl_dim_type type, unsigned pos, isl_int value)
5765 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5768 /* Fix the value of the variable at position "pos" of type "type" of "map"
5769 * to be equal to "v".
5771 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5772 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5774 int i;
5776 map = isl_map_cow(map);
5777 if (!map || !v)
5778 goto error;
5780 if (!isl_val_is_int(v))
5781 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5782 "expecting integer value", goto error);
5783 if (pos >= isl_map_dim(map, type))
5784 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5785 "index out of bounds", goto error);
5786 for (i = map->n - 1; i >= 0; --i) {
5787 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5788 isl_val_copy(v));
5789 if (remove_if_empty(map, i) < 0)
5790 goto error;
5792 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5793 isl_val_free(v);
5794 return map;
5795 error:
5796 isl_map_free(map);
5797 isl_val_free(v);
5798 return NULL;
5801 /* Fix the value of the variable at position "pos" of type "type" of "set"
5802 * to be equal to "v".
5804 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5805 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5807 return isl_map_fix_val(set, type, pos, v);
5810 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5811 unsigned input, int value)
5813 return isl_map_fix_si(map, isl_dim_in, input, value);
5816 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5818 return (struct isl_set *)
5819 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5822 static __isl_give isl_basic_map *basic_map_bound_si(
5823 __isl_take isl_basic_map *bmap,
5824 enum isl_dim_type type, unsigned pos, int value, int upper)
5826 int j;
5828 if (!bmap)
5829 return NULL;
5830 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5831 pos += isl_basic_map_offset(bmap, type);
5832 bmap = isl_basic_map_cow(bmap);
5833 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5834 j = isl_basic_map_alloc_inequality(bmap);
5835 if (j < 0)
5836 goto error;
5837 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5838 if (upper) {
5839 isl_int_set_si(bmap->ineq[j][pos], -1);
5840 isl_int_set_si(bmap->ineq[j][0], value);
5841 } else {
5842 isl_int_set_si(bmap->ineq[j][pos], 1);
5843 isl_int_set_si(bmap->ineq[j][0], -value);
5845 bmap = isl_basic_map_simplify(bmap);
5846 return isl_basic_map_finalize(bmap);
5847 error:
5848 isl_basic_map_free(bmap);
5849 return NULL;
5852 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5853 __isl_take isl_basic_map *bmap,
5854 enum isl_dim_type type, unsigned pos, int value)
5856 return basic_map_bound_si(bmap, type, pos, value, 0);
5859 /* Constrain the values of the given dimension to be no greater than "value".
5861 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5862 __isl_take isl_basic_map *bmap,
5863 enum isl_dim_type type, unsigned pos, int value)
5865 return basic_map_bound_si(bmap, type, pos, value, 1);
5868 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5869 unsigned dim, isl_int value)
5871 int j;
5873 bset = isl_basic_set_cow(bset);
5874 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5875 j = isl_basic_set_alloc_inequality(bset);
5876 if (j < 0)
5877 goto error;
5878 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5879 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5880 isl_int_neg(bset->ineq[j][0], value);
5881 bset = isl_basic_set_simplify(bset);
5882 return isl_basic_set_finalize(bset);
5883 error:
5884 isl_basic_set_free(bset);
5885 return NULL;
5888 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5889 enum isl_dim_type type, unsigned pos, int value, int upper)
5891 int i;
5893 map = isl_map_cow(map);
5894 if (!map)
5895 return NULL;
5897 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5898 for (i = 0; i < map->n; ++i) {
5899 map->p[i] = basic_map_bound_si(map->p[i],
5900 type, pos, value, upper);
5901 if (!map->p[i])
5902 goto error;
5904 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5905 return map;
5906 error:
5907 isl_map_free(map);
5908 return NULL;
5911 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5912 enum isl_dim_type type, unsigned pos, int value)
5914 return map_bound_si(map, type, pos, value, 0);
5917 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5918 enum isl_dim_type type, unsigned pos, int value)
5920 return map_bound_si(map, type, pos, value, 1);
5923 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5924 enum isl_dim_type type, unsigned pos, int value)
5926 return (struct isl_set *)
5927 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5930 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5931 enum isl_dim_type type, unsigned pos, int value)
5933 return isl_map_upper_bound_si(set, type, pos, value);
5936 /* Bound the given variable of "bmap" from below (or above is "upper"
5937 * is set) to "value".
5939 static __isl_give isl_basic_map *basic_map_bound(
5940 __isl_take isl_basic_map *bmap,
5941 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5943 int j;
5945 if (!bmap)
5946 return NULL;
5947 if (pos >= isl_basic_map_dim(bmap, type))
5948 isl_die(bmap->ctx, isl_error_invalid,
5949 "index out of bounds", goto error);
5950 pos += isl_basic_map_offset(bmap, type);
5951 bmap = isl_basic_map_cow(bmap);
5952 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5953 j = isl_basic_map_alloc_inequality(bmap);
5954 if (j < 0)
5955 goto error;
5956 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5957 if (upper) {
5958 isl_int_set_si(bmap->ineq[j][pos], -1);
5959 isl_int_set(bmap->ineq[j][0], value);
5960 } else {
5961 isl_int_set_si(bmap->ineq[j][pos], 1);
5962 isl_int_neg(bmap->ineq[j][0], value);
5964 bmap = isl_basic_map_simplify(bmap);
5965 return isl_basic_map_finalize(bmap);
5966 error:
5967 isl_basic_map_free(bmap);
5968 return NULL;
5971 /* Bound the given variable of "map" from below (or above is "upper"
5972 * is set) to "value".
5974 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5975 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5977 int i;
5979 map = isl_map_cow(map);
5980 if (!map)
5981 return NULL;
5983 if (pos >= isl_map_dim(map, type))
5984 isl_die(map->ctx, isl_error_invalid,
5985 "index out of bounds", goto error);
5986 for (i = map->n - 1; i >= 0; --i) {
5987 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5988 if (remove_if_empty(map, i) < 0)
5989 goto error;
5991 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5992 return map;
5993 error:
5994 isl_map_free(map);
5995 return NULL;
5998 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5999 enum isl_dim_type type, unsigned pos, isl_int value)
6001 return map_bound(map, type, pos, value, 0);
6004 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6005 enum isl_dim_type type, unsigned pos, isl_int value)
6007 return map_bound(map, type, pos, value, 1);
6010 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6011 enum isl_dim_type type, unsigned pos, isl_int value)
6013 return isl_map_lower_bound(set, type, pos, value);
6016 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6017 enum isl_dim_type type, unsigned pos, isl_int value)
6019 return isl_map_upper_bound(set, type, pos, value);
6022 /* Force the values of the variable at position "pos" of type "type" of "set"
6023 * to be no smaller than "value".
6025 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6026 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6028 if (!value)
6029 goto error;
6030 if (!isl_val_is_int(value))
6031 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6032 "expecting integer value", goto error);
6033 set = isl_set_lower_bound(set, type, pos, value->n);
6034 isl_val_free(value);
6035 return set;
6036 error:
6037 isl_val_free(value);
6038 isl_set_free(set);
6039 return NULL;
6042 /* Force the values of the variable at position "pos" of type "type" of "set"
6043 * to be no greater than "value".
6045 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6046 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6048 if (!value)
6049 goto error;
6050 if (!isl_val_is_int(value))
6051 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6052 "expecting integer value", goto error);
6053 set = isl_set_upper_bound(set, type, pos, value->n);
6054 isl_val_free(value);
6055 return set;
6056 error:
6057 isl_val_free(value);
6058 isl_set_free(set);
6059 return NULL;
6062 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6063 isl_int value)
6065 int i;
6067 set = isl_set_cow(set);
6068 if (!set)
6069 return NULL;
6071 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6072 for (i = 0; i < set->n; ++i) {
6073 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6074 if (!set->p[i])
6075 goto error;
6077 return set;
6078 error:
6079 isl_set_free(set);
6080 return NULL;
6083 struct isl_map *isl_map_reverse(struct isl_map *map)
6085 int i;
6087 map = isl_map_cow(map);
6088 if (!map)
6089 return NULL;
6091 map->dim = isl_space_reverse(map->dim);
6092 if (!map->dim)
6093 goto error;
6094 for (i = 0; i < map->n; ++i) {
6095 map->p[i] = isl_basic_map_reverse(map->p[i]);
6096 if (!map->p[i])
6097 goto error;
6099 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6100 return map;
6101 error:
6102 isl_map_free(map);
6103 return NULL;
6106 #undef TYPE
6107 #define TYPE isl_pw_multi_aff
6108 #undef SUFFIX
6109 #define SUFFIX _pw_multi_aff
6110 #undef EMPTY
6111 #define EMPTY isl_pw_multi_aff_empty
6112 #undef ADD
6113 #define ADD isl_pw_multi_aff_union_add
6114 #include "isl_map_lexopt_templ.c"
6116 /* Given a map "map", compute the lexicographically minimal
6117 * (or maximal) image element for each domain element in dom,
6118 * in the form of an isl_pw_multi_aff.
6119 * If "empty" is not NULL, then set *empty to those elements in dom that
6120 * do not have an image element.
6121 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6122 * should be computed over the domain of "map". "empty" is also NULL
6123 * in this case.
6125 * We first compute the lexicographically minimal or maximal element
6126 * in the first basic map. This results in a partial solution "res"
6127 * and a subset "todo" of dom that still need to be handled.
6128 * We then consider each of the remaining maps in "map" and successively
6129 * update both "res" and "todo".
6130 * If "empty" is NULL, then the todo sets are not needed and therefore
6131 * also not computed.
6133 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6134 __isl_take isl_map *map, __isl_take isl_set *dom,
6135 __isl_give isl_set **empty, unsigned flags)
6137 int i;
6138 int full;
6139 isl_pw_multi_aff *res;
6140 isl_set *todo;
6142 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6143 if (!map || (!full && !dom))
6144 goto error;
6146 if (isl_map_plain_is_empty(map)) {
6147 if (empty)
6148 *empty = dom;
6149 else
6150 isl_set_free(dom);
6151 return isl_pw_multi_aff_from_map(map);
6154 res = basic_map_partial_lexopt_pw_multi_aff(
6155 isl_basic_map_copy(map->p[0]),
6156 isl_set_copy(dom), empty, flags);
6158 if (empty)
6159 todo = *empty;
6160 for (i = 1; i < map->n; ++i) {
6161 isl_pw_multi_aff *res_i;
6163 res_i = basic_map_partial_lexopt_pw_multi_aff(
6164 isl_basic_map_copy(map->p[i]),
6165 isl_set_copy(dom), empty, flags);
6167 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6168 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6169 else
6170 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6172 if (empty)
6173 todo = isl_set_intersect(todo, *empty);
6176 isl_set_free(dom);
6177 isl_map_free(map);
6179 if (empty)
6180 *empty = todo;
6182 return res;
6183 error:
6184 if (empty)
6185 *empty = NULL;
6186 isl_set_free(dom);
6187 isl_map_free(map);
6188 return NULL;
6191 #undef TYPE
6192 #define TYPE isl_map
6193 #undef SUFFIX
6194 #define SUFFIX
6195 #undef EMPTY
6196 #define EMPTY isl_map_empty
6197 #undef ADD
6198 #define ADD isl_map_union_disjoint
6199 #include "isl_map_lexopt_templ.c"
6201 /* Given a map "map", compute the lexicographically minimal
6202 * (or maximal) image element for each domain element in "dom",
6203 * in the form of an isl_map.
6204 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6205 * do not have an image element.
6206 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6207 * should be computed over the domain of "map". "empty" is also NULL
6208 * in this case.
6210 * If the input consists of more than one disjunct, then first
6211 * compute the desired result in the form of an isl_pw_multi_aff and
6212 * then convert that into an isl_map.
6214 * This function used to have an explicit implementation in terms
6215 * of isl_maps, but it would continually intersect the domains of
6216 * partial results with the complement of the domain of the next
6217 * partial solution, potentially leading to an explosion in the number
6218 * of disjuncts if there are several disjuncts in the input.
6219 * An even earlier implementation of this function would look for
6220 * better results in the domain of the partial result and for extra
6221 * results in the complement of this domain, which would lead to
6222 * even more splintering.
6224 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6225 __isl_take isl_map *map, __isl_take isl_set *dom,
6226 __isl_give isl_set **empty, unsigned flags)
6228 int full;
6229 struct isl_map *res;
6230 isl_pw_multi_aff *pma;
6232 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6233 if (!map || (!full && !dom))
6234 goto error;
6236 if (isl_map_plain_is_empty(map)) {
6237 if (empty)
6238 *empty = dom;
6239 else
6240 isl_set_free(dom);
6241 return map;
6244 if (map->n == 1) {
6245 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6246 dom, empty, flags);
6247 isl_map_free(map);
6248 return res;
6251 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6252 flags);
6253 return isl_map_from_pw_multi_aff(pma);
6254 error:
6255 if (empty)
6256 *empty = NULL;
6257 isl_set_free(dom);
6258 isl_map_free(map);
6259 return NULL;
6262 __isl_give isl_map *isl_map_partial_lexmax(
6263 __isl_take isl_map *map, __isl_take isl_set *dom,
6264 __isl_give isl_set **empty)
6266 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6269 __isl_give isl_map *isl_map_partial_lexmin(
6270 __isl_take isl_map *map, __isl_take isl_set *dom,
6271 __isl_give isl_set **empty)
6273 return isl_map_partial_lexopt(map, dom, empty, 0);
6276 __isl_give isl_set *isl_set_partial_lexmin(
6277 __isl_take isl_set *set, __isl_take isl_set *dom,
6278 __isl_give isl_set **empty)
6280 return (struct isl_set *)
6281 isl_map_partial_lexmin((struct isl_map *)set,
6282 dom, empty);
6285 __isl_give isl_set *isl_set_partial_lexmax(
6286 __isl_take isl_set *set, __isl_take isl_set *dom,
6287 __isl_give isl_set **empty)
6289 return (struct isl_set *)
6290 isl_map_partial_lexmax((struct isl_map *)set,
6291 dom, empty);
6294 /* Compute the lexicographic minimum (or maximum if "flags" includes
6295 * ISL_OPT_MAX) of "bset" over its parametric domain.
6297 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6298 unsigned flags)
6300 return isl_basic_map_lexopt(bset, flags);
6303 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6305 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6308 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6310 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6313 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6315 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6318 /* Compute the lexicographic minimum of "bset" over its parametric domain
6319 * for the purpose of quantifier elimination.
6320 * That is, find an explicit representation for all the existentially
6321 * quantified variables in "bset" by computing their lexicographic
6322 * minimum.
6324 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6325 __isl_take isl_basic_set *bset)
6327 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6330 /* Extract the first and only affine expression from list
6331 * and then add it to *pwaff with the given dom.
6332 * This domain is known to be disjoint from other domains
6333 * because of the way isl_basic_map_foreach_lexmax works.
6335 static int update_dim_opt(__isl_take isl_basic_set *dom,
6336 __isl_take isl_aff_list *list, void *user)
6338 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6339 isl_aff *aff;
6340 isl_pw_aff **pwaff = user;
6341 isl_pw_aff *pwaff_i;
6343 if (!list)
6344 goto error;
6345 if (isl_aff_list_n_aff(list) != 1)
6346 isl_die(ctx, isl_error_internal,
6347 "expecting single element list", goto error);
6349 aff = isl_aff_list_get_aff(list, 0);
6350 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6352 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6354 isl_aff_list_free(list);
6356 return 0;
6357 error:
6358 isl_basic_set_free(dom);
6359 isl_aff_list_free(list);
6360 return -1;
6363 /* Given a basic map with one output dimension, compute the minimum or
6364 * maximum of that dimension as an isl_pw_aff.
6366 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6367 * call update_dim_opt on each leaf of the result.
6369 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6370 int max)
6372 isl_space *dim = isl_basic_map_get_space(bmap);
6373 isl_pw_aff *pwaff;
6374 int r;
6376 dim = isl_space_from_domain(isl_space_domain(dim));
6377 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6378 pwaff = isl_pw_aff_empty(dim);
6380 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6381 if (r < 0)
6382 return isl_pw_aff_free(pwaff);
6384 return pwaff;
6387 /* Compute the minimum or maximum of the given output dimension
6388 * as a function of the parameters and the input dimensions,
6389 * but independently of the other output dimensions.
6391 * We first project out the other output dimension and then compute
6392 * the "lexicographic" maximum in each basic map, combining the results
6393 * using isl_pw_aff_union_max.
6395 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6396 int max)
6398 int i;
6399 isl_pw_aff *pwaff;
6400 unsigned n_out;
6402 n_out = isl_map_dim(map, isl_dim_out);
6403 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6404 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6405 if (!map)
6406 return NULL;
6408 if (map->n == 0) {
6409 isl_space *dim = isl_map_get_space(map);
6410 isl_map_free(map);
6411 return isl_pw_aff_empty(dim);
6414 pwaff = basic_map_dim_opt(map->p[0], max);
6415 for (i = 1; i < map->n; ++i) {
6416 isl_pw_aff *pwaff_i;
6418 pwaff_i = basic_map_dim_opt(map->p[i], max);
6419 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6422 isl_map_free(map);
6424 return pwaff;
6427 /* Compute the minimum of the given output dimension as a function of the
6428 * parameters and input dimensions, but independently of
6429 * the other output dimensions.
6431 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6433 return map_dim_opt(map, pos, 0);
6436 /* Compute the maximum of the given output dimension as a function of the
6437 * parameters and input dimensions, but independently of
6438 * the other output dimensions.
6440 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6442 return map_dim_opt(map, pos, 1);
6445 /* Compute the minimum or maximum of the given set dimension
6446 * as a function of the parameters,
6447 * but independently of the other set dimensions.
6449 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6450 int max)
6452 return map_dim_opt(set, pos, max);
6455 /* Compute the maximum of the given set dimension as a function of the
6456 * parameters, but independently of the other set dimensions.
6458 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6460 return set_dim_opt(set, pos, 1);
6463 /* Compute the minimum of the given set dimension as a function of the
6464 * parameters, but independently of the other set dimensions.
6466 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6468 return set_dim_opt(set, pos, 0);
6471 /* Apply a preimage specified by "mat" on the parameters of "bset".
6472 * bset is assumed to have only parameters and divs.
6474 static struct isl_basic_set *basic_set_parameter_preimage(
6475 struct isl_basic_set *bset, struct isl_mat *mat)
6477 unsigned nparam;
6479 if (!bset || !mat)
6480 goto error;
6482 bset->dim = isl_space_cow(bset->dim);
6483 if (!bset->dim)
6484 goto error;
6486 nparam = isl_basic_set_dim(bset, isl_dim_param);
6488 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6490 bset->dim->nparam = 0;
6491 bset->dim->n_out = nparam;
6492 bset = isl_basic_set_preimage(bset, mat);
6493 if (bset) {
6494 bset->dim->nparam = bset->dim->n_out;
6495 bset->dim->n_out = 0;
6497 return bset;
6498 error:
6499 isl_mat_free(mat);
6500 isl_basic_set_free(bset);
6501 return NULL;
6504 /* Apply a preimage specified by "mat" on the parameters of "set".
6505 * set is assumed to have only parameters and divs.
6507 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6508 __isl_take isl_mat *mat)
6510 isl_space *space;
6511 unsigned nparam;
6513 if (!set || !mat)
6514 goto error;
6516 nparam = isl_set_dim(set, isl_dim_param);
6518 if (mat->n_row != 1 + nparam)
6519 isl_die(isl_set_get_ctx(set), isl_error_internal,
6520 "unexpected number of rows", goto error);
6522 space = isl_set_get_space(set);
6523 space = isl_space_move_dims(space, isl_dim_set, 0,
6524 isl_dim_param, 0, nparam);
6525 set = isl_set_reset_space(set, space);
6526 set = isl_set_preimage(set, mat);
6527 nparam = isl_set_dim(set, isl_dim_out);
6528 space = isl_set_get_space(set);
6529 space = isl_space_move_dims(space, isl_dim_param, 0,
6530 isl_dim_out, 0, nparam);
6531 set = isl_set_reset_space(set, space);
6532 return set;
6533 error:
6534 isl_mat_free(mat);
6535 isl_set_free(set);
6536 return NULL;
6539 /* Intersect the basic set "bset" with the affine space specified by the
6540 * equalities in "eq".
6542 static struct isl_basic_set *basic_set_append_equalities(
6543 struct isl_basic_set *bset, struct isl_mat *eq)
6545 int i, k;
6546 unsigned len;
6548 if (!bset || !eq)
6549 goto error;
6551 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6552 eq->n_row, 0);
6553 if (!bset)
6554 goto error;
6556 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6557 for (i = 0; i < eq->n_row; ++i) {
6558 k = isl_basic_set_alloc_equality(bset);
6559 if (k < 0)
6560 goto error;
6561 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6562 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6564 isl_mat_free(eq);
6566 bset = isl_basic_set_gauss(bset, NULL);
6567 bset = isl_basic_set_finalize(bset);
6569 return bset;
6570 error:
6571 isl_mat_free(eq);
6572 isl_basic_set_free(bset);
6573 return NULL;
6576 /* Intersect the set "set" with the affine space specified by the
6577 * equalities in "eq".
6579 static struct isl_set *set_append_equalities(struct isl_set *set,
6580 struct isl_mat *eq)
6582 int i;
6584 if (!set || !eq)
6585 goto error;
6587 for (i = 0; i < set->n; ++i) {
6588 set->p[i] = basic_set_append_equalities(set->p[i],
6589 isl_mat_copy(eq));
6590 if (!set->p[i])
6591 goto error;
6593 isl_mat_free(eq);
6594 return set;
6595 error:
6596 isl_mat_free(eq);
6597 isl_set_free(set);
6598 return NULL;
6601 /* Given a basic set "bset" that only involves parameters and existentially
6602 * quantified variables, return the index of the first equality
6603 * that only involves parameters. If there is no such equality then
6604 * return bset->n_eq.
6606 * This function assumes that isl_basic_set_gauss has been called on "bset".
6608 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6610 int i, j;
6611 unsigned nparam, n_div;
6613 if (!bset)
6614 return -1;
6616 nparam = isl_basic_set_dim(bset, isl_dim_param);
6617 n_div = isl_basic_set_dim(bset, isl_dim_div);
6619 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6620 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6621 ++i;
6624 return i;
6627 /* Compute an explicit representation for the existentially quantified
6628 * variables in "bset" by computing the "minimal value" of the set
6629 * variables. Since there are no set variables, the computation of
6630 * the minimal value essentially computes an explicit representation
6631 * of the non-empty part(s) of "bset".
6633 * The input only involves parameters and existentially quantified variables.
6634 * All equalities among parameters have been removed.
6636 * Since the existentially quantified variables in the result are in general
6637 * going to be different from those in the input, we first replace
6638 * them by the minimal number of variables based on their equalities.
6639 * This should simplify the parametric integer programming.
6641 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6643 isl_morph *morph1, *morph2;
6644 isl_set *set;
6645 unsigned n;
6647 if (!bset)
6648 return NULL;
6649 if (bset->n_eq == 0)
6650 return isl_basic_set_lexmin_compute_divs(bset);
6652 morph1 = isl_basic_set_parameter_compression(bset);
6653 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6654 bset = isl_basic_set_lift(bset);
6655 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6656 bset = isl_morph_basic_set(morph2, bset);
6657 n = isl_basic_set_dim(bset, isl_dim_set);
6658 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6660 set = isl_basic_set_lexmin_compute_divs(bset);
6662 set = isl_morph_set(isl_morph_inverse(morph1), set);
6664 return set;
6667 /* Project the given basic set onto its parameter domain, possibly introducing
6668 * new, explicit, existential variables in the constraints.
6669 * The input has parameters and (possibly implicit) existential variables.
6670 * The output has the same parameters, but only
6671 * explicit existentially quantified variables.
6673 * The actual projection is performed by pip, but pip doesn't seem
6674 * to like equalities very much, so we first remove the equalities
6675 * among the parameters by performing a variable compression on
6676 * the parameters. Afterward, an inverse transformation is performed
6677 * and the equalities among the parameters are inserted back in.
6679 * The variable compression on the parameters may uncover additional
6680 * equalities that were only implicit before. We therefore check
6681 * if there are any new parameter equalities in the result and
6682 * if so recurse. The removal of parameter equalities is required
6683 * for the parameter compression performed by base_compute_divs.
6685 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6687 int i;
6688 struct isl_mat *eq;
6689 struct isl_mat *T, *T2;
6690 struct isl_set *set;
6691 unsigned nparam;
6693 bset = isl_basic_set_cow(bset);
6694 if (!bset)
6695 return NULL;
6697 if (bset->n_eq == 0)
6698 return base_compute_divs(bset);
6700 bset = isl_basic_set_gauss(bset, NULL);
6701 if (!bset)
6702 return NULL;
6703 if (isl_basic_set_plain_is_empty(bset))
6704 return isl_set_from_basic_set(bset);
6706 i = first_parameter_equality(bset);
6707 if (i == bset->n_eq)
6708 return base_compute_divs(bset);
6710 nparam = isl_basic_set_dim(bset, isl_dim_param);
6711 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6712 0, 1 + nparam);
6713 eq = isl_mat_cow(eq);
6714 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6715 if (T && T->n_col == 0) {
6716 isl_mat_free(T);
6717 isl_mat_free(T2);
6718 isl_mat_free(eq);
6719 bset = isl_basic_set_set_to_empty(bset);
6720 return isl_set_from_basic_set(bset);
6722 bset = basic_set_parameter_preimage(bset, T);
6724 i = first_parameter_equality(bset);
6725 if (!bset)
6726 set = NULL;
6727 else if (i == bset->n_eq)
6728 set = base_compute_divs(bset);
6729 else
6730 set = parameter_compute_divs(bset);
6731 set = set_parameter_preimage(set, T2);
6732 set = set_append_equalities(set, eq);
6733 return set;
6736 /* Insert the divs from "ls" before those of "bmap".
6738 * The number of columns is not changed, which means that the last
6739 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6740 * The caller is responsible for removing the same number of dimensions
6741 * from the space of "bmap".
6743 static __isl_give isl_basic_map *insert_divs_from_local_space(
6744 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6746 int i;
6747 int n_div;
6748 int old_n_div;
6750 n_div = isl_local_space_dim(ls, isl_dim_div);
6751 if (n_div == 0)
6752 return bmap;
6754 old_n_div = bmap->n_div;
6755 bmap = insert_div_rows(bmap, n_div);
6756 if (!bmap)
6757 return NULL;
6759 for (i = 0; i < n_div; ++i) {
6760 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6761 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6764 return bmap;
6767 /* Replace the space of "bmap" by the space and divs of "ls".
6769 * If "ls" has any divs, then we simplify the result since we may
6770 * have discovered some additional equalities that could simplify
6771 * the div expressions.
6773 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6774 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6776 int n_div;
6778 bmap = isl_basic_map_cow(bmap);
6779 if (!bmap || !ls)
6780 goto error;
6782 n_div = isl_local_space_dim(ls, isl_dim_div);
6783 bmap = insert_divs_from_local_space(bmap, ls);
6784 if (!bmap)
6785 goto error;
6787 isl_space_free(bmap->dim);
6788 bmap->dim = isl_local_space_get_space(ls);
6789 if (!bmap->dim)
6790 goto error;
6792 isl_local_space_free(ls);
6793 if (n_div > 0)
6794 bmap = isl_basic_map_simplify(bmap);
6795 bmap = isl_basic_map_finalize(bmap);
6796 return bmap;
6797 error:
6798 isl_basic_map_free(bmap);
6799 isl_local_space_free(ls);
6800 return NULL;
6803 /* Replace the space of "map" by the space and divs of "ls".
6805 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6806 __isl_take isl_local_space *ls)
6808 int i;
6810 map = isl_map_cow(map);
6811 if (!map || !ls)
6812 goto error;
6814 for (i = 0; i < map->n; ++i) {
6815 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6816 isl_local_space_copy(ls));
6817 if (!map->p[i])
6818 goto error;
6820 isl_space_free(map->dim);
6821 map->dim = isl_local_space_get_space(ls);
6822 if (!map->dim)
6823 goto error;
6825 isl_local_space_free(ls);
6826 return map;
6827 error:
6828 isl_local_space_free(ls);
6829 isl_map_free(map);
6830 return NULL;
6833 /* Compute an explicit representation for the existentially
6834 * quantified variables for which do not know any explicit representation yet.
6836 * We first sort the existentially quantified variables so that the
6837 * existentially quantified variables for which we already have an explicit
6838 * representation are placed before those for which we do not.
6839 * The input dimensions, the output dimensions and the existentially
6840 * quantified variables for which we already have an explicit
6841 * representation are then turned into parameters.
6842 * compute_divs returns a map with the same parameters and
6843 * no input or output dimensions and the dimension specification
6844 * is reset to that of the input, including the existentially quantified
6845 * variables for which we already had an explicit representation.
6847 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6849 struct isl_basic_set *bset;
6850 struct isl_set *set;
6851 struct isl_map *map;
6852 isl_space *dim;
6853 isl_local_space *ls;
6854 unsigned nparam;
6855 unsigned n_in;
6856 unsigned n_out;
6857 int n_known;
6858 int i;
6860 bmap = isl_basic_map_sort_divs(bmap);
6861 bmap = isl_basic_map_cow(bmap);
6862 if (!bmap)
6863 return NULL;
6865 n_known = isl_basic_map_first_unknown_div(bmap);
6866 if (n_known < 0)
6867 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6869 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6870 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6871 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6872 dim = isl_space_set_alloc(bmap->ctx,
6873 nparam + n_in + n_out + n_known, 0);
6874 if (!dim)
6875 goto error;
6877 ls = isl_basic_map_get_local_space(bmap);
6878 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6879 n_known, bmap->n_div - n_known);
6880 if (n_known > 0) {
6881 for (i = n_known; i < bmap->n_div; ++i)
6882 swap_div(bmap, i - n_known, i);
6883 bmap->n_div -= n_known;
6884 bmap->extra -= n_known;
6886 bmap = isl_basic_map_reset_space(bmap, dim);
6887 bset = (struct isl_basic_set *)bmap;
6889 set = parameter_compute_divs(bset);
6890 map = (struct isl_map *)set;
6891 map = replace_space_by_local_space(map, ls);
6893 return map;
6894 error:
6895 isl_basic_map_free(bmap);
6896 return NULL;
6899 /* Remove the explicit representation of local variable "div",
6900 * if there is any.
6902 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6903 __isl_take isl_basic_map *bmap, int div)
6905 isl_bool unknown;
6907 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6908 if (unknown < 0)
6909 return isl_basic_map_free(bmap);
6910 if (unknown)
6911 return bmap;
6913 bmap = isl_basic_map_cow(bmap);
6914 if (!bmap)
6915 return NULL;
6916 isl_int_set_si(bmap->div[div][0], 0);
6917 return bmap;
6920 /* Is local variable "div" of "bmap" marked as not having an explicit
6921 * representation?
6922 * Note that even if "div" is not marked in this way and therefore
6923 * has an explicit representation, this representation may still
6924 * depend (indirectly) on other local variables that do not
6925 * have an explicit representation.
6927 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
6928 int div)
6930 if (!bmap)
6931 return isl_bool_error;
6932 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6933 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6934 "position out of bounds", return isl_bool_error);
6935 return isl_int_is_zero(bmap->div[div][0]);
6938 /* Return the position of the first local variable that does not
6939 * have an explicit representation.
6940 * Return the total number of local variables if they all have
6941 * an explicit representation.
6942 * Return -1 on error.
6944 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
6946 int i;
6948 if (!bmap)
6949 return -1;
6951 for (i = 0; i < bmap->n_div; ++i) {
6952 if (!isl_basic_map_div_is_known(bmap, i))
6953 return i;
6955 return bmap->n_div;
6958 /* Return the position of the first local variable that does not
6959 * have an explicit representation.
6960 * Return the total number of local variables if they all have
6961 * an explicit representation.
6962 * Return -1 on error.
6964 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
6966 return isl_basic_map_first_unknown_div(bset);
6969 /* Does "bmap" have an explicit representation for all local variables?
6971 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6973 int first, n;
6975 n = isl_basic_map_dim(bmap, isl_dim_div);
6976 first = isl_basic_map_first_unknown_div(bmap);
6977 if (first < 0)
6978 return isl_bool_error;
6979 return first == n;
6982 /* Do all basic maps in "map" have an explicit representation
6983 * for all local variables?
6985 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
6987 int i;
6989 if (!map)
6990 return isl_bool_error;
6992 for (i = 0; i < map->n; ++i) {
6993 int known = isl_basic_map_divs_known(map->p[i]);
6994 if (known <= 0)
6995 return known;
6998 return isl_bool_true;
7001 /* If bmap contains any unknown divs, then compute explicit
7002 * expressions for them. However, this computation may be
7003 * quite expensive, so first try to remove divs that aren't
7004 * strictly needed.
7006 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7008 int known;
7009 struct isl_map *map;
7011 known = isl_basic_map_divs_known(bmap);
7012 if (known < 0)
7013 goto error;
7014 if (known)
7015 return isl_map_from_basic_map(bmap);
7017 bmap = isl_basic_map_drop_redundant_divs(bmap);
7019 known = isl_basic_map_divs_known(bmap);
7020 if (known < 0)
7021 goto error;
7022 if (known)
7023 return isl_map_from_basic_map(bmap);
7025 map = compute_divs(bmap);
7026 return map;
7027 error:
7028 isl_basic_map_free(bmap);
7029 return NULL;
7032 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7034 int i;
7035 int known;
7036 struct isl_map *res;
7038 if (!map)
7039 return NULL;
7040 if (map->n == 0)
7041 return map;
7043 known = isl_map_divs_known(map);
7044 if (known < 0) {
7045 isl_map_free(map);
7046 return NULL;
7048 if (known)
7049 return map;
7051 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7052 for (i = 1 ; i < map->n; ++i) {
7053 struct isl_map *r2;
7054 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7055 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7056 res = isl_map_union_disjoint(res, r2);
7057 else
7058 res = isl_map_union(res, r2);
7060 isl_map_free(map);
7062 return res;
7065 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7067 return (struct isl_set *)
7068 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7071 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7073 return (struct isl_set *)
7074 isl_map_compute_divs((struct isl_map *)set);
7077 struct isl_set *isl_map_domain(struct isl_map *map)
7079 int i;
7080 struct isl_set *set;
7082 if (!map)
7083 goto error;
7085 map = isl_map_cow(map);
7086 if (!map)
7087 return NULL;
7089 set = (struct isl_set *)map;
7090 set->dim = isl_space_domain(set->dim);
7091 if (!set->dim)
7092 goto error;
7093 for (i = 0; i < map->n; ++i) {
7094 set->p[i] = isl_basic_map_domain(map->p[i]);
7095 if (!set->p[i])
7096 goto error;
7098 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7099 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7100 return set;
7101 error:
7102 isl_map_free(map);
7103 return NULL;
7106 /* Return the union of "map1" and "map2", where we assume for now that
7107 * "map1" and "map2" are disjoint. Note that the basic maps inside
7108 * "map1" or "map2" may not be disjoint from each other.
7109 * Also note that this function is also called from isl_map_union,
7110 * which takes care of handling the situation where "map1" and "map2"
7111 * may not be disjoint.
7113 * If one of the inputs is empty, we can simply return the other input.
7114 * Similarly, if one of the inputs is universal, then it is equal to the union.
7116 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7117 __isl_take isl_map *map2)
7119 int i;
7120 unsigned flags = 0;
7121 struct isl_map *map = NULL;
7122 int is_universe;
7124 if (!map1 || !map2)
7125 goto error;
7127 if (!isl_space_is_equal(map1->dim, map2->dim))
7128 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7129 "spaces don't match", goto error);
7131 if (map1->n == 0) {
7132 isl_map_free(map1);
7133 return map2;
7135 if (map2->n == 0) {
7136 isl_map_free(map2);
7137 return map1;
7140 is_universe = isl_map_plain_is_universe(map1);
7141 if (is_universe < 0)
7142 goto error;
7143 if (is_universe) {
7144 isl_map_free(map2);
7145 return map1;
7148 is_universe = isl_map_plain_is_universe(map2);
7149 if (is_universe < 0)
7150 goto error;
7151 if (is_universe) {
7152 isl_map_free(map1);
7153 return map2;
7156 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7157 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7158 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7160 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7161 map1->n + map2->n, flags);
7162 if (!map)
7163 goto error;
7164 for (i = 0; i < map1->n; ++i) {
7165 map = isl_map_add_basic_map(map,
7166 isl_basic_map_copy(map1->p[i]));
7167 if (!map)
7168 goto error;
7170 for (i = 0; i < map2->n; ++i) {
7171 map = isl_map_add_basic_map(map,
7172 isl_basic_map_copy(map2->p[i]));
7173 if (!map)
7174 goto error;
7176 isl_map_free(map1);
7177 isl_map_free(map2);
7178 return map;
7179 error:
7180 isl_map_free(map);
7181 isl_map_free(map1);
7182 isl_map_free(map2);
7183 return NULL;
7186 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7187 * guaranteed to be disjoint by the caller.
7189 * Note that this functions is called from within isl_map_make_disjoint,
7190 * so we have to be careful not to touch the constraints of the inputs
7191 * in any way.
7193 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7194 __isl_take isl_map *map2)
7196 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7199 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7200 * not be disjoint. The parameters are assumed to have been aligned.
7202 * We currently simply call map_union_disjoint, the internal operation
7203 * of which does not really depend on the inputs being disjoint.
7204 * If the result contains more than one basic map, then we clear
7205 * the disjoint flag since the result may contain basic maps from
7206 * both inputs and these are not guaranteed to be disjoint.
7208 * As a special case, if "map1" and "map2" are obviously equal,
7209 * then we simply return "map1".
7211 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7212 __isl_take isl_map *map2)
7214 int equal;
7216 if (!map1 || !map2)
7217 goto error;
7219 equal = isl_map_plain_is_equal(map1, map2);
7220 if (equal < 0)
7221 goto error;
7222 if (equal) {
7223 isl_map_free(map2);
7224 return map1;
7227 map1 = map_union_disjoint(map1, map2);
7228 if (!map1)
7229 return NULL;
7230 if (map1->n > 1)
7231 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7232 return map1;
7233 error:
7234 isl_map_free(map1);
7235 isl_map_free(map2);
7236 return NULL;
7239 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7240 * not be disjoint.
7242 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7243 __isl_take isl_map *map2)
7245 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7248 struct isl_set *isl_set_union_disjoint(
7249 struct isl_set *set1, struct isl_set *set2)
7251 return (struct isl_set *)
7252 isl_map_union_disjoint(
7253 (struct isl_map *)set1, (struct isl_map *)set2);
7256 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7258 return (struct isl_set *)
7259 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7262 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7263 * the results.
7265 * "map" and "set" are assumed to be compatible and non-NULL.
7267 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7268 __isl_take isl_set *set,
7269 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7270 __isl_take isl_basic_set *bset))
7272 unsigned flags = 0;
7273 struct isl_map *result;
7274 int i, j;
7276 if (isl_set_plain_is_universe(set)) {
7277 isl_set_free(set);
7278 return map;
7281 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7282 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7283 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7285 result = isl_map_alloc_space(isl_space_copy(map->dim),
7286 map->n * set->n, flags);
7287 for (i = 0; result && i < map->n; ++i)
7288 for (j = 0; j < set->n; ++j) {
7289 result = isl_map_add_basic_map(result,
7290 fn(isl_basic_map_copy(map->p[i]),
7291 isl_basic_set_copy(set->p[j])));
7292 if (!result)
7293 break;
7296 isl_map_free(map);
7297 isl_set_free(set);
7298 return result;
7301 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7302 __isl_take isl_set *set)
7304 if (!map || !set)
7305 goto error;
7307 if (!isl_map_compatible_range(map, set))
7308 isl_die(set->ctx, isl_error_invalid,
7309 "incompatible spaces", goto error);
7311 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7312 error:
7313 isl_map_free(map);
7314 isl_set_free(set);
7315 return NULL;
7318 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7319 __isl_take isl_set *set)
7321 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7324 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7325 __isl_take isl_set *set)
7327 if (!map || !set)
7328 goto error;
7330 if (!isl_map_compatible_domain(map, set))
7331 isl_die(set->ctx, isl_error_invalid,
7332 "incompatible spaces", goto error);
7334 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7335 error:
7336 isl_map_free(map);
7337 isl_set_free(set);
7338 return NULL;
7341 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7342 __isl_take isl_set *set)
7344 return isl_map_align_params_map_map_and(map, set,
7345 &map_intersect_domain);
7348 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7349 __isl_take isl_map *map2)
7351 if (!map1 || !map2)
7352 goto error;
7353 map1 = isl_map_reverse(map1);
7354 map1 = isl_map_apply_range(map1, map2);
7355 return isl_map_reverse(map1);
7356 error:
7357 isl_map_free(map1);
7358 isl_map_free(map2);
7359 return NULL;
7362 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7363 __isl_take isl_map *map2)
7365 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7368 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7369 __isl_take isl_map *map2)
7371 isl_space *dim_result;
7372 struct isl_map *result;
7373 int i, j;
7375 if (!map1 || !map2)
7376 goto error;
7378 dim_result = isl_space_join(isl_space_copy(map1->dim),
7379 isl_space_copy(map2->dim));
7381 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7382 if (!result)
7383 goto error;
7384 for (i = 0; i < map1->n; ++i)
7385 for (j = 0; j < map2->n; ++j) {
7386 result = isl_map_add_basic_map(result,
7387 isl_basic_map_apply_range(
7388 isl_basic_map_copy(map1->p[i]),
7389 isl_basic_map_copy(map2->p[j])));
7390 if (!result)
7391 goto error;
7393 isl_map_free(map1);
7394 isl_map_free(map2);
7395 if (result && result->n <= 1)
7396 ISL_F_SET(result, ISL_MAP_DISJOINT);
7397 return result;
7398 error:
7399 isl_map_free(map1);
7400 isl_map_free(map2);
7401 return NULL;
7404 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7405 __isl_take isl_map *map2)
7407 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7411 * returns range - domain
7413 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7415 isl_space *target_space;
7416 struct isl_basic_set *bset;
7417 unsigned dim;
7418 unsigned nparam;
7419 int i;
7421 if (!bmap)
7422 goto error;
7423 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7424 bmap->dim, isl_dim_out),
7425 goto error);
7426 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7427 dim = isl_basic_map_n_in(bmap);
7428 nparam = isl_basic_map_n_param(bmap);
7429 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7430 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7431 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7432 for (i = 0; i < dim; ++i) {
7433 int j = isl_basic_map_alloc_equality(bmap);
7434 if (j < 0) {
7435 bmap = isl_basic_map_free(bmap);
7436 break;
7438 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7439 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7440 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7441 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7443 bset = isl_basic_map_domain(bmap);
7444 bset = isl_basic_set_reset_space(bset, target_space);
7445 return bset;
7446 error:
7447 isl_basic_map_free(bmap);
7448 return NULL;
7452 * returns range - domain
7454 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7456 int i;
7457 isl_space *dim;
7458 struct isl_set *result;
7460 if (!map)
7461 return NULL;
7463 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7464 map->dim, isl_dim_out),
7465 goto error);
7466 dim = isl_map_get_space(map);
7467 dim = isl_space_domain(dim);
7468 result = isl_set_alloc_space(dim, map->n, 0);
7469 if (!result)
7470 goto error;
7471 for (i = 0; i < map->n; ++i)
7472 result = isl_set_add_basic_set(result,
7473 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7474 isl_map_free(map);
7475 return result;
7476 error:
7477 isl_map_free(map);
7478 return NULL;
7482 * returns [domain -> range] -> range - domain
7484 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7485 __isl_take isl_basic_map *bmap)
7487 int i, k;
7488 isl_space *dim;
7489 isl_basic_map *domain;
7490 int nparam, n;
7491 unsigned total;
7493 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7494 bmap->dim, isl_dim_out))
7495 isl_die(bmap->ctx, isl_error_invalid,
7496 "domain and range don't match", goto error);
7498 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7499 n = isl_basic_map_dim(bmap, isl_dim_in);
7501 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7502 domain = isl_basic_map_universe(dim);
7504 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7505 bmap = isl_basic_map_apply_range(bmap, domain);
7506 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7508 total = isl_basic_map_total_dim(bmap);
7510 for (i = 0; i < n; ++i) {
7511 k = isl_basic_map_alloc_equality(bmap);
7512 if (k < 0)
7513 goto error;
7514 isl_seq_clr(bmap->eq[k], 1 + total);
7515 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7516 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7517 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7520 bmap = isl_basic_map_gauss(bmap, NULL);
7521 return isl_basic_map_finalize(bmap);
7522 error:
7523 isl_basic_map_free(bmap);
7524 return NULL;
7528 * returns [domain -> range] -> range - domain
7530 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7532 int i;
7533 isl_space *domain_dim;
7535 if (!map)
7536 return NULL;
7538 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7539 map->dim, isl_dim_out))
7540 isl_die(map->ctx, isl_error_invalid,
7541 "domain and range don't match", goto error);
7543 map = isl_map_cow(map);
7544 if (!map)
7545 return NULL;
7547 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7548 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7549 map->dim = isl_space_join(map->dim, domain_dim);
7550 if (!map->dim)
7551 goto error;
7552 for (i = 0; i < map->n; ++i) {
7553 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7554 if (!map->p[i])
7555 goto error;
7557 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7558 return map;
7559 error:
7560 isl_map_free(map);
7561 return NULL;
7564 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7566 struct isl_basic_map *bmap;
7567 unsigned nparam;
7568 unsigned dim;
7569 int i;
7571 if (!dims)
7572 return NULL;
7574 nparam = dims->nparam;
7575 dim = dims->n_out;
7576 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7577 if (!bmap)
7578 goto error;
7580 for (i = 0; i < dim; ++i) {
7581 int j = isl_basic_map_alloc_equality(bmap);
7582 if (j < 0)
7583 goto error;
7584 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7585 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7586 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7588 return isl_basic_map_finalize(bmap);
7589 error:
7590 isl_basic_map_free(bmap);
7591 return NULL;
7594 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7596 if (!dim)
7597 return NULL;
7598 if (dim->n_in != dim->n_out)
7599 isl_die(dim->ctx, isl_error_invalid,
7600 "number of input and output dimensions needs to be "
7601 "the same", goto error);
7602 return basic_map_identity(dim);
7603 error:
7604 isl_space_free(dim);
7605 return NULL;
7608 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7610 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7613 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7615 isl_space *dim = isl_set_get_space(set);
7616 isl_map *id;
7617 id = isl_map_identity(isl_space_map_from_set(dim));
7618 return isl_map_intersect_range(id, set);
7621 /* Construct a basic set with all set dimensions having only non-negative
7622 * values.
7624 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7625 __isl_take isl_space *space)
7627 int i;
7628 unsigned nparam;
7629 unsigned dim;
7630 struct isl_basic_set *bset;
7632 if (!space)
7633 return NULL;
7634 nparam = space->nparam;
7635 dim = space->n_out;
7636 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7637 if (!bset)
7638 return NULL;
7639 for (i = 0; i < dim; ++i) {
7640 int k = isl_basic_set_alloc_inequality(bset);
7641 if (k < 0)
7642 goto error;
7643 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7644 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7646 return bset;
7647 error:
7648 isl_basic_set_free(bset);
7649 return NULL;
7652 /* Construct the half-space x_pos >= 0.
7654 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7655 int pos)
7657 int k;
7658 isl_basic_set *nonneg;
7660 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7661 k = isl_basic_set_alloc_inequality(nonneg);
7662 if (k < 0)
7663 goto error;
7664 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7665 isl_int_set_si(nonneg->ineq[k][pos], 1);
7667 return isl_basic_set_finalize(nonneg);
7668 error:
7669 isl_basic_set_free(nonneg);
7670 return NULL;
7673 /* Construct the half-space x_pos <= -1.
7675 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7677 int k;
7678 isl_basic_set *neg;
7680 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7681 k = isl_basic_set_alloc_inequality(neg);
7682 if (k < 0)
7683 goto error;
7684 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7685 isl_int_set_si(neg->ineq[k][0], -1);
7686 isl_int_set_si(neg->ineq[k][pos], -1);
7688 return isl_basic_set_finalize(neg);
7689 error:
7690 isl_basic_set_free(neg);
7691 return NULL;
7694 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7695 enum isl_dim_type type, unsigned first, unsigned n)
7697 int i;
7698 unsigned offset;
7699 isl_basic_set *nonneg;
7700 isl_basic_set *neg;
7702 if (!set)
7703 return NULL;
7704 if (n == 0)
7705 return set;
7707 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7709 offset = pos(set->dim, type);
7710 for (i = 0; i < n; ++i) {
7711 nonneg = nonneg_halfspace(isl_set_get_space(set),
7712 offset + first + i);
7713 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7715 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7718 return set;
7719 error:
7720 isl_set_free(set);
7721 return NULL;
7724 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7725 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7726 void *user)
7728 isl_set *half;
7730 if (!set)
7731 return -1;
7732 if (isl_set_plain_is_empty(set)) {
7733 isl_set_free(set);
7734 return 0;
7736 if (first == len)
7737 return fn(set, signs, user);
7739 signs[first] = 1;
7740 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7741 1 + first));
7742 half = isl_set_intersect(half, isl_set_copy(set));
7743 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7744 goto error;
7746 signs[first] = -1;
7747 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7748 1 + first));
7749 half = isl_set_intersect(half, set);
7750 return foreach_orthant(half, signs, first + 1, len, fn, user);
7751 error:
7752 isl_set_free(set);
7753 return -1;
7756 /* Call "fn" on the intersections of "set" with each of the orthants
7757 * (except for obviously empty intersections). The orthant is identified
7758 * by the signs array, with each entry having value 1 or -1 according
7759 * to the sign of the corresponding variable.
7761 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7762 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7763 void *user)
7765 unsigned nparam;
7766 unsigned nvar;
7767 int *signs;
7768 int r;
7770 if (!set)
7771 return -1;
7772 if (isl_set_plain_is_empty(set))
7773 return 0;
7775 nparam = isl_set_dim(set, isl_dim_param);
7776 nvar = isl_set_dim(set, isl_dim_set);
7778 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7780 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7781 fn, user);
7783 free(signs);
7785 return r;
7788 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7790 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7793 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7794 __isl_keep isl_basic_map *bmap2)
7796 int is_subset;
7797 struct isl_map *map1;
7798 struct isl_map *map2;
7800 if (!bmap1 || !bmap2)
7801 return isl_bool_error;
7803 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7804 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7806 is_subset = isl_map_is_subset(map1, map2);
7808 isl_map_free(map1);
7809 isl_map_free(map2);
7811 return is_subset;
7814 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7815 __isl_keep isl_basic_set *bset2)
7817 return isl_basic_map_is_subset(bset1, bset2);
7820 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7821 __isl_keep isl_basic_map *bmap2)
7823 isl_bool is_subset;
7825 if (!bmap1 || !bmap2)
7826 return isl_bool_error;
7827 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7828 if (is_subset != isl_bool_true)
7829 return is_subset;
7830 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7831 return is_subset;
7834 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7835 __isl_keep isl_basic_set *bset2)
7837 return isl_basic_map_is_equal(
7838 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7841 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7843 int i;
7844 int is_empty;
7846 if (!map)
7847 return isl_bool_error;
7848 for (i = 0; i < map->n; ++i) {
7849 is_empty = isl_basic_map_is_empty(map->p[i]);
7850 if (is_empty < 0)
7851 return isl_bool_error;
7852 if (!is_empty)
7853 return isl_bool_false;
7855 return isl_bool_true;
7858 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7860 return map ? map->n == 0 : isl_bool_error;
7863 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7865 return set ? set->n == 0 : isl_bool_error;
7868 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7870 return isl_map_is_empty((struct isl_map *)set);
7873 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7875 if (!map1 || !map2)
7876 return -1;
7878 return isl_space_is_equal(map1->dim, map2->dim);
7881 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7883 if (!set1 || !set2)
7884 return -1;
7886 return isl_space_is_equal(set1->dim, set2->dim);
7889 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7891 isl_bool is_subset;
7893 if (!map1 || !map2)
7894 return isl_bool_error;
7895 is_subset = isl_map_is_subset(map1, map2);
7896 if (is_subset != isl_bool_true)
7897 return is_subset;
7898 is_subset = isl_map_is_subset(map2, map1);
7899 return is_subset;
7902 /* Is "map1" equal to "map2"?
7904 * First check if they are obviously equal.
7905 * If not, then perform a more detailed analysis.
7907 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7909 isl_bool equal;
7911 equal = isl_map_plain_is_equal(map1, map2);
7912 if (equal < 0 || equal)
7913 return equal;
7914 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7917 isl_bool isl_basic_map_is_strict_subset(
7918 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7920 isl_bool is_subset;
7922 if (!bmap1 || !bmap2)
7923 return isl_bool_error;
7924 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7925 if (is_subset != isl_bool_true)
7926 return is_subset;
7927 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7928 if (is_subset == isl_bool_error)
7929 return is_subset;
7930 return !is_subset;
7933 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7934 __isl_keep isl_map *map2)
7936 isl_bool is_subset;
7938 if (!map1 || !map2)
7939 return isl_bool_error;
7940 is_subset = isl_map_is_subset(map1, map2);
7941 if (is_subset != isl_bool_true)
7942 return is_subset;
7943 is_subset = isl_map_is_subset(map2, map1);
7944 if (is_subset == isl_bool_error)
7945 return is_subset;
7946 return !is_subset;
7949 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7950 __isl_keep isl_set *set2)
7952 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7955 /* Is "bmap" obviously equal to the universe with the same space?
7957 * That is, does it not have any constraints?
7959 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
7961 if (!bmap)
7962 return isl_bool_error;
7963 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7966 /* Is "bset" obviously equal to the universe with the same space?
7968 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
7970 return isl_basic_map_plain_is_universe(bset);
7973 /* If "c" does not involve any existentially quantified variables,
7974 * then set *univ to false and abort
7976 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
7978 isl_bool *univ = user;
7979 unsigned n;
7981 n = isl_constraint_dim(c, isl_dim_div);
7982 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
7983 isl_constraint_free(c);
7984 if (*univ < 0 || !*univ)
7985 return isl_stat_error;
7986 return isl_stat_ok;
7989 /* Is "bmap" equal to the universe with the same space?
7991 * First check if it is obviously equal to the universe.
7992 * If not and if there are any constraints not involving
7993 * existentially quantified variables, then it is certainly
7994 * not equal to the universe.
7995 * Otherwise, check if the universe is a subset of "bmap".
7997 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
7999 isl_bool univ;
8000 isl_basic_map *test;
8002 univ = isl_basic_map_plain_is_universe(bmap);
8003 if (univ < 0 || univ)
8004 return univ;
8005 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8006 return isl_bool_false;
8007 univ = isl_bool_true;
8008 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8009 univ)
8010 return isl_bool_error;
8011 if (univ < 0 || !univ)
8012 return univ;
8013 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8014 univ = isl_basic_map_is_subset(test, bmap);
8015 isl_basic_map_free(test);
8016 return univ;
8019 /* Is "bset" equal to the universe with the same space?
8021 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8023 return isl_basic_map_is_universe(bset);
8026 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8028 int i;
8030 if (!map)
8031 return isl_bool_error;
8033 for (i = 0; i < map->n; ++i) {
8034 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8035 if (r < 0 || r)
8036 return r;
8039 return isl_bool_false;
8042 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8044 return isl_map_plain_is_universe((isl_map *) set);
8047 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8049 struct isl_basic_set *bset = NULL;
8050 struct isl_vec *sample = NULL;
8051 isl_bool empty, non_empty;
8053 if (!bmap)
8054 return isl_bool_error;
8056 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8057 return isl_bool_true;
8059 if (isl_basic_map_plain_is_universe(bmap))
8060 return isl_bool_false;
8062 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8063 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8064 copy = isl_basic_map_remove_redundancies(copy);
8065 empty = isl_basic_map_plain_is_empty(copy);
8066 isl_basic_map_free(copy);
8067 return empty;
8070 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8071 if (non_empty < 0)
8072 return isl_bool_error;
8073 if (non_empty)
8074 return isl_bool_false;
8075 isl_vec_free(bmap->sample);
8076 bmap->sample = NULL;
8077 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8078 if (!bset)
8079 return isl_bool_error;
8080 sample = isl_basic_set_sample_vec(bset);
8081 if (!sample)
8082 return isl_bool_error;
8083 empty = sample->size == 0;
8084 isl_vec_free(bmap->sample);
8085 bmap->sample = sample;
8086 if (empty)
8087 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8089 return empty;
8092 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8094 if (!bmap)
8095 return isl_bool_error;
8096 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8099 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8101 if (!bset)
8102 return isl_bool_error;
8103 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8106 /* Is "bmap" known to be non-empty?
8108 * That is, is the cached sample still valid?
8110 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8112 unsigned total;
8114 if (!bmap)
8115 return isl_bool_error;
8116 if (!bmap->sample)
8117 return isl_bool_false;
8118 total = 1 + isl_basic_map_total_dim(bmap);
8119 if (bmap->sample->size != total)
8120 return isl_bool_false;
8121 return isl_basic_map_contains(bmap, bmap->sample);
8124 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8126 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8129 struct isl_map *isl_basic_map_union(
8130 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8132 struct isl_map *map;
8133 if (!bmap1 || !bmap2)
8134 goto error;
8136 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8138 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8139 if (!map)
8140 goto error;
8141 map = isl_map_add_basic_map(map, bmap1);
8142 map = isl_map_add_basic_map(map, bmap2);
8143 return map;
8144 error:
8145 isl_basic_map_free(bmap1);
8146 isl_basic_map_free(bmap2);
8147 return NULL;
8150 struct isl_set *isl_basic_set_union(
8151 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8153 return (struct isl_set *)isl_basic_map_union(
8154 (struct isl_basic_map *)bset1,
8155 (struct isl_basic_map *)bset2);
8158 /* Order divs such that any div only depends on previous divs */
8159 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8161 int i;
8162 unsigned off;
8164 if (!bmap)
8165 return NULL;
8167 off = isl_space_dim(bmap->dim, isl_dim_all);
8169 for (i = 0; i < bmap->n_div; ++i) {
8170 int pos;
8171 if (isl_int_is_zero(bmap->div[i][0]))
8172 continue;
8173 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8174 bmap->n_div-i);
8175 if (pos == -1)
8176 continue;
8177 if (pos == 0)
8178 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8179 "integer division depends on itself",
8180 return isl_basic_map_free(bmap));
8181 isl_basic_map_swap_div(bmap, i, i + pos);
8182 --i;
8184 return bmap;
8187 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8189 return (struct isl_basic_set *)
8190 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8193 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8195 int i;
8197 if (!map)
8198 return 0;
8200 for (i = 0; i < map->n; ++i) {
8201 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8202 if (!map->p[i])
8203 goto error;
8206 return map;
8207 error:
8208 isl_map_free(map);
8209 return NULL;
8212 /* Apply the expansion computed by isl_merge_divs.
8213 * The expansion itself is given by "exp" while the resulting
8214 * list of divs is given by "div".
8216 * Move the integer divisions of "bmap" into the right position
8217 * according to "exp" and then introduce the additional integer
8218 * divisions, adding div constraints.
8219 * The moving should be done first to avoid moving coefficients
8220 * in the definitions of the extra integer divisions.
8222 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8223 __isl_take isl_basic_set *bmap, __isl_take isl_mat *div, int *exp)
8225 int i, j;
8226 int n_div;
8228 bmap = isl_basic_map_cow(bmap);
8229 if (!bmap || !div)
8230 goto error;
8232 if (div->n_row < bmap->n_div)
8233 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8234 "not an expansion", goto error);
8236 n_div = bmap->n_div;
8237 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8238 div->n_row - n_div, 0,
8239 2 * (div->n_row - n_div));
8241 for (i = n_div; i < div->n_row; ++i)
8242 if (isl_basic_map_alloc_div(bmap) < 0)
8243 goto error;
8245 for (j = n_div - 1; j >= 0; --j) {
8246 if (exp[j] == j)
8247 break;
8248 isl_basic_map_swap_div(bmap, j, exp[j]);
8250 j = 0;
8251 for (i = 0; i < div->n_row; ++i) {
8252 if (j < n_div && exp[j] == i) {
8253 j++;
8254 } else {
8255 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8256 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8257 continue;
8258 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8259 goto error;
8263 isl_mat_free(div);
8264 return bmap;
8265 error:
8266 isl_basic_map_free(bmap);
8267 isl_mat_free(div);
8268 return NULL;
8271 /* Apply the expansion computed by isl_merge_divs.
8272 * The expansion itself is given by "exp" while the resulting
8273 * list of divs is given by "div".
8275 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8276 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8278 return isl_basic_map_expand_divs(bset, div, exp);
8281 /* Look for a div in dst that corresponds to the div "div" in src.
8282 * The divs before "div" in src and dst are assumed to be the same.
8284 * Returns -1 if no corresponding div was found and the position
8285 * of the corresponding div in dst otherwise.
8287 static int find_div(struct isl_basic_map *dst,
8288 struct isl_basic_map *src, unsigned div)
8290 int i;
8292 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8294 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8295 for (i = div; i < dst->n_div; ++i)
8296 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8297 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8298 dst->n_div - div) == -1)
8299 return i;
8300 return -1;
8303 /* Align the divs of "dst" to those of "src", adding divs from "src"
8304 * if needed. That is, make sure that the first src->n_div divs
8305 * of the result are equal to those of src.
8307 * The result is not finalized as by design it will have redundant
8308 * divs if any divs from "src" were copied.
8310 __isl_give isl_basic_map *isl_basic_map_align_divs(
8311 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8313 int i;
8314 int known, extended;
8315 unsigned total;
8317 if (!dst || !src)
8318 return isl_basic_map_free(dst);
8320 if (src->n_div == 0)
8321 return dst;
8323 known = isl_basic_map_divs_known(src);
8324 if (known < 0)
8325 return isl_basic_map_free(dst);
8326 if (!known)
8327 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8328 "some src divs are unknown",
8329 return isl_basic_map_free(dst));
8331 src = isl_basic_map_order_divs(src);
8333 extended = 0;
8334 total = isl_space_dim(src->dim, isl_dim_all);
8335 for (i = 0; i < src->n_div; ++i) {
8336 int j = find_div(dst, src, i);
8337 if (j < 0) {
8338 if (!extended) {
8339 int extra = src->n_div - i;
8340 dst = isl_basic_map_cow(dst);
8341 if (!dst)
8342 return NULL;
8343 dst = isl_basic_map_extend_space(dst,
8344 isl_space_copy(dst->dim),
8345 extra, 0, 2 * extra);
8346 extended = 1;
8348 j = isl_basic_map_alloc_div(dst);
8349 if (j < 0)
8350 return isl_basic_map_free(dst);
8351 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8352 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8353 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8354 return isl_basic_map_free(dst);
8356 if (j != i)
8357 isl_basic_map_swap_div(dst, i, j);
8359 return dst;
8362 struct isl_basic_set *isl_basic_set_align_divs(
8363 struct isl_basic_set *dst, struct isl_basic_set *src)
8365 return (struct isl_basic_set *)isl_basic_map_align_divs(
8366 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8369 struct isl_map *isl_map_align_divs(struct isl_map *map)
8371 int i;
8373 if (!map)
8374 return NULL;
8375 if (map->n == 0)
8376 return map;
8377 map = isl_map_compute_divs(map);
8378 map = isl_map_cow(map);
8379 if (!map)
8380 return NULL;
8382 for (i = 1; i < map->n; ++i)
8383 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8384 for (i = 1; i < map->n; ++i) {
8385 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8386 if (!map->p[i])
8387 return isl_map_free(map);
8390 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8391 return map;
8394 struct isl_set *isl_set_align_divs(struct isl_set *set)
8396 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8399 /* Align the divs of the basic maps in "map" to those
8400 * of the basic maps in "list", as well as to the other basic maps in "map".
8401 * The elements in "list" are assumed to have known divs.
8403 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8404 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8406 int i, n;
8408 map = isl_map_compute_divs(map);
8409 map = isl_map_cow(map);
8410 if (!map || !list)
8411 return isl_map_free(map);
8412 if (map->n == 0)
8413 return map;
8415 n = isl_basic_map_list_n_basic_map(list);
8416 for (i = 0; i < n; ++i) {
8417 isl_basic_map *bmap;
8419 bmap = isl_basic_map_list_get_basic_map(list, i);
8420 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8421 isl_basic_map_free(bmap);
8423 if (!map->p[0])
8424 return isl_map_free(map);
8426 return isl_map_align_divs(map);
8429 /* Align the divs of each element of "list" to those of "bmap".
8430 * Both "bmap" and the elements of "list" are assumed to have known divs.
8432 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8433 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8435 int i, n;
8437 if (!list || !bmap)
8438 return isl_basic_map_list_free(list);
8440 n = isl_basic_map_list_n_basic_map(list);
8441 for (i = 0; i < n; ++i) {
8442 isl_basic_map *bmap_i;
8444 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8445 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8446 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8449 return list;
8452 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8453 __isl_take isl_map *map)
8455 if (!set || !map)
8456 goto error;
8457 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8458 map = isl_map_intersect_domain(map, set);
8459 set = isl_map_range(map);
8460 return set;
8461 error:
8462 isl_set_free(set);
8463 isl_map_free(map);
8464 return NULL;
8467 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8468 __isl_take isl_map *map)
8470 return isl_map_align_params_map_map_and(set, map, &set_apply);
8473 /* There is no need to cow as removing empty parts doesn't change
8474 * the meaning of the set.
8476 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8478 int i;
8480 if (!map)
8481 return NULL;
8483 for (i = map->n - 1; i >= 0; --i)
8484 remove_if_empty(map, i);
8486 return map;
8489 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8491 return (struct isl_set *)
8492 isl_map_remove_empty_parts((struct isl_map *)set);
8495 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8497 struct isl_basic_map *bmap;
8498 if (!map || map->n == 0)
8499 return NULL;
8500 bmap = map->p[map->n-1];
8501 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8502 return isl_basic_map_copy(bmap);
8505 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8507 return (struct isl_basic_set *)
8508 isl_map_copy_basic_map((struct isl_map *)set);
8511 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8512 __isl_keep isl_basic_map *bmap)
8514 int i;
8516 if (!map || !bmap)
8517 goto error;
8518 for (i = map->n-1; i >= 0; --i) {
8519 if (map->p[i] != bmap)
8520 continue;
8521 map = isl_map_cow(map);
8522 if (!map)
8523 goto error;
8524 isl_basic_map_free(map->p[i]);
8525 if (i != map->n-1) {
8526 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8527 map->p[i] = map->p[map->n-1];
8529 map->n--;
8530 return map;
8532 return map;
8533 error:
8534 isl_map_free(map);
8535 return NULL;
8538 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8539 struct isl_basic_set *bset)
8541 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8542 (struct isl_basic_map *)bset);
8545 /* Given two basic sets bset1 and bset2, compute the maximal difference
8546 * between the values of dimension pos in bset1 and those in bset2
8547 * for any common value of the parameters and dimensions preceding pos.
8549 static enum isl_lp_result basic_set_maximal_difference_at(
8550 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8551 int pos, isl_int *opt)
8553 isl_space *dims;
8554 struct isl_basic_map *bmap1 = NULL;
8555 struct isl_basic_map *bmap2 = NULL;
8556 struct isl_ctx *ctx;
8557 struct isl_vec *obj;
8558 unsigned total;
8559 unsigned nparam;
8560 unsigned dim1, dim2;
8561 enum isl_lp_result res;
8563 if (!bset1 || !bset2)
8564 return isl_lp_error;
8566 nparam = isl_basic_set_n_param(bset1);
8567 dim1 = isl_basic_set_n_dim(bset1);
8568 dim2 = isl_basic_set_n_dim(bset2);
8569 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8570 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8571 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8572 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8573 if (!bmap1 || !bmap2)
8574 goto error;
8575 bmap1 = isl_basic_map_cow(bmap1);
8576 bmap1 = isl_basic_map_extend(bmap1, nparam,
8577 pos, (dim1 - pos) + (dim2 - pos),
8578 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8579 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8580 if (!bmap1)
8581 goto error2;
8582 total = isl_basic_map_total_dim(bmap1);
8583 ctx = bmap1->ctx;
8584 obj = isl_vec_alloc(ctx, 1 + total);
8585 if (!obj)
8586 goto error2;
8587 isl_seq_clr(obj->block.data, 1 + total);
8588 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8589 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8590 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8591 opt, NULL, NULL);
8592 isl_basic_map_free(bmap1);
8593 isl_vec_free(obj);
8594 return res;
8595 error:
8596 isl_basic_map_free(bmap2);
8597 error2:
8598 isl_basic_map_free(bmap1);
8599 return isl_lp_error;
8602 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8603 * for any common value of the parameters and dimensions preceding pos
8604 * in both basic sets, the values of dimension pos in bset1 are
8605 * smaller or larger than those in bset2.
8607 * Returns
8608 * 1 if bset1 follows bset2
8609 * -1 if bset1 precedes bset2
8610 * 0 if bset1 and bset2 are incomparable
8611 * -2 if some error occurred.
8613 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8614 struct isl_basic_set *bset2, int pos)
8616 isl_int opt;
8617 enum isl_lp_result res;
8618 int cmp;
8620 isl_int_init(opt);
8622 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8624 if (res == isl_lp_empty)
8625 cmp = 0;
8626 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8627 res == isl_lp_unbounded)
8628 cmp = 1;
8629 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8630 cmp = -1;
8631 else
8632 cmp = -2;
8634 isl_int_clear(opt);
8635 return cmp;
8638 /* Given two basic sets bset1 and bset2, check whether
8639 * for any common value of the parameters and dimensions preceding pos
8640 * there is a value of dimension pos in bset1 that is larger
8641 * than a value of the same dimension in bset2.
8643 * Return
8644 * 1 if there exists such a pair
8645 * 0 if there is no such pair, but there is a pair of equal values
8646 * -1 otherwise
8647 * -2 if some error occurred.
8649 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8650 __isl_keep isl_basic_set *bset2, int pos)
8652 isl_int opt;
8653 enum isl_lp_result res;
8654 int cmp;
8656 isl_int_init(opt);
8658 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8660 if (res == isl_lp_empty)
8661 cmp = -1;
8662 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8663 res == isl_lp_unbounded)
8664 cmp = 1;
8665 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8666 cmp = -1;
8667 else if (res == isl_lp_ok)
8668 cmp = 0;
8669 else
8670 cmp = -2;
8672 isl_int_clear(opt);
8673 return cmp;
8676 /* Given two sets set1 and set2, check whether
8677 * for any common value of the parameters and dimensions preceding pos
8678 * there is a value of dimension pos in set1 that is larger
8679 * than a value of the same dimension in set2.
8681 * Return
8682 * 1 if there exists such a pair
8683 * 0 if there is no such pair, but there is a pair of equal values
8684 * -1 otherwise
8685 * -2 if some error occurred.
8687 int isl_set_follows_at(__isl_keep isl_set *set1,
8688 __isl_keep isl_set *set2, int pos)
8690 int i, j;
8691 int follows = -1;
8693 if (!set1 || !set2)
8694 return -2;
8696 for (i = 0; i < set1->n; ++i)
8697 for (j = 0; j < set2->n; ++j) {
8698 int f;
8699 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8700 if (f == 1 || f == -2)
8701 return f;
8702 if (f > follows)
8703 follows = f;
8706 return follows;
8709 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8710 unsigned pos, isl_int *val)
8712 int i;
8713 int d;
8714 unsigned total;
8716 if (!bmap)
8717 return -1;
8718 total = isl_basic_map_total_dim(bmap);
8719 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8720 for (; d+1 > pos; --d)
8721 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8722 break;
8723 if (d != pos)
8724 continue;
8725 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8726 return 0;
8727 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8728 return 0;
8729 if (!isl_int_is_one(bmap->eq[i][1+d]))
8730 return 0;
8731 if (val)
8732 isl_int_neg(*val, bmap->eq[i][0]);
8733 return 1;
8735 return 0;
8738 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8739 unsigned pos, isl_int *val)
8741 int i;
8742 isl_int v;
8743 isl_int tmp;
8744 int fixed;
8746 if (!map)
8747 return -1;
8748 if (map->n == 0)
8749 return 0;
8750 if (map->n == 1)
8751 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8752 isl_int_init(v);
8753 isl_int_init(tmp);
8754 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8755 for (i = 1; fixed == 1 && i < map->n; ++i) {
8756 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8757 if (fixed == 1 && isl_int_ne(tmp, v))
8758 fixed = 0;
8760 if (val)
8761 isl_int_set(*val, v);
8762 isl_int_clear(tmp);
8763 isl_int_clear(v);
8764 return fixed;
8767 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8768 unsigned pos, isl_int *val)
8770 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8771 pos, val);
8774 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8775 isl_int *val)
8777 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8780 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8781 enum isl_dim_type type, unsigned pos, isl_int *val)
8783 if (pos >= isl_basic_map_dim(bmap, type))
8784 return -1;
8785 return isl_basic_map_plain_has_fixed_var(bmap,
8786 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8789 /* If "bmap" obviously lies on a hyperplane where the given dimension
8790 * has a fixed value, then return that value.
8791 * Otherwise return NaN.
8793 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8794 __isl_keep isl_basic_map *bmap,
8795 enum isl_dim_type type, unsigned pos)
8797 isl_ctx *ctx;
8798 isl_val *v;
8799 int fixed;
8801 if (!bmap)
8802 return NULL;
8803 ctx = isl_basic_map_get_ctx(bmap);
8804 v = isl_val_alloc(ctx);
8805 if (!v)
8806 return NULL;
8807 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8808 if (fixed < 0)
8809 return isl_val_free(v);
8810 if (fixed) {
8811 isl_int_set_si(v->d, 1);
8812 return v;
8814 isl_val_free(v);
8815 return isl_val_nan(ctx);
8818 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8819 enum isl_dim_type type, unsigned pos, isl_int *val)
8821 if (pos >= isl_map_dim(map, type))
8822 return -1;
8823 return isl_map_plain_has_fixed_var(map,
8824 map_offset(map, type) - 1 + pos, val);
8827 /* If "map" obviously lies on a hyperplane where the given dimension
8828 * has a fixed value, then return that value.
8829 * Otherwise return NaN.
8831 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8832 enum isl_dim_type type, unsigned pos)
8834 isl_ctx *ctx;
8835 isl_val *v;
8836 int fixed;
8838 if (!map)
8839 return NULL;
8840 ctx = isl_map_get_ctx(map);
8841 v = isl_val_alloc(ctx);
8842 if (!v)
8843 return NULL;
8844 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8845 if (fixed < 0)
8846 return isl_val_free(v);
8847 if (fixed) {
8848 isl_int_set_si(v->d, 1);
8849 return v;
8851 isl_val_free(v);
8852 return isl_val_nan(ctx);
8855 /* If "set" obviously lies on a hyperplane where the given dimension
8856 * has a fixed value, then return that value.
8857 * Otherwise return NaN.
8859 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8860 enum isl_dim_type type, unsigned pos)
8862 return isl_map_plain_get_val_if_fixed(set, type, pos);
8865 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8866 enum isl_dim_type type, unsigned pos, isl_int *val)
8868 return isl_map_plain_is_fixed(set, type, pos, val);
8871 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8872 * then return this fixed value in *val.
8874 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8875 unsigned dim, isl_int *val)
8877 return isl_basic_set_plain_has_fixed_var(bset,
8878 isl_basic_set_n_param(bset) + dim, val);
8881 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8882 * then return this fixed value in *val.
8884 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8885 unsigned dim, isl_int *val)
8887 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8890 /* Check if input variable in has fixed value and if so and if val is not NULL,
8891 * then return this fixed value in *val.
8893 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8894 unsigned in, isl_int *val)
8896 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8899 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8900 * and if val is not NULL, then return this lower bound in *val.
8902 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8903 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8905 int i, i_eq = -1, i_ineq = -1;
8906 isl_int *c;
8907 unsigned total;
8908 unsigned nparam;
8910 if (!bset)
8911 return -1;
8912 total = isl_basic_set_total_dim(bset);
8913 nparam = isl_basic_set_n_param(bset);
8914 for (i = 0; i < bset->n_eq; ++i) {
8915 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8916 continue;
8917 if (i_eq != -1)
8918 return 0;
8919 i_eq = i;
8921 for (i = 0; i < bset->n_ineq; ++i) {
8922 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8923 continue;
8924 if (i_eq != -1 || i_ineq != -1)
8925 return 0;
8926 i_ineq = i;
8928 if (i_eq == -1 && i_ineq == -1)
8929 return 0;
8930 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8931 /* The coefficient should always be one due to normalization. */
8932 if (!isl_int_is_one(c[1+nparam+dim]))
8933 return 0;
8934 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8935 return 0;
8936 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8937 total - nparam - dim - 1) != -1)
8938 return 0;
8939 if (val)
8940 isl_int_neg(*val, c[0]);
8941 return 1;
8944 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8945 unsigned dim, isl_int *val)
8947 int i;
8948 isl_int v;
8949 isl_int tmp;
8950 int fixed;
8952 if (!set)
8953 return -1;
8954 if (set->n == 0)
8955 return 0;
8956 if (set->n == 1)
8957 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8958 dim, val);
8959 isl_int_init(v);
8960 isl_int_init(tmp);
8961 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8962 dim, &v);
8963 for (i = 1; fixed == 1 && i < set->n; ++i) {
8964 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8965 dim, &tmp);
8966 if (fixed == 1 && isl_int_ne(tmp, v))
8967 fixed = 0;
8969 if (val)
8970 isl_int_set(*val, v);
8971 isl_int_clear(tmp);
8972 isl_int_clear(v);
8973 return fixed;
8976 /* Return -1 if the constraint "c1" should be sorted before "c2"
8977 * and 1 if it should be sorted after "c2".
8978 * Return 0 if the two constraints are the same (up to the constant term).
8980 * In particular, if a constraint involves later variables than another
8981 * then it is sorted after this other constraint.
8982 * uset_gist depends on constraints without existentially quantified
8983 * variables sorting first.
8985 * For constraints that have the same latest variable, those
8986 * with the same coefficient for this latest variable (first in absolute value
8987 * and then in actual value) are grouped together.
8988 * This is useful for detecting pairs of constraints that can
8989 * be chained in their printed representation.
8991 * Finally, within a group, constraints are sorted according to
8992 * their coefficients (excluding the constant term).
8994 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8996 isl_int **c1 = (isl_int **) p1;
8997 isl_int **c2 = (isl_int **) p2;
8998 int l1, l2;
8999 unsigned size = *(unsigned *) arg;
9000 int cmp;
9002 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9003 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9005 if (l1 != l2)
9006 return l1 - l2;
9008 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9009 if (cmp != 0)
9010 return cmp;
9011 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9012 if (cmp != 0)
9013 return -cmp;
9015 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9018 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9019 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9020 * and 0 if the two constraints are the same (up to the constant term).
9022 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9023 isl_int *c1, isl_int *c2)
9025 unsigned total;
9027 if (!bmap)
9028 return -2;
9029 total = isl_basic_map_total_dim(bmap);
9030 return sort_constraint_cmp(&c1, &c2, &total);
9033 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9034 __isl_take isl_basic_map *bmap)
9036 unsigned total;
9038 if (!bmap)
9039 return NULL;
9040 if (bmap->n_ineq == 0)
9041 return bmap;
9042 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9043 return bmap;
9044 total = isl_basic_map_total_dim(bmap);
9045 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9046 &sort_constraint_cmp, &total) < 0)
9047 return isl_basic_map_free(bmap);
9048 return bmap;
9051 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9052 __isl_take isl_basic_set *bset)
9054 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9055 (struct isl_basic_map *)bset);
9058 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9060 if (!bmap)
9061 return NULL;
9062 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9063 return bmap;
9064 bmap = isl_basic_map_remove_redundancies(bmap);
9065 bmap = isl_basic_map_sort_constraints(bmap);
9066 if (bmap)
9067 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9068 return bmap;
9071 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9073 return (struct isl_basic_set *)isl_basic_map_normalize(
9074 (struct isl_basic_map *)bset);
9077 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9078 const __isl_keep isl_basic_map *bmap2)
9080 int i, cmp;
9081 unsigned total;
9083 if (!bmap1 || !bmap2)
9084 return -1;
9086 if (bmap1 == bmap2)
9087 return 0;
9088 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9089 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9090 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9091 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9092 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9093 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9094 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9095 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9096 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9097 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9098 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9099 return 0;
9100 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9101 return 1;
9102 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9103 return -1;
9104 if (bmap1->n_eq != bmap2->n_eq)
9105 return bmap1->n_eq - bmap2->n_eq;
9106 if (bmap1->n_ineq != bmap2->n_ineq)
9107 return bmap1->n_ineq - bmap2->n_ineq;
9108 if (bmap1->n_div != bmap2->n_div)
9109 return bmap1->n_div - bmap2->n_div;
9110 total = isl_basic_map_total_dim(bmap1);
9111 for (i = 0; i < bmap1->n_eq; ++i) {
9112 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9113 if (cmp)
9114 return cmp;
9116 for (i = 0; i < bmap1->n_ineq; ++i) {
9117 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9118 if (cmp)
9119 return cmp;
9121 for (i = 0; i < bmap1->n_div; ++i) {
9122 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9123 if (cmp)
9124 return cmp;
9126 return 0;
9129 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9130 const __isl_keep isl_basic_set *bset2)
9132 return isl_basic_map_plain_cmp(bset1, bset2);
9135 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9137 int i, cmp;
9139 if (set1 == set2)
9140 return 0;
9141 if (set1->n != set2->n)
9142 return set1->n - set2->n;
9144 for (i = 0; i < set1->n; ++i) {
9145 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9146 if (cmp)
9147 return cmp;
9150 return 0;
9153 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9154 __isl_keep isl_basic_map *bmap2)
9156 if (!bmap1 || !bmap2)
9157 return isl_bool_error;
9158 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9161 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9162 __isl_keep isl_basic_set *bset2)
9164 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9165 (isl_basic_map *)bset2);
9168 static int qsort_bmap_cmp(const void *p1, const void *p2)
9170 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9171 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9173 return isl_basic_map_plain_cmp(bmap1, bmap2);
9176 /* Sort the basic maps of "map" and remove duplicate basic maps.
9178 * While removing basic maps, we make sure that the basic maps remain
9179 * sorted because isl_map_normalize expects the basic maps of the result
9180 * to be sorted.
9182 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9184 int i, j;
9186 map = isl_map_remove_empty_parts(map);
9187 if (!map)
9188 return NULL;
9189 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9190 for (i = map->n - 1; i >= 1; --i) {
9191 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9192 continue;
9193 isl_basic_map_free(map->p[i-1]);
9194 for (j = i; j < map->n; ++j)
9195 map->p[j - 1] = map->p[j];
9196 map->n--;
9199 return map;
9202 /* Remove obvious duplicates among the basic maps of "map".
9204 * Unlike isl_map_normalize, this function does not remove redundant
9205 * constraints and only removes duplicates that have exactly the same
9206 * constraints in the input. It does sort the constraints and
9207 * the basic maps to ease the detection of duplicates.
9209 * If "map" has already been normalized or if the basic maps are
9210 * disjoint, then there can be no duplicates.
9212 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9214 int i;
9215 isl_basic_map *bmap;
9217 if (!map)
9218 return NULL;
9219 if (map->n <= 1)
9220 return map;
9221 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9222 return map;
9223 for (i = 0; i < map->n; ++i) {
9224 bmap = isl_basic_map_copy(map->p[i]);
9225 bmap = isl_basic_map_sort_constraints(bmap);
9226 if (!bmap)
9227 return isl_map_free(map);
9228 isl_basic_map_free(map->p[i]);
9229 map->p[i] = bmap;
9232 map = sort_and_remove_duplicates(map);
9233 return map;
9236 /* We normalize in place, but if anything goes wrong we need
9237 * to return NULL, so we need to make sure we don't change the
9238 * meaning of any possible other copies of map.
9240 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9242 int i;
9243 struct isl_basic_map *bmap;
9245 if (!map)
9246 return NULL;
9247 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9248 return map;
9249 for (i = 0; i < map->n; ++i) {
9250 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9251 if (!bmap)
9252 goto error;
9253 isl_basic_map_free(map->p[i]);
9254 map->p[i] = bmap;
9257 map = sort_and_remove_duplicates(map);
9258 if (map)
9259 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9260 return map;
9261 error:
9262 isl_map_free(map);
9263 return NULL;
9266 struct isl_set *isl_set_normalize(struct isl_set *set)
9268 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9271 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9272 __isl_keep isl_map *map2)
9274 int i;
9275 isl_bool equal;
9277 if (!map1 || !map2)
9278 return isl_bool_error;
9280 if (map1 == map2)
9281 return isl_bool_true;
9282 if (!isl_space_is_equal(map1->dim, map2->dim))
9283 return isl_bool_false;
9285 map1 = isl_map_copy(map1);
9286 map2 = isl_map_copy(map2);
9287 map1 = isl_map_normalize(map1);
9288 map2 = isl_map_normalize(map2);
9289 if (!map1 || !map2)
9290 goto error;
9291 equal = map1->n == map2->n;
9292 for (i = 0; equal && i < map1->n; ++i) {
9293 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9294 if (equal < 0)
9295 goto error;
9297 isl_map_free(map1);
9298 isl_map_free(map2);
9299 return equal;
9300 error:
9301 isl_map_free(map1);
9302 isl_map_free(map2);
9303 return isl_bool_error;
9306 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9307 __isl_keep isl_set *set2)
9309 return isl_map_plain_is_equal((struct isl_map *)set1,
9310 (struct isl_map *)set2);
9313 /* Return an interval that ranges from min to max (inclusive)
9315 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9316 isl_int min, isl_int max)
9318 int k;
9319 struct isl_basic_set *bset = NULL;
9321 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9322 if (!bset)
9323 goto error;
9325 k = isl_basic_set_alloc_inequality(bset);
9326 if (k < 0)
9327 goto error;
9328 isl_int_set_si(bset->ineq[k][1], 1);
9329 isl_int_neg(bset->ineq[k][0], min);
9331 k = isl_basic_set_alloc_inequality(bset);
9332 if (k < 0)
9333 goto error;
9334 isl_int_set_si(bset->ineq[k][1], -1);
9335 isl_int_set(bset->ineq[k][0], max);
9337 return bset;
9338 error:
9339 isl_basic_set_free(bset);
9340 return NULL;
9343 /* Return the basic maps in "map" as a list.
9345 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9346 __isl_keep isl_map *map)
9348 int i;
9349 isl_ctx *ctx;
9350 isl_basic_map_list *list;
9352 if (!map)
9353 return NULL;
9354 ctx = isl_map_get_ctx(map);
9355 list = isl_basic_map_list_alloc(ctx, map->n);
9357 for (i = 0; i < map->n; ++i) {
9358 isl_basic_map *bmap;
9360 bmap = isl_basic_map_copy(map->p[i]);
9361 list = isl_basic_map_list_add(list, bmap);
9364 return list;
9367 /* Return the intersection of the elements in the non-empty list "list".
9368 * All elements are assumed to live in the same space.
9370 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9371 __isl_take isl_basic_map_list *list)
9373 int i, n;
9374 isl_basic_map *bmap;
9376 if (!list)
9377 return NULL;
9378 n = isl_basic_map_list_n_basic_map(list);
9379 if (n < 1)
9380 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9381 "expecting non-empty list", goto error);
9383 bmap = isl_basic_map_list_get_basic_map(list, 0);
9384 for (i = 1; i < n; ++i) {
9385 isl_basic_map *bmap_i;
9387 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9388 bmap = isl_basic_map_intersect(bmap, bmap_i);
9391 isl_basic_map_list_free(list);
9392 return bmap;
9393 error:
9394 isl_basic_map_list_free(list);
9395 return NULL;
9398 /* Return the intersection of the elements in the non-empty list "list".
9399 * All elements are assumed to live in the same space.
9401 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9402 __isl_take isl_basic_set_list *list)
9404 return isl_basic_map_list_intersect(list);
9407 /* Return the union of the elements in the non-empty list "list".
9408 * All elements are assumed to live in the same space.
9410 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9412 int i, n;
9413 isl_set *set;
9415 if (!list)
9416 return NULL;
9417 n = isl_set_list_n_set(list);
9418 if (n < 1)
9419 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9420 "expecting non-empty list", goto error);
9422 set = isl_set_list_get_set(list, 0);
9423 for (i = 1; i < n; ++i) {
9424 isl_set *set_i;
9426 set_i = isl_set_list_get_set(list, i);
9427 set = isl_set_union(set, set_i);
9430 isl_set_list_free(list);
9431 return set;
9432 error:
9433 isl_set_list_free(list);
9434 return NULL;
9437 /* Return the Cartesian product of the basic sets in list (in the given order).
9439 __isl_give isl_basic_set *isl_basic_set_list_product(
9440 __isl_take struct isl_basic_set_list *list)
9442 int i;
9443 unsigned dim;
9444 unsigned nparam;
9445 unsigned extra;
9446 unsigned n_eq;
9447 unsigned n_ineq;
9448 struct isl_basic_set *product = NULL;
9450 if (!list)
9451 goto error;
9452 isl_assert(list->ctx, list->n > 0, goto error);
9453 isl_assert(list->ctx, list->p[0], goto error);
9454 nparam = isl_basic_set_n_param(list->p[0]);
9455 dim = isl_basic_set_n_dim(list->p[0]);
9456 extra = list->p[0]->n_div;
9457 n_eq = list->p[0]->n_eq;
9458 n_ineq = list->p[0]->n_ineq;
9459 for (i = 1; i < list->n; ++i) {
9460 isl_assert(list->ctx, list->p[i], goto error);
9461 isl_assert(list->ctx,
9462 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9463 dim += isl_basic_set_n_dim(list->p[i]);
9464 extra += list->p[i]->n_div;
9465 n_eq += list->p[i]->n_eq;
9466 n_ineq += list->p[i]->n_ineq;
9468 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9469 n_eq, n_ineq);
9470 if (!product)
9471 goto error;
9472 dim = 0;
9473 for (i = 0; i < list->n; ++i) {
9474 isl_basic_set_add_constraints(product,
9475 isl_basic_set_copy(list->p[i]), dim);
9476 dim += isl_basic_set_n_dim(list->p[i]);
9478 isl_basic_set_list_free(list);
9479 return product;
9480 error:
9481 isl_basic_set_free(product);
9482 isl_basic_set_list_free(list);
9483 return NULL;
9486 struct isl_basic_map *isl_basic_map_product(
9487 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9489 isl_space *dim_result = NULL;
9490 struct isl_basic_map *bmap;
9491 unsigned in1, in2, out1, out2, nparam, total, pos;
9492 struct isl_dim_map *dim_map1, *dim_map2;
9494 if (!bmap1 || !bmap2)
9495 goto error;
9497 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9498 bmap2->dim, isl_dim_param), goto error);
9499 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9500 isl_space_copy(bmap2->dim));
9502 in1 = isl_basic_map_n_in(bmap1);
9503 in2 = isl_basic_map_n_in(bmap2);
9504 out1 = isl_basic_map_n_out(bmap1);
9505 out2 = isl_basic_map_n_out(bmap2);
9506 nparam = isl_basic_map_n_param(bmap1);
9508 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9509 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9510 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9511 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9512 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9513 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9514 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9515 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9516 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9517 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9518 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9520 bmap = isl_basic_map_alloc_space(dim_result,
9521 bmap1->n_div + bmap2->n_div,
9522 bmap1->n_eq + bmap2->n_eq,
9523 bmap1->n_ineq + bmap2->n_ineq);
9524 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9525 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9526 bmap = isl_basic_map_simplify(bmap);
9527 return isl_basic_map_finalize(bmap);
9528 error:
9529 isl_basic_map_free(bmap1);
9530 isl_basic_map_free(bmap2);
9531 return NULL;
9534 __isl_give isl_basic_map *isl_basic_map_flat_product(
9535 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9537 isl_basic_map *prod;
9539 prod = isl_basic_map_product(bmap1, bmap2);
9540 prod = isl_basic_map_flatten(prod);
9541 return prod;
9544 __isl_give isl_basic_set *isl_basic_set_flat_product(
9545 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9547 return isl_basic_map_flat_range_product(bset1, bset2);
9550 __isl_give isl_basic_map *isl_basic_map_domain_product(
9551 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9553 isl_space *space_result = NULL;
9554 isl_basic_map *bmap;
9555 unsigned in1, in2, out, nparam, total, pos;
9556 struct isl_dim_map *dim_map1, *dim_map2;
9558 if (!bmap1 || !bmap2)
9559 goto error;
9561 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9562 isl_space_copy(bmap2->dim));
9564 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9565 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9566 out = isl_basic_map_dim(bmap1, isl_dim_out);
9567 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9569 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9570 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9571 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9572 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9573 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9574 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9575 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9576 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9577 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9578 isl_dim_map_div(dim_map1, bmap1, pos += out);
9579 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9581 bmap = isl_basic_map_alloc_space(space_result,
9582 bmap1->n_div + bmap2->n_div,
9583 bmap1->n_eq + bmap2->n_eq,
9584 bmap1->n_ineq + bmap2->n_ineq);
9585 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9586 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9587 bmap = isl_basic_map_simplify(bmap);
9588 return isl_basic_map_finalize(bmap);
9589 error:
9590 isl_basic_map_free(bmap1);
9591 isl_basic_map_free(bmap2);
9592 return NULL;
9595 __isl_give isl_basic_map *isl_basic_map_range_product(
9596 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9598 isl_space *dim_result = NULL;
9599 isl_basic_map *bmap;
9600 unsigned in, out1, out2, nparam, total, pos;
9601 struct isl_dim_map *dim_map1, *dim_map2;
9603 if (!bmap1 || !bmap2)
9604 goto error;
9606 if (!isl_space_match(bmap1->dim, isl_dim_param,
9607 bmap2->dim, isl_dim_param))
9608 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9609 "parameters don't match", goto error);
9611 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9612 isl_space_copy(bmap2->dim));
9614 in = isl_basic_map_dim(bmap1, isl_dim_in);
9615 out1 = isl_basic_map_n_out(bmap1);
9616 out2 = isl_basic_map_n_out(bmap2);
9617 nparam = isl_basic_map_n_param(bmap1);
9619 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9620 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9621 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9622 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9623 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9624 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9625 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9626 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9627 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9628 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9629 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9631 bmap = isl_basic_map_alloc_space(dim_result,
9632 bmap1->n_div + bmap2->n_div,
9633 bmap1->n_eq + bmap2->n_eq,
9634 bmap1->n_ineq + bmap2->n_ineq);
9635 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9636 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9637 bmap = isl_basic_map_simplify(bmap);
9638 return isl_basic_map_finalize(bmap);
9639 error:
9640 isl_basic_map_free(bmap1);
9641 isl_basic_map_free(bmap2);
9642 return NULL;
9645 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9646 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9648 isl_basic_map *prod;
9650 prod = isl_basic_map_range_product(bmap1, bmap2);
9651 prod = isl_basic_map_flatten_range(prod);
9652 return prod;
9655 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9656 * and collect the results.
9657 * The result live in the space obtained by calling "space_product"
9658 * on the spaces of "map1" and "map2".
9659 * If "remove_duplicates" is set then the result may contain duplicates
9660 * (even if the inputs do not) and so we try and remove the obvious
9661 * duplicates.
9663 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9664 __isl_take isl_map *map2,
9665 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9666 __isl_take isl_space *right),
9667 __isl_give isl_basic_map *(*basic_map_product)(
9668 __isl_take isl_basic_map *left,
9669 __isl_take isl_basic_map *right),
9670 int remove_duplicates)
9672 unsigned flags = 0;
9673 struct isl_map *result;
9674 int i, j;
9676 if (!map1 || !map2)
9677 goto error;
9679 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9680 map2->dim, isl_dim_param), goto error);
9682 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9683 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9684 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9686 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9687 isl_space_copy(map2->dim)),
9688 map1->n * map2->n, flags);
9689 if (!result)
9690 goto error;
9691 for (i = 0; i < map1->n; ++i)
9692 for (j = 0; j < map2->n; ++j) {
9693 struct isl_basic_map *part;
9694 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9695 isl_basic_map_copy(map2->p[j]));
9696 if (isl_basic_map_is_empty(part))
9697 isl_basic_map_free(part);
9698 else
9699 result = isl_map_add_basic_map(result, part);
9700 if (!result)
9701 goto error;
9703 if (remove_duplicates)
9704 result = isl_map_remove_obvious_duplicates(result);
9705 isl_map_free(map1);
9706 isl_map_free(map2);
9707 return result;
9708 error:
9709 isl_map_free(map1);
9710 isl_map_free(map2);
9711 return NULL;
9714 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9716 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9717 __isl_take isl_map *map2)
9719 return map_product(map1, map2, &isl_space_product,
9720 &isl_basic_map_product, 0);
9723 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9724 __isl_take isl_map *map2)
9726 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9729 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9731 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9732 __isl_take isl_map *map2)
9734 isl_map *prod;
9736 prod = isl_map_product(map1, map2);
9737 prod = isl_map_flatten(prod);
9738 return prod;
9741 /* Given two set A and B, construct its Cartesian product A x B.
9743 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9745 return isl_map_range_product(set1, set2);
9748 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9749 __isl_take isl_set *set2)
9751 return isl_map_flat_range_product(set1, set2);
9754 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9756 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9757 __isl_take isl_map *map2)
9759 return map_product(map1, map2, &isl_space_domain_product,
9760 &isl_basic_map_domain_product, 1);
9763 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9765 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9766 __isl_take isl_map *map2)
9768 return map_product(map1, map2, &isl_space_range_product,
9769 &isl_basic_map_range_product, 1);
9772 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9773 __isl_take isl_map *map2)
9775 return isl_map_align_params_map_map_and(map1, map2,
9776 &map_domain_product_aligned);
9779 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9780 __isl_take isl_map *map2)
9782 return isl_map_align_params_map_map_and(map1, map2,
9783 &map_range_product_aligned);
9786 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9788 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9790 isl_space *space;
9791 int total1, keep1, total2, keep2;
9793 if (!map)
9794 return NULL;
9795 if (!isl_space_domain_is_wrapping(map->dim) ||
9796 !isl_space_range_is_wrapping(map->dim))
9797 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9798 "not a product", return isl_map_free(map));
9800 space = isl_map_get_space(map);
9801 total1 = isl_space_dim(space, isl_dim_in);
9802 total2 = isl_space_dim(space, isl_dim_out);
9803 space = isl_space_factor_domain(space);
9804 keep1 = isl_space_dim(space, isl_dim_in);
9805 keep2 = isl_space_dim(space, isl_dim_out);
9806 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9807 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9808 map = isl_map_reset_space(map, space);
9810 return map;
9813 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9815 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9817 isl_space *space;
9818 int total1, keep1, total2, keep2;
9820 if (!map)
9821 return NULL;
9822 if (!isl_space_domain_is_wrapping(map->dim) ||
9823 !isl_space_range_is_wrapping(map->dim))
9824 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9825 "not a product", return isl_map_free(map));
9827 space = isl_map_get_space(map);
9828 total1 = isl_space_dim(space, isl_dim_in);
9829 total2 = isl_space_dim(space, isl_dim_out);
9830 space = isl_space_factor_range(space);
9831 keep1 = isl_space_dim(space, isl_dim_in);
9832 keep2 = isl_space_dim(space, isl_dim_out);
9833 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9834 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9835 map = isl_map_reset_space(map, space);
9837 return map;
9840 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9842 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9844 isl_space *space;
9845 int total, keep;
9847 if (!map)
9848 return NULL;
9849 if (!isl_space_domain_is_wrapping(map->dim))
9850 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9851 "domain is not a product", return isl_map_free(map));
9853 space = isl_map_get_space(map);
9854 total = isl_space_dim(space, isl_dim_in);
9855 space = isl_space_domain_factor_domain(space);
9856 keep = isl_space_dim(space, isl_dim_in);
9857 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9858 map = isl_map_reset_space(map, space);
9860 return map;
9863 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9865 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9867 isl_space *space;
9868 int total, keep;
9870 if (!map)
9871 return NULL;
9872 if (!isl_space_domain_is_wrapping(map->dim))
9873 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9874 "domain is not a product", return isl_map_free(map));
9876 space = isl_map_get_space(map);
9877 total = isl_space_dim(space, isl_dim_in);
9878 space = isl_space_domain_factor_range(space);
9879 keep = isl_space_dim(space, isl_dim_in);
9880 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9881 map = isl_map_reset_space(map, space);
9883 return map;
9886 /* Given a map A -> [B -> C], extract the map A -> B.
9888 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9890 isl_space *space;
9891 int total, keep;
9893 if (!map)
9894 return NULL;
9895 if (!isl_space_range_is_wrapping(map->dim))
9896 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9897 "range is not a product", return isl_map_free(map));
9899 space = isl_map_get_space(map);
9900 total = isl_space_dim(space, isl_dim_out);
9901 space = isl_space_range_factor_domain(space);
9902 keep = isl_space_dim(space, isl_dim_out);
9903 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9904 map = isl_map_reset_space(map, space);
9906 return map;
9909 /* Given a map A -> [B -> C], extract the map A -> C.
9911 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9913 isl_space *space;
9914 int total, keep;
9916 if (!map)
9917 return NULL;
9918 if (!isl_space_range_is_wrapping(map->dim))
9919 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9920 "range is not a product", return isl_map_free(map));
9922 space = isl_map_get_space(map);
9923 total = isl_space_dim(space, isl_dim_out);
9924 space = isl_space_range_factor_range(space);
9925 keep = isl_space_dim(space, isl_dim_out);
9926 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9927 map = isl_map_reset_space(map, space);
9929 return map;
9932 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9934 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9935 __isl_take isl_map *map2)
9937 isl_map *prod;
9939 prod = isl_map_domain_product(map1, map2);
9940 prod = isl_map_flatten_domain(prod);
9941 return prod;
9944 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9946 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9947 __isl_take isl_map *map2)
9949 isl_map *prod;
9951 prod = isl_map_range_product(map1, map2);
9952 prod = isl_map_flatten_range(prod);
9953 return prod;
9956 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9958 int i;
9959 uint32_t hash = isl_hash_init();
9960 unsigned total;
9962 if (!bmap)
9963 return 0;
9964 bmap = isl_basic_map_copy(bmap);
9965 bmap = isl_basic_map_normalize(bmap);
9966 if (!bmap)
9967 return 0;
9968 total = isl_basic_map_total_dim(bmap);
9969 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9970 for (i = 0; i < bmap->n_eq; ++i) {
9971 uint32_t c_hash;
9972 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9973 isl_hash_hash(hash, c_hash);
9975 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9976 for (i = 0; i < bmap->n_ineq; ++i) {
9977 uint32_t c_hash;
9978 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9979 isl_hash_hash(hash, c_hash);
9981 isl_hash_byte(hash, bmap->n_div & 0xFF);
9982 for (i = 0; i < bmap->n_div; ++i) {
9983 uint32_t c_hash;
9984 if (isl_int_is_zero(bmap->div[i][0]))
9985 continue;
9986 isl_hash_byte(hash, i & 0xFF);
9987 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9988 isl_hash_hash(hash, c_hash);
9990 isl_basic_map_free(bmap);
9991 return hash;
9994 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9996 return isl_basic_map_get_hash((isl_basic_map *)bset);
9999 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10001 int i;
10002 uint32_t hash;
10004 if (!map)
10005 return 0;
10006 map = isl_map_copy(map);
10007 map = isl_map_normalize(map);
10008 if (!map)
10009 return 0;
10011 hash = isl_hash_init();
10012 for (i = 0; i < map->n; ++i) {
10013 uint32_t bmap_hash;
10014 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10015 isl_hash_hash(hash, bmap_hash);
10018 isl_map_free(map);
10020 return hash;
10023 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10025 return isl_map_get_hash((isl_map *)set);
10028 /* Check if the value for dimension dim is completely determined
10029 * by the values of the other parameters and variables.
10030 * That is, check if dimension dim is involved in an equality.
10032 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10034 int i;
10035 unsigned nparam;
10037 if (!bset)
10038 return -1;
10039 nparam = isl_basic_set_n_param(bset);
10040 for (i = 0; i < bset->n_eq; ++i)
10041 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10042 return 1;
10043 return 0;
10046 /* Check if the value for dimension dim is completely determined
10047 * by the values of the other parameters and variables.
10048 * That is, check if dimension dim is involved in an equality
10049 * for each of the subsets.
10051 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10053 int i;
10055 if (!set)
10056 return -1;
10057 for (i = 0; i < set->n; ++i) {
10058 int unique;
10059 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10060 if (unique != 1)
10061 return unique;
10063 return 1;
10066 /* Return the number of basic maps in the (current) representation of "map".
10068 int isl_map_n_basic_map(__isl_keep isl_map *map)
10070 return map ? map->n : 0;
10073 int isl_set_n_basic_set(__isl_keep isl_set *set)
10075 return set ? set->n : 0;
10078 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10079 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10081 int i;
10083 if (!map)
10084 return isl_stat_error;
10086 for (i = 0; i < map->n; ++i)
10087 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10088 return isl_stat_error;
10090 return isl_stat_ok;
10093 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10094 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10096 int i;
10098 if (!set)
10099 return isl_stat_error;
10101 for (i = 0; i < set->n; ++i)
10102 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10103 return isl_stat_error;
10105 return isl_stat_ok;
10108 /* Return a list of basic sets, the union of which is equal to "set".
10110 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10111 __isl_keep isl_set *set)
10113 int i;
10114 isl_basic_set_list *list;
10116 if (!set)
10117 return NULL;
10119 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10120 for (i = 0; i < set->n; ++i) {
10121 isl_basic_set *bset;
10123 bset = isl_basic_set_copy(set->p[i]);
10124 list = isl_basic_set_list_add(list, bset);
10127 return list;
10130 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10132 isl_space *dim;
10134 if (!bset)
10135 return NULL;
10137 bset = isl_basic_set_cow(bset);
10138 if (!bset)
10139 return NULL;
10141 dim = isl_basic_set_get_space(bset);
10142 dim = isl_space_lift(dim, bset->n_div);
10143 if (!dim)
10144 goto error;
10145 isl_space_free(bset->dim);
10146 bset->dim = dim;
10147 bset->extra -= bset->n_div;
10148 bset->n_div = 0;
10150 bset = isl_basic_set_finalize(bset);
10152 return bset;
10153 error:
10154 isl_basic_set_free(bset);
10155 return NULL;
10158 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10160 int i;
10161 isl_space *dim;
10162 unsigned n_div;
10164 set = isl_set_align_divs(set);
10166 if (!set)
10167 return NULL;
10169 set = isl_set_cow(set);
10170 if (!set)
10171 return NULL;
10173 n_div = set->p[0]->n_div;
10174 dim = isl_set_get_space(set);
10175 dim = isl_space_lift(dim, n_div);
10176 if (!dim)
10177 goto error;
10178 isl_space_free(set->dim);
10179 set->dim = dim;
10181 for (i = 0; i < set->n; ++i) {
10182 set->p[i] = isl_basic_set_lift(set->p[i]);
10183 if (!set->p[i])
10184 goto error;
10187 return set;
10188 error:
10189 isl_set_free(set);
10190 return NULL;
10193 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10195 isl_space *dim;
10196 struct isl_basic_map *bmap;
10197 unsigned n_set;
10198 unsigned n_div;
10199 unsigned n_param;
10200 unsigned total;
10201 int i, k, l;
10203 set = isl_set_align_divs(set);
10205 if (!set)
10206 return NULL;
10208 dim = isl_set_get_space(set);
10209 if (set->n == 0 || set->p[0]->n_div == 0) {
10210 isl_set_free(set);
10211 return isl_map_identity(isl_space_map_from_set(dim));
10214 n_div = set->p[0]->n_div;
10215 dim = isl_space_map_from_set(dim);
10216 n_param = isl_space_dim(dim, isl_dim_param);
10217 n_set = isl_space_dim(dim, isl_dim_in);
10218 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10219 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10220 for (i = 0; i < n_set; ++i)
10221 bmap = var_equal(bmap, i);
10223 total = n_param + n_set + n_set + n_div;
10224 for (i = 0; i < n_div; ++i) {
10225 k = isl_basic_map_alloc_inequality(bmap);
10226 if (k < 0)
10227 goto error;
10228 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10229 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10230 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10231 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10232 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10233 set->p[0]->div[i][0]);
10235 l = isl_basic_map_alloc_inequality(bmap);
10236 if (l < 0)
10237 goto error;
10238 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10239 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10240 set->p[0]->div[i][0]);
10241 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10244 isl_set_free(set);
10245 bmap = isl_basic_map_simplify(bmap);
10246 bmap = isl_basic_map_finalize(bmap);
10247 return isl_map_from_basic_map(bmap);
10248 error:
10249 isl_set_free(set);
10250 isl_basic_map_free(bmap);
10251 return NULL;
10254 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10256 unsigned dim;
10257 int size = 0;
10259 if (!bset)
10260 return -1;
10262 dim = isl_basic_set_total_dim(bset);
10263 size += bset->n_eq * (1 + dim);
10264 size += bset->n_ineq * (1 + dim);
10265 size += bset->n_div * (2 + dim);
10267 return size;
10270 int isl_set_size(__isl_keep isl_set *set)
10272 int i;
10273 int size = 0;
10275 if (!set)
10276 return -1;
10278 for (i = 0; i < set->n; ++i)
10279 size += isl_basic_set_size(set->p[i]);
10281 return size;
10284 /* Check if there is any lower bound (if lower == 0) and/or upper
10285 * bound (if upper == 0) on the specified dim.
10287 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10288 enum isl_dim_type type, unsigned pos, int lower, int upper)
10290 int i;
10292 if (!bmap)
10293 return isl_bool_error;
10295 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10296 return isl_bool_error);
10298 pos += isl_basic_map_offset(bmap, type);
10300 for (i = 0; i < bmap->n_div; ++i) {
10301 if (isl_int_is_zero(bmap->div[i][0]))
10302 continue;
10303 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10304 return isl_bool_true;
10307 for (i = 0; i < bmap->n_eq; ++i)
10308 if (!isl_int_is_zero(bmap->eq[i][pos]))
10309 return isl_bool_true;
10311 for (i = 0; i < bmap->n_ineq; ++i) {
10312 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10313 if (sgn > 0)
10314 lower = 1;
10315 if (sgn < 0)
10316 upper = 1;
10319 return lower && upper;
10322 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10323 enum isl_dim_type type, unsigned pos)
10325 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10328 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10329 enum isl_dim_type type, unsigned pos)
10331 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10334 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10335 enum isl_dim_type type, unsigned pos)
10337 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10340 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10341 enum isl_dim_type type, unsigned pos)
10343 int i;
10345 if (!map)
10346 return -1;
10348 for (i = 0; i < map->n; ++i) {
10349 int bounded;
10350 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10351 if (bounded < 0 || !bounded)
10352 return bounded;
10355 return 1;
10358 /* Return 1 if the specified dim is involved in both an upper bound
10359 * and a lower bound.
10361 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10362 enum isl_dim_type type, unsigned pos)
10364 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10367 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10369 static isl_bool has_any_bound(__isl_keep isl_map *map,
10370 enum isl_dim_type type, unsigned pos,
10371 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10372 enum isl_dim_type type, unsigned pos))
10374 int i;
10376 if (!map)
10377 return isl_bool_error;
10379 for (i = 0; i < map->n; ++i) {
10380 isl_bool bounded;
10381 bounded = fn(map->p[i], type, pos);
10382 if (bounded < 0 || bounded)
10383 return bounded;
10386 return isl_bool_false;
10389 /* Return 1 if the specified dim is involved in any lower bound.
10391 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10392 enum isl_dim_type type, unsigned pos)
10394 return has_any_bound(set, type, pos,
10395 &isl_basic_map_dim_has_lower_bound);
10398 /* Return 1 if the specified dim is involved in any upper bound.
10400 isl_bool isl_set_dim_has_any_upper_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_upper_bound);
10407 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10409 static isl_bool has_bound(__isl_keep isl_map *map,
10410 enum isl_dim_type type, unsigned pos,
10411 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10412 enum isl_dim_type type, unsigned pos))
10414 int i;
10416 if (!map)
10417 return isl_bool_error;
10419 for (i = 0; i < map->n; ++i) {
10420 isl_bool bounded;
10421 bounded = fn(map->p[i], type, pos);
10422 if (bounded < 0 || !bounded)
10423 return bounded;
10426 return isl_bool_true;
10429 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10431 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10432 enum isl_dim_type type, unsigned pos)
10434 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10437 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10439 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10440 enum isl_dim_type type, unsigned pos)
10442 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10445 /* For each of the "n" variables starting at "first", determine
10446 * the sign of the variable and put the results in the first "n"
10447 * elements of the array "signs".
10448 * Sign
10449 * 1 means that the variable is non-negative
10450 * -1 means that the variable is non-positive
10451 * 0 means the variable attains both positive and negative values.
10453 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10454 unsigned first, unsigned n, int *signs)
10456 isl_vec *bound = NULL;
10457 struct isl_tab *tab = NULL;
10458 struct isl_tab_undo *snap;
10459 int i;
10461 if (!bset || !signs)
10462 return -1;
10464 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10465 tab = isl_tab_from_basic_set(bset, 0);
10466 if (!bound || !tab)
10467 goto error;
10469 isl_seq_clr(bound->el, bound->size);
10470 isl_int_set_si(bound->el[0], -1);
10472 snap = isl_tab_snap(tab);
10473 for (i = 0; i < n; ++i) {
10474 int empty;
10476 isl_int_set_si(bound->el[1 + first + i], -1);
10477 if (isl_tab_add_ineq(tab, bound->el) < 0)
10478 goto error;
10479 empty = tab->empty;
10480 isl_int_set_si(bound->el[1 + first + i], 0);
10481 if (isl_tab_rollback(tab, snap) < 0)
10482 goto error;
10484 if (empty) {
10485 signs[i] = 1;
10486 continue;
10489 isl_int_set_si(bound->el[1 + first + i], 1);
10490 if (isl_tab_add_ineq(tab, bound->el) < 0)
10491 goto error;
10492 empty = tab->empty;
10493 isl_int_set_si(bound->el[1 + first + i], 0);
10494 if (isl_tab_rollback(tab, snap) < 0)
10495 goto error;
10497 signs[i] = empty ? -1 : 0;
10500 isl_tab_free(tab);
10501 isl_vec_free(bound);
10502 return 0;
10503 error:
10504 isl_tab_free(tab);
10505 isl_vec_free(bound);
10506 return -1;
10509 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10510 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10512 if (!bset || !signs)
10513 return -1;
10514 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10515 return -1);
10517 first += pos(bset->dim, type) - 1;
10518 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10521 /* Is it possible for the integer division "div" to depend (possibly
10522 * indirectly) on any output dimensions?
10524 * If the div is undefined, then we conservatively assume that it
10525 * may depend on them.
10526 * Otherwise, we check if it actually depends on them or on any integer
10527 * divisions that may depend on them.
10529 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10531 int i;
10532 unsigned n_out, o_out;
10533 unsigned n_div, o_div;
10535 if (isl_int_is_zero(bmap->div[div][0]))
10536 return 1;
10538 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10539 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10541 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10542 return 1;
10544 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10545 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10547 for (i = 0; i < n_div; ++i) {
10548 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10549 continue;
10550 if (div_may_involve_output(bmap, i))
10551 return 1;
10554 return 0;
10557 /* Return the first integer division of "bmap" in the range
10558 * [first, first + n[ that may depend on any output dimensions and
10559 * that has a non-zero coefficient in "c" (where the first coefficient
10560 * in "c" corresponds to integer division "first").
10562 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10563 isl_int *c, int first, int n)
10565 int k;
10567 if (!bmap)
10568 return -1;
10570 for (k = first; k < first + n; ++k) {
10571 if (isl_int_is_zero(c[k]))
10572 continue;
10573 if (div_may_involve_output(bmap, k))
10574 return k;
10577 return first + n;
10580 /* Look for a pair of inequality constraints in "bmap" of the form
10582 * -l + i >= 0 or i >= l
10583 * and
10584 * n + l - i >= 0 or i <= l + n
10586 * with n < "m" and i the output dimension at position "pos".
10587 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10588 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10589 * and earlier output dimensions, as well as integer divisions that do
10590 * not involve any of the output dimensions.
10592 * Return the index of the first inequality constraint or bmap->n_ineq
10593 * if no such pair can be found.
10595 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10596 int pos, isl_int m)
10598 int i, j;
10599 isl_ctx *ctx;
10600 unsigned total;
10601 unsigned n_div, o_div;
10602 unsigned n_out, o_out;
10603 int less;
10605 if (!bmap)
10606 return -1;
10608 ctx = isl_basic_map_get_ctx(bmap);
10609 total = isl_basic_map_total_dim(bmap);
10610 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10611 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10612 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10613 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10614 for (i = 0; i < bmap->n_ineq; ++i) {
10615 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10616 continue;
10617 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10618 n_out - (pos + 1)) != -1)
10619 continue;
10620 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10621 0, n_div) < n_div)
10622 continue;
10623 for (j = i + 1; j < bmap->n_ineq; ++j) {
10624 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10625 ctx->one))
10626 continue;
10627 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10628 bmap->ineq[j] + 1, total))
10629 continue;
10630 break;
10632 if (j >= bmap->n_ineq)
10633 continue;
10634 isl_int_add(bmap->ineq[i][0],
10635 bmap->ineq[i][0], bmap->ineq[j][0]);
10636 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10637 isl_int_sub(bmap->ineq[i][0],
10638 bmap->ineq[i][0], bmap->ineq[j][0]);
10639 if (!less)
10640 continue;
10641 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10642 return i;
10643 else
10644 return j;
10647 return bmap->n_ineq;
10650 /* Return the index of the equality of "bmap" that defines
10651 * the output dimension "pos" in terms of earlier dimensions.
10652 * The equality may also involve integer divisions, as long
10653 * as those integer divisions are defined in terms of
10654 * parameters or input dimensions.
10655 * In this case, *div is set to the number of integer divisions and
10656 * *ineq is set to the number of inequality constraints (provided
10657 * div and ineq are not NULL).
10659 * The equality may also involve a single integer division involving
10660 * the output dimensions (typically only output dimension "pos") as
10661 * long as the coefficient of output dimension "pos" is 1 or -1 and
10662 * there is a pair of constraints i >= l and i <= l + n, with i referring
10663 * to output dimension "pos", l an expression involving only earlier
10664 * dimensions and n smaller than the coefficient of the integer division
10665 * in the equality. In this case, the output dimension can be defined
10666 * in terms of a modulo expression that does not involve the integer division.
10667 * *div is then set to this single integer division and
10668 * *ineq is set to the index of constraint i >= l.
10670 * Return bmap->n_eq if there is no such equality.
10671 * Return -1 on error.
10673 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10674 int pos, int *div, int *ineq)
10676 int j, k, l;
10677 unsigned n_out, o_out;
10678 unsigned n_div, o_div;
10680 if (!bmap)
10681 return -1;
10683 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10684 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10685 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10686 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10688 if (ineq)
10689 *ineq = bmap->n_ineq;
10690 if (div)
10691 *div = n_div;
10692 for (j = 0; j < bmap->n_eq; ++j) {
10693 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10694 continue;
10695 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10696 n_out - (pos + 1)) != -1)
10697 continue;
10698 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10699 0, n_div);
10700 if (k >= n_div)
10701 return j;
10702 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10703 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10704 continue;
10705 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10706 k + 1, n_div - (k+1)) < n_div)
10707 continue;
10708 l = find_modulo_constraint_pair(bmap, pos,
10709 bmap->eq[j][o_div + k]);
10710 if (l < 0)
10711 return -1;
10712 if (l >= bmap->n_ineq)
10713 continue;
10714 if (div)
10715 *div = k;
10716 if (ineq)
10717 *ineq = l;
10718 return j;
10721 return bmap->n_eq;
10724 /* Check if the given basic map is obviously single-valued.
10725 * In particular, for each output dimension, check that there is
10726 * an equality that defines the output dimension in terms of
10727 * earlier dimensions.
10729 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10731 int i;
10732 unsigned n_out;
10734 if (!bmap)
10735 return isl_bool_error;
10737 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10739 for (i = 0; i < n_out; ++i) {
10740 int eq;
10742 eq = isl_basic_map_output_defining_equality(bmap, i,
10743 NULL, NULL);
10744 if (eq < 0)
10745 return isl_bool_error;
10746 if (eq >= bmap->n_eq)
10747 return isl_bool_false;
10750 return isl_bool_true;
10753 /* Check if the given basic map is single-valued.
10754 * We simply compute
10756 * M \circ M^-1
10758 * and check if the result is a subset of the identity mapping.
10760 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10762 isl_space *space;
10763 isl_basic_map *test;
10764 isl_basic_map *id;
10765 isl_bool sv;
10767 sv = isl_basic_map_plain_is_single_valued(bmap);
10768 if (sv < 0 || sv)
10769 return sv;
10771 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10772 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10774 space = isl_basic_map_get_space(bmap);
10775 space = isl_space_map_from_set(isl_space_range(space));
10776 id = isl_basic_map_identity(space);
10778 sv = isl_basic_map_is_subset(test, id);
10780 isl_basic_map_free(test);
10781 isl_basic_map_free(id);
10783 return sv;
10786 /* Check if the given map is obviously single-valued.
10788 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10790 if (!map)
10791 return isl_bool_error;
10792 if (map->n == 0)
10793 return isl_bool_true;
10794 if (map->n >= 2)
10795 return isl_bool_false;
10797 return isl_basic_map_plain_is_single_valued(map->p[0]);
10800 /* Check if the given map is single-valued.
10801 * We simply compute
10803 * M \circ M^-1
10805 * and check if the result is a subset of the identity mapping.
10807 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10809 isl_space *dim;
10810 isl_map *test;
10811 isl_map *id;
10812 isl_bool sv;
10814 sv = isl_map_plain_is_single_valued(map);
10815 if (sv < 0 || sv)
10816 return sv;
10818 test = isl_map_reverse(isl_map_copy(map));
10819 test = isl_map_apply_range(test, isl_map_copy(map));
10821 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10822 id = isl_map_identity(dim);
10824 sv = isl_map_is_subset(test, id);
10826 isl_map_free(test);
10827 isl_map_free(id);
10829 return sv;
10832 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10834 isl_bool in;
10836 map = isl_map_copy(map);
10837 map = isl_map_reverse(map);
10838 in = isl_map_is_single_valued(map);
10839 isl_map_free(map);
10841 return in;
10844 /* Check if the given map is obviously injective.
10846 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10848 isl_bool in;
10850 map = isl_map_copy(map);
10851 map = isl_map_reverse(map);
10852 in = isl_map_plain_is_single_valued(map);
10853 isl_map_free(map);
10855 return in;
10858 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10860 isl_bool sv;
10862 sv = isl_map_is_single_valued(map);
10863 if (sv < 0 || !sv)
10864 return sv;
10866 return isl_map_is_injective(map);
10869 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10871 return isl_map_is_single_valued((isl_map *)set);
10874 /* Does "map" only map elements to themselves?
10876 * If the domain and range spaces are different, then "map"
10877 * is considered not to be an identity relation, even if it is empty.
10878 * Otherwise, construct the maximal identity relation and
10879 * check whether "map" is a subset of this relation.
10881 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10883 isl_space *space;
10884 isl_map *id;
10885 isl_bool equal, is_identity;
10887 space = isl_map_get_space(map);
10888 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10889 isl_space_free(space);
10890 if (equal < 0 || !equal)
10891 return equal;
10893 id = isl_map_identity(isl_map_get_space(map));
10894 is_identity = isl_map_is_subset(map, id);
10895 isl_map_free(id);
10897 return is_identity;
10900 int isl_map_is_translation(__isl_keep isl_map *map)
10902 int ok;
10903 isl_set *delta;
10905 delta = isl_map_deltas(isl_map_copy(map));
10906 ok = isl_set_is_singleton(delta);
10907 isl_set_free(delta);
10909 return ok;
10912 static int unique(isl_int *p, unsigned pos, unsigned len)
10914 if (isl_seq_first_non_zero(p, pos) != -1)
10915 return 0;
10916 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10917 return 0;
10918 return 1;
10921 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10923 int i, j;
10924 unsigned nvar;
10925 unsigned ovar;
10927 if (!bset)
10928 return -1;
10930 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10931 return 0;
10933 nvar = isl_basic_set_dim(bset, isl_dim_set);
10934 ovar = isl_space_offset(bset->dim, isl_dim_set);
10935 for (j = 0; j < nvar; ++j) {
10936 int lower = 0, upper = 0;
10937 for (i = 0; i < bset->n_eq; ++i) {
10938 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10939 continue;
10940 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10941 return 0;
10942 break;
10944 if (i < bset->n_eq)
10945 continue;
10946 for (i = 0; i < bset->n_ineq; ++i) {
10947 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10948 continue;
10949 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10950 return 0;
10951 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10952 lower = 1;
10953 else
10954 upper = 1;
10956 if (!lower || !upper)
10957 return 0;
10960 return 1;
10963 int isl_set_is_box(__isl_keep isl_set *set)
10965 if (!set)
10966 return -1;
10967 if (set->n != 1)
10968 return 0;
10970 return isl_basic_set_is_box(set->p[0]);
10973 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10975 if (!bset)
10976 return isl_bool_error;
10978 return isl_space_is_wrapping(bset->dim);
10981 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10983 if (!set)
10984 return isl_bool_error;
10986 return isl_space_is_wrapping(set->dim);
10989 /* Modify the space of "map" through a call to "change".
10990 * If "can_change" is set (not NULL), then first call it to check
10991 * if the modification is allowed, printing the error message "cannot_change"
10992 * if it is not.
10994 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
10995 isl_bool (*can_change)(__isl_keep isl_map *map),
10996 const char *cannot_change,
10997 __isl_give isl_space *(*change)(__isl_take isl_space *space))
10999 isl_bool ok;
11000 isl_space *space;
11002 if (!map)
11003 return NULL;
11005 ok = can_change ? can_change(map) : isl_bool_true;
11006 if (ok < 0)
11007 return isl_map_free(map);
11008 if (!ok)
11009 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11010 return isl_map_free(map));
11012 space = change(isl_map_get_space(map));
11013 map = isl_map_reset_space(map, space);
11015 return map;
11018 /* Is the domain of "map" a wrapped relation?
11020 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11022 if (!map)
11023 return isl_bool_error;
11025 return isl_space_domain_is_wrapping(map->dim);
11028 /* Is the range of "map" a wrapped relation?
11030 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11032 if (!map)
11033 return isl_bool_error;
11035 return isl_space_range_is_wrapping(map->dim);
11038 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11040 bmap = isl_basic_map_cow(bmap);
11041 if (!bmap)
11042 return NULL;
11044 bmap->dim = isl_space_wrap(bmap->dim);
11045 if (!bmap->dim)
11046 goto error;
11048 bmap = isl_basic_map_finalize(bmap);
11050 return (isl_basic_set *)bmap;
11051 error:
11052 isl_basic_map_free(bmap);
11053 return NULL;
11056 /* Given a map A -> B, return the set (A -> B).
11058 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11060 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11063 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11065 bset = isl_basic_set_cow(bset);
11066 if (!bset)
11067 return NULL;
11069 bset->dim = isl_space_unwrap(bset->dim);
11070 if (!bset->dim)
11071 goto error;
11073 bset = isl_basic_set_finalize(bset);
11075 return (isl_basic_map *)bset;
11076 error:
11077 isl_basic_set_free(bset);
11078 return NULL;
11081 /* Given a set (A -> B), return the map A -> B.
11082 * Error out if "set" is not of the form (A -> B).
11084 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11086 return isl_map_change_space(set, &isl_set_is_wrapping,
11087 "not a wrapping set", &isl_space_unwrap);
11090 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11091 enum isl_dim_type type)
11093 if (!bmap)
11094 return NULL;
11096 if (!isl_space_is_named_or_nested(bmap->dim, type))
11097 return bmap;
11099 bmap = isl_basic_map_cow(bmap);
11100 if (!bmap)
11101 return NULL;
11103 bmap->dim = isl_space_reset(bmap->dim, type);
11104 if (!bmap->dim)
11105 goto error;
11107 bmap = isl_basic_map_finalize(bmap);
11109 return bmap;
11110 error:
11111 isl_basic_map_free(bmap);
11112 return NULL;
11115 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11116 enum isl_dim_type type)
11118 int i;
11120 if (!map)
11121 return NULL;
11123 if (!isl_space_is_named_or_nested(map->dim, type))
11124 return map;
11126 map = isl_map_cow(map);
11127 if (!map)
11128 return NULL;
11130 for (i = 0; i < map->n; ++i) {
11131 map->p[i] = isl_basic_map_reset(map->p[i], type);
11132 if (!map->p[i])
11133 goto error;
11135 map->dim = isl_space_reset(map->dim, type);
11136 if (!map->dim)
11137 goto error;
11139 return map;
11140 error:
11141 isl_map_free(map);
11142 return NULL;
11145 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11147 if (!bmap)
11148 return NULL;
11150 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11151 return bmap;
11153 bmap = isl_basic_map_cow(bmap);
11154 if (!bmap)
11155 return NULL;
11157 bmap->dim = isl_space_flatten(bmap->dim);
11158 if (!bmap->dim)
11159 goto error;
11161 bmap = isl_basic_map_finalize(bmap);
11163 return bmap;
11164 error:
11165 isl_basic_map_free(bmap);
11166 return NULL;
11169 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11171 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
11174 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11175 __isl_take isl_basic_map *bmap)
11177 if (!bmap)
11178 return NULL;
11180 if (!bmap->dim->nested[0])
11181 return bmap;
11183 bmap = isl_basic_map_cow(bmap);
11184 if (!bmap)
11185 return NULL;
11187 bmap->dim = isl_space_flatten_domain(bmap->dim);
11188 if (!bmap->dim)
11189 goto error;
11191 bmap = isl_basic_map_finalize(bmap);
11193 return bmap;
11194 error:
11195 isl_basic_map_free(bmap);
11196 return NULL;
11199 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11200 __isl_take isl_basic_map *bmap)
11202 if (!bmap)
11203 return NULL;
11205 if (!bmap->dim->nested[1])
11206 return bmap;
11208 bmap = isl_basic_map_cow(bmap);
11209 if (!bmap)
11210 return NULL;
11212 bmap->dim = isl_space_flatten_range(bmap->dim);
11213 if (!bmap->dim)
11214 goto error;
11216 bmap = isl_basic_map_finalize(bmap);
11218 return bmap;
11219 error:
11220 isl_basic_map_free(bmap);
11221 return NULL;
11224 /* Remove any internal structure from the spaces of domain and range of "map".
11226 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11228 if (!map)
11229 return NULL;
11231 if (!map->dim->nested[0] && !map->dim->nested[1])
11232 return map;
11234 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11237 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11239 return (isl_set *)isl_map_flatten((isl_map *)set);
11242 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11244 isl_space *dim, *flat_dim;
11245 isl_map *map;
11247 dim = isl_set_get_space(set);
11248 flat_dim = isl_space_flatten(isl_space_copy(dim));
11249 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11250 map = isl_map_intersect_domain(map, set);
11252 return map;
11255 /* Remove any internal structure from the space of the domain of "map".
11257 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11259 if (!map)
11260 return NULL;
11262 if (!map->dim->nested[0])
11263 return map;
11265 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11268 /* Remove any internal structure from the space of the range of "map".
11270 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11272 if (!map)
11273 return NULL;
11275 if (!map->dim->nested[1])
11276 return map;
11278 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11281 /* Reorder the dimensions of "bmap" according to the given dim_map
11282 * and set the dimension specification to "dim" and
11283 * perform Gaussian elimination on the result.
11285 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11286 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11288 isl_basic_map *res;
11289 unsigned flags;
11291 bmap = isl_basic_map_cow(bmap);
11292 if (!bmap || !dim || !dim_map)
11293 goto error;
11295 flags = bmap->flags;
11296 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11297 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11298 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11299 res = isl_basic_map_alloc_space(dim,
11300 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11301 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11302 if (res)
11303 res->flags = flags;
11304 res = isl_basic_map_gauss(res, NULL);
11305 res = isl_basic_map_finalize(res);
11306 return res;
11307 error:
11308 free(dim_map);
11309 isl_basic_map_free(bmap);
11310 isl_space_free(dim);
11311 return NULL;
11314 /* Reorder the dimensions of "map" according to given reordering.
11316 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11317 __isl_take isl_reordering *r)
11319 int i;
11320 struct isl_dim_map *dim_map;
11322 map = isl_map_cow(map);
11323 dim_map = isl_dim_map_from_reordering(r);
11324 if (!map || !r || !dim_map)
11325 goto error;
11327 for (i = 0; i < map->n; ++i) {
11328 struct isl_dim_map *dim_map_i;
11330 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11332 map->p[i] = isl_basic_map_realign(map->p[i],
11333 isl_space_copy(r->dim), dim_map_i);
11335 if (!map->p[i])
11336 goto error;
11339 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11341 isl_reordering_free(r);
11342 free(dim_map);
11343 return map;
11344 error:
11345 free(dim_map);
11346 isl_map_free(map);
11347 isl_reordering_free(r);
11348 return NULL;
11351 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11352 __isl_take isl_reordering *r)
11354 return (isl_set *)isl_map_realign((isl_map *)set, r);
11357 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11358 __isl_take isl_space *model)
11360 isl_ctx *ctx;
11362 if (!map || !model)
11363 goto error;
11365 ctx = isl_space_get_ctx(model);
11366 if (!isl_space_has_named_params(model))
11367 isl_die(ctx, isl_error_invalid,
11368 "model has unnamed parameters", goto error);
11369 if (!isl_space_has_named_params(map->dim))
11370 isl_die(ctx, isl_error_invalid,
11371 "relation has unnamed parameters", goto error);
11372 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11373 isl_reordering *exp;
11375 model = isl_space_drop_dims(model, isl_dim_in,
11376 0, isl_space_dim(model, isl_dim_in));
11377 model = isl_space_drop_dims(model, isl_dim_out,
11378 0, isl_space_dim(model, isl_dim_out));
11379 exp = isl_parameter_alignment_reordering(map->dim, model);
11380 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11381 map = isl_map_realign(map, exp);
11384 isl_space_free(model);
11385 return map;
11386 error:
11387 isl_space_free(model);
11388 isl_map_free(map);
11389 return NULL;
11392 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11393 __isl_take isl_space *model)
11395 return isl_map_align_params(set, model);
11398 /* Align the parameters of "bmap" to those of "model", introducing
11399 * additional parameters if needed.
11401 __isl_give isl_basic_map *isl_basic_map_align_params(
11402 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11404 isl_ctx *ctx;
11406 if (!bmap || !model)
11407 goto error;
11409 ctx = isl_space_get_ctx(model);
11410 if (!isl_space_has_named_params(model))
11411 isl_die(ctx, isl_error_invalid,
11412 "model has unnamed parameters", goto error);
11413 if (!isl_space_has_named_params(bmap->dim))
11414 isl_die(ctx, isl_error_invalid,
11415 "relation has unnamed parameters", goto error);
11416 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11417 isl_reordering *exp;
11418 struct isl_dim_map *dim_map;
11420 model = isl_space_drop_dims(model, isl_dim_in,
11421 0, isl_space_dim(model, isl_dim_in));
11422 model = isl_space_drop_dims(model, isl_dim_out,
11423 0, isl_space_dim(model, isl_dim_out));
11424 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11425 exp = isl_reordering_extend_space(exp,
11426 isl_basic_map_get_space(bmap));
11427 dim_map = isl_dim_map_from_reordering(exp);
11428 bmap = isl_basic_map_realign(bmap,
11429 exp ? isl_space_copy(exp->dim) : NULL,
11430 isl_dim_map_extend(dim_map, bmap));
11431 isl_reordering_free(exp);
11432 free(dim_map);
11435 isl_space_free(model);
11436 return bmap;
11437 error:
11438 isl_space_free(model);
11439 isl_basic_map_free(bmap);
11440 return NULL;
11443 /* Align the parameters of "bset" to those of "model", introducing
11444 * additional parameters if needed.
11446 __isl_give isl_basic_set *isl_basic_set_align_params(
11447 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11449 return isl_basic_map_align_params(bset, model);
11452 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11453 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11454 enum isl_dim_type c2, enum isl_dim_type c3,
11455 enum isl_dim_type c4, enum isl_dim_type c5)
11457 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11458 struct isl_mat *mat;
11459 int i, j, k;
11460 int pos;
11462 if (!bmap)
11463 return NULL;
11464 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11465 isl_basic_map_total_dim(bmap) + 1);
11466 if (!mat)
11467 return NULL;
11468 for (i = 0; i < bmap->n_eq; ++i)
11469 for (j = 0, pos = 0; j < 5; ++j) {
11470 int off = isl_basic_map_offset(bmap, c[j]);
11471 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11472 isl_int_set(mat->row[i][pos],
11473 bmap->eq[i][off + k]);
11474 ++pos;
11478 return mat;
11481 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11482 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11483 enum isl_dim_type c2, enum isl_dim_type c3,
11484 enum isl_dim_type c4, enum isl_dim_type c5)
11486 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11487 struct isl_mat *mat;
11488 int i, j, k;
11489 int pos;
11491 if (!bmap)
11492 return NULL;
11493 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11494 isl_basic_map_total_dim(bmap) + 1);
11495 if (!mat)
11496 return NULL;
11497 for (i = 0; i < bmap->n_ineq; ++i)
11498 for (j = 0, pos = 0; j < 5; ++j) {
11499 int off = isl_basic_map_offset(bmap, c[j]);
11500 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11501 isl_int_set(mat->row[i][pos],
11502 bmap->ineq[i][off + k]);
11503 ++pos;
11507 return mat;
11510 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11511 __isl_take isl_space *dim,
11512 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11513 enum isl_dim_type c2, enum isl_dim_type c3,
11514 enum isl_dim_type c4, enum isl_dim_type c5)
11516 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11517 isl_basic_map *bmap;
11518 unsigned total;
11519 unsigned extra;
11520 int i, j, k, l;
11521 int pos;
11523 if (!dim || !eq || !ineq)
11524 goto error;
11526 if (eq->n_col != ineq->n_col)
11527 isl_die(dim->ctx, isl_error_invalid,
11528 "equalities and inequalities matrices should have "
11529 "same number of columns", goto error);
11531 total = 1 + isl_space_dim(dim, isl_dim_all);
11533 if (eq->n_col < total)
11534 isl_die(dim->ctx, isl_error_invalid,
11535 "number of columns too small", goto error);
11537 extra = eq->n_col - total;
11539 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11540 eq->n_row, ineq->n_row);
11541 if (!bmap)
11542 goto error;
11543 for (i = 0; i < extra; ++i) {
11544 k = isl_basic_map_alloc_div(bmap);
11545 if (k < 0)
11546 goto error;
11547 isl_int_set_si(bmap->div[k][0], 0);
11549 for (i = 0; i < eq->n_row; ++i) {
11550 l = isl_basic_map_alloc_equality(bmap);
11551 if (l < 0)
11552 goto error;
11553 for (j = 0, pos = 0; j < 5; ++j) {
11554 int off = isl_basic_map_offset(bmap, c[j]);
11555 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11556 isl_int_set(bmap->eq[l][off + k],
11557 eq->row[i][pos]);
11558 ++pos;
11562 for (i = 0; i < ineq->n_row; ++i) {
11563 l = isl_basic_map_alloc_inequality(bmap);
11564 if (l < 0)
11565 goto error;
11566 for (j = 0, pos = 0; j < 5; ++j) {
11567 int off = isl_basic_map_offset(bmap, c[j]);
11568 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11569 isl_int_set(bmap->ineq[l][off + k],
11570 ineq->row[i][pos]);
11571 ++pos;
11576 isl_space_free(dim);
11577 isl_mat_free(eq);
11578 isl_mat_free(ineq);
11580 bmap = isl_basic_map_simplify(bmap);
11581 return isl_basic_map_finalize(bmap);
11582 error:
11583 isl_space_free(dim);
11584 isl_mat_free(eq);
11585 isl_mat_free(ineq);
11586 return NULL;
11589 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11590 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11591 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11593 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11594 c1, c2, c3, c4, isl_dim_in);
11597 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11598 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11599 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11601 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11602 c1, c2, c3, c4, isl_dim_in);
11605 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11606 __isl_take isl_space *dim,
11607 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, 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_set*)
11611 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11612 c1, c2, c3, c4, isl_dim_in);
11615 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11617 if (!bmap)
11618 return isl_bool_error;
11620 return isl_space_can_zip(bmap->dim);
11623 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11625 if (!map)
11626 return isl_bool_error;
11628 return isl_space_can_zip(map->dim);
11631 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11632 * (A -> C) -> (B -> D).
11634 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11636 unsigned pos;
11637 unsigned n1;
11638 unsigned n2;
11640 if (!bmap)
11641 return NULL;
11643 if (!isl_basic_map_can_zip(bmap))
11644 isl_die(bmap->ctx, isl_error_invalid,
11645 "basic map cannot be zipped", goto error);
11646 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11647 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11648 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11649 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11650 bmap = isl_basic_map_cow(bmap);
11651 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11652 if (!bmap)
11653 return NULL;
11654 bmap->dim = isl_space_zip(bmap->dim);
11655 if (!bmap->dim)
11656 goto error;
11657 bmap = isl_basic_map_mark_final(bmap);
11658 return bmap;
11659 error:
11660 isl_basic_map_free(bmap);
11661 return NULL;
11664 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11665 * (A -> C) -> (B -> D).
11667 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11669 int i;
11671 if (!map)
11672 return NULL;
11674 if (!isl_map_can_zip(map))
11675 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11676 goto error);
11678 map = isl_map_cow(map);
11679 if (!map)
11680 return NULL;
11682 for (i = 0; i < map->n; ++i) {
11683 map->p[i] = isl_basic_map_zip(map->p[i]);
11684 if (!map->p[i])
11685 goto error;
11688 map->dim = isl_space_zip(map->dim);
11689 if (!map->dim)
11690 goto error;
11692 return map;
11693 error:
11694 isl_map_free(map);
11695 return NULL;
11698 /* Can we apply isl_basic_map_curry to "bmap"?
11699 * That is, does it have a nested relation in its domain?
11701 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11703 if (!bmap)
11704 return isl_bool_error;
11706 return isl_space_can_curry(bmap->dim);
11709 /* Can we apply isl_map_curry to "map"?
11710 * That is, does it have a nested relation in its domain?
11712 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11714 if (!map)
11715 return isl_bool_error;
11717 return isl_space_can_curry(map->dim);
11720 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11721 * A -> (B -> C).
11723 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11726 if (!bmap)
11727 return NULL;
11729 if (!isl_basic_map_can_curry(bmap))
11730 isl_die(bmap->ctx, isl_error_invalid,
11731 "basic map cannot be curried", goto error);
11732 bmap = isl_basic_map_cow(bmap);
11733 if (!bmap)
11734 return NULL;
11735 bmap->dim = isl_space_curry(bmap->dim);
11736 if (!bmap->dim)
11737 goto error;
11738 bmap = isl_basic_map_mark_final(bmap);
11739 return bmap;
11740 error:
11741 isl_basic_map_free(bmap);
11742 return NULL;
11745 /* Given a map (A -> B) -> C, return the corresponding map
11746 * A -> (B -> C).
11748 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11750 return isl_map_change_space(map, &isl_map_can_curry,
11751 "map cannot be curried", &isl_space_curry);
11754 /* Can isl_map_range_curry be applied to "map"?
11755 * That is, does it have a nested relation in its range,
11756 * the domain of which is itself a nested relation?
11758 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11760 if (!map)
11761 return isl_bool_error;
11763 return isl_space_can_range_curry(map->dim);
11766 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11767 * A -> (B -> (C -> D)).
11769 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11771 return isl_map_change_space(map, &isl_map_can_range_curry,
11772 "map range cannot be curried",
11773 &isl_space_range_curry);
11776 /* Can we apply isl_basic_map_uncurry to "bmap"?
11777 * That is, does it have a nested relation in its domain?
11779 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11781 if (!bmap)
11782 return isl_bool_error;
11784 return isl_space_can_uncurry(bmap->dim);
11787 /* Can we apply isl_map_uncurry to "map"?
11788 * That is, does it have a nested relation in its domain?
11790 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11792 if (!map)
11793 return isl_bool_error;
11795 return isl_space_can_uncurry(map->dim);
11798 /* Given a basic map A -> (B -> C), return the corresponding basic map
11799 * (A -> B) -> C.
11801 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11804 if (!bmap)
11805 return NULL;
11807 if (!isl_basic_map_can_uncurry(bmap))
11808 isl_die(bmap->ctx, isl_error_invalid,
11809 "basic map cannot be uncurried",
11810 return isl_basic_map_free(bmap));
11811 bmap = isl_basic_map_cow(bmap);
11812 if (!bmap)
11813 return NULL;
11814 bmap->dim = isl_space_uncurry(bmap->dim);
11815 if (!bmap->dim)
11816 return isl_basic_map_free(bmap);
11817 bmap = isl_basic_map_mark_final(bmap);
11818 return bmap;
11821 /* Given a map A -> (B -> C), return the corresponding map
11822 * (A -> B) -> C.
11824 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11826 return isl_map_change_space(map, &isl_map_can_uncurry,
11827 "map cannot be uncurried", &isl_space_uncurry);
11830 /* Construct a basic map mapping the domain of the affine expression
11831 * to a one-dimensional range prescribed by the affine expression.
11833 * A NaN affine expression cannot be converted to a basic map.
11835 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11837 int k;
11838 int pos;
11839 isl_bool is_nan;
11840 isl_local_space *ls;
11841 isl_basic_map *bmap = NULL;
11843 if (!aff)
11844 return NULL;
11845 is_nan = isl_aff_is_nan(aff);
11846 if (is_nan < 0)
11847 goto error;
11848 if (is_nan)
11849 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11850 "cannot convert NaN", goto error);
11852 ls = isl_aff_get_local_space(aff);
11853 bmap = isl_basic_map_from_local_space(ls);
11854 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11855 k = isl_basic_map_alloc_equality(bmap);
11856 if (k < 0)
11857 goto error;
11859 pos = isl_basic_map_offset(bmap, isl_dim_out);
11860 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11861 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11862 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11863 aff->v->size - (pos + 1));
11865 isl_aff_free(aff);
11866 bmap = isl_basic_map_finalize(bmap);
11867 return bmap;
11868 error:
11869 isl_aff_free(aff);
11870 isl_basic_map_free(bmap);
11871 return NULL;
11874 /* Construct a map mapping the domain of the affine expression
11875 * to a one-dimensional range prescribed by the affine expression.
11877 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11879 isl_basic_map *bmap;
11881 bmap = isl_basic_map_from_aff(aff);
11882 return isl_map_from_basic_map(bmap);
11885 /* Construct a basic map mapping the domain the multi-affine expression
11886 * to its range, with each dimension in the range equated to the
11887 * corresponding affine expression.
11889 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11890 __isl_take isl_multi_aff *maff)
11892 int i;
11893 isl_space *space;
11894 isl_basic_map *bmap;
11896 if (!maff)
11897 return NULL;
11899 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11900 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11901 "invalid space", goto error);
11903 space = isl_space_domain(isl_multi_aff_get_space(maff));
11904 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11906 for (i = 0; i < maff->n; ++i) {
11907 isl_aff *aff;
11908 isl_basic_map *bmap_i;
11910 aff = isl_aff_copy(maff->p[i]);
11911 bmap_i = isl_basic_map_from_aff(aff);
11913 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11916 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11918 isl_multi_aff_free(maff);
11919 return bmap;
11920 error:
11921 isl_multi_aff_free(maff);
11922 return NULL;
11925 /* Construct a map mapping the domain the multi-affine expression
11926 * to its range, with each dimension in the range equated to the
11927 * corresponding affine expression.
11929 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11931 isl_basic_map *bmap;
11933 bmap = isl_basic_map_from_multi_aff(maff);
11934 return isl_map_from_basic_map(bmap);
11937 /* Construct a basic map mapping a domain in the given space to
11938 * to an n-dimensional range, with n the number of elements in the list,
11939 * where each coordinate in the range is prescribed by the
11940 * corresponding affine expression.
11941 * The domains of all affine expressions in the list are assumed to match
11942 * domain_dim.
11944 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11945 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11947 int i;
11948 isl_space *dim;
11949 isl_basic_map *bmap;
11951 if (!list)
11952 return NULL;
11954 dim = isl_space_from_domain(domain_dim);
11955 bmap = isl_basic_map_universe(dim);
11957 for (i = 0; i < list->n; ++i) {
11958 isl_aff *aff;
11959 isl_basic_map *bmap_i;
11961 aff = isl_aff_copy(list->p[i]);
11962 bmap_i = isl_basic_map_from_aff(aff);
11964 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11967 isl_aff_list_free(list);
11968 return bmap;
11971 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11972 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11974 return isl_map_equate(set, type1, pos1, type2, pos2);
11977 /* Construct a basic map where the given dimensions are equal to each other.
11979 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11980 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11982 isl_basic_map *bmap = NULL;
11983 int i;
11985 if (!space)
11986 return NULL;
11988 if (pos1 >= isl_space_dim(space, type1))
11989 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11990 "index out of bounds", goto error);
11991 if (pos2 >= isl_space_dim(space, type2))
11992 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11993 "index out of bounds", goto error);
11995 if (type1 == type2 && pos1 == pos2)
11996 return isl_basic_map_universe(space);
11998 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11999 i = isl_basic_map_alloc_equality(bmap);
12000 if (i < 0)
12001 goto error;
12002 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12003 pos1 += isl_basic_map_offset(bmap, type1);
12004 pos2 += isl_basic_map_offset(bmap, type2);
12005 isl_int_set_si(bmap->eq[i][pos1], -1);
12006 isl_int_set_si(bmap->eq[i][pos2], 1);
12007 bmap = isl_basic_map_finalize(bmap);
12008 isl_space_free(space);
12009 return bmap;
12010 error:
12011 isl_space_free(space);
12012 isl_basic_map_free(bmap);
12013 return NULL;
12016 /* Add a constraint imposing that the given two dimensions are equal.
12018 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12019 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12021 isl_basic_map *eq;
12023 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12025 bmap = isl_basic_map_intersect(bmap, eq);
12027 return bmap;
12030 /* Add a constraint imposing that the given two dimensions are equal.
12032 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12033 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12035 isl_basic_map *bmap;
12037 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12039 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12041 return map;
12044 /* Add a constraint imposing that the given two dimensions have opposite values.
12046 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12047 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12049 isl_basic_map *bmap = NULL;
12050 int i;
12052 if (!map)
12053 return NULL;
12055 if (pos1 >= isl_map_dim(map, type1))
12056 isl_die(map->ctx, isl_error_invalid,
12057 "index out of bounds", goto error);
12058 if (pos2 >= isl_map_dim(map, type2))
12059 isl_die(map->ctx, isl_error_invalid,
12060 "index out of bounds", goto error);
12062 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12063 i = isl_basic_map_alloc_equality(bmap);
12064 if (i < 0)
12065 goto error;
12066 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12067 pos1 += isl_basic_map_offset(bmap, type1);
12068 pos2 += isl_basic_map_offset(bmap, type2);
12069 isl_int_set_si(bmap->eq[i][pos1], 1);
12070 isl_int_set_si(bmap->eq[i][pos2], 1);
12071 bmap = isl_basic_map_finalize(bmap);
12073 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12075 return map;
12076 error:
12077 isl_basic_map_free(bmap);
12078 isl_map_free(map);
12079 return NULL;
12082 /* Construct a constraint imposing that the value of the first dimension is
12083 * greater than or equal to that of the second.
12085 static __isl_give isl_constraint *constraint_order_ge(
12086 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12087 enum isl_dim_type type2, int pos2)
12089 isl_constraint *c;
12091 if (!space)
12092 return NULL;
12094 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12096 if (pos1 >= isl_constraint_dim(c, type1))
12097 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12098 "index out of bounds", return isl_constraint_free(c));
12099 if (pos2 >= isl_constraint_dim(c, type2))
12100 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12101 "index out of bounds", return isl_constraint_free(c));
12103 if (type1 == type2 && pos1 == pos2)
12104 return c;
12106 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12107 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12109 return c;
12112 /* Add a constraint imposing that the value of the first dimension is
12113 * greater than or equal to that of the second.
12115 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12116 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12118 isl_constraint *c;
12119 isl_space *space;
12121 if (type1 == type2 && pos1 == pos2)
12122 return bmap;
12123 space = isl_basic_map_get_space(bmap);
12124 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12125 bmap = isl_basic_map_add_constraint(bmap, c);
12127 return bmap;
12130 /* Add a constraint imposing that the value of the first dimension is
12131 * greater than or equal to that of the second.
12133 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12134 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12136 isl_constraint *c;
12137 isl_space *space;
12139 if (type1 == type2 && pos1 == pos2)
12140 return map;
12141 space = isl_map_get_space(map);
12142 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12143 map = isl_map_add_constraint(map, c);
12145 return map;
12148 /* Add a constraint imposing that the value of the first dimension is
12149 * less than or equal to that of the second.
12151 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12152 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12154 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12157 /* Construct a basic map where the value of the first dimension is
12158 * greater than that of the second.
12160 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12161 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12163 isl_basic_map *bmap = NULL;
12164 int i;
12166 if (!space)
12167 return NULL;
12169 if (pos1 >= isl_space_dim(space, type1))
12170 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12171 "index out of bounds", goto error);
12172 if (pos2 >= isl_space_dim(space, type2))
12173 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12174 "index out of bounds", goto error);
12176 if (type1 == type2 && pos1 == pos2)
12177 return isl_basic_map_empty(space);
12179 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12180 i = isl_basic_map_alloc_inequality(bmap);
12181 if (i < 0)
12182 return isl_basic_map_free(bmap);
12183 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12184 pos1 += isl_basic_map_offset(bmap, type1);
12185 pos2 += isl_basic_map_offset(bmap, type2);
12186 isl_int_set_si(bmap->ineq[i][pos1], 1);
12187 isl_int_set_si(bmap->ineq[i][pos2], -1);
12188 isl_int_set_si(bmap->ineq[i][0], -1);
12189 bmap = isl_basic_map_finalize(bmap);
12191 return bmap;
12192 error:
12193 isl_space_free(space);
12194 isl_basic_map_free(bmap);
12195 return NULL;
12198 /* Add a constraint imposing that the value of the first dimension is
12199 * greater than that of the second.
12201 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12202 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12204 isl_basic_map *gt;
12206 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12208 bmap = isl_basic_map_intersect(bmap, gt);
12210 return bmap;
12213 /* Add a constraint imposing that the value of the first dimension is
12214 * greater than that of the second.
12216 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12217 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12219 isl_basic_map *bmap;
12221 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12223 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12225 return map;
12228 /* Add a constraint imposing that the value of the first dimension is
12229 * smaller than that of the second.
12231 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12232 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12234 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12237 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12238 int pos)
12240 isl_aff *div;
12241 isl_local_space *ls;
12243 if (!bmap)
12244 return NULL;
12246 if (!isl_basic_map_divs_known(bmap))
12247 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12248 "some divs are unknown", return NULL);
12250 ls = isl_basic_map_get_local_space(bmap);
12251 div = isl_local_space_get_div(ls, pos);
12252 isl_local_space_free(ls);
12254 return div;
12257 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12258 int pos)
12260 return isl_basic_map_get_div(bset, pos);
12263 /* Plug in "subs" for dimension "type", "pos" of "bset".
12265 * Let i be the dimension to replace and let "subs" be of the form
12267 * f/d
12269 * Any integer division with a non-zero coefficient for i,
12271 * floor((a i + g)/m)
12273 * is replaced by
12275 * floor((a f + d g)/(m d))
12277 * Constraints of the form
12279 * a i + g
12281 * are replaced by
12283 * a f + d g
12285 * We currently require that "subs" is an integral expression.
12286 * Handling rational expressions may require us to add stride constraints
12287 * as we do in isl_basic_set_preimage_multi_aff.
12289 __isl_give isl_basic_set *isl_basic_set_substitute(
12290 __isl_take isl_basic_set *bset,
12291 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12293 int i;
12294 isl_int v;
12295 isl_ctx *ctx;
12297 if (bset && isl_basic_set_plain_is_empty(bset))
12298 return bset;
12300 bset = isl_basic_set_cow(bset);
12301 if (!bset || !subs)
12302 goto error;
12304 ctx = isl_basic_set_get_ctx(bset);
12305 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12306 isl_die(ctx, isl_error_invalid,
12307 "spaces don't match", goto error);
12308 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12309 isl_die(ctx, isl_error_unsupported,
12310 "cannot handle divs yet", goto error);
12311 if (!isl_int_is_one(subs->v->el[0]))
12312 isl_die(ctx, isl_error_invalid,
12313 "can only substitute integer expressions", goto error);
12315 pos += isl_basic_set_offset(bset, type);
12317 isl_int_init(v);
12319 for (i = 0; i < bset->n_eq; ++i) {
12320 if (isl_int_is_zero(bset->eq[i][pos]))
12321 continue;
12322 isl_int_set(v, bset->eq[i][pos]);
12323 isl_int_set_si(bset->eq[i][pos], 0);
12324 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12325 v, subs->v->el + 1, subs->v->size - 1);
12328 for (i = 0; i < bset->n_ineq; ++i) {
12329 if (isl_int_is_zero(bset->ineq[i][pos]))
12330 continue;
12331 isl_int_set(v, bset->ineq[i][pos]);
12332 isl_int_set_si(bset->ineq[i][pos], 0);
12333 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12334 v, subs->v->el + 1, subs->v->size - 1);
12337 for (i = 0; i < bset->n_div; ++i) {
12338 if (isl_int_is_zero(bset->div[i][1 + pos]))
12339 continue;
12340 isl_int_set(v, bset->div[i][1 + pos]);
12341 isl_int_set_si(bset->div[i][1 + pos], 0);
12342 isl_seq_combine(bset->div[i] + 1,
12343 subs->v->el[0], bset->div[i] + 1,
12344 v, subs->v->el + 1, subs->v->size - 1);
12345 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12348 isl_int_clear(v);
12350 bset = isl_basic_set_simplify(bset);
12351 return isl_basic_set_finalize(bset);
12352 error:
12353 isl_basic_set_free(bset);
12354 return NULL;
12357 /* Plug in "subs" for dimension "type", "pos" of "set".
12359 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12360 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12362 int i;
12364 if (set && isl_set_plain_is_empty(set))
12365 return set;
12367 set = isl_set_cow(set);
12368 if (!set || !subs)
12369 goto error;
12371 for (i = set->n - 1; i >= 0; --i) {
12372 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12373 if (remove_if_empty(set, i) < 0)
12374 goto error;
12377 return set;
12378 error:
12379 isl_set_free(set);
12380 return NULL;
12383 /* Check if the range of "ma" is compatible with the domain or range
12384 * (depending on "type") of "bmap".
12385 * Return -1 if anything is wrong.
12387 static int check_basic_map_compatible_range_multi_aff(
12388 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12389 __isl_keep isl_multi_aff *ma)
12391 int m;
12392 isl_space *ma_space;
12394 ma_space = isl_multi_aff_get_space(ma);
12396 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12397 if (m < 0)
12398 goto error;
12399 if (!m)
12400 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12401 "parameters don't match", goto error);
12402 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12403 if (m < 0)
12404 goto error;
12405 if (!m)
12406 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12407 "spaces don't match", goto error);
12409 isl_space_free(ma_space);
12410 return m;
12411 error:
12412 isl_space_free(ma_space);
12413 return -1;
12416 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12417 * coefficients before the transformed range of dimensions,
12418 * the "n_after" coefficients after the transformed range of dimensions
12419 * and the coefficients of the other divs in "bmap".
12421 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12422 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12424 int i;
12425 int n_param;
12426 int n_set;
12427 isl_local_space *ls;
12429 if (n_div == 0)
12430 return 0;
12432 ls = isl_aff_get_domain_local_space(ma->p[0]);
12433 if (!ls)
12434 return -1;
12436 n_param = isl_local_space_dim(ls, isl_dim_param);
12437 n_set = isl_local_space_dim(ls, isl_dim_set);
12438 for (i = 0; i < n_div; ++i) {
12439 int o_bmap = 0, o_ls = 0;
12441 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12442 o_bmap += 1 + 1 + n_param;
12443 o_ls += 1 + 1 + n_param;
12444 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12445 o_bmap += n_before;
12446 isl_seq_cpy(bmap->div[i] + o_bmap,
12447 ls->div->row[i] + o_ls, n_set);
12448 o_bmap += n_set;
12449 o_ls += n_set;
12450 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12451 o_bmap += n_after;
12452 isl_seq_cpy(bmap->div[i] + o_bmap,
12453 ls->div->row[i] + o_ls, n_div);
12454 o_bmap += n_div;
12455 o_ls += n_div;
12456 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12457 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12458 goto error;
12461 isl_local_space_free(ls);
12462 return 0;
12463 error:
12464 isl_local_space_free(ls);
12465 return -1;
12468 /* How many stride constraints does "ma" enforce?
12469 * That is, how many of the affine expressions have a denominator
12470 * different from one?
12472 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12474 int i;
12475 int strides = 0;
12477 for (i = 0; i < ma->n; ++i)
12478 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12479 strides++;
12481 return strides;
12484 /* For each affine expression in ma of the form
12486 * x_i = (f_i y + h_i)/m_i
12488 * with m_i different from one, add a constraint to "bmap"
12489 * of the form
12491 * f_i y + h_i = m_i alpha_i
12493 * with alpha_i an additional existentially quantified variable.
12495 * The input variables of "ma" correspond to a subset of the variables
12496 * of "bmap". There are "n_before" variables in "bmap" before this
12497 * subset and "n_after" variables after this subset.
12498 * The integer divisions of the affine expressions in "ma" are assumed
12499 * to have been aligned. There are "n_div_ma" of them and
12500 * they appear first in "bmap", straight after the "n_after" variables.
12502 static __isl_give isl_basic_map *add_ma_strides(
12503 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12504 int n_before, int n_after, int n_div_ma)
12506 int i, k;
12507 int div;
12508 int total;
12509 int n_param;
12510 int n_in;
12512 total = isl_basic_map_total_dim(bmap);
12513 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12514 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12515 for (i = 0; i < ma->n; ++i) {
12516 int o_bmap = 0, o_ma = 1;
12518 if (isl_int_is_one(ma->p[i]->v->el[0]))
12519 continue;
12520 div = isl_basic_map_alloc_div(bmap);
12521 k = isl_basic_map_alloc_equality(bmap);
12522 if (div < 0 || k < 0)
12523 goto error;
12524 isl_int_set_si(bmap->div[div][0], 0);
12525 isl_seq_cpy(bmap->eq[k] + o_bmap,
12526 ma->p[i]->v->el + o_ma, 1 + n_param);
12527 o_bmap += 1 + n_param;
12528 o_ma += 1 + n_param;
12529 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12530 o_bmap += n_before;
12531 isl_seq_cpy(bmap->eq[k] + o_bmap,
12532 ma->p[i]->v->el + o_ma, n_in);
12533 o_bmap += n_in;
12534 o_ma += n_in;
12535 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12536 o_bmap += n_after;
12537 isl_seq_cpy(bmap->eq[k] + o_bmap,
12538 ma->p[i]->v->el + o_ma, n_div_ma);
12539 o_bmap += n_div_ma;
12540 o_ma += n_div_ma;
12541 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12542 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12543 total++;
12546 return bmap;
12547 error:
12548 isl_basic_map_free(bmap);
12549 return NULL;
12552 /* Replace the domain or range space (depending on "type) of "space" by "set".
12554 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12555 enum isl_dim_type type, __isl_take isl_space *set)
12557 if (type == isl_dim_in) {
12558 space = isl_space_range(space);
12559 space = isl_space_map_from_domain_and_range(set, space);
12560 } else {
12561 space = isl_space_domain(space);
12562 space = isl_space_map_from_domain_and_range(space, set);
12565 return space;
12568 /* Compute the preimage of the domain or range (depending on "type")
12569 * of "bmap" under the function represented by "ma".
12570 * In other words, plug in "ma" in the domain or range of "bmap".
12571 * The result is a basic map that lives in the same space as "bmap"
12572 * except that the domain or range has been replaced by
12573 * the domain space of "ma".
12575 * If bmap is represented by
12577 * A(p) + S u + B x + T v + C(divs) >= 0,
12579 * where u and x are input and output dimensions if type == isl_dim_out
12580 * while x and v are input and output dimensions if type == isl_dim_in,
12581 * and ma is represented by
12583 * x = D(p) + F(y) + G(divs')
12585 * then the result is
12587 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12589 * The divs in the input set are similarly adjusted.
12590 * In particular
12592 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12594 * becomes
12596 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12597 * B_i G(divs') + c_i(divs))/n_i)
12599 * If bmap is not a rational map and if F(y) involves any denominators
12601 * x_i = (f_i y + h_i)/m_i
12603 * then additional constraints are added to ensure that we only
12604 * map back integer points. That is we enforce
12606 * f_i y + h_i = m_i alpha_i
12608 * with alpha_i an additional existentially quantified variable.
12610 * We first copy over the divs from "ma".
12611 * Then we add the modified constraints and divs from "bmap".
12612 * Finally, we add the stride constraints, if needed.
12614 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12615 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12616 __isl_take isl_multi_aff *ma)
12618 int i, k;
12619 isl_space *space;
12620 isl_basic_map *res = NULL;
12621 int n_before, n_after, n_div_bmap, n_div_ma;
12622 isl_int f, c1, c2, g;
12623 int rational, strides;
12625 isl_int_init(f);
12626 isl_int_init(c1);
12627 isl_int_init(c2);
12628 isl_int_init(g);
12630 ma = isl_multi_aff_align_divs(ma);
12631 if (!bmap || !ma)
12632 goto error;
12633 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12634 goto error;
12636 if (type == isl_dim_in) {
12637 n_before = 0;
12638 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12639 } else {
12640 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12641 n_after = 0;
12643 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12644 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12646 space = isl_multi_aff_get_domain_space(ma);
12647 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12648 rational = isl_basic_map_is_rational(bmap);
12649 strides = rational ? 0 : multi_aff_strides(ma);
12650 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12651 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12652 if (rational)
12653 res = isl_basic_map_set_rational(res);
12655 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12656 if (isl_basic_map_alloc_div(res) < 0)
12657 goto error;
12659 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12660 goto error;
12662 for (i = 0; i < bmap->n_eq; ++i) {
12663 k = isl_basic_map_alloc_equality(res);
12664 if (k < 0)
12665 goto error;
12666 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12667 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12670 for (i = 0; i < bmap->n_ineq; ++i) {
12671 k = isl_basic_map_alloc_inequality(res);
12672 if (k < 0)
12673 goto error;
12674 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12675 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12678 for (i = 0; i < bmap->n_div; ++i) {
12679 if (isl_int_is_zero(bmap->div[i][0])) {
12680 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12681 continue;
12683 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12684 n_before, n_after, n_div_ma, n_div_bmap,
12685 f, c1, c2, g, 1);
12688 if (strides)
12689 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12691 isl_int_clear(f);
12692 isl_int_clear(c1);
12693 isl_int_clear(c2);
12694 isl_int_clear(g);
12695 isl_basic_map_free(bmap);
12696 isl_multi_aff_free(ma);
12697 res = isl_basic_set_simplify(res);
12698 return isl_basic_map_finalize(res);
12699 error:
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 isl_basic_map_free(res);
12707 return NULL;
12710 /* Compute the preimage of "bset" under the function represented by "ma".
12711 * In other words, plug in "ma" in "bset". The result is a basic set
12712 * that lives in the domain space of "ma".
12714 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12715 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12717 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12720 /* Compute the preimage of the domain of "bmap" under the function
12721 * represented by "ma".
12722 * In other words, plug in "ma" in the domain of "bmap".
12723 * The result is a basic map that lives in the same space as "bmap"
12724 * except that the domain has been replaced by the domain space of "ma".
12726 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12727 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12729 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12732 /* Compute the preimage of the range of "bmap" under the function
12733 * represented by "ma".
12734 * In other words, plug in "ma" in the range of "bmap".
12735 * The result is a basic map that lives in the same space as "bmap"
12736 * except that the range has been replaced by the domain space of "ma".
12738 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12739 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12741 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12744 /* Check if the range of "ma" is compatible with the domain or range
12745 * (depending on "type") of "map".
12746 * Return -1 if anything is wrong.
12748 static int check_map_compatible_range_multi_aff(
12749 __isl_keep isl_map *map, enum isl_dim_type type,
12750 __isl_keep isl_multi_aff *ma)
12752 int m;
12753 isl_space *ma_space;
12755 ma_space = isl_multi_aff_get_space(ma);
12756 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12757 isl_space_free(ma_space);
12758 if (m >= 0 && !m)
12759 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12760 "spaces don't match", return -1);
12761 return m;
12764 /* Compute the preimage of the domain or range (depending on "type")
12765 * of "map" under the function represented by "ma".
12766 * In other words, plug in "ma" in the domain or range of "map".
12767 * The result is a map that lives in the same space as "map"
12768 * except that the domain or range has been replaced by
12769 * the domain space of "ma".
12771 * The parameters are assumed to have been aligned.
12773 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12774 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12776 int i;
12777 isl_space *space;
12779 map = isl_map_cow(map);
12780 ma = isl_multi_aff_align_divs(ma);
12781 if (!map || !ma)
12782 goto error;
12783 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12784 goto error;
12786 for (i = 0; i < map->n; ++i) {
12787 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12788 isl_multi_aff_copy(ma));
12789 if (!map->p[i])
12790 goto error;
12793 space = isl_multi_aff_get_domain_space(ma);
12794 space = isl_space_set(isl_map_get_space(map), type, space);
12796 isl_space_free(map->dim);
12797 map->dim = space;
12798 if (!map->dim)
12799 goto error;
12801 isl_multi_aff_free(ma);
12802 if (map->n > 1)
12803 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12804 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12805 return map;
12806 error:
12807 isl_multi_aff_free(ma);
12808 isl_map_free(map);
12809 return NULL;
12812 /* Compute the preimage of the domain or range (depending on "type")
12813 * of "map" under the function represented by "ma".
12814 * In other words, plug in "ma" in the domain or range of "map".
12815 * The result is a map that lives in the same space as "map"
12816 * except that the domain or range has been replaced by
12817 * the domain space of "ma".
12819 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12820 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12822 if (!map || !ma)
12823 goto error;
12825 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12826 return map_preimage_multi_aff(map, type, ma);
12828 if (!isl_space_has_named_params(map->dim) ||
12829 !isl_space_has_named_params(ma->space))
12830 isl_die(map->ctx, isl_error_invalid,
12831 "unaligned unnamed parameters", goto error);
12832 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12833 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12835 return map_preimage_multi_aff(map, type, ma);
12836 error:
12837 isl_multi_aff_free(ma);
12838 return isl_map_free(map);
12841 /* Compute the preimage of "set" under the function represented by "ma".
12842 * In other words, plug in "ma" in "set". The result is a set
12843 * that lives in the domain space of "ma".
12845 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12846 __isl_take isl_multi_aff *ma)
12848 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12851 /* Compute the preimage of the domain of "map" under the function
12852 * represented by "ma".
12853 * In other words, plug in "ma" in the domain of "map".
12854 * The result is a map that lives in the same space as "map"
12855 * except that the domain has been replaced by the domain space of "ma".
12857 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12858 __isl_take isl_multi_aff *ma)
12860 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12863 /* Compute the preimage of the range of "map" under the function
12864 * represented by "ma".
12865 * In other words, plug in "ma" in the range of "map".
12866 * The result is a map that lives in the same space as "map"
12867 * except that the range has been replaced by the domain space of "ma".
12869 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12870 __isl_take isl_multi_aff *ma)
12872 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12875 /* Compute the preimage of "map" under the function represented by "pma".
12876 * In other words, plug in "pma" in the domain or range of "map".
12877 * The result is a map that lives in the same space as "map",
12878 * except that the space of type "type" has been replaced by
12879 * the domain space of "pma".
12881 * The parameters of "map" and "pma" are assumed to have been aligned.
12883 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12884 __isl_take isl_map *map, enum isl_dim_type type,
12885 __isl_take isl_pw_multi_aff *pma)
12887 int i;
12888 isl_map *res;
12890 if (!pma)
12891 goto error;
12893 if (pma->n == 0) {
12894 isl_pw_multi_aff_free(pma);
12895 res = isl_map_empty(isl_map_get_space(map));
12896 isl_map_free(map);
12897 return res;
12900 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12901 isl_multi_aff_copy(pma->p[0].maff));
12902 if (type == isl_dim_in)
12903 res = isl_map_intersect_domain(res,
12904 isl_map_copy(pma->p[0].set));
12905 else
12906 res = isl_map_intersect_range(res,
12907 isl_map_copy(pma->p[0].set));
12909 for (i = 1; i < pma->n; ++i) {
12910 isl_map *res_i;
12912 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12913 isl_multi_aff_copy(pma->p[i].maff));
12914 if (type == isl_dim_in)
12915 res_i = isl_map_intersect_domain(res_i,
12916 isl_map_copy(pma->p[i].set));
12917 else
12918 res_i = isl_map_intersect_range(res_i,
12919 isl_map_copy(pma->p[i].set));
12920 res = isl_map_union(res, res_i);
12923 isl_pw_multi_aff_free(pma);
12924 isl_map_free(map);
12925 return res;
12926 error:
12927 isl_pw_multi_aff_free(pma);
12928 isl_map_free(map);
12929 return NULL;
12932 /* Compute the preimage of "map" under the function represented by "pma".
12933 * In other words, plug in "pma" in the domain or range of "map".
12934 * The result is a map that lives in the same space as "map",
12935 * except that the space of type "type" has been replaced by
12936 * the domain space of "pma".
12938 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12939 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12941 if (!map || !pma)
12942 goto error;
12944 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12945 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12947 if (!isl_space_has_named_params(map->dim) ||
12948 !isl_space_has_named_params(pma->dim))
12949 isl_die(map->ctx, isl_error_invalid,
12950 "unaligned unnamed parameters", goto error);
12951 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12952 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12954 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12955 error:
12956 isl_pw_multi_aff_free(pma);
12957 return isl_map_free(map);
12960 /* Compute the preimage of "set" under the function represented by "pma".
12961 * In other words, plug in "pma" in "set". The result is a set
12962 * that lives in the domain space of "pma".
12964 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12965 __isl_take isl_pw_multi_aff *pma)
12967 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12970 /* Compute the preimage of the domain of "map" under the function
12971 * represented by "pma".
12972 * In other words, plug in "pma" in the domain of "map".
12973 * The result is a map that lives in the same space as "map",
12974 * except that domain space has been replaced by the domain space of "pma".
12976 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12977 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12979 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12982 /* Compute the preimage of the range of "map" under the function
12983 * represented by "pma".
12984 * In other words, plug in "pma" in the range of "map".
12985 * The result is a map that lives in the same space as "map",
12986 * except that range space has been replaced by the domain space of "pma".
12988 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12989 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12991 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12994 /* Compute the preimage of "map" under the function represented by "mpa".
12995 * In other words, plug in "mpa" in the domain or range of "map".
12996 * The result is a map that lives in the same space as "map",
12997 * except that the space of type "type" has been replaced by
12998 * the domain space of "mpa".
13000 * If the map does not involve any constraints that refer to the
13001 * dimensions of the substituted space, then the only possible
13002 * effect of "mpa" on the map is to map the space to a different space.
13003 * We create a separate isl_multi_aff to effectuate this change
13004 * in order to avoid spurious splitting of the map along the pieces
13005 * of "mpa".
13007 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13008 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13010 int n;
13011 isl_pw_multi_aff *pma;
13013 if (!map || !mpa)
13014 goto error;
13016 n = isl_map_dim(map, type);
13017 if (!isl_map_involves_dims(map, type, 0, n)) {
13018 isl_space *space;
13019 isl_multi_aff *ma;
13021 space = isl_multi_pw_aff_get_space(mpa);
13022 isl_multi_pw_aff_free(mpa);
13023 ma = isl_multi_aff_zero(space);
13024 return isl_map_preimage_multi_aff(map, type, ma);
13027 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13028 return isl_map_preimage_pw_multi_aff(map, type, pma);
13029 error:
13030 isl_map_free(map);
13031 isl_multi_pw_aff_free(mpa);
13032 return NULL;
13035 /* Compute the preimage of "map" under the function represented by "mpa".
13036 * In other words, plug in "mpa" in the domain "map".
13037 * The result is a map that lives in the same space as "map",
13038 * except that domain space has been replaced by the domain space of "mpa".
13040 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13041 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13043 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13046 /* Compute the preimage of "set" by the function represented by "mpa".
13047 * In other words, plug in "mpa" in "set".
13049 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13050 __isl_take isl_multi_pw_aff *mpa)
13052 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);