rename basic_map_partial_lexopt*_pma to basic_map_partial_lexopt*_pw_multi_aff
[isl.git] / isl_map.c
blobc4fb53ab3ec95834a1650ae010ea40f3cb3dc4ec
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 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1430 if (!bmap)
1431 return -1;
1432 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1433 bmap->n_div -= n;
1434 return 0;
1437 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1439 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1442 /* Copy constraint from src to dst, putting the vars of src at offset
1443 * dim_off in dst and the divs of src at offset div_off in dst.
1444 * If both sets are actually map, then dim_off applies to the input
1445 * variables.
1447 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1448 struct isl_basic_map *src_map, isl_int *src,
1449 unsigned in_off, unsigned out_off, unsigned div_off)
1451 unsigned src_nparam = isl_basic_map_n_param(src_map);
1452 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1453 unsigned src_in = isl_basic_map_n_in(src_map);
1454 unsigned dst_in = isl_basic_map_n_in(dst_map);
1455 unsigned src_out = isl_basic_map_n_out(src_map);
1456 unsigned dst_out = isl_basic_map_n_out(dst_map);
1457 isl_int_set(dst[0], src[0]);
1458 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1459 if (dst_nparam > src_nparam)
1460 isl_seq_clr(dst+1+src_nparam,
1461 dst_nparam - src_nparam);
1462 isl_seq_clr(dst+1+dst_nparam, in_off);
1463 isl_seq_cpy(dst+1+dst_nparam+in_off,
1464 src+1+src_nparam,
1465 isl_min(dst_in-in_off, src_in));
1466 if (dst_in-in_off > src_in)
1467 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1468 dst_in - in_off - src_in);
1469 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1470 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1471 src+1+src_nparam+src_in,
1472 isl_min(dst_out-out_off, src_out));
1473 if (dst_out-out_off > src_out)
1474 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1475 dst_out - out_off - src_out);
1476 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1477 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1478 src+1+src_nparam+src_in+src_out,
1479 isl_min(dst_map->extra-div_off, src_map->n_div));
1480 if (dst_map->n_div-div_off > src_map->n_div)
1481 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1482 div_off+src_map->n_div,
1483 dst_map->n_div - div_off - src_map->n_div);
1486 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1487 struct isl_basic_map *src_map, isl_int *src,
1488 unsigned in_off, unsigned out_off, unsigned div_off)
1490 isl_int_set(dst[0], src[0]);
1491 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1494 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1495 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1497 int i;
1498 unsigned div_off;
1500 if (!bmap1 || !bmap2)
1501 goto error;
1503 div_off = bmap1->n_div;
1505 for (i = 0; i < bmap2->n_eq; ++i) {
1506 int i1 = isl_basic_map_alloc_equality(bmap1);
1507 if (i1 < 0)
1508 goto error;
1509 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1510 i_pos, o_pos, div_off);
1513 for (i = 0; i < bmap2->n_ineq; ++i) {
1514 int i1 = isl_basic_map_alloc_inequality(bmap1);
1515 if (i1 < 0)
1516 goto error;
1517 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1518 i_pos, o_pos, div_off);
1521 for (i = 0; i < bmap2->n_div; ++i) {
1522 int i1 = isl_basic_map_alloc_div(bmap1);
1523 if (i1 < 0)
1524 goto error;
1525 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1526 i_pos, o_pos, div_off);
1529 isl_basic_map_free(bmap2);
1531 return bmap1;
1533 error:
1534 isl_basic_map_free(bmap1);
1535 isl_basic_map_free(bmap2);
1536 return NULL;
1539 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1540 struct isl_basic_set *bset2, unsigned pos)
1542 return (struct isl_basic_set *)
1543 add_constraints((struct isl_basic_map *)bset1,
1544 (struct isl_basic_map *)bset2, 0, pos);
1547 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1548 __isl_take isl_space *dim, unsigned extra,
1549 unsigned n_eq, unsigned n_ineq)
1551 struct isl_basic_map *ext;
1552 unsigned flags;
1553 int dims_ok;
1555 if (!dim)
1556 goto error;
1558 if (!base)
1559 goto error;
1561 dims_ok = isl_space_is_equal(base->dim, dim) &&
1562 base->extra >= base->n_div + extra;
1564 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1565 room_for_ineq(base, n_ineq)) {
1566 isl_space_free(dim);
1567 return base;
1570 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1571 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1572 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1573 extra += base->extra;
1574 n_eq += base->n_eq;
1575 n_ineq += base->n_ineq;
1577 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1578 dim = NULL;
1579 if (!ext)
1580 goto error;
1582 if (dims_ok)
1583 ext->sample = isl_vec_copy(base->sample);
1584 flags = base->flags;
1585 ext = add_constraints(ext, base, 0, 0);
1586 if (ext) {
1587 ext->flags = flags;
1588 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1591 return ext;
1593 error:
1594 isl_space_free(dim);
1595 isl_basic_map_free(base);
1596 return NULL;
1599 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1600 __isl_take isl_space *dim, unsigned extra,
1601 unsigned n_eq, unsigned n_ineq)
1603 return (struct isl_basic_set *)
1604 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1605 extra, n_eq, n_ineq);
1608 struct isl_basic_map *isl_basic_map_extend_constraints(
1609 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1611 if (!base)
1612 return NULL;
1613 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1614 0, n_eq, n_ineq);
1617 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1618 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1619 unsigned n_eq, unsigned n_ineq)
1621 struct isl_basic_map *bmap;
1622 isl_space *dim;
1624 if (!base)
1625 return NULL;
1626 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1627 if (!dim)
1628 goto error;
1630 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1631 return bmap;
1632 error:
1633 isl_basic_map_free(base);
1634 return NULL;
1637 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1638 unsigned nparam, unsigned dim, unsigned extra,
1639 unsigned n_eq, unsigned n_ineq)
1641 return (struct isl_basic_set *)
1642 isl_basic_map_extend((struct isl_basic_map *)base,
1643 nparam, 0, dim, extra, n_eq, n_ineq);
1646 struct isl_basic_set *isl_basic_set_extend_constraints(
1647 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1649 return (struct isl_basic_set *)
1650 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1651 n_eq, n_ineq);
1654 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1656 return (struct isl_basic_set *)
1657 isl_basic_map_cow((struct isl_basic_map *)bset);
1660 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1662 if (!bmap)
1663 return NULL;
1665 if (bmap->ref > 1) {
1666 bmap->ref--;
1667 bmap = isl_basic_map_dup(bmap);
1669 if (bmap) {
1670 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1671 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1673 return bmap;
1676 /* Clear all cached information in "map", either because it is about
1677 * to be modified or because it is being freed.
1678 * Always return the same pointer that is passed in.
1679 * This is needed for the use in isl_map_free.
1681 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1683 isl_basic_map_free(map->cached_simple_hull[0]);
1684 isl_basic_map_free(map->cached_simple_hull[1]);
1685 map->cached_simple_hull[0] = NULL;
1686 map->cached_simple_hull[1] = NULL;
1687 return map;
1690 struct isl_set *isl_set_cow(struct isl_set *set)
1692 return isl_map_cow(set);
1695 /* Return an isl_map that is equal to "map" and that has only
1696 * a single reference.
1698 * If the original input already has only one reference, then
1699 * simply return it, but clear all cached information, since
1700 * it may be rendered invalid by the operations that will be
1701 * performed on the result.
1703 * Otherwise, create a duplicate (without any cached information).
1705 struct isl_map *isl_map_cow(struct isl_map *map)
1707 if (!map)
1708 return NULL;
1710 if (map->ref == 1)
1711 return clear_caches(map);
1712 map->ref--;
1713 return isl_map_dup(map);
1716 static void swap_vars(struct isl_blk blk, isl_int *a,
1717 unsigned a_len, unsigned b_len)
1719 isl_seq_cpy(blk.data, a+a_len, b_len);
1720 isl_seq_cpy(blk.data+b_len, a, a_len);
1721 isl_seq_cpy(a, blk.data, b_len+a_len);
1724 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1725 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1727 int i;
1728 struct isl_blk blk;
1730 if (!bmap)
1731 goto error;
1733 isl_assert(bmap->ctx,
1734 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1736 if (n1 == 0 || n2 == 0)
1737 return bmap;
1739 bmap = isl_basic_map_cow(bmap);
1740 if (!bmap)
1741 return NULL;
1743 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1744 if (isl_blk_is_error(blk))
1745 goto error;
1747 for (i = 0; i < bmap->n_eq; ++i)
1748 swap_vars(blk,
1749 bmap->eq[i] + pos, n1, n2);
1751 for (i = 0; i < bmap->n_ineq; ++i)
1752 swap_vars(blk,
1753 bmap->ineq[i] + pos, n1, n2);
1755 for (i = 0; i < bmap->n_div; ++i)
1756 swap_vars(blk,
1757 bmap->div[i]+1 + pos, n1, n2);
1759 isl_blk_free(bmap->ctx, blk);
1761 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1762 bmap = isl_basic_map_gauss(bmap, NULL);
1763 return isl_basic_map_finalize(bmap);
1764 error:
1765 isl_basic_map_free(bmap);
1766 return NULL;
1769 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1771 int i = 0;
1772 unsigned total;
1773 if (!bmap)
1774 goto error;
1775 total = isl_basic_map_total_dim(bmap);
1776 isl_basic_map_free_div(bmap, bmap->n_div);
1777 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1778 if (bmap->n_eq > 0)
1779 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1780 else {
1781 i = isl_basic_map_alloc_equality(bmap);
1782 if (i < 0)
1783 goto error;
1785 isl_int_set_si(bmap->eq[i][0], 1);
1786 isl_seq_clr(bmap->eq[i]+1, total);
1787 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1788 isl_vec_free(bmap->sample);
1789 bmap->sample = NULL;
1790 return isl_basic_map_finalize(bmap);
1791 error:
1792 isl_basic_map_free(bmap);
1793 return NULL;
1796 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1798 return (struct isl_basic_set *)
1799 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1802 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1803 * of "bmap").
1805 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1807 isl_int *t = bmap->div[a];
1808 bmap->div[a] = bmap->div[b];
1809 bmap->div[b] = t;
1812 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1813 * div definitions accordingly.
1815 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1817 int i;
1818 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1820 swap_div(bmap, a, b);
1822 for (i = 0; i < bmap->n_eq; ++i)
1823 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1825 for (i = 0; i < bmap->n_ineq; ++i)
1826 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1828 for (i = 0; i < bmap->n_div; ++i)
1829 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1830 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1833 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1834 * div definitions accordingly.
1836 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1838 isl_basic_map_swap_div(bset, a, b);
1841 /* Eliminate the specified n dimensions starting at first from the
1842 * constraints, without removing the dimensions from the space.
1843 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1845 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1846 enum isl_dim_type type, unsigned first, unsigned n)
1848 int i;
1850 if (!map)
1851 return NULL;
1852 if (n == 0)
1853 return map;
1855 if (first + n > isl_map_dim(map, type) || first + n < first)
1856 isl_die(map->ctx, isl_error_invalid,
1857 "index out of bounds", goto error);
1859 map = isl_map_cow(map);
1860 if (!map)
1861 return NULL;
1863 for (i = 0; i < map->n; ++i) {
1864 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1865 if (!map->p[i])
1866 goto error;
1868 return map;
1869 error:
1870 isl_map_free(map);
1871 return NULL;
1874 /* Eliminate the specified n dimensions starting at first from the
1875 * constraints, without removing the dimensions from the space.
1876 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1878 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1879 enum isl_dim_type type, unsigned first, unsigned n)
1881 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1884 /* Eliminate the specified n dimensions starting at first from the
1885 * constraints, without removing the dimensions from the space.
1886 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1888 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1889 unsigned first, unsigned n)
1891 return isl_set_eliminate(set, isl_dim_set, first, n);
1894 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1895 __isl_take isl_basic_map *bmap)
1897 if (!bmap)
1898 return NULL;
1899 bmap = isl_basic_map_eliminate_vars(bmap,
1900 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1901 if (!bmap)
1902 return NULL;
1903 bmap->n_div = 0;
1904 return isl_basic_map_finalize(bmap);
1907 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1908 __isl_take isl_basic_set *bset)
1910 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1911 (struct isl_basic_map *)bset);
1914 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1916 int i;
1918 if (!map)
1919 return NULL;
1920 if (map->n == 0)
1921 return map;
1923 map = isl_map_cow(map);
1924 if (!map)
1925 return NULL;
1927 for (i = 0; i < map->n; ++i) {
1928 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1929 if (!map->p[i])
1930 goto error;
1932 return map;
1933 error:
1934 isl_map_free(map);
1935 return NULL;
1938 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1940 return isl_map_remove_divs(set);
1943 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1944 enum isl_dim_type type, unsigned first, unsigned n)
1946 if (!bmap)
1947 return NULL;
1948 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1949 goto error);
1950 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1951 return bmap;
1952 bmap = isl_basic_map_eliminate_vars(bmap,
1953 isl_basic_map_offset(bmap, type) - 1 + first, n);
1954 if (!bmap)
1955 return bmap;
1956 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1957 return bmap;
1958 bmap = isl_basic_map_drop(bmap, type, first, n);
1959 return bmap;
1960 error:
1961 isl_basic_map_free(bmap);
1962 return NULL;
1965 /* Return true if the definition of the given div (recursively) involves
1966 * any of the given variables.
1968 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1969 unsigned first, unsigned n)
1971 int i;
1972 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1974 if (isl_int_is_zero(bmap->div[div][0]))
1975 return 0;
1976 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1977 return 1;
1979 for (i = bmap->n_div - 1; i >= 0; --i) {
1980 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1981 continue;
1982 if (div_involves_vars(bmap, i, first, n))
1983 return 1;
1986 return 0;
1989 /* Try and add a lower and/or upper bound on "div" to "bmap"
1990 * based on inequality "i".
1991 * "total" is the total number of variables (excluding the divs).
1992 * "v" is a temporary object that can be used during the calculations.
1993 * If "lb" is set, then a lower bound should be constructed.
1994 * If "ub" is set, then an upper bound should be constructed.
1996 * The calling function has already checked that the inequality does not
1997 * reference "div", but we still need to check that the inequality is
1998 * of the right form. We'll consider the case where we want to construct
1999 * a lower bound. The construction of upper bounds is similar.
2001 * Let "div" be of the form
2003 * q = floor((a + f(x))/d)
2005 * We essentially check if constraint "i" is of the form
2007 * b + f(x) >= 0
2009 * so that we can use it to derive a lower bound on "div".
2010 * However, we allow a slightly more general form
2012 * b + g(x) >= 0
2014 * with the condition that the coefficients of g(x) - f(x) are all
2015 * divisible by d.
2016 * Rewriting this constraint as
2018 * 0 >= -b - g(x)
2020 * adding a + f(x) to both sides and dividing by d, we obtain
2022 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2024 * Taking the floor on both sides, we obtain
2026 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2028 * or
2030 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2032 * In the case of an upper bound, we construct the constraint
2034 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2037 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2038 __isl_take isl_basic_map *bmap, int div, int i,
2039 unsigned total, isl_int v, int lb, int ub)
2041 int j;
2043 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2044 if (lb) {
2045 isl_int_sub(v, bmap->ineq[i][1 + j],
2046 bmap->div[div][1 + 1 + j]);
2047 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2049 if (ub) {
2050 isl_int_add(v, bmap->ineq[i][1 + j],
2051 bmap->div[div][1 + 1 + j]);
2052 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2055 if (!lb && !ub)
2056 return bmap;
2058 bmap = isl_basic_map_cow(bmap);
2059 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2060 if (lb) {
2061 int k = isl_basic_map_alloc_inequality(bmap);
2062 if (k < 0)
2063 goto error;
2064 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2065 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2066 bmap->div[div][1 + j]);
2067 isl_int_cdiv_q(bmap->ineq[k][j],
2068 bmap->ineq[k][j], bmap->div[div][0]);
2070 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2072 if (ub) {
2073 int k = isl_basic_map_alloc_inequality(bmap);
2074 if (k < 0)
2075 goto error;
2076 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2077 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2078 bmap->div[div][1 + j]);
2079 isl_int_fdiv_q(bmap->ineq[k][j],
2080 bmap->ineq[k][j], bmap->div[div][0]);
2082 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2085 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2086 return bmap;
2087 error:
2088 isl_basic_map_free(bmap);
2089 return NULL;
2092 /* This function is called right before "div" is eliminated from "bmap"
2093 * using Fourier-Motzkin.
2094 * Look through the constraints of "bmap" for constraints on the argument
2095 * of the integer division and use them to construct constraints on the
2096 * integer division itself. These constraints can then be combined
2097 * during the Fourier-Motzkin elimination.
2098 * Note that it is only useful to introduce lower bounds on "div"
2099 * if "bmap" already contains upper bounds on "div" as the newly
2100 * introduce lower bounds can then be combined with the pre-existing
2101 * upper bounds. Similarly for upper bounds.
2102 * We therefore first check if "bmap" contains any lower and/or upper bounds
2103 * on "div".
2105 * It is interesting to note that the introduction of these constraints
2106 * can indeed lead to more accurate results, even when compared to
2107 * deriving constraints on the argument of "div" from constraints on "div".
2108 * Consider, for example, the set
2110 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2112 * The second constraint can be rewritten as
2114 * 2 * [(-i-2j+3)/4] + k >= 0
2116 * from which we can derive
2118 * -i - 2j + 3 >= -2k
2120 * or
2122 * i + 2j <= 3 + 2k
2124 * Combined with the first constraint, we obtain
2126 * -3 <= 3 + 2k or k >= -3
2128 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2129 * the first constraint, we obtain
2131 * [(i + 2j)/4] >= [-3/4] = -1
2133 * Combining this constraint with the second constraint, we obtain
2135 * k >= -2
2137 static __isl_give isl_basic_map *insert_bounds_on_div(
2138 __isl_take isl_basic_map *bmap, int div)
2140 int i;
2141 int check_lb, check_ub;
2142 isl_int v;
2143 unsigned total;
2145 if (!bmap)
2146 return NULL;
2148 if (isl_int_is_zero(bmap->div[div][0]))
2149 return bmap;
2151 total = isl_space_dim(bmap->dim, isl_dim_all);
2153 check_lb = 0;
2154 check_ub = 0;
2155 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2156 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2157 if (s > 0)
2158 check_ub = 1;
2159 if (s < 0)
2160 check_lb = 1;
2163 if (!check_lb && !check_ub)
2164 return bmap;
2166 isl_int_init(v);
2168 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2169 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2170 continue;
2172 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2173 check_lb, check_ub);
2176 isl_int_clear(v);
2178 return bmap;
2181 /* Remove all divs (recursively) involving any of the given dimensions
2182 * in their definitions.
2184 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2185 __isl_take isl_basic_map *bmap,
2186 enum isl_dim_type type, unsigned first, unsigned n)
2188 int i;
2190 if (!bmap)
2191 return NULL;
2192 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2193 goto error);
2194 first += isl_basic_map_offset(bmap, type);
2196 for (i = bmap->n_div - 1; i >= 0; --i) {
2197 if (!div_involves_vars(bmap, i, first, n))
2198 continue;
2199 bmap = insert_bounds_on_div(bmap, i);
2200 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2201 if (!bmap)
2202 return NULL;
2203 i = bmap->n_div;
2206 return bmap;
2207 error:
2208 isl_basic_map_free(bmap);
2209 return NULL;
2212 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2213 __isl_take isl_basic_set *bset,
2214 enum isl_dim_type type, unsigned first, unsigned n)
2216 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2219 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2220 enum isl_dim_type type, unsigned first, unsigned n)
2222 int i;
2224 if (!map)
2225 return NULL;
2226 if (map->n == 0)
2227 return map;
2229 map = isl_map_cow(map);
2230 if (!map)
2231 return NULL;
2233 for (i = 0; i < map->n; ++i) {
2234 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2235 type, first, n);
2236 if (!map->p[i])
2237 goto error;
2239 return map;
2240 error:
2241 isl_map_free(map);
2242 return NULL;
2245 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2246 enum isl_dim_type type, unsigned first, unsigned n)
2248 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2249 type, first, n);
2252 /* Does the desciption of "bmap" depend on the specified dimensions?
2253 * We also check whether the dimensions appear in any of the div definitions.
2254 * In principle there is no need for this check. If the dimensions appear
2255 * in a div definition, they also appear in the defining constraints of that
2256 * div.
2258 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2259 enum isl_dim_type type, unsigned first, unsigned n)
2261 int i;
2263 if (!bmap)
2264 return isl_bool_error;
2266 if (first + n > isl_basic_map_dim(bmap, type))
2267 isl_die(bmap->ctx, isl_error_invalid,
2268 "index out of bounds", return isl_bool_error);
2270 first += isl_basic_map_offset(bmap, type);
2271 for (i = 0; i < bmap->n_eq; ++i)
2272 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2273 return isl_bool_true;
2274 for (i = 0; i < bmap->n_ineq; ++i)
2275 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2276 return isl_bool_true;
2277 for (i = 0; i < bmap->n_div; ++i) {
2278 if (isl_int_is_zero(bmap->div[i][0]))
2279 continue;
2280 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2281 return isl_bool_true;
2284 return isl_bool_false;
2287 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2288 enum isl_dim_type type, unsigned first, unsigned n)
2290 int i;
2292 if (!map)
2293 return isl_bool_error;
2295 if (first + n > isl_map_dim(map, type))
2296 isl_die(map->ctx, isl_error_invalid,
2297 "index out of bounds", return isl_bool_error);
2299 for (i = 0; i < map->n; ++i) {
2300 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2301 type, first, n);
2302 if (involves < 0 || involves)
2303 return involves;
2306 return isl_bool_false;
2309 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2310 enum isl_dim_type type, unsigned first, unsigned n)
2312 return isl_basic_map_involves_dims(bset, type, first, n);
2315 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2316 enum isl_dim_type type, unsigned first, unsigned n)
2318 return isl_map_involves_dims(set, type, first, n);
2321 /* Does local variable "div" of "bmap" have a complete explicit representation?
2322 * Having a complete explicit representation requires not only
2323 * an explicit representation, but also that all local variables
2324 * that appear in this explicit representation in turn have
2325 * a complete explicit representation.
2327 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2329 int i;
2330 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2331 isl_bool marked;
2333 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2334 if (marked < 0 || marked)
2335 return isl_bool_not(marked);
2337 for (i = bmap->n_div - 1; i >= 0; --i) {
2338 isl_bool known;
2340 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2341 continue;
2342 known = isl_basic_map_div_is_known(bmap, i);
2343 if (known < 0 || !known)
2344 return known;
2347 return isl_bool_true;
2350 /* Does local variable "div" of "bset" have a complete explicit representation?
2352 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2354 return isl_basic_map_div_is_known(bset, div);
2357 /* Remove all divs that are unknown or defined in terms of unknown divs.
2359 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2360 __isl_take isl_basic_map *bmap)
2362 int i;
2364 if (!bmap)
2365 return NULL;
2367 for (i = bmap->n_div - 1; i >= 0; --i) {
2368 if (isl_basic_map_div_is_known(bmap, i))
2369 continue;
2370 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2371 if (!bmap)
2372 return NULL;
2373 i = bmap->n_div;
2376 return bmap;
2379 /* Remove all divs that are unknown or defined in terms of unknown divs.
2381 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2382 __isl_take isl_basic_set *bset)
2384 return isl_basic_map_remove_unknown_divs(bset);
2387 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2389 int i;
2391 if (!map)
2392 return NULL;
2393 if (map->n == 0)
2394 return map;
2396 map = isl_map_cow(map);
2397 if (!map)
2398 return NULL;
2400 for (i = 0; i < map->n; ++i) {
2401 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2402 if (!map->p[i])
2403 goto error;
2405 return map;
2406 error:
2407 isl_map_free(map);
2408 return NULL;
2411 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2413 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2416 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2417 __isl_take isl_basic_set *bset,
2418 enum isl_dim_type type, unsigned first, unsigned n)
2420 return (isl_basic_set *)
2421 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2424 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2425 enum isl_dim_type type, unsigned first, unsigned n)
2427 int i;
2429 if (n == 0)
2430 return map;
2432 map = isl_map_cow(map);
2433 if (!map)
2434 return NULL;
2435 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2437 for (i = 0; i < map->n; ++i) {
2438 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2439 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2440 if (!map->p[i])
2441 goto error;
2443 map = isl_map_drop(map, type, first, n);
2444 return map;
2445 error:
2446 isl_map_free(map);
2447 return NULL;
2450 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2451 enum isl_dim_type type, unsigned first, unsigned n)
2453 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2456 /* Project out n inputs starting at first using Fourier-Motzkin */
2457 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2458 unsigned first, unsigned n)
2460 return isl_map_remove_dims(map, isl_dim_in, first, n);
2463 static void dump_term(struct isl_basic_map *bmap,
2464 isl_int c, int pos, FILE *out)
2466 const char *name;
2467 unsigned in = isl_basic_map_n_in(bmap);
2468 unsigned dim = in + isl_basic_map_n_out(bmap);
2469 unsigned nparam = isl_basic_map_n_param(bmap);
2470 if (!pos)
2471 isl_int_print(out, c, 0);
2472 else {
2473 if (!isl_int_is_one(c))
2474 isl_int_print(out, c, 0);
2475 if (pos < 1 + nparam) {
2476 name = isl_space_get_dim_name(bmap->dim,
2477 isl_dim_param, pos - 1);
2478 if (name)
2479 fprintf(out, "%s", name);
2480 else
2481 fprintf(out, "p%d", pos - 1);
2482 } else if (pos < 1 + nparam + in)
2483 fprintf(out, "i%d", pos - 1 - nparam);
2484 else if (pos < 1 + nparam + dim)
2485 fprintf(out, "o%d", pos - 1 - nparam - in);
2486 else
2487 fprintf(out, "e%d", pos - 1 - nparam - dim);
2491 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2492 int sign, FILE *out)
2494 int i;
2495 int first;
2496 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2497 isl_int v;
2499 isl_int_init(v);
2500 for (i = 0, first = 1; i < len; ++i) {
2501 if (isl_int_sgn(c[i]) * sign <= 0)
2502 continue;
2503 if (!first)
2504 fprintf(out, " + ");
2505 first = 0;
2506 isl_int_abs(v, c[i]);
2507 dump_term(bmap, v, i, out);
2509 isl_int_clear(v);
2510 if (first)
2511 fprintf(out, "0");
2514 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2515 const char *op, FILE *out, int indent)
2517 int i;
2519 fprintf(out, "%*s", indent, "");
2521 dump_constraint_sign(bmap, c, 1, out);
2522 fprintf(out, " %s ", op);
2523 dump_constraint_sign(bmap, c, -1, out);
2525 fprintf(out, "\n");
2527 for (i = bmap->n_div; i < bmap->extra; ++i) {
2528 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2529 continue;
2530 fprintf(out, "%*s", indent, "");
2531 fprintf(out, "ERROR: unused div coefficient not zero\n");
2532 abort();
2536 static void dump_constraints(struct isl_basic_map *bmap,
2537 isl_int **c, unsigned n,
2538 const char *op, FILE *out, int indent)
2540 int i;
2542 for (i = 0; i < n; ++i)
2543 dump_constraint(bmap, c[i], op, out, indent);
2546 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2548 int j;
2549 int first = 1;
2550 unsigned total = isl_basic_map_total_dim(bmap);
2552 for (j = 0; j < 1 + total; ++j) {
2553 if (isl_int_is_zero(exp[j]))
2554 continue;
2555 if (!first && isl_int_is_pos(exp[j]))
2556 fprintf(out, "+");
2557 dump_term(bmap, exp[j], j, out);
2558 first = 0;
2562 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2564 int i;
2566 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2567 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2569 for (i = 0; i < bmap->n_div; ++i) {
2570 fprintf(out, "%*s", indent, "");
2571 fprintf(out, "e%d = [(", i);
2572 dump_affine(bmap, bmap->div[i]+1, out);
2573 fprintf(out, ")/");
2574 isl_int_print(out, bmap->div[i][0], 0);
2575 fprintf(out, "]\n");
2579 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2580 FILE *out, int indent)
2582 if (!bset) {
2583 fprintf(out, "null basic set\n");
2584 return;
2587 fprintf(out, "%*s", indent, "");
2588 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2589 bset->ref, bset->dim->nparam, bset->dim->n_out,
2590 bset->extra, bset->flags);
2591 dump((struct isl_basic_map *)bset, out, indent);
2594 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2595 FILE *out, int indent)
2597 if (!bmap) {
2598 fprintf(out, "null basic map\n");
2599 return;
2602 fprintf(out, "%*s", indent, "");
2603 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2604 "flags: %x, n_name: %d\n",
2605 bmap->ref,
2606 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2607 bmap->extra, bmap->flags, bmap->dim->n_id);
2608 dump(bmap, out, indent);
2611 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2613 unsigned total;
2614 if (!bmap)
2615 return -1;
2616 total = isl_basic_map_total_dim(bmap);
2617 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2618 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2619 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2620 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2621 return 0;
2624 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2625 unsigned flags)
2627 if (!space)
2628 return NULL;
2629 if (isl_space_dim(space, isl_dim_in) != 0)
2630 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2631 "set cannot have input dimensions", goto error);
2632 return isl_map_alloc_space(space, n, flags);
2633 error:
2634 isl_space_free(space);
2635 return NULL;
2638 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2639 unsigned nparam, unsigned dim, int n, unsigned flags)
2641 struct isl_set *set;
2642 isl_space *dims;
2644 dims = isl_space_alloc(ctx, nparam, 0, dim);
2645 if (!dims)
2646 return NULL;
2648 set = isl_set_alloc_space(dims, n, flags);
2649 return set;
2652 /* Make sure "map" has room for at least "n" more basic maps.
2654 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2656 int i;
2657 struct isl_map *grown = NULL;
2659 if (!map)
2660 return NULL;
2661 isl_assert(map->ctx, n >= 0, goto error);
2662 if (map->n + n <= map->size)
2663 return map;
2664 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2665 if (!grown)
2666 goto error;
2667 for (i = 0; i < map->n; ++i) {
2668 grown->p[i] = isl_basic_map_copy(map->p[i]);
2669 if (!grown->p[i])
2670 goto error;
2671 grown->n++;
2673 isl_map_free(map);
2674 return grown;
2675 error:
2676 isl_map_free(grown);
2677 isl_map_free(map);
2678 return NULL;
2681 /* Make sure "set" has room for at least "n" more basic sets.
2683 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2685 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2688 struct isl_set *isl_set_dup(struct isl_set *set)
2690 int i;
2691 struct isl_set *dup;
2693 if (!set)
2694 return NULL;
2696 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2697 if (!dup)
2698 return NULL;
2699 for (i = 0; i < set->n; ++i)
2700 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2701 return dup;
2704 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2706 return isl_map_from_basic_map(bset);
2709 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2711 struct isl_map *map;
2713 if (!bmap)
2714 return NULL;
2716 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2717 return isl_map_add_basic_map(map, bmap);
2720 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2721 __isl_take isl_basic_set *bset)
2723 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2724 (struct isl_basic_map *)bset);
2727 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2729 return isl_map_free(set);
2732 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2734 int i;
2736 if (!set) {
2737 fprintf(out, "null set\n");
2738 return;
2741 fprintf(out, "%*s", indent, "");
2742 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2743 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2744 set->flags);
2745 for (i = 0; i < set->n; ++i) {
2746 fprintf(out, "%*s", indent, "");
2747 fprintf(out, "basic set %d:\n", i);
2748 isl_basic_set_print_internal(set->p[i], out, indent+4);
2752 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2754 int i;
2756 if (!map) {
2757 fprintf(out, "null map\n");
2758 return;
2761 fprintf(out, "%*s", indent, "");
2762 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2763 "flags: %x, n_name: %d\n",
2764 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2765 map->dim->n_out, map->flags, map->dim->n_id);
2766 for (i = 0; i < map->n; ++i) {
2767 fprintf(out, "%*s", indent, "");
2768 fprintf(out, "basic map %d:\n", i);
2769 isl_basic_map_print_internal(map->p[i], out, indent+4);
2773 struct isl_basic_map *isl_basic_map_intersect_domain(
2774 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2776 struct isl_basic_map *bmap_domain;
2778 if (!bmap || !bset)
2779 goto error;
2781 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2782 bset->dim, isl_dim_param), goto error);
2784 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2785 isl_assert(bset->ctx,
2786 isl_basic_map_compatible_domain(bmap, bset), goto error);
2788 bmap = isl_basic_map_cow(bmap);
2789 if (!bmap)
2790 goto error;
2791 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2792 bset->n_div, bset->n_eq, bset->n_ineq);
2793 bmap_domain = isl_basic_map_from_domain(bset);
2794 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2796 bmap = isl_basic_map_simplify(bmap);
2797 return isl_basic_map_finalize(bmap);
2798 error:
2799 isl_basic_map_free(bmap);
2800 isl_basic_set_free(bset);
2801 return NULL;
2804 struct isl_basic_map *isl_basic_map_intersect_range(
2805 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2807 struct isl_basic_map *bmap_range;
2809 if (!bmap || !bset)
2810 goto error;
2812 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2813 bset->dim, isl_dim_param), goto error);
2815 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2816 isl_assert(bset->ctx,
2817 isl_basic_map_compatible_range(bmap, bset), goto error);
2819 if (isl_basic_set_plain_is_universe(bset)) {
2820 isl_basic_set_free(bset);
2821 return bmap;
2824 bmap = isl_basic_map_cow(bmap);
2825 if (!bmap)
2826 goto error;
2827 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2828 bset->n_div, bset->n_eq, bset->n_ineq);
2829 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2830 bmap = add_constraints(bmap, bmap_range, 0, 0);
2832 bmap = isl_basic_map_simplify(bmap);
2833 return isl_basic_map_finalize(bmap);
2834 error:
2835 isl_basic_map_free(bmap);
2836 isl_basic_set_free(bset);
2837 return NULL;
2840 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2841 __isl_keep isl_vec *vec)
2843 int i;
2844 unsigned total;
2845 isl_int s;
2847 if (!bmap || !vec)
2848 return isl_bool_error;
2850 total = 1 + isl_basic_map_total_dim(bmap);
2851 if (total != vec->size)
2852 return isl_bool_error;
2854 isl_int_init(s);
2856 for (i = 0; i < bmap->n_eq; ++i) {
2857 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2858 if (!isl_int_is_zero(s)) {
2859 isl_int_clear(s);
2860 return isl_bool_false;
2864 for (i = 0; i < bmap->n_ineq; ++i) {
2865 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2866 if (isl_int_is_neg(s)) {
2867 isl_int_clear(s);
2868 return isl_bool_false;
2872 isl_int_clear(s);
2874 return isl_bool_true;
2877 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2878 __isl_keep isl_vec *vec)
2880 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2883 struct isl_basic_map *isl_basic_map_intersect(
2884 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2886 struct isl_vec *sample = NULL;
2888 if (!bmap1 || !bmap2)
2889 goto error;
2891 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2892 bmap2->dim, isl_dim_param), goto error);
2893 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2894 isl_space_dim(bmap1->dim, isl_dim_param) &&
2895 isl_space_dim(bmap2->dim, isl_dim_all) !=
2896 isl_space_dim(bmap2->dim, isl_dim_param))
2897 return isl_basic_map_intersect(bmap2, bmap1);
2899 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2900 isl_space_dim(bmap2->dim, isl_dim_param))
2901 isl_assert(bmap1->ctx,
2902 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2904 if (isl_basic_map_plain_is_empty(bmap1)) {
2905 isl_basic_map_free(bmap2);
2906 return bmap1;
2908 if (isl_basic_map_plain_is_empty(bmap2)) {
2909 isl_basic_map_free(bmap1);
2910 return bmap2;
2913 if (bmap1->sample &&
2914 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2915 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2916 sample = isl_vec_copy(bmap1->sample);
2917 else if (bmap2->sample &&
2918 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2919 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2920 sample = isl_vec_copy(bmap2->sample);
2922 bmap1 = isl_basic_map_cow(bmap1);
2923 if (!bmap1)
2924 goto error;
2925 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2926 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2927 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2929 if (!bmap1)
2930 isl_vec_free(sample);
2931 else if (sample) {
2932 isl_vec_free(bmap1->sample);
2933 bmap1->sample = sample;
2936 bmap1 = isl_basic_map_simplify(bmap1);
2937 return isl_basic_map_finalize(bmap1);
2938 error:
2939 if (sample)
2940 isl_vec_free(sample);
2941 isl_basic_map_free(bmap1);
2942 isl_basic_map_free(bmap2);
2943 return NULL;
2946 struct isl_basic_set *isl_basic_set_intersect(
2947 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2949 return (struct isl_basic_set *)
2950 isl_basic_map_intersect(
2951 (struct isl_basic_map *)bset1,
2952 (struct isl_basic_map *)bset2);
2955 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2956 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2958 return isl_basic_set_intersect(bset1, bset2);
2961 /* Special case of isl_map_intersect, where both map1 and map2
2962 * are convex, without any divs and such that either map1 or map2
2963 * contains a single constraint. This constraint is then simply
2964 * added to the other map.
2966 static __isl_give isl_map *map_intersect_add_constraint(
2967 __isl_take isl_map *map1, __isl_take isl_map *map2)
2969 isl_assert(map1->ctx, map1->n == 1, goto error);
2970 isl_assert(map2->ctx, map1->n == 1, goto error);
2971 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2972 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2974 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2975 return isl_map_intersect(map2, map1);
2977 isl_assert(map2->ctx,
2978 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2980 map1 = isl_map_cow(map1);
2981 if (!map1)
2982 goto error;
2983 if (isl_map_plain_is_empty(map1)) {
2984 isl_map_free(map2);
2985 return map1;
2987 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2988 if (map2->p[0]->n_eq == 1)
2989 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2990 else
2991 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2992 map2->p[0]->ineq[0]);
2994 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2995 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2996 if (!map1->p[0])
2997 goto error;
2999 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3000 isl_basic_map_free(map1->p[0]);
3001 map1->n = 0;
3004 isl_map_free(map2);
3006 return map1;
3007 error:
3008 isl_map_free(map1);
3009 isl_map_free(map2);
3010 return NULL;
3013 /* map2 may be either a parameter domain or a map living in the same
3014 * space as map1.
3016 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3017 __isl_take isl_map *map2)
3019 unsigned flags = 0;
3020 isl_map *result;
3021 int i, j;
3023 if (!map1 || !map2)
3024 goto error;
3026 if ((isl_map_plain_is_empty(map1) ||
3027 isl_map_plain_is_universe(map2)) &&
3028 isl_space_is_equal(map1->dim, map2->dim)) {
3029 isl_map_free(map2);
3030 return map1;
3032 if ((isl_map_plain_is_empty(map2) ||
3033 isl_map_plain_is_universe(map1)) &&
3034 isl_space_is_equal(map1->dim, map2->dim)) {
3035 isl_map_free(map1);
3036 return map2;
3039 if (map1->n == 1 && map2->n == 1 &&
3040 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3041 isl_space_is_equal(map1->dim, map2->dim) &&
3042 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3043 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3044 return map_intersect_add_constraint(map1, map2);
3046 if (isl_space_dim(map2->dim, isl_dim_all) !=
3047 isl_space_dim(map2->dim, isl_dim_param))
3048 isl_assert(map1->ctx,
3049 isl_space_is_equal(map1->dim, map2->dim), goto error);
3051 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3052 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3053 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3055 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3056 map1->n * map2->n, flags);
3057 if (!result)
3058 goto error;
3059 for (i = 0; i < map1->n; ++i)
3060 for (j = 0; j < map2->n; ++j) {
3061 struct isl_basic_map *part;
3062 part = isl_basic_map_intersect(
3063 isl_basic_map_copy(map1->p[i]),
3064 isl_basic_map_copy(map2->p[j]));
3065 if (isl_basic_map_is_empty(part) < 0)
3066 part = isl_basic_map_free(part);
3067 result = isl_map_add_basic_map(result, part);
3068 if (!result)
3069 goto error;
3071 isl_map_free(map1);
3072 isl_map_free(map2);
3073 return result;
3074 error:
3075 isl_map_free(map1);
3076 isl_map_free(map2);
3077 return NULL;
3080 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3081 __isl_take isl_map *map2)
3083 if (!map1 || !map2)
3084 goto error;
3085 if (!isl_space_is_equal(map1->dim, map2->dim))
3086 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3087 "spaces don't match", goto error);
3088 return map_intersect_internal(map1, map2);
3089 error:
3090 isl_map_free(map1);
3091 isl_map_free(map2);
3092 return NULL;
3095 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3096 __isl_take isl_map *map2)
3098 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3101 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3103 return (struct isl_set *)
3104 isl_map_intersect((struct isl_map *)set1,
3105 (struct isl_map *)set2);
3108 /* map_intersect_internal accepts intersections
3109 * with parameter domains, so we can just call that function.
3111 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3112 __isl_take isl_set *params)
3114 return map_intersect_internal(map, params);
3117 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3118 __isl_take isl_map *map2)
3120 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3123 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3124 __isl_take isl_set *params)
3126 return isl_map_intersect_params(set, params);
3129 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3131 isl_space *space;
3132 unsigned pos, n1, n2;
3134 if (!bmap)
3135 return NULL;
3136 bmap = isl_basic_map_cow(bmap);
3137 if (!bmap)
3138 return NULL;
3139 space = isl_space_reverse(isl_space_copy(bmap->dim));
3140 pos = isl_basic_map_offset(bmap, isl_dim_in);
3141 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3142 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3143 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3144 return isl_basic_map_reset_space(bmap, space);
3147 static __isl_give isl_basic_map *basic_map_space_reset(
3148 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3150 isl_space *space;
3152 if (!bmap)
3153 return NULL;
3154 if (!isl_space_is_named_or_nested(bmap->dim, type))
3155 return bmap;
3157 space = isl_basic_map_get_space(bmap);
3158 space = isl_space_reset(space, type);
3159 bmap = isl_basic_map_reset_space(bmap, space);
3160 return bmap;
3163 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3164 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3165 unsigned pos, unsigned n)
3167 isl_space *res_dim;
3168 struct isl_basic_map *res;
3169 struct isl_dim_map *dim_map;
3170 unsigned total, off;
3171 enum isl_dim_type t;
3173 if (n == 0)
3174 return basic_map_space_reset(bmap, type);
3176 if (!bmap)
3177 return NULL;
3179 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3181 total = isl_basic_map_total_dim(bmap) + n;
3182 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3183 off = 0;
3184 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3185 if (t != type) {
3186 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3187 } else {
3188 unsigned size = isl_basic_map_dim(bmap, t);
3189 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3190 0, pos, off);
3191 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3192 pos, size - pos, off + pos + n);
3194 off += isl_space_dim(res_dim, t);
3196 isl_dim_map_div(dim_map, bmap, off);
3198 res = isl_basic_map_alloc_space(res_dim,
3199 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3200 if (isl_basic_map_is_rational(bmap))
3201 res = isl_basic_map_set_rational(res);
3202 if (isl_basic_map_plain_is_empty(bmap)) {
3203 isl_basic_map_free(bmap);
3204 free(dim_map);
3205 return isl_basic_map_set_to_empty(res);
3207 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3208 return isl_basic_map_finalize(res);
3211 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3212 __isl_take isl_basic_set *bset,
3213 enum isl_dim_type type, unsigned pos, unsigned n)
3215 return isl_basic_map_insert_dims(bset, type, pos, n);
3218 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3219 enum isl_dim_type type, unsigned n)
3221 if (!bmap)
3222 return NULL;
3223 return isl_basic_map_insert_dims(bmap, type,
3224 isl_basic_map_dim(bmap, type), n);
3227 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3228 enum isl_dim_type type, unsigned n)
3230 if (!bset)
3231 return NULL;
3232 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3233 return isl_basic_map_add_dims(bset, type, n);
3234 error:
3235 isl_basic_set_free(bset);
3236 return NULL;
3239 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3240 enum isl_dim_type type)
3242 isl_space *space;
3244 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3245 return map;
3247 space = isl_map_get_space(map);
3248 space = isl_space_reset(space, type);
3249 map = isl_map_reset_space(map, space);
3250 return map;
3253 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3254 enum isl_dim_type type, unsigned pos, unsigned n)
3256 int i;
3258 if (n == 0)
3259 return map_space_reset(map, type);
3261 map = isl_map_cow(map);
3262 if (!map)
3263 return NULL;
3265 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3266 if (!map->dim)
3267 goto error;
3269 for (i = 0; i < map->n; ++i) {
3270 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3271 if (!map->p[i])
3272 goto error;
3275 return map;
3276 error:
3277 isl_map_free(map);
3278 return NULL;
3281 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3282 enum isl_dim_type type, unsigned pos, unsigned n)
3284 return isl_map_insert_dims(set, type, pos, n);
3287 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3288 enum isl_dim_type type, unsigned n)
3290 if (!map)
3291 return NULL;
3292 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3295 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3296 enum isl_dim_type type, unsigned n)
3298 if (!set)
3299 return NULL;
3300 isl_assert(set->ctx, type != isl_dim_in, goto error);
3301 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3302 error:
3303 isl_set_free(set);
3304 return NULL;
3307 __isl_give isl_basic_map *isl_basic_map_move_dims(
3308 __isl_take isl_basic_map *bmap,
3309 enum isl_dim_type dst_type, unsigned dst_pos,
3310 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3312 struct isl_dim_map *dim_map;
3313 struct isl_basic_map *res;
3314 enum isl_dim_type t;
3315 unsigned total, off;
3317 if (!bmap)
3318 return NULL;
3319 if (n == 0)
3320 return bmap;
3322 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3323 goto error);
3325 if (dst_type == src_type && dst_pos == src_pos)
3326 return bmap;
3328 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3330 if (pos(bmap->dim, dst_type) + dst_pos ==
3331 pos(bmap->dim, src_type) + src_pos +
3332 ((src_type < dst_type) ? n : 0)) {
3333 bmap = isl_basic_map_cow(bmap);
3334 if (!bmap)
3335 return NULL;
3337 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3338 src_type, src_pos, n);
3339 if (!bmap->dim)
3340 goto error;
3342 bmap = isl_basic_map_finalize(bmap);
3344 return bmap;
3347 total = isl_basic_map_total_dim(bmap);
3348 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3350 off = 0;
3351 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3352 unsigned size = isl_space_dim(bmap->dim, t);
3353 if (t == dst_type) {
3354 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3355 0, dst_pos, off);
3356 off += dst_pos;
3357 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3358 src_pos, n, off);
3359 off += n;
3360 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3361 dst_pos, size - dst_pos, off);
3362 off += size - dst_pos;
3363 } else if (t == src_type) {
3364 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3365 0, src_pos, off);
3366 off += src_pos;
3367 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3368 src_pos + n, size - src_pos - n, off);
3369 off += size - src_pos - n;
3370 } else {
3371 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3372 off += size;
3375 isl_dim_map_div(dim_map, bmap, off);
3377 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3378 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3379 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3380 if (!bmap)
3381 goto error;
3383 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3384 src_type, src_pos, n);
3385 if (!bmap->dim)
3386 goto error;
3388 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3389 bmap = isl_basic_map_gauss(bmap, NULL);
3390 bmap = isl_basic_map_finalize(bmap);
3392 return bmap;
3393 error:
3394 isl_basic_map_free(bmap);
3395 return NULL;
3398 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3399 enum isl_dim_type dst_type, unsigned dst_pos,
3400 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3402 return (isl_basic_set *)isl_basic_map_move_dims(
3403 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3406 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3407 enum isl_dim_type dst_type, unsigned dst_pos,
3408 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3410 if (!set)
3411 return NULL;
3412 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3413 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3414 src_type, src_pos, n);
3415 error:
3416 isl_set_free(set);
3417 return NULL;
3420 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3421 enum isl_dim_type dst_type, unsigned dst_pos,
3422 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3424 int i;
3426 if (!map)
3427 return NULL;
3428 if (n == 0)
3429 return map;
3431 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3432 goto error);
3434 if (dst_type == src_type && dst_pos == src_pos)
3435 return map;
3437 isl_assert(map->ctx, dst_type != src_type, goto error);
3439 map = isl_map_cow(map);
3440 if (!map)
3441 return NULL;
3443 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3444 if (!map->dim)
3445 goto error;
3447 for (i = 0; i < map->n; ++i) {
3448 map->p[i] = isl_basic_map_move_dims(map->p[i],
3449 dst_type, dst_pos,
3450 src_type, src_pos, n);
3451 if (!map->p[i])
3452 goto error;
3455 return map;
3456 error:
3457 isl_map_free(map);
3458 return NULL;
3461 /* Move the specified dimensions to the last columns right before
3462 * the divs. Don't change the dimension specification of bmap.
3463 * That's the responsibility of the caller.
3465 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3466 enum isl_dim_type type, unsigned first, unsigned n)
3468 struct isl_dim_map *dim_map;
3469 struct isl_basic_map *res;
3470 enum isl_dim_type t;
3471 unsigned total, off;
3473 if (!bmap)
3474 return NULL;
3475 if (pos(bmap->dim, type) + first + n ==
3476 1 + isl_space_dim(bmap->dim, isl_dim_all))
3477 return bmap;
3479 total = isl_basic_map_total_dim(bmap);
3480 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3482 off = 0;
3483 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3484 unsigned size = isl_space_dim(bmap->dim, t);
3485 if (t == type) {
3486 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3487 0, first, off);
3488 off += first;
3489 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3490 first, n, total - bmap->n_div - n);
3491 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3492 first + n, size - (first + n), off);
3493 off += size - (first + n);
3494 } else {
3495 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3496 off += size;
3499 isl_dim_map_div(dim_map, bmap, off + n);
3501 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3502 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3503 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3504 return res;
3507 /* Insert "n" rows in the divs of "bmap".
3509 * The number of columns is not changed, which means that the last
3510 * dimensions of "bmap" are being reintepreted as the new divs.
3511 * The space of "bmap" is not adjusted, however, which means
3512 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3513 * from the space of "bmap" is the responsibility of the caller.
3515 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3516 int n)
3518 int i;
3519 size_t row_size;
3520 isl_int **new_div;
3521 isl_int *old;
3523 bmap = isl_basic_map_cow(bmap);
3524 if (!bmap)
3525 return NULL;
3527 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3528 old = bmap->block2.data;
3529 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3530 (bmap->extra + n) * (1 + row_size));
3531 if (!bmap->block2.data)
3532 return isl_basic_map_free(bmap);
3533 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3534 if (!new_div)
3535 return isl_basic_map_free(bmap);
3536 for (i = 0; i < n; ++i) {
3537 new_div[i] = bmap->block2.data +
3538 (bmap->extra + i) * (1 + row_size);
3539 isl_seq_clr(new_div[i], 1 + row_size);
3541 for (i = 0; i < bmap->extra; ++i)
3542 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3543 free(bmap->div);
3544 bmap->div = new_div;
3545 bmap->n_div += n;
3546 bmap->extra += n;
3548 return bmap;
3551 /* Drop constraints from "bmap" that only involve the variables
3552 * of "type" in the range [first, first + n] that are not related
3553 * to any of the variables outside that interval.
3554 * These constraints cannot influence the values for the variables
3555 * outside the interval, except in case they cause "bmap" to be empty.
3556 * Only drop the constraints if "bmap" is known to be non-empty.
3558 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3559 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3560 unsigned first, unsigned n)
3562 int i;
3563 int *groups;
3564 unsigned dim, n_div;
3565 isl_bool non_empty;
3567 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3568 if (non_empty < 0)
3569 return isl_basic_map_free(bmap);
3570 if (!non_empty)
3571 return bmap;
3573 dim = isl_basic_map_dim(bmap, isl_dim_all);
3574 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3575 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3576 if (!groups)
3577 return isl_basic_map_free(bmap);
3578 first += isl_basic_map_offset(bmap, type) - 1;
3579 for (i = 0; i < first; ++i)
3580 groups[i] = -1;
3581 for (i = first + n; i < dim - n_div; ++i)
3582 groups[i] = -1;
3584 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3586 return bmap;
3589 /* Turn the n dimensions of type type, starting at first
3590 * into existentially quantified variables.
3592 * If a subset of the projected out variables are unrelated
3593 * to any of the variables that remain, then the constraints
3594 * involving this subset are simply dropped first.
3596 __isl_give isl_basic_map *isl_basic_map_project_out(
3597 __isl_take isl_basic_map *bmap,
3598 enum isl_dim_type type, unsigned first, unsigned n)
3600 if (n == 0)
3601 return basic_map_space_reset(bmap, type);
3602 if (type == isl_dim_div)
3603 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3604 "cannot project out existentially quantified variables",
3605 return isl_basic_map_free(bmap));
3607 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3608 if (!bmap)
3609 return NULL;
3611 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3612 return isl_basic_map_remove_dims(bmap, type, first, n);
3614 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3615 goto error);
3617 bmap = move_last(bmap, type, first, n);
3618 bmap = isl_basic_map_cow(bmap);
3619 bmap = insert_div_rows(bmap, n);
3620 if (!bmap)
3621 return NULL;
3623 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3624 if (!bmap->dim)
3625 goto error;
3626 bmap = isl_basic_map_simplify(bmap);
3627 bmap = isl_basic_map_drop_redundant_divs(bmap);
3628 return isl_basic_map_finalize(bmap);
3629 error:
3630 isl_basic_map_free(bmap);
3631 return NULL;
3634 /* Turn the n dimensions of type type, starting at first
3635 * into existentially quantified variables.
3637 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3638 enum isl_dim_type type, unsigned first, unsigned n)
3640 return (isl_basic_set *)isl_basic_map_project_out(
3641 (isl_basic_map *)bset, type, first, n);
3644 /* Turn the n dimensions of type type, starting at first
3645 * into existentially quantified variables.
3647 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3648 enum isl_dim_type type, unsigned first, unsigned n)
3650 int i;
3652 if (!map)
3653 return NULL;
3655 if (n == 0)
3656 return map_space_reset(map, type);
3658 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3660 map = isl_map_cow(map);
3661 if (!map)
3662 return NULL;
3664 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3665 if (!map->dim)
3666 goto error;
3668 for (i = 0; i < map->n; ++i) {
3669 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3670 if (!map->p[i])
3671 goto error;
3674 return map;
3675 error:
3676 isl_map_free(map);
3677 return NULL;
3680 /* Turn the n dimensions of type type, starting at first
3681 * into existentially quantified variables.
3683 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3684 enum isl_dim_type type, unsigned first, unsigned n)
3686 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3689 /* Return a map that projects the elements in "set" onto their
3690 * "n" set dimensions starting at "first".
3691 * "type" should be equal to isl_dim_set.
3693 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3694 enum isl_dim_type type, unsigned first, unsigned n)
3696 int i;
3697 int dim;
3698 isl_map *map;
3700 if (!set)
3701 return NULL;
3702 if (type != isl_dim_set)
3703 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3704 "only set dimensions can be projected out", goto error);
3705 dim = isl_set_dim(set, isl_dim_set);
3706 if (first + n > dim || first + n < first)
3707 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3708 "index out of bounds", goto error);
3710 map = isl_map_from_domain(set);
3711 map = isl_map_add_dims(map, isl_dim_out, n);
3712 for (i = 0; i < n; ++i)
3713 map = isl_map_equate(map, isl_dim_in, first + i,
3714 isl_dim_out, i);
3715 return map;
3716 error:
3717 isl_set_free(set);
3718 return NULL;
3721 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3723 int i, j;
3725 for (i = 0; i < n; ++i) {
3726 j = isl_basic_map_alloc_div(bmap);
3727 if (j < 0)
3728 goto error;
3729 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3731 return bmap;
3732 error:
3733 isl_basic_map_free(bmap);
3734 return NULL;
3737 struct isl_basic_map *isl_basic_map_apply_range(
3738 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3740 isl_space *dim_result = NULL;
3741 struct isl_basic_map *bmap;
3742 unsigned n_in, n_out, n, nparam, total, pos;
3743 struct isl_dim_map *dim_map1, *dim_map2;
3745 if (!bmap1 || !bmap2)
3746 goto error;
3747 if (!isl_space_match(bmap1->dim, isl_dim_param,
3748 bmap2->dim, isl_dim_param))
3749 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3750 "parameters don't match", goto error);
3751 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3752 bmap2->dim, isl_dim_in))
3753 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3754 "spaces don't match", goto error);
3756 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3757 isl_space_copy(bmap2->dim));
3759 n_in = isl_basic_map_n_in(bmap1);
3760 n_out = isl_basic_map_n_out(bmap2);
3761 n = isl_basic_map_n_out(bmap1);
3762 nparam = isl_basic_map_n_param(bmap1);
3764 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3765 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3766 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3767 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3768 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3769 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3770 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3771 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3772 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3773 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3774 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3776 bmap = isl_basic_map_alloc_space(dim_result,
3777 bmap1->n_div + bmap2->n_div + n,
3778 bmap1->n_eq + bmap2->n_eq,
3779 bmap1->n_ineq + bmap2->n_ineq);
3780 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3781 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3782 bmap = add_divs(bmap, n);
3783 bmap = isl_basic_map_simplify(bmap);
3784 bmap = isl_basic_map_drop_redundant_divs(bmap);
3785 return isl_basic_map_finalize(bmap);
3786 error:
3787 isl_basic_map_free(bmap1);
3788 isl_basic_map_free(bmap2);
3789 return NULL;
3792 struct isl_basic_set *isl_basic_set_apply(
3793 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3795 if (!bset || !bmap)
3796 goto error;
3798 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3799 goto error);
3801 return (struct isl_basic_set *)
3802 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3803 error:
3804 isl_basic_set_free(bset);
3805 isl_basic_map_free(bmap);
3806 return NULL;
3809 struct isl_basic_map *isl_basic_map_apply_domain(
3810 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3812 if (!bmap1 || !bmap2)
3813 goto error;
3815 isl_assert(bmap1->ctx,
3816 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3817 isl_assert(bmap1->ctx,
3818 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3819 goto error);
3821 bmap1 = isl_basic_map_reverse(bmap1);
3822 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3823 return isl_basic_map_reverse(bmap1);
3824 error:
3825 isl_basic_map_free(bmap1);
3826 isl_basic_map_free(bmap2);
3827 return NULL;
3830 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3831 * A \cap B -> f(A) + f(B)
3833 struct isl_basic_map *isl_basic_map_sum(
3834 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3836 unsigned n_in, n_out, nparam, total, pos;
3837 struct isl_basic_map *bmap = NULL;
3838 struct isl_dim_map *dim_map1, *dim_map2;
3839 int i;
3841 if (!bmap1 || !bmap2)
3842 goto error;
3844 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3845 goto error);
3847 nparam = isl_basic_map_n_param(bmap1);
3848 n_in = isl_basic_map_n_in(bmap1);
3849 n_out = isl_basic_map_n_out(bmap1);
3851 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3852 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3853 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3854 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3855 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3856 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3857 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3858 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3859 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3860 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3861 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3863 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3864 bmap1->n_div + bmap2->n_div + 2 * n_out,
3865 bmap1->n_eq + bmap2->n_eq + n_out,
3866 bmap1->n_ineq + bmap2->n_ineq);
3867 for (i = 0; i < n_out; ++i) {
3868 int j = isl_basic_map_alloc_equality(bmap);
3869 if (j < 0)
3870 goto error;
3871 isl_seq_clr(bmap->eq[j], 1+total);
3872 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3873 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3874 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3876 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3877 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3878 bmap = add_divs(bmap, 2 * n_out);
3880 bmap = isl_basic_map_simplify(bmap);
3881 return isl_basic_map_finalize(bmap);
3882 error:
3883 isl_basic_map_free(bmap);
3884 isl_basic_map_free(bmap1);
3885 isl_basic_map_free(bmap2);
3886 return NULL;
3889 /* Given two maps A -> f(A) and B -> g(B), construct a map
3890 * A \cap B -> f(A) + f(B)
3892 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3894 struct isl_map *result;
3895 int i, j;
3897 if (!map1 || !map2)
3898 goto error;
3900 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3902 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3903 map1->n * map2->n, 0);
3904 if (!result)
3905 goto error;
3906 for (i = 0; i < map1->n; ++i)
3907 for (j = 0; j < map2->n; ++j) {
3908 struct isl_basic_map *part;
3909 part = isl_basic_map_sum(
3910 isl_basic_map_copy(map1->p[i]),
3911 isl_basic_map_copy(map2->p[j]));
3912 if (isl_basic_map_is_empty(part))
3913 isl_basic_map_free(part);
3914 else
3915 result = isl_map_add_basic_map(result, part);
3916 if (!result)
3917 goto error;
3919 isl_map_free(map1);
3920 isl_map_free(map2);
3921 return result;
3922 error:
3923 isl_map_free(map1);
3924 isl_map_free(map2);
3925 return NULL;
3928 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3929 __isl_take isl_set *set2)
3931 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3934 /* Given a basic map A -> f(A), construct A -> -f(A).
3936 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3938 int i, j;
3939 unsigned off, n;
3941 bmap = isl_basic_map_cow(bmap);
3942 if (!bmap)
3943 return NULL;
3945 n = isl_basic_map_dim(bmap, isl_dim_out);
3946 off = isl_basic_map_offset(bmap, isl_dim_out);
3947 for (i = 0; i < bmap->n_eq; ++i)
3948 for (j = 0; j < n; ++j)
3949 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3950 for (i = 0; i < bmap->n_ineq; ++i)
3951 for (j = 0; j < n; ++j)
3952 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3953 for (i = 0; i < bmap->n_div; ++i)
3954 for (j = 0; j < n; ++j)
3955 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3956 bmap = isl_basic_map_gauss(bmap, NULL);
3957 return isl_basic_map_finalize(bmap);
3960 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3962 return isl_basic_map_neg(bset);
3965 /* Given a map A -> f(A), construct A -> -f(A).
3967 struct isl_map *isl_map_neg(struct isl_map *map)
3969 int i;
3971 map = isl_map_cow(map);
3972 if (!map)
3973 return NULL;
3975 for (i = 0; i < map->n; ++i) {
3976 map->p[i] = isl_basic_map_neg(map->p[i]);
3977 if (!map->p[i])
3978 goto error;
3981 return map;
3982 error:
3983 isl_map_free(map);
3984 return NULL;
3987 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3989 return (isl_set *)isl_map_neg((isl_map *)set);
3992 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3993 * A -> floor(f(A)/d).
3995 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3996 isl_int d)
3998 unsigned n_in, n_out, nparam, total, pos;
3999 struct isl_basic_map *result = NULL;
4000 struct isl_dim_map *dim_map;
4001 int i;
4003 if (!bmap)
4004 return NULL;
4006 nparam = isl_basic_map_n_param(bmap);
4007 n_in = isl_basic_map_n_in(bmap);
4008 n_out = isl_basic_map_n_out(bmap);
4010 total = nparam + n_in + n_out + bmap->n_div + n_out;
4011 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4012 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4013 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4014 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4015 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4017 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4018 bmap->n_div + n_out,
4019 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4020 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4021 result = add_divs(result, n_out);
4022 for (i = 0; i < n_out; ++i) {
4023 int j;
4024 j = isl_basic_map_alloc_inequality(result);
4025 if (j < 0)
4026 goto error;
4027 isl_seq_clr(result->ineq[j], 1+total);
4028 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4029 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4030 j = isl_basic_map_alloc_inequality(result);
4031 if (j < 0)
4032 goto error;
4033 isl_seq_clr(result->ineq[j], 1+total);
4034 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4035 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4036 isl_int_sub_ui(result->ineq[j][0], d, 1);
4039 result = isl_basic_map_simplify(result);
4040 return isl_basic_map_finalize(result);
4041 error:
4042 isl_basic_map_free(result);
4043 return NULL;
4046 /* Given a map A -> f(A) and an integer d, construct a map
4047 * A -> floor(f(A)/d).
4049 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4051 int i;
4053 map = isl_map_cow(map);
4054 if (!map)
4055 return NULL;
4057 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4058 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4059 for (i = 0; i < map->n; ++i) {
4060 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4061 if (!map->p[i])
4062 goto error;
4065 return map;
4066 error:
4067 isl_map_free(map);
4068 return NULL;
4071 /* Given a map A -> f(A) and an integer d, construct a map
4072 * A -> floor(f(A)/d).
4074 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4075 __isl_take isl_val *d)
4077 if (!map || !d)
4078 goto error;
4079 if (!isl_val_is_int(d))
4080 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4081 "expecting integer denominator", goto error);
4082 map = isl_map_floordiv(map, d->n);
4083 isl_val_free(d);
4084 return map;
4085 error:
4086 isl_map_free(map);
4087 isl_val_free(d);
4088 return NULL;
4091 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4093 int i;
4094 unsigned nparam;
4095 unsigned n_in;
4097 i = isl_basic_map_alloc_equality(bmap);
4098 if (i < 0)
4099 goto error;
4100 nparam = isl_basic_map_n_param(bmap);
4101 n_in = isl_basic_map_n_in(bmap);
4102 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4103 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4104 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4105 return isl_basic_map_finalize(bmap);
4106 error:
4107 isl_basic_map_free(bmap);
4108 return NULL;
4111 /* Add a constraints to "bmap" expressing i_pos < o_pos
4113 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4115 int i;
4116 unsigned nparam;
4117 unsigned n_in;
4119 i = isl_basic_map_alloc_inequality(bmap);
4120 if (i < 0)
4121 goto error;
4122 nparam = isl_basic_map_n_param(bmap);
4123 n_in = isl_basic_map_n_in(bmap);
4124 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4125 isl_int_set_si(bmap->ineq[i][0], -1);
4126 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4127 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4128 return isl_basic_map_finalize(bmap);
4129 error:
4130 isl_basic_map_free(bmap);
4131 return NULL;
4134 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4136 static __isl_give isl_basic_map *var_less_or_equal(
4137 __isl_take isl_basic_map *bmap, unsigned pos)
4139 int i;
4140 unsigned nparam;
4141 unsigned n_in;
4143 i = isl_basic_map_alloc_inequality(bmap);
4144 if (i < 0)
4145 goto error;
4146 nparam = isl_basic_map_n_param(bmap);
4147 n_in = isl_basic_map_n_in(bmap);
4148 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4149 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4150 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4151 return isl_basic_map_finalize(bmap);
4152 error:
4153 isl_basic_map_free(bmap);
4154 return NULL;
4157 /* Add a constraints to "bmap" expressing i_pos > o_pos
4159 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4161 int i;
4162 unsigned nparam;
4163 unsigned n_in;
4165 i = isl_basic_map_alloc_inequality(bmap);
4166 if (i < 0)
4167 goto error;
4168 nparam = isl_basic_map_n_param(bmap);
4169 n_in = isl_basic_map_n_in(bmap);
4170 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4171 isl_int_set_si(bmap->ineq[i][0], -1);
4172 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4173 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4174 return isl_basic_map_finalize(bmap);
4175 error:
4176 isl_basic_map_free(bmap);
4177 return NULL;
4180 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4182 static __isl_give isl_basic_map *var_more_or_equal(
4183 __isl_take isl_basic_map *bmap, unsigned pos)
4185 int i;
4186 unsigned nparam;
4187 unsigned n_in;
4189 i = isl_basic_map_alloc_inequality(bmap);
4190 if (i < 0)
4191 goto error;
4192 nparam = isl_basic_map_n_param(bmap);
4193 n_in = isl_basic_map_n_in(bmap);
4194 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4195 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4196 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4197 return isl_basic_map_finalize(bmap);
4198 error:
4199 isl_basic_map_free(bmap);
4200 return NULL;
4203 __isl_give isl_basic_map *isl_basic_map_equal(
4204 __isl_take isl_space *dim, unsigned n_equal)
4206 int i;
4207 struct isl_basic_map *bmap;
4208 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4209 if (!bmap)
4210 return NULL;
4211 for (i = 0; i < n_equal && bmap; ++i)
4212 bmap = var_equal(bmap, i);
4213 return isl_basic_map_finalize(bmap);
4216 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4218 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4219 unsigned pos)
4221 int i;
4222 struct isl_basic_map *bmap;
4223 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4224 if (!bmap)
4225 return NULL;
4226 for (i = 0; i < pos && bmap; ++i)
4227 bmap = var_equal(bmap, i);
4228 if (bmap)
4229 bmap = var_less(bmap, pos);
4230 return isl_basic_map_finalize(bmap);
4233 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4235 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4236 __isl_take isl_space *dim, unsigned pos)
4238 int i;
4239 isl_basic_map *bmap;
4241 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4242 for (i = 0; i < pos; ++i)
4243 bmap = var_equal(bmap, i);
4244 bmap = var_less_or_equal(bmap, pos);
4245 return isl_basic_map_finalize(bmap);
4248 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4250 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4251 unsigned pos)
4253 int i;
4254 struct isl_basic_map *bmap;
4255 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4256 if (!bmap)
4257 return NULL;
4258 for (i = 0; i < pos && bmap; ++i)
4259 bmap = var_equal(bmap, i);
4260 if (bmap)
4261 bmap = var_more(bmap, pos);
4262 return isl_basic_map_finalize(bmap);
4265 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4267 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4268 __isl_take isl_space *dim, unsigned pos)
4270 int i;
4271 isl_basic_map *bmap;
4273 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4274 for (i = 0; i < pos; ++i)
4275 bmap = var_equal(bmap, i);
4276 bmap = var_more_or_equal(bmap, pos);
4277 return isl_basic_map_finalize(bmap);
4280 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4281 unsigned n, int equal)
4283 struct isl_map *map;
4284 int i;
4286 if (n == 0 && equal)
4287 return isl_map_universe(dims);
4289 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4291 for (i = 0; i + 1 < n; ++i)
4292 map = isl_map_add_basic_map(map,
4293 isl_basic_map_less_at(isl_space_copy(dims), i));
4294 if (n > 0) {
4295 if (equal)
4296 map = isl_map_add_basic_map(map,
4297 isl_basic_map_less_or_equal_at(dims, n - 1));
4298 else
4299 map = isl_map_add_basic_map(map,
4300 isl_basic_map_less_at(dims, n - 1));
4301 } else
4302 isl_space_free(dims);
4304 return map;
4307 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4309 if (!dims)
4310 return NULL;
4311 return map_lex_lte_first(dims, dims->n_out, equal);
4314 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4316 return map_lex_lte_first(dim, n, 0);
4319 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4321 return map_lex_lte_first(dim, n, 1);
4324 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4326 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4329 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4331 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4334 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4335 unsigned n, int equal)
4337 struct isl_map *map;
4338 int i;
4340 if (n == 0 && equal)
4341 return isl_map_universe(dims);
4343 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4345 for (i = 0; i + 1 < n; ++i)
4346 map = isl_map_add_basic_map(map,
4347 isl_basic_map_more_at(isl_space_copy(dims), i));
4348 if (n > 0) {
4349 if (equal)
4350 map = isl_map_add_basic_map(map,
4351 isl_basic_map_more_or_equal_at(dims, n - 1));
4352 else
4353 map = isl_map_add_basic_map(map,
4354 isl_basic_map_more_at(dims, n - 1));
4355 } else
4356 isl_space_free(dims);
4358 return map;
4361 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4363 if (!dims)
4364 return NULL;
4365 return map_lex_gte_first(dims, dims->n_out, equal);
4368 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4370 return map_lex_gte_first(dim, n, 0);
4373 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4375 return map_lex_gte_first(dim, n, 1);
4378 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4380 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4383 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4385 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4388 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4389 __isl_take isl_set *set2)
4391 isl_map *map;
4392 map = isl_map_lex_le(isl_set_get_space(set1));
4393 map = isl_map_intersect_domain(map, set1);
4394 map = isl_map_intersect_range(map, set2);
4395 return map;
4398 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4399 __isl_take isl_set *set2)
4401 isl_map *map;
4402 map = isl_map_lex_lt(isl_set_get_space(set1));
4403 map = isl_map_intersect_domain(map, set1);
4404 map = isl_map_intersect_range(map, set2);
4405 return map;
4408 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4409 __isl_take isl_set *set2)
4411 isl_map *map;
4412 map = isl_map_lex_ge(isl_set_get_space(set1));
4413 map = isl_map_intersect_domain(map, set1);
4414 map = isl_map_intersect_range(map, set2);
4415 return map;
4418 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4419 __isl_take isl_set *set2)
4421 isl_map *map;
4422 map = isl_map_lex_gt(isl_set_get_space(set1));
4423 map = isl_map_intersect_domain(map, set1);
4424 map = isl_map_intersect_range(map, set2);
4425 return map;
4428 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4429 __isl_take isl_map *map2)
4431 isl_map *map;
4432 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4433 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4434 map = isl_map_apply_range(map, isl_map_reverse(map2));
4435 return map;
4438 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4439 __isl_take isl_map *map2)
4441 isl_map *map;
4442 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4443 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4444 map = isl_map_apply_range(map, isl_map_reverse(map2));
4445 return map;
4448 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4449 __isl_take isl_map *map2)
4451 isl_map *map;
4452 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4453 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4454 map = isl_map_apply_range(map, isl_map_reverse(map2));
4455 return map;
4458 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4459 __isl_take isl_map *map2)
4461 isl_map *map;
4462 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4463 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4464 map = isl_map_apply_range(map, isl_map_reverse(map2));
4465 return map;
4468 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4469 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4471 struct isl_basic_map *bmap;
4473 bset = isl_basic_set_cow(bset);
4474 if (!bset || !dim)
4475 goto error;
4477 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4478 isl_space_free(bset->dim);
4479 bmap = (struct isl_basic_map *) bset;
4480 bmap->dim = dim;
4481 return isl_basic_map_finalize(bmap);
4482 error:
4483 isl_basic_set_free(bset);
4484 isl_space_free(dim);
4485 return NULL;
4488 /* For a div d = floor(f/m), add the constraint
4490 * f - m d >= 0
4492 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4493 unsigned pos, isl_int *div)
4495 int i;
4496 unsigned total = isl_basic_map_total_dim(bmap);
4498 i = isl_basic_map_alloc_inequality(bmap);
4499 if (i < 0)
4500 return -1;
4501 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4502 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4504 return 0;
4507 /* For a div d = floor(f/m), add the constraint
4509 * -(f-(m-1)) + m d >= 0
4511 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4512 unsigned pos, isl_int *div)
4514 int i;
4515 unsigned total = isl_basic_map_total_dim(bmap);
4517 i = isl_basic_map_alloc_inequality(bmap);
4518 if (i < 0)
4519 return -1;
4520 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4521 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4522 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4523 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4525 return 0;
4528 /* For a div d = floor(f/m), add the constraints
4530 * f - m d >= 0
4531 * -(f-(m-1)) + m d >= 0
4533 * Note that the second constraint is the negation of
4535 * f - m d >= m
4537 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4538 unsigned pos, isl_int *div)
4540 if (add_upper_div_constraint(bmap, pos, div) < 0)
4541 return -1;
4542 if (add_lower_div_constraint(bmap, pos, div) < 0)
4543 return -1;
4544 return 0;
4547 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4548 unsigned pos, isl_int *div)
4550 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4551 pos, div);
4554 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4556 unsigned total = isl_basic_map_total_dim(bmap);
4557 unsigned div_pos = total - bmap->n_div + div;
4559 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4560 bmap->div[div]);
4563 /* For each known div d = floor(f/m), add the constraints
4565 * f - m d >= 0
4566 * -(f-(m-1)) + m d >= 0
4568 * Remove duplicate constraints in case of some these div constraints
4569 * already appear in "bmap".
4571 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4572 __isl_take isl_basic_map *bmap)
4574 unsigned n_div;
4576 if (!bmap)
4577 return NULL;
4578 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4579 if (n_div == 0)
4580 return bmap;
4582 bmap = add_known_div_constraints(bmap);
4583 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4584 bmap = isl_basic_map_finalize(bmap);
4585 return bmap;
4588 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4590 * In particular, if this div is of the form d = floor(f/m),
4591 * then add the constraint
4593 * f - m d >= 0
4595 * if sign < 0 or the constraint
4597 * -(f-(m-1)) + m d >= 0
4599 * if sign > 0.
4601 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4602 unsigned div, int sign)
4604 unsigned total;
4605 unsigned div_pos;
4607 if (!bmap)
4608 return -1;
4610 total = isl_basic_map_total_dim(bmap);
4611 div_pos = total - bmap->n_div + div;
4613 if (sign < 0)
4614 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4615 else
4616 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4619 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4621 return isl_basic_map_add_div_constraints(bset, div);
4624 struct isl_basic_set *isl_basic_map_underlying_set(
4625 struct isl_basic_map *bmap)
4627 if (!bmap)
4628 goto error;
4629 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4630 bmap->n_div == 0 &&
4631 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4632 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4633 return (struct isl_basic_set *)bmap;
4634 bmap = isl_basic_map_cow(bmap);
4635 if (!bmap)
4636 goto error;
4637 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4638 if (!bmap->dim)
4639 goto error;
4640 bmap->extra -= bmap->n_div;
4641 bmap->n_div = 0;
4642 bmap = isl_basic_map_finalize(bmap);
4643 return (struct isl_basic_set *)bmap;
4644 error:
4645 isl_basic_map_free(bmap);
4646 return NULL;
4649 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4650 __isl_take isl_basic_set *bset)
4652 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4655 /* Replace each element in "list" by the result of applying
4656 * isl_basic_map_underlying_set to the element.
4658 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4659 __isl_take isl_basic_map_list *list)
4661 int i, n;
4663 if (!list)
4664 return NULL;
4666 n = isl_basic_map_list_n_basic_map(list);
4667 for (i = 0; i < n; ++i) {
4668 isl_basic_map *bmap;
4669 isl_basic_set *bset;
4671 bmap = isl_basic_map_list_get_basic_map(list, i);
4672 bset = isl_basic_set_underlying_set(bmap);
4673 list = isl_basic_set_list_set_basic_set(list, i, bset);
4676 return list;
4679 struct isl_basic_map *isl_basic_map_overlying_set(
4680 struct isl_basic_set *bset, struct isl_basic_map *like)
4682 struct isl_basic_map *bmap;
4683 struct isl_ctx *ctx;
4684 unsigned total;
4685 int i;
4687 if (!bset || !like)
4688 goto error;
4689 ctx = bset->ctx;
4690 isl_assert(ctx, bset->n_div == 0, goto error);
4691 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4692 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4693 goto error);
4694 if (like->n_div == 0) {
4695 isl_space *space = isl_basic_map_get_space(like);
4696 isl_basic_map_free(like);
4697 return isl_basic_map_reset_space(bset, space);
4699 bset = isl_basic_set_cow(bset);
4700 if (!bset)
4701 goto error;
4702 total = bset->dim->n_out + bset->extra;
4703 bmap = (struct isl_basic_map *)bset;
4704 isl_space_free(bmap->dim);
4705 bmap->dim = isl_space_copy(like->dim);
4706 if (!bmap->dim)
4707 goto error;
4708 bmap->n_div = like->n_div;
4709 bmap->extra += like->n_div;
4710 if (bmap->extra) {
4711 unsigned ltotal;
4712 isl_int **div;
4713 ltotal = total - bmap->extra + like->extra;
4714 if (ltotal > total)
4715 ltotal = total;
4716 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4717 bmap->extra * (1 + 1 + total));
4718 if (isl_blk_is_error(bmap->block2))
4719 goto error;
4720 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4721 if (!div)
4722 goto error;
4723 bmap->div = div;
4724 for (i = 0; i < bmap->extra; ++i)
4725 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4726 for (i = 0; i < like->n_div; ++i) {
4727 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4728 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4730 bmap = isl_basic_map_add_known_div_constraints(bmap);
4732 isl_basic_map_free(like);
4733 bmap = isl_basic_map_simplify(bmap);
4734 bmap = isl_basic_map_finalize(bmap);
4735 return bmap;
4736 error:
4737 isl_basic_map_free(like);
4738 isl_basic_set_free(bset);
4739 return NULL;
4742 struct isl_basic_set *isl_basic_set_from_underlying_set(
4743 struct isl_basic_set *bset, struct isl_basic_set *like)
4745 return (struct isl_basic_set *)
4746 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4749 struct isl_set *isl_set_from_underlying_set(
4750 struct isl_set *set, struct isl_basic_set *like)
4752 int i;
4754 if (!set || !like)
4755 goto error;
4756 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4757 goto error);
4758 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4759 isl_basic_set_free(like);
4760 return set;
4762 set = isl_set_cow(set);
4763 if (!set)
4764 goto error;
4765 for (i = 0; i < set->n; ++i) {
4766 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4767 isl_basic_set_copy(like));
4768 if (!set->p[i])
4769 goto error;
4771 isl_space_free(set->dim);
4772 set->dim = isl_space_copy(like->dim);
4773 if (!set->dim)
4774 goto error;
4775 isl_basic_set_free(like);
4776 return set;
4777 error:
4778 isl_basic_set_free(like);
4779 isl_set_free(set);
4780 return NULL;
4783 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4785 int i;
4787 map = isl_map_cow(map);
4788 if (!map)
4789 return NULL;
4790 map->dim = isl_space_cow(map->dim);
4791 if (!map->dim)
4792 goto error;
4794 for (i = 1; i < map->n; ++i)
4795 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4796 goto error);
4797 for (i = 0; i < map->n; ++i) {
4798 map->p[i] = (struct isl_basic_map *)
4799 isl_basic_map_underlying_set(map->p[i]);
4800 if (!map->p[i])
4801 goto error;
4803 if (map->n == 0)
4804 map->dim = isl_space_underlying(map->dim, 0);
4805 else {
4806 isl_space_free(map->dim);
4807 map->dim = isl_space_copy(map->p[0]->dim);
4809 if (!map->dim)
4810 goto error;
4811 return (struct isl_set *)map;
4812 error:
4813 isl_map_free(map);
4814 return NULL;
4817 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4819 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4822 /* Replace the space of "bmap" by "space".
4824 * If the space of "bmap" is identical to "space" (including the identifiers
4825 * of the input and output dimensions), then simply return the original input.
4827 __isl_give isl_basic_map *isl_basic_map_reset_space(
4828 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4830 isl_bool equal;
4832 if (!bmap)
4833 goto error;
4834 equal = isl_space_is_equal(bmap->dim, space);
4835 if (equal >= 0 && equal)
4836 equal = isl_space_match(bmap->dim, isl_dim_in,
4837 space, isl_dim_in);
4838 if (equal >= 0 && equal)
4839 equal = isl_space_match(bmap->dim, isl_dim_out,
4840 space, isl_dim_out);
4841 if (equal < 0)
4842 goto error;
4843 if (equal) {
4844 isl_space_free(space);
4845 return bmap;
4847 bmap = isl_basic_map_cow(bmap);
4848 if (!bmap || !space)
4849 goto error;
4851 isl_space_free(bmap->dim);
4852 bmap->dim = space;
4854 bmap = isl_basic_map_finalize(bmap);
4856 return bmap;
4857 error:
4858 isl_basic_map_free(bmap);
4859 isl_space_free(space);
4860 return NULL;
4863 __isl_give isl_basic_set *isl_basic_set_reset_space(
4864 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4866 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4867 dim);
4870 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4871 __isl_take isl_space *dim)
4873 int i;
4875 map = isl_map_cow(map);
4876 if (!map || !dim)
4877 goto error;
4879 for (i = 0; i < map->n; ++i) {
4880 map->p[i] = isl_basic_map_reset_space(map->p[i],
4881 isl_space_copy(dim));
4882 if (!map->p[i])
4883 goto error;
4885 isl_space_free(map->dim);
4886 map->dim = dim;
4888 return map;
4889 error:
4890 isl_map_free(map);
4891 isl_space_free(dim);
4892 return NULL;
4895 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4896 __isl_take isl_space *dim)
4898 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4901 /* Compute the parameter domain of the given basic set.
4903 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4905 isl_space *space;
4906 unsigned n;
4908 if (isl_basic_set_is_params(bset))
4909 return bset;
4911 n = isl_basic_set_dim(bset, isl_dim_set);
4912 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4913 space = isl_basic_set_get_space(bset);
4914 space = isl_space_params(space);
4915 bset = isl_basic_set_reset_space(bset, space);
4916 return bset;
4919 /* Construct a zero-dimensional basic set with the given parameter domain.
4921 __isl_give isl_basic_set *isl_basic_set_from_params(
4922 __isl_take isl_basic_set *bset)
4924 isl_space *space;
4925 space = isl_basic_set_get_space(bset);
4926 space = isl_space_set_from_params(space);
4927 bset = isl_basic_set_reset_space(bset, space);
4928 return bset;
4931 /* Compute the parameter domain of the given set.
4933 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4935 isl_space *space;
4936 unsigned n;
4938 if (isl_set_is_params(set))
4939 return set;
4941 n = isl_set_dim(set, isl_dim_set);
4942 set = isl_set_project_out(set, isl_dim_set, 0, n);
4943 space = isl_set_get_space(set);
4944 space = isl_space_params(space);
4945 set = isl_set_reset_space(set, space);
4946 return set;
4949 /* Construct a zero-dimensional set with the given parameter domain.
4951 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4953 isl_space *space;
4954 space = isl_set_get_space(set);
4955 space = isl_space_set_from_params(space);
4956 set = isl_set_reset_space(set, space);
4957 return set;
4960 /* Compute the parameter domain of the given map.
4962 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4964 isl_space *space;
4965 unsigned n;
4967 n = isl_map_dim(map, isl_dim_in);
4968 map = isl_map_project_out(map, isl_dim_in, 0, n);
4969 n = isl_map_dim(map, isl_dim_out);
4970 map = isl_map_project_out(map, isl_dim_out, 0, n);
4971 space = isl_map_get_space(map);
4972 space = isl_space_params(space);
4973 map = isl_map_reset_space(map, space);
4974 return map;
4977 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4979 isl_space *space;
4980 unsigned n_out;
4982 if (!bmap)
4983 return NULL;
4984 space = isl_space_domain(isl_basic_map_get_space(bmap));
4986 n_out = isl_basic_map_n_out(bmap);
4987 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
4989 return isl_basic_map_reset_space(bmap, space);
4992 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4994 if (!bmap)
4995 return -1;
4996 return isl_space_may_be_set(bmap->dim);
4999 /* Is this basic map actually a set?
5000 * Users should never call this function. Outside of isl,
5001 * the type should indicate whether something is a set or a map.
5003 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5005 if (!bmap)
5006 return -1;
5007 return isl_space_is_set(bmap->dim);
5010 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5012 if (!bmap)
5013 return NULL;
5014 if (isl_basic_map_is_set(bmap))
5015 return bmap;
5016 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5019 __isl_give isl_basic_map *isl_basic_map_domain_map(
5020 __isl_take isl_basic_map *bmap)
5022 int i, k;
5023 isl_space *dim;
5024 isl_basic_map *domain;
5025 int nparam, n_in, n_out;
5026 unsigned total;
5028 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5029 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5030 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5032 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5033 domain = isl_basic_map_universe(dim);
5035 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5036 bmap = isl_basic_map_apply_range(bmap, domain);
5037 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5039 total = isl_basic_map_total_dim(bmap);
5041 for (i = 0; i < n_in; ++i) {
5042 k = isl_basic_map_alloc_equality(bmap);
5043 if (k < 0)
5044 goto error;
5045 isl_seq_clr(bmap->eq[k], 1 + total);
5046 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5047 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5050 bmap = isl_basic_map_gauss(bmap, NULL);
5051 return isl_basic_map_finalize(bmap);
5052 error:
5053 isl_basic_map_free(bmap);
5054 return NULL;
5057 __isl_give isl_basic_map *isl_basic_map_range_map(
5058 __isl_take isl_basic_map *bmap)
5060 int i, k;
5061 isl_space *dim;
5062 isl_basic_map *range;
5063 int nparam, n_in, n_out;
5064 unsigned total;
5066 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5067 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5068 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5070 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5071 range = isl_basic_map_universe(dim);
5073 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5074 bmap = isl_basic_map_apply_range(bmap, range);
5075 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5077 total = isl_basic_map_total_dim(bmap);
5079 for (i = 0; i < n_out; ++i) {
5080 k = isl_basic_map_alloc_equality(bmap);
5081 if (k < 0)
5082 goto error;
5083 isl_seq_clr(bmap->eq[k], 1 + total);
5084 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5085 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5088 bmap = isl_basic_map_gauss(bmap, NULL);
5089 return isl_basic_map_finalize(bmap);
5090 error:
5091 isl_basic_map_free(bmap);
5092 return NULL;
5095 int isl_map_may_be_set(__isl_keep isl_map *map)
5097 if (!map)
5098 return -1;
5099 return isl_space_may_be_set(map->dim);
5102 /* Is this map actually a set?
5103 * Users should never call this function. Outside of isl,
5104 * the type should indicate whether something is a set or a map.
5106 int isl_map_is_set(__isl_keep isl_map *map)
5108 if (!map)
5109 return -1;
5110 return isl_space_is_set(map->dim);
5113 struct isl_set *isl_map_range(struct isl_map *map)
5115 int i;
5116 struct isl_set *set;
5118 if (!map)
5119 goto error;
5120 if (isl_map_is_set(map))
5121 return (isl_set *)map;
5123 map = isl_map_cow(map);
5124 if (!map)
5125 goto error;
5127 set = (struct isl_set *) map;
5128 set->dim = isl_space_range(set->dim);
5129 if (!set->dim)
5130 goto error;
5131 for (i = 0; i < map->n; ++i) {
5132 set->p[i] = isl_basic_map_range(map->p[i]);
5133 if (!set->p[i])
5134 goto error;
5136 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5137 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5138 return set;
5139 error:
5140 isl_map_free(map);
5141 return NULL;
5144 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5146 int i;
5148 map = isl_map_cow(map);
5149 if (!map)
5150 return NULL;
5152 map->dim = isl_space_domain_map(map->dim);
5153 if (!map->dim)
5154 goto error;
5155 for (i = 0; i < map->n; ++i) {
5156 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5157 if (!map->p[i])
5158 goto error;
5160 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5161 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5162 return map;
5163 error:
5164 isl_map_free(map);
5165 return NULL;
5168 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5170 int i;
5171 isl_space *range_dim;
5173 map = isl_map_cow(map);
5174 if (!map)
5175 return NULL;
5177 range_dim = isl_space_range(isl_map_get_space(map));
5178 range_dim = isl_space_from_range(range_dim);
5179 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5180 map->dim = isl_space_join(map->dim, range_dim);
5181 if (!map->dim)
5182 goto error;
5183 for (i = 0; i < map->n; ++i) {
5184 map->p[i] = isl_basic_map_range_map(map->p[i]);
5185 if (!map->p[i])
5186 goto error;
5188 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5189 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5190 return map;
5191 error:
5192 isl_map_free(map);
5193 return NULL;
5196 /* Given a wrapped map of the form A[B -> C],
5197 * return the map A[B -> C] -> B.
5199 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5201 isl_id *id;
5202 isl_map *map;
5204 if (!set)
5205 return NULL;
5206 if (!isl_set_has_tuple_id(set))
5207 return isl_map_domain_map(isl_set_unwrap(set));
5209 id = isl_set_get_tuple_id(set);
5210 map = isl_map_domain_map(isl_set_unwrap(set));
5211 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5213 return map;
5216 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5217 __isl_take isl_space *dim)
5219 int i;
5220 struct isl_map *map = NULL;
5222 set = isl_set_cow(set);
5223 if (!set || !dim)
5224 goto error;
5225 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5226 map = (struct isl_map *)set;
5227 for (i = 0; i < set->n; ++i) {
5228 map->p[i] = isl_basic_map_from_basic_set(
5229 set->p[i], isl_space_copy(dim));
5230 if (!map->p[i])
5231 goto error;
5233 isl_space_free(map->dim);
5234 map->dim = dim;
5235 return map;
5236 error:
5237 isl_space_free(dim);
5238 isl_set_free(set);
5239 return NULL;
5242 __isl_give isl_basic_map *isl_basic_map_from_domain(
5243 __isl_take isl_basic_set *bset)
5245 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5248 __isl_give isl_basic_map *isl_basic_map_from_range(
5249 __isl_take isl_basic_set *bset)
5251 isl_space *space;
5252 space = isl_basic_set_get_space(bset);
5253 space = isl_space_from_range(space);
5254 bset = isl_basic_set_reset_space(bset, space);
5255 return (isl_basic_map *)bset;
5258 /* Create a relation with the given set as range.
5259 * The domain of the created relation is a zero-dimensional
5260 * flat anonymous space.
5262 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5264 isl_space *space;
5265 space = isl_set_get_space(set);
5266 space = isl_space_from_range(space);
5267 set = isl_set_reset_space(set, space);
5268 return (struct isl_map *)set;
5271 /* Create a relation with the given set as domain.
5272 * The range of the created relation is a zero-dimensional
5273 * flat anonymous space.
5275 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5277 return isl_map_reverse(isl_map_from_range(set));
5280 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5281 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5283 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5286 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5287 __isl_take isl_set *range)
5289 return isl_map_apply_range(isl_map_reverse(domain), range);
5292 /* Return a newly allocated isl_map with given space and flags and
5293 * room for "n" basic maps.
5294 * Make sure that all cached information is cleared.
5296 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5297 unsigned flags)
5299 struct isl_map *map;
5301 if (!space)
5302 return NULL;
5303 if (n < 0)
5304 isl_die(space->ctx, isl_error_internal,
5305 "negative number of basic maps", goto error);
5306 map = isl_calloc(space->ctx, struct isl_map,
5307 sizeof(struct isl_map) +
5308 (n - 1) * sizeof(struct isl_basic_map *));
5309 if (!map)
5310 goto error;
5312 map->ctx = space->ctx;
5313 isl_ctx_ref(map->ctx);
5314 map->ref = 1;
5315 map->size = n;
5316 map->n = 0;
5317 map->dim = space;
5318 map->flags = flags;
5319 return map;
5320 error:
5321 isl_space_free(space);
5322 return NULL;
5325 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5326 unsigned nparam, unsigned in, unsigned out, int n,
5327 unsigned flags)
5329 struct isl_map *map;
5330 isl_space *dims;
5332 dims = isl_space_alloc(ctx, nparam, in, out);
5333 if (!dims)
5334 return NULL;
5336 map = isl_map_alloc_space(dims, n, flags);
5337 return map;
5340 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5342 struct isl_basic_map *bmap;
5343 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5344 bmap = isl_basic_map_set_to_empty(bmap);
5345 return bmap;
5348 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5350 struct isl_basic_set *bset;
5351 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5352 bset = isl_basic_set_set_to_empty(bset);
5353 return bset;
5356 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5358 struct isl_basic_map *bmap;
5359 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5360 bmap = isl_basic_map_finalize(bmap);
5361 return bmap;
5364 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5366 struct isl_basic_set *bset;
5367 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5368 bset = isl_basic_set_finalize(bset);
5369 return bset;
5372 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5374 int i;
5375 unsigned total = isl_space_dim(dim, isl_dim_all);
5376 isl_basic_map *bmap;
5378 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5379 for (i = 0; i < total; ++i) {
5380 int k = isl_basic_map_alloc_inequality(bmap);
5381 if (k < 0)
5382 goto error;
5383 isl_seq_clr(bmap->ineq[k], 1 + total);
5384 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5386 return bmap;
5387 error:
5388 isl_basic_map_free(bmap);
5389 return NULL;
5392 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5394 return isl_basic_map_nat_universe(dim);
5397 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5399 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5402 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5404 return isl_map_nat_universe(dim);
5407 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5409 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5412 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5414 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5417 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5419 struct isl_map *map;
5420 if (!dim)
5421 return NULL;
5422 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5423 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5424 return map;
5427 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5429 struct isl_set *set;
5430 if (!dim)
5431 return NULL;
5432 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5433 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5434 return set;
5437 struct isl_map *isl_map_dup(struct isl_map *map)
5439 int i;
5440 struct isl_map *dup;
5442 if (!map)
5443 return NULL;
5444 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5445 for (i = 0; i < map->n; ++i)
5446 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5447 return dup;
5450 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5451 __isl_take isl_basic_map *bmap)
5453 if (!bmap || !map)
5454 goto error;
5455 if (isl_basic_map_plain_is_empty(bmap)) {
5456 isl_basic_map_free(bmap);
5457 return map;
5459 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5460 isl_assert(map->ctx, map->n < map->size, goto error);
5461 map->p[map->n] = bmap;
5462 map->n++;
5463 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5464 return map;
5465 error:
5466 if (map)
5467 isl_map_free(map);
5468 if (bmap)
5469 isl_basic_map_free(bmap);
5470 return NULL;
5473 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5475 int i;
5477 if (!map)
5478 return NULL;
5480 if (--map->ref > 0)
5481 return NULL;
5483 clear_caches(map);
5484 isl_ctx_deref(map->ctx);
5485 for (i = 0; i < map->n; ++i)
5486 isl_basic_map_free(map->p[i]);
5487 isl_space_free(map->dim);
5488 free(map);
5490 return NULL;
5493 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5494 struct isl_basic_map *bmap, unsigned pos, int value)
5496 int j;
5498 bmap = isl_basic_map_cow(bmap);
5499 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5500 j = isl_basic_map_alloc_equality(bmap);
5501 if (j < 0)
5502 goto error;
5503 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5504 isl_int_set_si(bmap->eq[j][pos], -1);
5505 isl_int_set_si(bmap->eq[j][0], value);
5506 bmap = isl_basic_map_simplify(bmap);
5507 return isl_basic_map_finalize(bmap);
5508 error:
5509 isl_basic_map_free(bmap);
5510 return NULL;
5513 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5514 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5516 int j;
5518 bmap = isl_basic_map_cow(bmap);
5519 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5520 j = isl_basic_map_alloc_equality(bmap);
5521 if (j < 0)
5522 goto error;
5523 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5524 isl_int_set_si(bmap->eq[j][pos], -1);
5525 isl_int_set(bmap->eq[j][0], value);
5526 bmap = isl_basic_map_simplify(bmap);
5527 return isl_basic_map_finalize(bmap);
5528 error:
5529 isl_basic_map_free(bmap);
5530 return NULL;
5533 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5534 enum isl_dim_type type, unsigned pos, int value)
5536 if (!bmap)
5537 return NULL;
5538 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5539 return isl_basic_map_fix_pos_si(bmap,
5540 isl_basic_map_offset(bmap, type) + pos, value);
5541 error:
5542 isl_basic_map_free(bmap);
5543 return NULL;
5546 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5547 enum isl_dim_type type, unsigned pos, isl_int value)
5549 if (!bmap)
5550 return NULL;
5551 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5552 return isl_basic_map_fix_pos(bmap,
5553 isl_basic_map_offset(bmap, type) + pos, value);
5554 error:
5555 isl_basic_map_free(bmap);
5556 return NULL;
5559 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5560 * to be equal to "v".
5562 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5563 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5565 if (!bmap || !v)
5566 goto error;
5567 if (!isl_val_is_int(v))
5568 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5569 "expecting integer value", goto error);
5570 if (pos >= isl_basic_map_dim(bmap, type))
5571 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5572 "index out of bounds", goto error);
5573 pos += isl_basic_map_offset(bmap, type);
5574 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5575 isl_val_free(v);
5576 return bmap;
5577 error:
5578 isl_basic_map_free(bmap);
5579 isl_val_free(v);
5580 return NULL;
5583 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5584 * to be equal to "v".
5586 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5587 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5589 return isl_basic_map_fix_val(bset, type, pos, v);
5592 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5593 enum isl_dim_type type, unsigned pos, int value)
5595 return (struct isl_basic_set *)
5596 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5597 type, pos, value);
5600 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5601 enum isl_dim_type type, unsigned pos, isl_int value)
5603 return (struct isl_basic_set *)
5604 isl_basic_map_fix((struct isl_basic_map *)bset,
5605 type, pos, value);
5608 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5609 unsigned input, int value)
5611 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5614 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5615 unsigned dim, int value)
5617 return (struct isl_basic_set *)
5618 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5619 isl_dim_set, dim, value);
5622 static int remove_if_empty(__isl_keep isl_map *map, int i)
5624 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5626 if (empty < 0)
5627 return -1;
5628 if (!empty)
5629 return 0;
5631 isl_basic_map_free(map->p[i]);
5632 if (i != map->n - 1) {
5633 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5634 map->p[i] = map->p[map->n - 1];
5636 map->n--;
5638 return 0;
5641 /* Perform "fn" on each basic map of "map", where we may not be holding
5642 * the only reference to "map".
5643 * In particular, "fn" should be a semantics preserving operation
5644 * that we want to apply to all copies of "map". We therefore need
5645 * to be careful not to modify "map" in a way that breaks "map"
5646 * in case anything goes wrong.
5648 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5649 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5651 struct isl_basic_map *bmap;
5652 int i;
5654 if (!map)
5655 return NULL;
5657 for (i = map->n - 1; i >= 0; --i) {
5658 bmap = isl_basic_map_copy(map->p[i]);
5659 bmap = fn(bmap);
5660 if (!bmap)
5661 goto error;
5662 isl_basic_map_free(map->p[i]);
5663 map->p[i] = bmap;
5664 if (remove_if_empty(map, i) < 0)
5665 goto error;
5668 return map;
5669 error:
5670 isl_map_free(map);
5671 return NULL;
5674 struct isl_map *isl_map_fix_si(struct isl_map *map,
5675 enum isl_dim_type type, unsigned pos, int value)
5677 int i;
5679 map = isl_map_cow(map);
5680 if (!map)
5681 return NULL;
5683 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5684 for (i = map->n - 1; i >= 0; --i) {
5685 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5686 if (remove_if_empty(map, i) < 0)
5687 goto error;
5689 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5690 return map;
5691 error:
5692 isl_map_free(map);
5693 return NULL;
5696 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5697 enum isl_dim_type type, unsigned pos, int value)
5699 return (struct isl_set *)
5700 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5703 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5704 enum isl_dim_type type, unsigned pos, isl_int value)
5706 int i;
5708 map = isl_map_cow(map);
5709 if (!map)
5710 return NULL;
5712 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5713 for (i = 0; i < map->n; ++i) {
5714 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5715 if (!map->p[i])
5716 goto error;
5718 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5719 return map;
5720 error:
5721 isl_map_free(map);
5722 return NULL;
5725 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5726 enum isl_dim_type type, unsigned pos, isl_int value)
5728 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5731 /* Fix the value of the variable at position "pos" of type "type" of "map"
5732 * to be equal to "v".
5734 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5735 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5737 int i;
5739 map = isl_map_cow(map);
5740 if (!map || !v)
5741 goto error;
5743 if (!isl_val_is_int(v))
5744 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5745 "expecting integer value", goto error);
5746 if (pos >= isl_map_dim(map, type))
5747 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5748 "index out of bounds", goto error);
5749 for (i = map->n - 1; i >= 0; --i) {
5750 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5751 isl_val_copy(v));
5752 if (remove_if_empty(map, i) < 0)
5753 goto error;
5755 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5756 isl_val_free(v);
5757 return map;
5758 error:
5759 isl_map_free(map);
5760 isl_val_free(v);
5761 return NULL;
5764 /* Fix the value of the variable at position "pos" of type "type" of "set"
5765 * to be equal to "v".
5767 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5768 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5770 return isl_map_fix_val(set, type, pos, v);
5773 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5774 unsigned input, int value)
5776 return isl_map_fix_si(map, isl_dim_in, input, value);
5779 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5781 return (struct isl_set *)
5782 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5785 static __isl_give isl_basic_map *basic_map_bound_si(
5786 __isl_take isl_basic_map *bmap,
5787 enum isl_dim_type type, unsigned pos, int value, int upper)
5789 int j;
5791 if (!bmap)
5792 return NULL;
5793 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5794 pos += isl_basic_map_offset(bmap, type);
5795 bmap = isl_basic_map_cow(bmap);
5796 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5797 j = isl_basic_map_alloc_inequality(bmap);
5798 if (j < 0)
5799 goto error;
5800 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5801 if (upper) {
5802 isl_int_set_si(bmap->ineq[j][pos], -1);
5803 isl_int_set_si(bmap->ineq[j][0], value);
5804 } else {
5805 isl_int_set_si(bmap->ineq[j][pos], 1);
5806 isl_int_set_si(bmap->ineq[j][0], -value);
5808 bmap = isl_basic_map_simplify(bmap);
5809 return isl_basic_map_finalize(bmap);
5810 error:
5811 isl_basic_map_free(bmap);
5812 return NULL;
5815 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5816 __isl_take isl_basic_map *bmap,
5817 enum isl_dim_type type, unsigned pos, int value)
5819 return basic_map_bound_si(bmap, type, pos, value, 0);
5822 /* Constrain the values of the given dimension to be no greater than "value".
5824 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5825 __isl_take isl_basic_map *bmap,
5826 enum isl_dim_type type, unsigned pos, int value)
5828 return basic_map_bound_si(bmap, type, pos, value, 1);
5831 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5832 unsigned dim, isl_int value)
5834 int j;
5836 bset = isl_basic_set_cow(bset);
5837 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5838 j = isl_basic_set_alloc_inequality(bset);
5839 if (j < 0)
5840 goto error;
5841 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5842 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5843 isl_int_neg(bset->ineq[j][0], value);
5844 bset = isl_basic_set_simplify(bset);
5845 return isl_basic_set_finalize(bset);
5846 error:
5847 isl_basic_set_free(bset);
5848 return NULL;
5851 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5852 enum isl_dim_type type, unsigned pos, int value, int upper)
5854 int i;
5856 map = isl_map_cow(map);
5857 if (!map)
5858 return NULL;
5860 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5861 for (i = 0; i < map->n; ++i) {
5862 map->p[i] = basic_map_bound_si(map->p[i],
5863 type, pos, value, upper);
5864 if (!map->p[i])
5865 goto error;
5867 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5868 return map;
5869 error:
5870 isl_map_free(map);
5871 return NULL;
5874 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5875 enum isl_dim_type type, unsigned pos, int value)
5877 return map_bound_si(map, type, pos, value, 0);
5880 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5881 enum isl_dim_type type, unsigned pos, int value)
5883 return map_bound_si(map, type, pos, value, 1);
5886 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5887 enum isl_dim_type type, unsigned pos, int value)
5889 return (struct isl_set *)
5890 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5893 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5894 enum isl_dim_type type, unsigned pos, int value)
5896 return isl_map_upper_bound_si(set, type, pos, value);
5899 /* Bound the given variable of "bmap" from below (or above is "upper"
5900 * is set) to "value".
5902 static __isl_give isl_basic_map *basic_map_bound(
5903 __isl_take isl_basic_map *bmap,
5904 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5906 int j;
5908 if (!bmap)
5909 return NULL;
5910 if (pos >= isl_basic_map_dim(bmap, type))
5911 isl_die(bmap->ctx, isl_error_invalid,
5912 "index out of bounds", goto error);
5913 pos += isl_basic_map_offset(bmap, type);
5914 bmap = isl_basic_map_cow(bmap);
5915 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5916 j = isl_basic_map_alloc_inequality(bmap);
5917 if (j < 0)
5918 goto error;
5919 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5920 if (upper) {
5921 isl_int_set_si(bmap->ineq[j][pos], -1);
5922 isl_int_set(bmap->ineq[j][0], value);
5923 } else {
5924 isl_int_set_si(bmap->ineq[j][pos], 1);
5925 isl_int_neg(bmap->ineq[j][0], value);
5927 bmap = isl_basic_map_simplify(bmap);
5928 return isl_basic_map_finalize(bmap);
5929 error:
5930 isl_basic_map_free(bmap);
5931 return NULL;
5934 /* Bound the given variable of "map" from below (or above is "upper"
5935 * is set) to "value".
5937 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5938 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5940 int i;
5942 map = isl_map_cow(map);
5943 if (!map)
5944 return NULL;
5946 if (pos >= isl_map_dim(map, type))
5947 isl_die(map->ctx, isl_error_invalid,
5948 "index out of bounds", goto error);
5949 for (i = map->n - 1; i >= 0; --i) {
5950 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5951 if (remove_if_empty(map, i) < 0)
5952 goto error;
5954 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5955 return map;
5956 error:
5957 isl_map_free(map);
5958 return NULL;
5961 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5962 enum isl_dim_type type, unsigned pos, isl_int value)
5964 return map_bound(map, type, pos, value, 0);
5967 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5968 enum isl_dim_type type, unsigned pos, isl_int value)
5970 return map_bound(map, type, pos, value, 1);
5973 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5974 enum isl_dim_type type, unsigned pos, isl_int value)
5976 return isl_map_lower_bound(set, type, pos, value);
5979 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5980 enum isl_dim_type type, unsigned pos, isl_int value)
5982 return isl_map_upper_bound(set, type, pos, value);
5985 /* Force the values of the variable at position "pos" of type "type" of "set"
5986 * to be no smaller than "value".
5988 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5989 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5991 if (!value)
5992 goto error;
5993 if (!isl_val_is_int(value))
5994 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5995 "expecting integer value", goto error);
5996 set = isl_set_lower_bound(set, type, pos, value->n);
5997 isl_val_free(value);
5998 return set;
5999 error:
6000 isl_val_free(value);
6001 isl_set_free(set);
6002 return NULL;
6005 /* Force the values of the variable at position "pos" of type "type" of "set"
6006 * to be no greater than "value".
6008 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6009 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6011 if (!value)
6012 goto error;
6013 if (!isl_val_is_int(value))
6014 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6015 "expecting integer value", goto error);
6016 set = isl_set_upper_bound(set, type, pos, value->n);
6017 isl_val_free(value);
6018 return set;
6019 error:
6020 isl_val_free(value);
6021 isl_set_free(set);
6022 return NULL;
6025 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6026 isl_int value)
6028 int i;
6030 set = isl_set_cow(set);
6031 if (!set)
6032 return NULL;
6034 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6035 for (i = 0; i < set->n; ++i) {
6036 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6037 if (!set->p[i])
6038 goto error;
6040 return set;
6041 error:
6042 isl_set_free(set);
6043 return NULL;
6046 struct isl_map *isl_map_reverse(struct isl_map *map)
6048 int i;
6050 map = isl_map_cow(map);
6051 if (!map)
6052 return NULL;
6054 map->dim = isl_space_reverse(map->dim);
6055 if (!map->dim)
6056 goto error;
6057 for (i = 0; i < map->n; ++i) {
6058 map->p[i] = isl_basic_map_reverse(map->p[i]);
6059 if (!map->p[i])
6060 goto error;
6062 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6063 return map;
6064 error:
6065 isl_map_free(map);
6066 return NULL;
6069 static __isl_give isl_map *isl_basic_map_partial_lexopt(
6070 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6071 __isl_give isl_set **empty, int max)
6073 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6076 __isl_give isl_map *isl_basic_map_partial_lexmax(
6077 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6078 __isl_give isl_set **empty)
6080 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6083 __isl_give isl_map *isl_basic_map_partial_lexmin(
6084 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6085 __isl_give isl_set **empty)
6087 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6090 __isl_give isl_set *isl_basic_set_partial_lexmin(
6091 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6092 __isl_give isl_set **empty)
6094 return isl_basic_map_partial_lexmin(bset, dom, empty);
6097 __isl_give isl_set *isl_basic_set_partial_lexmax(
6098 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6099 __isl_give isl_set **empty)
6101 return isl_basic_map_partial_lexmax(bset, dom, empty);
6104 /* Compute the lexicographic minimum (or maximum if "max" is set)
6105 * of "bmap" over the domain "dom" and return the result as a piecewise
6106 * multi-affine expression.
6107 * If "empty" is not NULL, then *empty is assigned a set that
6108 * contains those parts of the domain where there is no solution.
6109 * If "bmap" is marked as rational (ISL_BASIC_MAP_RATIONAL),
6110 * then the rational optimum is computed. Otherwise, the integral optimum
6111 * is computed.
6113 static __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexopt_pw_multi_aff(
6114 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6115 __isl_give isl_set **empty, int max)
6117 return isl_tab_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty,
6118 max);
6121 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6122 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6123 __isl_give isl_set **empty)
6125 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6128 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6129 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6130 __isl_give isl_set **empty)
6132 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6135 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6136 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6137 __isl_give isl_set **empty)
6139 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6142 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6143 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6144 __isl_give isl_set **empty)
6146 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6149 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6150 __isl_take isl_basic_map *bmap, int max)
6152 isl_basic_set *dom = NULL;
6153 isl_space *dom_space;
6155 if (!bmap)
6156 goto error;
6157 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6158 dom = isl_basic_set_universe(dom_space);
6159 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6160 error:
6161 isl_basic_map_free(bmap);
6162 return NULL;
6165 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6166 __isl_take isl_basic_map *bmap)
6168 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6171 #undef TYPE
6172 #define TYPE isl_pw_multi_aff
6173 #undef SUFFIX
6174 #define SUFFIX _pw_multi_aff
6175 #undef EMPTY
6176 #define EMPTY isl_pw_multi_aff_empty
6177 #undef ADD
6178 #define ADD isl_pw_multi_aff_union_add
6179 #include "isl_map_lexopt_templ.c"
6181 /* Given a map "map", compute the lexicographically minimal
6182 * (or maximal) image element for each domain element in dom,
6183 * in the form of an isl_pw_multi_aff.
6184 * If "empty" is not NULL, then set *empty to those elements in dom that
6185 * do not have an image element.
6187 * We first compute the lexicographically minimal or maximal element
6188 * in the first basic map. This results in a partial solution "res"
6189 * and a subset "todo" of dom that still need to be handled.
6190 * We then consider each of the remaining maps in "map" and successively
6191 * update both "res" and "todo".
6192 * If "empty" is NULL, then the todo sets are not needed and therefore
6193 * also not computed.
6195 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6196 __isl_take isl_map *map, __isl_take isl_set *dom,
6197 __isl_give isl_set **empty, int max)
6199 int i;
6200 isl_pw_multi_aff *res;
6201 isl_set *todo;
6203 if (!map || !dom)
6204 goto error;
6206 if (isl_map_plain_is_empty(map)) {
6207 if (empty)
6208 *empty = dom;
6209 else
6210 isl_set_free(dom);
6211 return isl_pw_multi_aff_from_map(map);
6214 res = basic_map_partial_lexopt_pw_multi_aff(
6215 isl_basic_map_copy(map->p[0]),
6216 isl_set_copy(dom), empty, max);
6218 if (empty)
6219 todo = *empty;
6220 for (i = 1; i < map->n; ++i) {
6221 isl_pw_multi_aff *res_i;
6223 res_i = basic_map_partial_lexopt_pw_multi_aff(
6224 isl_basic_map_copy(map->p[i]),
6225 isl_set_copy(dom), empty, max);
6227 if (max)
6228 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6229 else
6230 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6232 if (empty)
6233 todo = isl_set_intersect(todo, *empty);
6236 isl_set_free(dom);
6237 isl_map_free(map);
6239 if (empty)
6240 *empty = todo;
6242 return res;
6243 error:
6244 if (empty)
6245 *empty = NULL;
6246 isl_set_free(dom);
6247 isl_map_free(map);
6248 return NULL;
6251 #undef TYPE
6252 #define TYPE isl_map
6253 #undef SUFFIX
6254 #define SUFFIX
6255 #undef EMPTY
6256 #define EMPTY isl_map_empty
6257 #undef ADD
6258 #define ADD isl_map_union_disjoint
6259 #include "isl_map_lexopt_templ.c"
6261 /* Given a map "map", compute the lexicographically minimal
6262 * (or maximal) image element for each domain element in "dom",
6263 * in the form of an isl_map.
6264 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6265 * do not have an image element.
6267 * If the input consists of more than one disjunct, then first
6268 * compute the desired result in the form of an isl_pw_multi_aff and
6269 * then convert that into an isl_map.
6271 * This function used to have an explicit implementation in terms
6272 * of isl_maps, but it would continually intersect the domains of
6273 * partial results with the complement of the domain of the next
6274 * partial solution, potentially leading to an explosion in the number
6275 * of disjuncts if there are several disjuncts in the input.
6276 * An even earlier implementation of this function would look for
6277 * better results in the domain of the partial result and for extra
6278 * results in the complement of this domain, which would lead to
6279 * even more splintering.
6281 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6282 __isl_take isl_map *map, __isl_take isl_set *dom,
6283 __isl_give isl_set **empty, int max)
6285 struct isl_map *res;
6286 isl_pw_multi_aff *pma;
6288 if (!map || !dom)
6289 goto error;
6291 if (isl_map_plain_is_empty(map)) {
6292 if (empty)
6293 *empty = dom;
6294 else
6295 isl_set_free(dom);
6296 return map;
6299 if (map->n == 1) {
6300 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6301 dom, empty, max);
6302 isl_map_free(map);
6303 return res;
6306 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty, max);
6307 return isl_map_from_pw_multi_aff(pma);
6308 error:
6309 if (empty)
6310 *empty = NULL;
6311 isl_set_free(dom);
6312 isl_map_free(map);
6313 return NULL;
6316 __isl_give isl_map *isl_map_partial_lexmax(
6317 __isl_take isl_map *map, __isl_take isl_set *dom,
6318 __isl_give isl_set **empty)
6320 return isl_map_partial_lexopt(map, dom, empty, 1);
6323 __isl_give isl_map *isl_map_partial_lexmin(
6324 __isl_take isl_map *map, __isl_take isl_set *dom,
6325 __isl_give isl_set **empty)
6327 return isl_map_partial_lexopt(map, dom, empty, 0);
6330 __isl_give isl_set *isl_set_partial_lexmin(
6331 __isl_take isl_set *set, __isl_take isl_set *dom,
6332 __isl_give isl_set **empty)
6334 return (struct isl_set *)
6335 isl_map_partial_lexmin((struct isl_map *)set,
6336 dom, empty);
6339 __isl_give isl_set *isl_set_partial_lexmax(
6340 __isl_take isl_set *set, __isl_take isl_set *dom,
6341 __isl_give isl_set **empty)
6343 return (struct isl_set *)
6344 isl_map_partial_lexmax((struct isl_map *)set,
6345 dom, empty);
6348 /* Compute the lexicographic minimum (or maximum if "max" is set)
6349 * of "bmap" over its domain.
6351 * Since we are not interested in the part of the domain space where
6352 * there is no solution, we initialize the domain to those constraints
6353 * of "bmap" that only involve the parameters and the input dimensions.
6354 * This relieves the parametric programming engine from detecting those
6355 * inequalities and transferring them to the context. More importantly,
6356 * it ensures that those inequalities are transferred first and not
6357 * intermixed with inequalities that actually split the domain.
6359 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6361 int n_div;
6362 int n_out;
6363 isl_basic_map *copy;
6364 isl_basic_set *dom;
6366 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6367 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6368 copy = isl_basic_map_copy(bmap);
6369 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6370 isl_dim_div, 0, n_div);
6371 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6372 isl_dim_out, 0, n_out);
6373 dom = isl_basic_map_domain(copy);
6374 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6377 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6379 return isl_basic_map_lexopt(bmap, 0);
6382 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6384 return isl_basic_map_lexopt(bmap, 1);
6387 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6389 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6392 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6394 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6397 /* Extract the first and only affine expression from list
6398 * and then add it to *pwaff with the given dom.
6399 * This domain is known to be disjoint from other domains
6400 * because of the way isl_basic_map_foreach_lexmax works.
6402 static int update_dim_opt(__isl_take isl_basic_set *dom,
6403 __isl_take isl_aff_list *list, void *user)
6405 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6406 isl_aff *aff;
6407 isl_pw_aff **pwaff = user;
6408 isl_pw_aff *pwaff_i;
6410 if (!list)
6411 goto error;
6412 if (isl_aff_list_n_aff(list) != 1)
6413 isl_die(ctx, isl_error_internal,
6414 "expecting single element list", goto error);
6416 aff = isl_aff_list_get_aff(list, 0);
6417 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6419 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6421 isl_aff_list_free(list);
6423 return 0;
6424 error:
6425 isl_basic_set_free(dom);
6426 isl_aff_list_free(list);
6427 return -1;
6430 /* Given a basic map with one output dimension, compute the minimum or
6431 * maximum of that dimension as an isl_pw_aff.
6433 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6434 * call update_dim_opt on each leaf of the result.
6436 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6437 int max)
6439 isl_space *dim = isl_basic_map_get_space(bmap);
6440 isl_pw_aff *pwaff;
6441 int r;
6443 dim = isl_space_from_domain(isl_space_domain(dim));
6444 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6445 pwaff = isl_pw_aff_empty(dim);
6447 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6448 if (r < 0)
6449 return isl_pw_aff_free(pwaff);
6451 return pwaff;
6454 /* Compute the minimum or maximum of the given output dimension
6455 * as a function of the parameters and the input dimensions,
6456 * but independently of the other output dimensions.
6458 * We first project out the other output dimension and then compute
6459 * the "lexicographic" maximum in each basic map, combining the results
6460 * using isl_pw_aff_union_max.
6462 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6463 int max)
6465 int i;
6466 isl_pw_aff *pwaff;
6467 unsigned n_out;
6469 n_out = isl_map_dim(map, isl_dim_out);
6470 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6471 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6472 if (!map)
6473 return NULL;
6475 if (map->n == 0) {
6476 isl_space *dim = isl_map_get_space(map);
6477 isl_map_free(map);
6478 return isl_pw_aff_empty(dim);
6481 pwaff = basic_map_dim_opt(map->p[0], max);
6482 for (i = 1; i < map->n; ++i) {
6483 isl_pw_aff *pwaff_i;
6485 pwaff_i = basic_map_dim_opt(map->p[i], max);
6486 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6489 isl_map_free(map);
6491 return pwaff;
6494 /* Compute the maximum of the given output dimension as a function of the
6495 * parameters and input dimensions, but independently of
6496 * the other output dimensions.
6498 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6500 return map_dim_opt(map, pos, 1);
6503 /* Compute the minimum or maximum of the given set dimension
6504 * as a function of the parameters,
6505 * but independently of the other set dimensions.
6507 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6508 int max)
6510 return map_dim_opt(set, pos, max);
6513 /* Compute the maximum of the given set dimension as a function of the
6514 * parameters, but independently of the other set dimensions.
6516 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6518 return set_dim_opt(set, pos, 1);
6521 /* Compute the minimum of the given set dimension as a function of the
6522 * parameters, but independently of the other set dimensions.
6524 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6526 return set_dim_opt(set, pos, 0);
6529 /* Apply a preimage specified by "mat" on the parameters of "bset".
6530 * bset is assumed to have only parameters and divs.
6532 static struct isl_basic_set *basic_set_parameter_preimage(
6533 struct isl_basic_set *bset, struct isl_mat *mat)
6535 unsigned nparam;
6537 if (!bset || !mat)
6538 goto error;
6540 bset->dim = isl_space_cow(bset->dim);
6541 if (!bset->dim)
6542 goto error;
6544 nparam = isl_basic_set_dim(bset, isl_dim_param);
6546 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6548 bset->dim->nparam = 0;
6549 bset->dim->n_out = nparam;
6550 bset = isl_basic_set_preimage(bset, mat);
6551 if (bset) {
6552 bset->dim->nparam = bset->dim->n_out;
6553 bset->dim->n_out = 0;
6555 return bset;
6556 error:
6557 isl_mat_free(mat);
6558 isl_basic_set_free(bset);
6559 return NULL;
6562 /* Apply a preimage specified by "mat" on the parameters of "set".
6563 * set is assumed to have only parameters and divs.
6565 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6566 __isl_take isl_mat *mat)
6568 isl_space *space;
6569 unsigned nparam;
6571 if (!set || !mat)
6572 goto error;
6574 nparam = isl_set_dim(set, isl_dim_param);
6576 if (mat->n_row != 1 + nparam)
6577 isl_die(isl_set_get_ctx(set), isl_error_internal,
6578 "unexpected number of rows", goto error);
6580 space = isl_set_get_space(set);
6581 space = isl_space_move_dims(space, isl_dim_set, 0,
6582 isl_dim_param, 0, nparam);
6583 set = isl_set_reset_space(set, space);
6584 set = isl_set_preimage(set, mat);
6585 nparam = isl_set_dim(set, isl_dim_out);
6586 space = isl_set_get_space(set);
6587 space = isl_space_move_dims(space, isl_dim_param, 0,
6588 isl_dim_out, 0, nparam);
6589 set = isl_set_reset_space(set, space);
6590 return set;
6591 error:
6592 isl_mat_free(mat);
6593 isl_set_free(set);
6594 return NULL;
6597 /* Intersect the basic set "bset" with the affine space specified by the
6598 * equalities in "eq".
6600 static struct isl_basic_set *basic_set_append_equalities(
6601 struct isl_basic_set *bset, struct isl_mat *eq)
6603 int i, k;
6604 unsigned len;
6606 if (!bset || !eq)
6607 goto error;
6609 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6610 eq->n_row, 0);
6611 if (!bset)
6612 goto error;
6614 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6615 for (i = 0; i < eq->n_row; ++i) {
6616 k = isl_basic_set_alloc_equality(bset);
6617 if (k < 0)
6618 goto error;
6619 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6620 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6622 isl_mat_free(eq);
6624 bset = isl_basic_set_gauss(bset, NULL);
6625 bset = isl_basic_set_finalize(bset);
6627 return bset;
6628 error:
6629 isl_mat_free(eq);
6630 isl_basic_set_free(bset);
6631 return NULL;
6634 /* Intersect the set "set" with the affine space specified by the
6635 * equalities in "eq".
6637 static struct isl_set *set_append_equalities(struct isl_set *set,
6638 struct isl_mat *eq)
6640 int i;
6642 if (!set || !eq)
6643 goto error;
6645 for (i = 0; i < set->n; ++i) {
6646 set->p[i] = basic_set_append_equalities(set->p[i],
6647 isl_mat_copy(eq));
6648 if (!set->p[i])
6649 goto error;
6651 isl_mat_free(eq);
6652 return set;
6653 error:
6654 isl_mat_free(eq);
6655 isl_set_free(set);
6656 return NULL;
6659 /* Given a basic set "bset" that only involves parameters and existentially
6660 * quantified variables, return the index of the first equality
6661 * that only involves parameters. If there is no such equality then
6662 * return bset->n_eq.
6664 * This function assumes that isl_basic_set_gauss has been called on "bset".
6666 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6668 int i, j;
6669 unsigned nparam, n_div;
6671 if (!bset)
6672 return -1;
6674 nparam = isl_basic_set_dim(bset, isl_dim_param);
6675 n_div = isl_basic_set_dim(bset, isl_dim_div);
6677 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6678 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6679 ++i;
6682 return i;
6685 /* Compute an explicit representation for the existentially quantified
6686 * variables in "bset" by computing the "minimal value" of the set
6687 * variables. Since there are no set variables, the computation of
6688 * the minimal value essentially computes an explicit representation
6689 * of the non-empty part(s) of "bset".
6691 * The input only involves parameters and existentially quantified variables.
6692 * All equalities among parameters have been removed.
6694 * Since the existentially quantified variables in the result are in general
6695 * going to be different from those in the input, we first replace
6696 * them by the minimal number of variables based on their equalities.
6697 * This should simplify the parametric integer programming.
6699 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6701 isl_morph *morph1, *morph2;
6702 isl_set *set;
6703 unsigned n;
6705 if (!bset)
6706 return NULL;
6707 if (bset->n_eq == 0)
6708 return isl_basic_set_lexmin(bset);
6710 morph1 = isl_basic_set_parameter_compression(bset);
6711 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6712 bset = isl_basic_set_lift(bset);
6713 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6714 bset = isl_morph_basic_set(morph2, bset);
6715 n = isl_basic_set_dim(bset, isl_dim_set);
6716 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6718 set = isl_basic_set_lexmin(bset);
6720 set = isl_morph_set(isl_morph_inverse(morph1), set);
6722 return set;
6725 /* Project the given basic set onto its parameter domain, possibly introducing
6726 * new, explicit, existential variables in the constraints.
6727 * The input has parameters and (possibly implicit) existential variables.
6728 * The output has the same parameters, but only
6729 * explicit existentially quantified variables.
6731 * The actual projection is performed by pip, but pip doesn't seem
6732 * to like equalities very much, so we first remove the equalities
6733 * among the parameters by performing a variable compression on
6734 * the parameters. Afterward, an inverse transformation is performed
6735 * and the equalities among the parameters are inserted back in.
6737 * The variable compression on the parameters may uncover additional
6738 * equalities that were only implicit before. We therefore check
6739 * if there are any new parameter equalities in the result and
6740 * if so recurse. The removal of parameter equalities is required
6741 * for the parameter compression performed by base_compute_divs.
6743 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6745 int i;
6746 struct isl_mat *eq;
6747 struct isl_mat *T, *T2;
6748 struct isl_set *set;
6749 unsigned nparam;
6751 bset = isl_basic_set_cow(bset);
6752 if (!bset)
6753 return NULL;
6755 if (bset->n_eq == 0)
6756 return base_compute_divs(bset);
6758 bset = isl_basic_set_gauss(bset, NULL);
6759 if (!bset)
6760 return NULL;
6761 if (isl_basic_set_plain_is_empty(bset))
6762 return isl_set_from_basic_set(bset);
6764 i = first_parameter_equality(bset);
6765 if (i == bset->n_eq)
6766 return base_compute_divs(bset);
6768 nparam = isl_basic_set_dim(bset, isl_dim_param);
6769 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6770 0, 1 + nparam);
6771 eq = isl_mat_cow(eq);
6772 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6773 if (T && T->n_col == 0) {
6774 isl_mat_free(T);
6775 isl_mat_free(T2);
6776 isl_mat_free(eq);
6777 bset = isl_basic_set_set_to_empty(bset);
6778 return isl_set_from_basic_set(bset);
6780 bset = basic_set_parameter_preimage(bset, T);
6782 i = first_parameter_equality(bset);
6783 if (!bset)
6784 set = NULL;
6785 else if (i == bset->n_eq)
6786 set = base_compute_divs(bset);
6787 else
6788 set = parameter_compute_divs(bset);
6789 set = set_parameter_preimage(set, T2);
6790 set = set_append_equalities(set, eq);
6791 return set;
6794 /* Insert the divs from "ls" before those of "bmap".
6796 * The number of columns is not changed, which means that the last
6797 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6798 * The caller is responsible for removing the same number of dimensions
6799 * from the space of "bmap".
6801 static __isl_give isl_basic_map *insert_divs_from_local_space(
6802 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6804 int i;
6805 int n_div;
6806 int old_n_div;
6808 n_div = isl_local_space_dim(ls, isl_dim_div);
6809 if (n_div == 0)
6810 return bmap;
6812 old_n_div = bmap->n_div;
6813 bmap = insert_div_rows(bmap, n_div);
6814 if (!bmap)
6815 return NULL;
6817 for (i = 0; i < n_div; ++i) {
6818 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6819 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6822 return bmap;
6825 /* Replace the space of "bmap" by the space and divs of "ls".
6827 * If "ls" has any divs, then we simplify the result since we may
6828 * have discovered some additional equalities that could simplify
6829 * the div expressions.
6831 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6832 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6834 int n_div;
6836 bmap = isl_basic_map_cow(bmap);
6837 if (!bmap || !ls)
6838 goto error;
6840 n_div = isl_local_space_dim(ls, isl_dim_div);
6841 bmap = insert_divs_from_local_space(bmap, ls);
6842 if (!bmap)
6843 goto error;
6845 isl_space_free(bmap->dim);
6846 bmap->dim = isl_local_space_get_space(ls);
6847 if (!bmap->dim)
6848 goto error;
6850 isl_local_space_free(ls);
6851 if (n_div > 0)
6852 bmap = isl_basic_map_simplify(bmap);
6853 bmap = isl_basic_map_finalize(bmap);
6854 return bmap;
6855 error:
6856 isl_basic_map_free(bmap);
6857 isl_local_space_free(ls);
6858 return NULL;
6861 /* Replace the space of "map" by the space and divs of "ls".
6863 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6864 __isl_take isl_local_space *ls)
6866 int i;
6868 map = isl_map_cow(map);
6869 if (!map || !ls)
6870 goto error;
6872 for (i = 0; i < map->n; ++i) {
6873 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6874 isl_local_space_copy(ls));
6875 if (!map->p[i])
6876 goto error;
6878 isl_space_free(map->dim);
6879 map->dim = isl_local_space_get_space(ls);
6880 if (!map->dim)
6881 goto error;
6883 isl_local_space_free(ls);
6884 return map;
6885 error:
6886 isl_local_space_free(ls);
6887 isl_map_free(map);
6888 return NULL;
6891 /* Compute an explicit representation for the existentially
6892 * quantified variables for which do not know any explicit representation yet.
6894 * We first sort the existentially quantified variables so that the
6895 * existentially quantified variables for which we already have an explicit
6896 * representation are placed before those for which we do not.
6897 * The input dimensions, the output dimensions and the existentially
6898 * quantified variables for which we already have an explicit
6899 * representation are then turned into parameters.
6900 * compute_divs returns a map with the same parameters and
6901 * no input or output dimensions and the dimension specification
6902 * is reset to that of the input, including the existentially quantified
6903 * variables for which we already had an explicit representation.
6905 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6907 struct isl_basic_set *bset;
6908 struct isl_set *set;
6909 struct isl_map *map;
6910 isl_space *dim;
6911 isl_local_space *ls;
6912 unsigned nparam;
6913 unsigned n_in;
6914 unsigned n_out;
6915 int n_known;
6916 int i;
6918 bmap = isl_basic_map_sort_divs(bmap);
6919 bmap = isl_basic_map_cow(bmap);
6920 if (!bmap)
6921 return NULL;
6923 n_known = isl_basic_map_first_unknown_div(bmap);
6924 if (n_known < 0)
6925 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6927 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6928 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6929 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6930 dim = isl_space_set_alloc(bmap->ctx,
6931 nparam + n_in + n_out + n_known, 0);
6932 if (!dim)
6933 goto error;
6935 ls = isl_basic_map_get_local_space(bmap);
6936 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6937 n_known, bmap->n_div - n_known);
6938 if (n_known > 0) {
6939 for (i = n_known; i < bmap->n_div; ++i)
6940 swap_div(bmap, i - n_known, i);
6941 bmap->n_div -= n_known;
6942 bmap->extra -= n_known;
6944 bmap = isl_basic_map_reset_space(bmap, dim);
6945 bset = (struct isl_basic_set *)bmap;
6947 set = parameter_compute_divs(bset);
6948 map = (struct isl_map *)set;
6949 map = replace_space_by_local_space(map, ls);
6951 return map;
6952 error:
6953 isl_basic_map_free(bmap);
6954 return NULL;
6957 /* Remove the explicit representation of local variable "div",
6958 * if there is any.
6960 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6961 __isl_take isl_basic_map *bmap, int div)
6963 isl_bool unknown;
6965 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6966 if (unknown < 0)
6967 return isl_basic_map_free(bmap);
6968 if (unknown)
6969 return bmap;
6971 bmap = isl_basic_map_cow(bmap);
6972 if (!bmap)
6973 return NULL;
6974 isl_int_set_si(bmap->div[div][0], 0);
6975 return bmap;
6978 /* Is local variable "div" of "bmap" marked as not having an explicit
6979 * representation?
6980 * Note that even if "div" is not marked in this way and therefore
6981 * has an explicit representation, this representation may still
6982 * depend (indirectly) on other local variables that do not
6983 * have an explicit representation.
6985 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
6986 int div)
6988 if (!bmap)
6989 return isl_bool_error;
6990 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6991 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6992 "position out of bounds", return isl_bool_error);
6993 return isl_int_is_zero(bmap->div[div][0]);
6996 /* Return the position of the first local variable that does not
6997 * have an explicit representation.
6998 * Return the total number of local variables if they all have
6999 * an explicit representation.
7000 * Return -1 on error.
7002 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7004 int i;
7006 if (!bmap)
7007 return -1;
7009 for (i = 0; i < bmap->n_div; ++i) {
7010 if (!isl_basic_map_div_is_known(bmap, i))
7011 return i;
7013 return bmap->n_div;
7016 /* Does "bmap" have an explicit representation for all local variables?
7018 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7020 int first, n;
7022 n = isl_basic_map_dim(bmap, isl_dim_div);
7023 first = isl_basic_map_first_unknown_div(bmap);
7024 if (first < 0)
7025 return isl_bool_error;
7026 return first == n;
7029 /* Do all basic maps in "map" have an explicit representation
7030 * for all local variables?
7032 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7034 int i;
7036 if (!map)
7037 return isl_bool_error;
7039 for (i = 0; i < map->n; ++i) {
7040 int known = isl_basic_map_divs_known(map->p[i]);
7041 if (known <= 0)
7042 return known;
7045 return isl_bool_true;
7048 /* If bmap contains any unknown divs, then compute explicit
7049 * expressions for them. However, this computation may be
7050 * quite expensive, so first try to remove divs that aren't
7051 * strictly needed.
7053 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7055 int known;
7056 struct isl_map *map;
7058 known = isl_basic_map_divs_known(bmap);
7059 if (known < 0)
7060 goto error;
7061 if (known)
7062 return isl_map_from_basic_map(bmap);
7064 bmap = isl_basic_map_drop_redundant_divs(bmap);
7066 known = isl_basic_map_divs_known(bmap);
7067 if (known < 0)
7068 goto error;
7069 if (known)
7070 return isl_map_from_basic_map(bmap);
7072 map = compute_divs(bmap);
7073 return map;
7074 error:
7075 isl_basic_map_free(bmap);
7076 return NULL;
7079 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7081 int i;
7082 int known;
7083 struct isl_map *res;
7085 if (!map)
7086 return NULL;
7087 if (map->n == 0)
7088 return map;
7090 known = isl_map_divs_known(map);
7091 if (known < 0) {
7092 isl_map_free(map);
7093 return NULL;
7095 if (known)
7096 return map;
7098 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7099 for (i = 1 ; i < map->n; ++i) {
7100 struct isl_map *r2;
7101 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7102 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7103 res = isl_map_union_disjoint(res, r2);
7104 else
7105 res = isl_map_union(res, r2);
7107 isl_map_free(map);
7109 return res;
7112 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7114 return (struct isl_set *)
7115 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7118 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7120 return (struct isl_set *)
7121 isl_map_compute_divs((struct isl_map *)set);
7124 struct isl_set *isl_map_domain(struct isl_map *map)
7126 int i;
7127 struct isl_set *set;
7129 if (!map)
7130 goto error;
7132 map = isl_map_cow(map);
7133 if (!map)
7134 return NULL;
7136 set = (struct isl_set *)map;
7137 set->dim = isl_space_domain(set->dim);
7138 if (!set->dim)
7139 goto error;
7140 for (i = 0; i < map->n; ++i) {
7141 set->p[i] = isl_basic_map_domain(map->p[i]);
7142 if (!set->p[i])
7143 goto error;
7145 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7146 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7147 return set;
7148 error:
7149 isl_map_free(map);
7150 return NULL;
7153 /* Return the union of "map1" and "map2", where we assume for now that
7154 * "map1" and "map2" are disjoint. Note that the basic maps inside
7155 * "map1" or "map2" may not be disjoint from each other.
7156 * Also note that this function is also called from isl_map_union,
7157 * which takes care of handling the situation where "map1" and "map2"
7158 * may not be disjoint.
7160 * If one of the inputs is empty, we can simply return the other input.
7161 * Similarly, if one of the inputs is universal, then it is equal to the union.
7163 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7164 __isl_take isl_map *map2)
7166 int i;
7167 unsigned flags = 0;
7168 struct isl_map *map = NULL;
7169 int is_universe;
7171 if (!map1 || !map2)
7172 goto error;
7174 if (!isl_space_is_equal(map1->dim, map2->dim))
7175 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7176 "spaces don't match", goto error);
7178 if (map1->n == 0) {
7179 isl_map_free(map1);
7180 return map2;
7182 if (map2->n == 0) {
7183 isl_map_free(map2);
7184 return map1;
7187 is_universe = isl_map_plain_is_universe(map1);
7188 if (is_universe < 0)
7189 goto error;
7190 if (is_universe) {
7191 isl_map_free(map2);
7192 return map1;
7195 is_universe = isl_map_plain_is_universe(map2);
7196 if (is_universe < 0)
7197 goto error;
7198 if (is_universe) {
7199 isl_map_free(map1);
7200 return map2;
7203 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7204 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7205 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7207 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7208 map1->n + map2->n, flags);
7209 if (!map)
7210 goto error;
7211 for (i = 0; i < map1->n; ++i) {
7212 map = isl_map_add_basic_map(map,
7213 isl_basic_map_copy(map1->p[i]));
7214 if (!map)
7215 goto error;
7217 for (i = 0; i < map2->n; ++i) {
7218 map = isl_map_add_basic_map(map,
7219 isl_basic_map_copy(map2->p[i]));
7220 if (!map)
7221 goto error;
7223 isl_map_free(map1);
7224 isl_map_free(map2);
7225 return map;
7226 error:
7227 isl_map_free(map);
7228 isl_map_free(map1);
7229 isl_map_free(map2);
7230 return NULL;
7233 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7234 * guaranteed to be disjoint by the caller.
7236 * Note that this functions is called from within isl_map_make_disjoint,
7237 * so we have to be careful not to touch the constraints of the inputs
7238 * in any way.
7240 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7241 __isl_take isl_map *map2)
7243 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7246 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7247 * not be disjoint. The parameters are assumed to have been aligned.
7249 * We currently simply call map_union_disjoint, the internal operation
7250 * of which does not really depend on the inputs being disjoint.
7251 * If the result contains more than one basic map, then we clear
7252 * the disjoint flag since the result may contain basic maps from
7253 * both inputs and these are not guaranteed to be disjoint.
7255 * As a special case, if "map1" and "map2" are obviously equal,
7256 * then we simply return "map1".
7258 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7259 __isl_take isl_map *map2)
7261 int equal;
7263 if (!map1 || !map2)
7264 goto error;
7266 equal = isl_map_plain_is_equal(map1, map2);
7267 if (equal < 0)
7268 goto error;
7269 if (equal) {
7270 isl_map_free(map2);
7271 return map1;
7274 map1 = map_union_disjoint(map1, map2);
7275 if (!map1)
7276 return NULL;
7277 if (map1->n > 1)
7278 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7279 return map1;
7280 error:
7281 isl_map_free(map1);
7282 isl_map_free(map2);
7283 return NULL;
7286 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7287 * not be disjoint.
7289 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7290 __isl_take isl_map *map2)
7292 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7295 struct isl_set *isl_set_union_disjoint(
7296 struct isl_set *set1, struct isl_set *set2)
7298 return (struct isl_set *)
7299 isl_map_union_disjoint(
7300 (struct isl_map *)set1, (struct isl_map *)set2);
7303 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7305 return (struct isl_set *)
7306 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7309 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7310 * the results.
7312 * "map" and "set" are assumed to be compatible and non-NULL.
7314 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7315 __isl_take isl_set *set,
7316 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7317 __isl_take isl_basic_set *bset))
7319 unsigned flags = 0;
7320 struct isl_map *result;
7321 int i, j;
7323 if (isl_set_plain_is_universe(set)) {
7324 isl_set_free(set);
7325 return map;
7328 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7329 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7330 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7332 result = isl_map_alloc_space(isl_space_copy(map->dim),
7333 map->n * set->n, flags);
7334 for (i = 0; result && i < map->n; ++i)
7335 for (j = 0; j < set->n; ++j) {
7336 result = isl_map_add_basic_map(result,
7337 fn(isl_basic_map_copy(map->p[i]),
7338 isl_basic_set_copy(set->p[j])));
7339 if (!result)
7340 break;
7343 isl_map_free(map);
7344 isl_set_free(set);
7345 return result;
7348 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7349 __isl_take isl_set *set)
7351 if (!map || !set)
7352 goto error;
7354 if (!isl_map_compatible_range(map, set))
7355 isl_die(set->ctx, isl_error_invalid,
7356 "incompatible spaces", goto error);
7358 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7359 error:
7360 isl_map_free(map);
7361 isl_set_free(set);
7362 return NULL;
7365 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7366 __isl_take isl_set *set)
7368 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7371 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7372 __isl_take isl_set *set)
7374 if (!map || !set)
7375 goto error;
7377 if (!isl_map_compatible_domain(map, set))
7378 isl_die(set->ctx, isl_error_invalid,
7379 "incompatible spaces", goto error);
7381 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7382 error:
7383 isl_map_free(map);
7384 isl_set_free(set);
7385 return NULL;
7388 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7389 __isl_take isl_set *set)
7391 return isl_map_align_params_map_map_and(map, set,
7392 &map_intersect_domain);
7395 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7396 __isl_take isl_map *map2)
7398 if (!map1 || !map2)
7399 goto error;
7400 map1 = isl_map_reverse(map1);
7401 map1 = isl_map_apply_range(map1, map2);
7402 return isl_map_reverse(map1);
7403 error:
7404 isl_map_free(map1);
7405 isl_map_free(map2);
7406 return NULL;
7409 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7410 __isl_take isl_map *map2)
7412 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7415 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7416 __isl_take isl_map *map2)
7418 isl_space *dim_result;
7419 struct isl_map *result;
7420 int i, j;
7422 if (!map1 || !map2)
7423 goto error;
7425 dim_result = isl_space_join(isl_space_copy(map1->dim),
7426 isl_space_copy(map2->dim));
7428 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7429 if (!result)
7430 goto error;
7431 for (i = 0; i < map1->n; ++i)
7432 for (j = 0; j < map2->n; ++j) {
7433 result = isl_map_add_basic_map(result,
7434 isl_basic_map_apply_range(
7435 isl_basic_map_copy(map1->p[i]),
7436 isl_basic_map_copy(map2->p[j])));
7437 if (!result)
7438 goto error;
7440 isl_map_free(map1);
7441 isl_map_free(map2);
7442 if (result && result->n <= 1)
7443 ISL_F_SET(result, ISL_MAP_DISJOINT);
7444 return result;
7445 error:
7446 isl_map_free(map1);
7447 isl_map_free(map2);
7448 return NULL;
7451 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7452 __isl_take isl_map *map2)
7454 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7458 * returns range - domain
7460 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7462 isl_space *target_space;
7463 struct isl_basic_set *bset;
7464 unsigned dim;
7465 unsigned nparam;
7466 int i;
7468 if (!bmap)
7469 goto error;
7470 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7471 bmap->dim, isl_dim_out),
7472 goto error);
7473 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7474 dim = isl_basic_map_n_in(bmap);
7475 nparam = isl_basic_map_n_param(bmap);
7476 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7477 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7478 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7479 for (i = 0; i < dim; ++i) {
7480 int j = isl_basic_map_alloc_equality(bmap);
7481 if (j < 0) {
7482 bmap = isl_basic_map_free(bmap);
7483 break;
7485 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7486 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7487 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7488 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7490 bset = isl_basic_map_domain(bmap);
7491 bset = isl_basic_set_reset_space(bset, target_space);
7492 return bset;
7493 error:
7494 isl_basic_map_free(bmap);
7495 return NULL;
7499 * returns range - domain
7501 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7503 int i;
7504 isl_space *dim;
7505 struct isl_set *result;
7507 if (!map)
7508 return NULL;
7510 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7511 map->dim, isl_dim_out),
7512 goto error);
7513 dim = isl_map_get_space(map);
7514 dim = isl_space_domain(dim);
7515 result = isl_set_alloc_space(dim, map->n, 0);
7516 if (!result)
7517 goto error;
7518 for (i = 0; i < map->n; ++i)
7519 result = isl_set_add_basic_set(result,
7520 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7521 isl_map_free(map);
7522 return result;
7523 error:
7524 isl_map_free(map);
7525 return NULL;
7529 * returns [domain -> range] -> range - domain
7531 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7532 __isl_take isl_basic_map *bmap)
7534 int i, k;
7535 isl_space *dim;
7536 isl_basic_map *domain;
7537 int nparam, n;
7538 unsigned total;
7540 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7541 bmap->dim, isl_dim_out))
7542 isl_die(bmap->ctx, isl_error_invalid,
7543 "domain and range don't match", goto error);
7545 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7546 n = isl_basic_map_dim(bmap, isl_dim_in);
7548 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7549 domain = isl_basic_map_universe(dim);
7551 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7552 bmap = isl_basic_map_apply_range(bmap, domain);
7553 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7555 total = isl_basic_map_total_dim(bmap);
7557 for (i = 0; i < n; ++i) {
7558 k = isl_basic_map_alloc_equality(bmap);
7559 if (k < 0)
7560 goto error;
7561 isl_seq_clr(bmap->eq[k], 1 + total);
7562 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7563 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7564 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7567 bmap = isl_basic_map_gauss(bmap, NULL);
7568 return isl_basic_map_finalize(bmap);
7569 error:
7570 isl_basic_map_free(bmap);
7571 return NULL;
7575 * returns [domain -> range] -> range - domain
7577 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7579 int i;
7580 isl_space *domain_dim;
7582 if (!map)
7583 return NULL;
7585 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7586 map->dim, isl_dim_out))
7587 isl_die(map->ctx, isl_error_invalid,
7588 "domain and range don't match", goto error);
7590 map = isl_map_cow(map);
7591 if (!map)
7592 return NULL;
7594 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7595 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7596 map->dim = isl_space_join(map->dim, domain_dim);
7597 if (!map->dim)
7598 goto error;
7599 for (i = 0; i < map->n; ++i) {
7600 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7601 if (!map->p[i])
7602 goto error;
7604 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7605 return map;
7606 error:
7607 isl_map_free(map);
7608 return NULL;
7611 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7613 struct isl_basic_map *bmap;
7614 unsigned nparam;
7615 unsigned dim;
7616 int i;
7618 if (!dims)
7619 return NULL;
7621 nparam = dims->nparam;
7622 dim = dims->n_out;
7623 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7624 if (!bmap)
7625 goto error;
7627 for (i = 0; i < dim; ++i) {
7628 int j = isl_basic_map_alloc_equality(bmap);
7629 if (j < 0)
7630 goto error;
7631 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7632 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7633 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7635 return isl_basic_map_finalize(bmap);
7636 error:
7637 isl_basic_map_free(bmap);
7638 return NULL;
7641 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7643 if (!dim)
7644 return NULL;
7645 if (dim->n_in != dim->n_out)
7646 isl_die(dim->ctx, isl_error_invalid,
7647 "number of input and output dimensions needs to be "
7648 "the same", goto error);
7649 return basic_map_identity(dim);
7650 error:
7651 isl_space_free(dim);
7652 return NULL;
7655 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7657 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7660 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7662 isl_space *dim = isl_set_get_space(set);
7663 isl_map *id;
7664 id = isl_map_identity(isl_space_map_from_set(dim));
7665 return isl_map_intersect_range(id, set);
7668 /* Construct a basic set with all set dimensions having only non-negative
7669 * values.
7671 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7672 __isl_take isl_space *space)
7674 int i;
7675 unsigned nparam;
7676 unsigned dim;
7677 struct isl_basic_set *bset;
7679 if (!space)
7680 return NULL;
7681 nparam = space->nparam;
7682 dim = space->n_out;
7683 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7684 if (!bset)
7685 return NULL;
7686 for (i = 0; i < dim; ++i) {
7687 int k = isl_basic_set_alloc_inequality(bset);
7688 if (k < 0)
7689 goto error;
7690 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7691 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7693 return bset;
7694 error:
7695 isl_basic_set_free(bset);
7696 return NULL;
7699 /* Construct the half-space x_pos >= 0.
7701 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7702 int pos)
7704 int k;
7705 isl_basic_set *nonneg;
7707 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7708 k = isl_basic_set_alloc_inequality(nonneg);
7709 if (k < 0)
7710 goto error;
7711 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7712 isl_int_set_si(nonneg->ineq[k][pos], 1);
7714 return isl_basic_set_finalize(nonneg);
7715 error:
7716 isl_basic_set_free(nonneg);
7717 return NULL;
7720 /* Construct the half-space x_pos <= -1.
7722 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7724 int k;
7725 isl_basic_set *neg;
7727 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7728 k = isl_basic_set_alloc_inequality(neg);
7729 if (k < 0)
7730 goto error;
7731 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7732 isl_int_set_si(neg->ineq[k][0], -1);
7733 isl_int_set_si(neg->ineq[k][pos], -1);
7735 return isl_basic_set_finalize(neg);
7736 error:
7737 isl_basic_set_free(neg);
7738 return NULL;
7741 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7742 enum isl_dim_type type, unsigned first, unsigned n)
7744 int i;
7745 unsigned offset;
7746 isl_basic_set *nonneg;
7747 isl_basic_set *neg;
7749 if (!set)
7750 return NULL;
7751 if (n == 0)
7752 return set;
7754 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7756 offset = pos(set->dim, type);
7757 for (i = 0; i < n; ++i) {
7758 nonneg = nonneg_halfspace(isl_set_get_space(set),
7759 offset + first + i);
7760 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7762 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7765 return set;
7766 error:
7767 isl_set_free(set);
7768 return NULL;
7771 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7772 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7773 void *user)
7775 isl_set *half;
7777 if (!set)
7778 return -1;
7779 if (isl_set_plain_is_empty(set)) {
7780 isl_set_free(set);
7781 return 0;
7783 if (first == len)
7784 return fn(set, signs, user);
7786 signs[first] = 1;
7787 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7788 1 + first));
7789 half = isl_set_intersect(half, isl_set_copy(set));
7790 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7791 goto error;
7793 signs[first] = -1;
7794 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7795 1 + first));
7796 half = isl_set_intersect(half, set);
7797 return foreach_orthant(half, signs, first + 1, len, fn, user);
7798 error:
7799 isl_set_free(set);
7800 return -1;
7803 /* Call "fn" on the intersections of "set" with each of the orthants
7804 * (except for obviously empty intersections). The orthant is identified
7805 * by the signs array, with each entry having value 1 or -1 according
7806 * to the sign of the corresponding variable.
7808 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7809 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7810 void *user)
7812 unsigned nparam;
7813 unsigned nvar;
7814 int *signs;
7815 int r;
7817 if (!set)
7818 return -1;
7819 if (isl_set_plain_is_empty(set))
7820 return 0;
7822 nparam = isl_set_dim(set, isl_dim_param);
7823 nvar = isl_set_dim(set, isl_dim_set);
7825 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7827 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7828 fn, user);
7830 free(signs);
7832 return r;
7835 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7837 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7840 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7841 __isl_keep isl_basic_map *bmap2)
7843 int is_subset;
7844 struct isl_map *map1;
7845 struct isl_map *map2;
7847 if (!bmap1 || !bmap2)
7848 return isl_bool_error;
7850 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7851 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7853 is_subset = isl_map_is_subset(map1, map2);
7855 isl_map_free(map1);
7856 isl_map_free(map2);
7858 return is_subset;
7861 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7862 __isl_keep isl_basic_set *bset2)
7864 return isl_basic_map_is_subset(bset1, bset2);
7867 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7868 __isl_keep isl_basic_map *bmap2)
7870 isl_bool is_subset;
7872 if (!bmap1 || !bmap2)
7873 return isl_bool_error;
7874 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7875 if (is_subset != isl_bool_true)
7876 return is_subset;
7877 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7878 return is_subset;
7881 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7882 __isl_keep isl_basic_set *bset2)
7884 return isl_basic_map_is_equal(
7885 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7888 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7890 int i;
7891 int is_empty;
7893 if (!map)
7894 return isl_bool_error;
7895 for (i = 0; i < map->n; ++i) {
7896 is_empty = isl_basic_map_is_empty(map->p[i]);
7897 if (is_empty < 0)
7898 return isl_bool_error;
7899 if (!is_empty)
7900 return isl_bool_false;
7902 return isl_bool_true;
7905 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7907 return map ? map->n == 0 : isl_bool_error;
7910 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7912 return set ? set->n == 0 : isl_bool_error;
7915 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7917 return isl_map_is_empty((struct isl_map *)set);
7920 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7922 if (!map1 || !map2)
7923 return -1;
7925 return isl_space_is_equal(map1->dim, map2->dim);
7928 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7930 if (!set1 || !set2)
7931 return -1;
7933 return isl_space_is_equal(set1->dim, set2->dim);
7936 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7938 isl_bool is_subset;
7940 if (!map1 || !map2)
7941 return isl_bool_error;
7942 is_subset = isl_map_is_subset(map1, map2);
7943 if (is_subset != isl_bool_true)
7944 return is_subset;
7945 is_subset = isl_map_is_subset(map2, map1);
7946 return is_subset;
7949 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7951 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7954 isl_bool isl_basic_map_is_strict_subset(
7955 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7957 isl_bool is_subset;
7959 if (!bmap1 || !bmap2)
7960 return isl_bool_error;
7961 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7962 if (is_subset != isl_bool_true)
7963 return is_subset;
7964 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7965 if (is_subset == isl_bool_error)
7966 return is_subset;
7967 return !is_subset;
7970 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7971 __isl_keep isl_map *map2)
7973 isl_bool is_subset;
7975 if (!map1 || !map2)
7976 return isl_bool_error;
7977 is_subset = isl_map_is_subset(map1, map2);
7978 if (is_subset != isl_bool_true)
7979 return is_subset;
7980 is_subset = isl_map_is_subset(map2, map1);
7981 if (is_subset == isl_bool_error)
7982 return is_subset;
7983 return !is_subset;
7986 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7987 __isl_keep isl_set *set2)
7989 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7992 /* Is "bmap" obviously equal to the universe with the same space?
7994 * That is, does it not have any constraints?
7996 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
7998 if (!bmap)
7999 return isl_bool_error;
8000 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8003 /* Is "bset" obviously equal to the universe with the same space?
8005 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8007 return isl_basic_map_plain_is_universe(bset);
8010 /* If "c" does not involve any existentially quantified variables,
8011 * then set *univ to false and abort
8013 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8015 isl_bool *univ = user;
8016 unsigned n;
8018 n = isl_constraint_dim(c, isl_dim_div);
8019 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8020 isl_constraint_free(c);
8021 if (*univ < 0 || !*univ)
8022 return isl_stat_error;
8023 return isl_stat_ok;
8026 /* Is "bmap" equal to the universe with the same space?
8028 * First check if it is obviously equal to the universe.
8029 * If not and if there are any constraints not involving
8030 * existentially quantified variables, then it is certainly
8031 * not equal to the universe.
8032 * Otherwise, check if the universe is a subset of "bmap".
8034 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8036 isl_bool univ;
8037 isl_basic_map *test;
8039 univ = isl_basic_map_plain_is_universe(bmap);
8040 if (univ < 0 || univ)
8041 return univ;
8042 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8043 return isl_bool_false;
8044 univ = isl_bool_true;
8045 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8046 univ)
8047 return isl_bool_error;
8048 if (univ < 0 || !univ)
8049 return univ;
8050 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8051 univ = isl_basic_map_is_subset(test, bmap);
8052 isl_basic_map_free(test);
8053 return univ;
8056 /* Is "bset" equal to the universe with the same space?
8058 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8060 return isl_basic_map_is_universe(bset);
8063 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8065 int i;
8067 if (!map)
8068 return isl_bool_error;
8070 for (i = 0; i < map->n; ++i) {
8071 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8072 if (r < 0 || r)
8073 return r;
8076 return isl_bool_false;
8079 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8081 return isl_map_plain_is_universe((isl_map *) set);
8084 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8086 struct isl_basic_set *bset = NULL;
8087 struct isl_vec *sample = NULL;
8088 isl_bool empty, non_empty;
8090 if (!bmap)
8091 return isl_bool_error;
8093 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8094 return isl_bool_true;
8096 if (isl_basic_map_plain_is_universe(bmap))
8097 return isl_bool_false;
8099 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8100 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8101 copy = isl_basic_map_remove_redundancies(copy);
8102 empty = isl_basic_map_plain_is_empty(copy);
8103 isl_basic_map_free(copy);
8104 return empty;
8107 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8108 if (non_empty < 0)
8109 return isl_bool_error;
8110 if (non_empty)
8111 return isl_bool_false;
8112 isl_vec_free(bmap->sample);
8113 bmap->sample = NULL;
8114 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8115 if (!bset)
8116 return isl_bool_error;
8117 sample = isl_basic_set_sample_vec(bset);
8118 if (!sample)
8119 return isl_bool_error;
8120 empty = sample->size == 0;
8121 isl_vec_free(bmap->sample);
8122 bmap->sample = sample;
8123 if (empty)
8124 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8126 return empty;
8129 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8131 if (!bmap)
8132 return isl_bool_error;
8133 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8136 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8138 if (!bset)
8139 return isl_bool_error;
8140 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8143 /* Is "bmap" known to be non-empty?
8145 * That is, is the cached sample still valid?
8147 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8149 unsigned total;
8151 if (!bmap)
8152 return isl_bool_error;
8153 if (!bmap->sample)
8154 return isl_bool_false;
8155 total = 1 + isl_basic_map_total_dim(bmap);
8156 if (bmap->sample->size != total)
8157 return isl_bool_false;
8158 return isl_basic_map_contains(bmap, bmap->sample);
8161 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8163 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8166 struct isl_map *isl_basic_map_union(
8167 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8169 struct isl_map *map;
8170 if (!bmap1 || !bmap2)
8171 goto error;
8173 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8175 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8176 if (!map)
8177 goto error;
8178 map = isl_map_add_basic_map(map, bmap1);
8179 map = isl_map_add_basic_map(map, bmap2);
8180 return map;
8181 error:
8182 isl_basic_map_free(bmap1);
8183 isl_basic_map_free(bmap2);
8184 return NULL;
8187 struct isl_set *isl_basic_set_union(
8188 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8190 return (struct isl_set *)isl_basic_map_union(
8191 (struct isl_basic_map *)bset1,
8192 (struct isl_basic_map *)bset2);
8195 /* Order divs such that any div only depends on previous divs */
8196 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8198 int i;
8199 unsigned off;
8201 if (!bmap)
8202 return NULL;
8204 off = isl_space_dim(bmap->dim, isl_dim_all);
8206 for (i = 0; i < bmap->n_div; ++i) {
8207 int pos;
8208 if (isl_int_is_zero(bmap->div[i][0]))
8209 continue;
8210 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8211 bmap->n_div-i);
8212 if (pos == -1)
8213 continue;
8214 if (pos == 0)
8215 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8216 "integer division depends on itself",
8217 return isl_basic_map_free(bmap));
8218 isl_basic_map_swap_div(bmap, i, i + pos);
8219 --i;
8221 return bmap;
8224 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8226 return (struct isl_basic_set *)
8227 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8230 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8232 int i;
8234 if (!map)
8235 return 0;
8237 for (i = 0; i < map->n; ++i) {
8238 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8239 if (!map->p[i])
8240 goto error;
8243 return map;
8244 error:
8245 isl_map_free(map);
8246 return NULL;
8249 /* Apply the expansion computed by isl_merge_divs.
8250 * The expansion itself is given by "exp" while the resulting
8251 * list of divs is given by "div".
8253 * Move the integer divisions of "bmap" into the right position
8254 * according to "exp" and then introduce the additional integer
8255 * divisions, adding div constraints.
8256 * The moving should be done first to avoid moving coefficients
8257 * in the definitions of the extra integer divisions.
8259 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8260 __isl_take isl_basic_set *bmap, __isl_take isl_mat *div, int *exp)
8262 int i, j;
8263 int n_div;
8265 bmap = isl_basic_map_cow(bmap);
8266 if (!bmap || !div)
8267 goto error;
8269 if (div->n_row < bmap->n_div)
8270 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8271 "not an expansion", goto error);
8273 n_div = bmap->n_div;
8274 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8275 div->n_row - n_div, 0,
8276 2 * (div->n_row - n_div));
8278 for (i = n_div; i < div->n_row; ++i)
8279 if (isl_basic_map_alloc_div(bmap) < 0)
8280 goto error;
8282 for (j = n_div - 1; j >= 0; --j) {
8283 if (exp[j] == j)
8284 break;
8285 isl_basic_map_swap_div(bmap, j, exp[j]);
8287 j = 0;
8288 for (i = 0; i < div->n_row; ++i) {
8289 if (j < n_div && exp[j] == i) {
8290 j++;
8291 } else {
8292 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8293 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8294 continue;
8295 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8296 goto error;
8300 isl_mat_free(div);
8301 return bmap;
8302 error:
8303 isl_basic_map_free(bmap);
8304 isl_mat_free(div);
8305 return NULL;
8308 /* Apply the expansion computed by isl_merge_divs.
8309 * The expansion itself is given by "exp" while the resulting
8310 * list of divs is given by "div".
8312 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8313 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8315 return isl_basic_map_expand_divs(bset, div, exp);
8318 /* Look for a div in dst that corresponds to the div "div" in src.
8319 * The divs before "div" in src and dst are assumed to be the same.
8321 * Returns -1 if no corresponding div was found and the position
8322 * of the corresponding div in dst otherwise.
8324 static int find_div(struct isl_basic_map *dst,
8325 struct isl_basic_map *src, unsigned div)
8327 int i;
8329 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8331 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8332 for (i = div; i < dst->n_div; ++i)
8333 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8334 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8335 dst->n_div - div) == -1)
8336 return i;
8337 return -1;
8340 /* Align the divs of "dst" to those of "src", adding divs from "src"
8341 * if needed. That is, make sure that the first src->n_div divs
8342 * of the result are equal to those of src.
8344 * The result is not finalized as by design it will have redundant
8345 * divs if any divs from "src" were copied.
8347 __isl_give isl_basic_map *isl_basic_map_align_divs(
8348 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8350 int i;
8351 int known, extended;
8352 unsigned total;
8354 if (!dst || !src)
8355 return isl_basic_map_free(dst);
8357 if (src->n_div == 0)
8358 return dst;
8360 known = isl_basic_map_divs_known(src);
8361 if (known < 0)
8362 return isl_basic_map_free(dst);
8363 if (!known)
8364 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8365 "some src divs are unknown",
8366 return isl_basic_map_free(dst));
8368 src = isl_basic_map_order_divs(src);
8370 extended = 0;
8371 total = isl_space_dim(src->dim, isl_dim_all);
8372 for (i = 0; i < src->n_div; ++i) {
8373 int j = find_div(dst, src, i);
8374 if (j < 0) {
8375 if (!extended) {
8376 int extra = src->n_div - i;
8377 dst = isl_basic_map_cow(dst);
8378 if (!dst)
8379 return NULL;
8380 dst = isl_basic_map_extend_space(dst,
8381 isl_space_copy(dst->dim),
8382 extra, 0, 2 * extra);
8383 extended = 1;
8385 j = isl_basic_map_alloc_div(dst);
8386 if (j < 0)
8387 return isl_basic_map_free(dst);
8388 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8389 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8390 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8391 return isl_basic_map_free(dst);
8393 if (j != i)
8394 isl_basic_map_swap_div(dst, i, j);
8396 return dst;
8399 struct isl_basic_set *isl_basic_set_align_divs(
8400 struct isl_basic_set *dst, struct isl_basic_set *src)
8402 return (struct isl_basic_set *)isl_basic_map_align_divs(
8403 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8406 struct isl_map *isl_map_align_divs(struct isl_map *map)
8408 int i;
8410 if (!map)
8411 return NULL;
8412 if (map->n == 0)
8413 return map;
8414 map = isl_map_compute_divs(map);
8415 map = isl_map_cow(map);
8416 if (!map)
8417 return NULL;
8419 for (i = 1; i < map->n; ++i)
8420 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8421 for (i = 1; i < map->n; ++i) {
8422 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8423 if (!map->p[i])
8424 return isl_map_free(map);
8427 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8428 return map;
8431 struct isl_set *isl_set_align_divs(struct isl_set *set)
8433 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8436 /* Align the divs of the basic maps in "map" to those
8437 * of the basic maps in "list", as well as to the other basic maps in "map".
8438 * The elements in "list" are assumed to have known divs.
8440 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8441 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8443 int i, n;
8445 map = isl_map_compute_divs(map);
8446 map = isl_map_cow(map);
8447 if (!map || !list)
8448 return isl_map_free(map);
8449 if (map->n == 0)
8450 return map;
8452 n = isl_basic_map_list_n_basic_map(list);
8453 for (i = 0; i < n; ++i) {
8454 isl_basic_map *bmap;
8456 bmap = isl_basic_map_list_get_basic_map(list, i);
8457 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8458 isl_basic_map_free(bmap);
8460 if (!map->p[0])
8461 return isl_map_free(map);
8463 return isl_map_align_divs(map);
8466 /* Align the divs of each element of "list" to those of "bmap".
8467 * Both "bmap" and the elements of "list" are assumed to have known divs.
8469 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8470 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8472 int i, n;
8474 if (!list || !bmap)
8475 return isl_basic_map_list_free(list);
8477 n = isl_basic_map_list_n_basic_map(list);
8478 for (i = 0; i < n; ++i) {
8479 isl_basic_map *bmap_i;
8481 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8482 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8483 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8486 return list;
8489 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8490 __isl_take isl_map *map)
8492 if (!set || !map)
8493 goto error;
8494 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8495 map = isl_map_intersect_domain(map, set);
8496 set = isl_map_range(map);
8497 return set;
8498 error:
8499 isl_set_free(set);
8500 isl_map_free(map);
8501 return NULL;
8504 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8505 __isl_take isl_map *map)
8507 return isl_map_align_params_map_map_and(set, map, &set_apply);
8510 /* There is no need to cow as removing empty parts doesn't change
8511 * the meaning of the set.
8513 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8515 int i;
8517 if (!map)
8518 return NULL;
8520 for (i = map->n - 1; i >= 0; --i)
8521 remove_if_empty(map, i);
8523 return map;
8526 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8528 return (struct isl_set *)
8529 isl_map_remove_empty_parts((struct isl_map *)set);
8532 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8534 struct isl_basic_map *bmap;
8535 if (!map || map->n == 0)
8536 return NULL;
8537 bmap = map->p[map->n-1];
8538 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8539 return isl_basic_map_copy(bmap);
8542 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8544 return (struct isl_basic_set *)
8545 isl_map_copy_basic_map((struct isl_map *)set);
8548 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8549 __isl_keep isl_basic_map *bmap)
8551 int i;
8553 if (!map || !bmap)
8554 goto error;
8555 for (i = map->n-1; i >= 0; --i) {
8556 if (map->p[i] != bmap)
8557 continue;
8558 map = isl_map_cow(map);
8559 if (!map)
8560 goto error;
8561 isl_basic_map_free(map->p[i]);
8562 if (i != map->n-1) {
8563 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8564 map->p[i] = map->p[map->n-1];
8566 map->n--;
8567 return map;
8569 return map;
8570 error:
8571 isl_map_free(map);
8572 return NULL;
8575 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8576 struct isl_basic_set *bset)
8578 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8579 (struct isl_basic_map *)bset);
8582 /* Given two basic sets bset1 and bset2, compute the maximal difference
8583 * between the values of dimension pos in bset1 and those in bset2
8584 * for any common value of the parameters and dimensions preceding pos.
8586 static enum isl_lp_result basic_set_maximal_difference_at(
8587 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8588 int pos, isl_int *opt)
8590 isl_space *dims;
8591 struct isl_basic_map *bmap1 = NULL;
8592 struct isl_basic_map *bmap2 = NULL;
8593 struct isl_ctx *ctx;
8594 struct isl_vec *obj;
8595 unsigned total;
8596 unsigned nparam;
8597 unsigned dim1, dim2;
8598 enum isl_lp_result res;
8600 if (!bset1 || !bset2)
8601 return isl_lp_error;
8603 nparam = isl_basic_set_n_param(bset1);
8604 dim1 = isl_basic_set_n_dim(bset1);
8605 dim2 = isl_basic_set_n_dim(bset2);
8606 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8607 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8608 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8609 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8610 if (!bmap1 || !bmap2)
8611 goto error;
8612 bmap1 = isl_basic_map_cow(bmap1);
8613 bmap1 = isl_basic_map_extend(bmap1, nparam,
8614 pos, (dim1 - pos) + (dim2 - pos),
8615 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8616 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8617 if (!bmap1)
8618 goto error2;
8619 total = isl_basic_map_total_dim(bmap1);
8620 ctx = bmap1->ctx;
8621 obj = isl_vec_alloc(ctx, 1 + total);
8622 if (!obj)
8623 goto error2;
8624 isl_seq_clr(obj->block.data, 1 + total);
8625 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8626 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8627 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8628 opt, NULL, NULL);
8629 isl_basic_map_free(bmap1);
8630 isl_vec_free(obj);
8631 return res;
8632 error:
8633 isl_basic_map_free(bmap2);
8634 error2:
8635 isl_basic_map_free(bmap1);
8636 return isl_lp_error;
8639 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8640 * for any common value of the parameters and dimensions preceding pos
8641 * in both basic sets, the values of dimension pos in bset1 are
8642 * smaller or larger than those in bset2.
8644 * Returns
8645 * 1 if bset1 follows bset2
8646 * -1 if bset1 precedes bset2
8647 * 0 if bset1 and bset2 are incomparable
8648 * -2 if some error occurred.
8650 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8651 struct isl_basic_set *bset2, int pos)
8653 isl_int opt;
8654 enum isl_lp_result res;
8655 int cmp;
8657 isl_int_init(opt);
8659 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8661 if (res == isl_lp_empty)
8662 cmp = 0;
8663 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8664 res == isl_lp_unbounded)
8665 cmp = 1;
8666 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8667 cmp = -1;
8668 else
8669 cmp = -2;
8671 isl_int_clear(opt);
8672 return cmp;
8675 /* Given two basic sets bset1 and bset2, check whether
8676 * for any common value of the parameters and dimensions preceding pos
8677 * there is a value of dimension pos in bset1 that is larger
8678 * than a value of the same dimension in bset2.
8680 * Return
8681 * 1 if there exists such a pair
8682 * 0 if there is no such pair, but there is a pair of equal values
8683 * -1 otherwise
8684 * -2 if some error occurred.
8686 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8687 __isl_keep isl_basic_set *bset2, int pos)
8689 isl_int opt;
8690 enum isl_lp_result res;
8691 int cmp;
8693 isl_int_init(opt);
8695 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8697 if (res == isl_lp_empty)
8698 cmp = -1;
8699 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8700 res == isl_lp_unbounded)
8701 cmp = 1;
8702 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8703 cmp = -1;
8704 else if (res == isl_lp_ok)
8705 cmp = 0;
8706 else
8707 cmp = -2;
8709 isl_int_clear(opt);
8710 return cmp;
8713 /* Given two sets set1 and set2, check whether
8714 * for any common value of the parameters and dimensions preceding pos
8715 * there is a value of dimension pos in set1 that is larger
8716 * than a value of the same dimension in set2.
8718 * Return
8719 * 1 if there exists such a pair
8720 * 0 if there is no such pair, but there is a pair of equal values
8721 * -1 otherwise
8722 * -2 if some error occurred.
8724 int isl_set_follows_at(__isl_keep isl_set *set1,
8725 __isl_keep isl_set *set2, int pos)
8727 int i, j;
8728 int follows = -1;
8730 if (!set1 || !set2)
8731 return -2;
8733 for (i = 0; i < set1->n; ++i)
8734 for (j = 0; j < set2->n; ++j) {
8735 int f;
8736 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8737 if (f == 1 || f == -2)
8738 return f;
8739 if (f > follows)
8740 follows = f;
8743 return follows;
8746 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8747 unsigned pos, isl_int *val)
8749 int i;
8750 int d;
8751 unsigned total;
8753 if (!bmap)
8754 return -1;
8755 total = isl_basic_map_total_dim(bmap);
8756 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8757 for (; d+1 > pos; --d)
8758 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8759 break;
8760 if (d != pos)
8761 continue;
8762 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8763 return 0;
8764 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8765 return 0;
8766 if (!isl_int_is_one(bmap->eq[i][1+d]))
8767 return 0;
8768 if (val)
8769 isl_int_neg(*val, bmap->eq[i][0]);
8770 return 1;
8772 return 0;
8775 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8776 unsigned pos, isl_int *val)
8778 int i;
8779 isl_int v;
8780 isl_int tmp;
8781 int fixed;
8783 if (!map)
8784 return -1;
8785 if (map->n == 0)
8786 return 0;
8787 if (map->n == 1)
8788 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8789 isl_int_init(v);
8790 isl_int_init(tmp);
8791 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8792 for (i = 1; fixed == 1 && i < map->n; ++i) {
8793 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8794 if (fixed == 1 && isl_int_ne(tmp, v))
8795 fixed = 0;
8797 if (val)
8798 isl_int_set(*val, v);
8799 isl_int_clear(tmp);
8800 isl_int_clear(v);
8801 return fixed;
8804 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8805 unsigned pos, isl_int *val)
8807 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8808 pos, val);
8811 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8812 isl_int *val)
8814 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8817 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8818 enum isl_dim_type type, unsigned pos, isl_int *val)
8820 if (pos >= isl_basic_map_dim(bmap, type))
8821 return -1;
8822 return isl_basic_map_plain_has_fixed_var(bmap,
8823 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8826 /* If "bmap" obviously lies on a hyperplane where the given dimension
8827 * has a fixed value, then return that value.
8828 * Otherwise return NaN.
8830 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8831 __isl_keep isl_basic_map *bmap,
8832 enum isl_dim_type type, unsigned pos)
8834 isl_ctx *ctx;
8835 isl_val *v;
8836 int fixed;
8838 if (!bmap)
8839 return NULL;
8840 ctx = isl_basic_map_get_ctx(bmap);
8841 v = isl_val_alloc(ctx);
8842 if (!v)
8843 return NULL;
8844 fixed = isl_basic_map_plain_is_fixed(bmap, 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 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8856 enum isl_dim_type type, unsigned pos, isl_int *val)
8858 if (pos >= isl_map_dim(map, type))
8859 return -1;
8860 return isl_map_plain_has_fixed_var(map,
8861 map_offset(map, type) - 1 + pos, val);
8864 /* If "map" obviously lies on a hyperplane where the given dimension
8865 * has a fixed value, then return that value.
8866 * Otherwise return NaN.
8868 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8869 enum isl_dim_type type, unsigned pos)
8871 isl_ctx *ctx;
8872 isl_val *v;
8873 int fixed;
8875 if (!map)
8876 return NULL;
8877 ctx = isl_map_get_ctx(map);
8878 v = isl_val_alloc(ctx);
8879 if (!v)
8880 return NULL;
8881 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8882 if (fixed < 0)
8883 return isl_val_free(v);
8884 if (fixed) {
8885 isl_int_set_si(v->d, 1);
8886 return v;
8888 isl_val_free(v);
8889 return isl_val_nan(ctx);
8892 /* If "set" obviously lies on a hyperplane where the given dimension
8893 * has a fixed value, then return that value.
8894 * Otherwise return NaN.
8896 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8897 enum isl_dim_type type, unsigned pos)
8899 return isl_map_plain_get_val_if_fixed(set, type, pos);
8902 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8903 enum isl_dim_type type, unsigned pos, isl_int *val)
8905 return isl_map_plain_is_fixed(set, type, pos, val);
8908 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8909 * then return this fixed value in *val.
8911 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8912 unsigned dim, isl_int *val)
8914 return isl_basic_set_plain_has_fixed_var(bset,
8915 isl_basic_set_n_param(bset) + dim, val);
8918 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8919 * then return this fixed value in *val.
8921 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8922 unsigned dim, isl_int *val)
8924 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8927 /* Check if input variable in has fixed value and if so and if val is not NULL,
8928 * then return this fixed value in *val.
8930 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8931 unsigned in, isl_int *val)
8933 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8936 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8937 * and if val is not NULL, then return this lower bound in *val.
8939 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8940 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8942 int i, i_eq = -1, i_ineq = -1;
8943 isl_int *c;
8944 unsigned total;
8945 unsigned nparam;
8947 if (!bset)
8948 return -1;
8949 total = isl_basic_set_total_dim(bset);
8950 nparam = isl_basic_set_n_param(bset);
8951 for (i = 0; i < bset->n_eq; ++i) {
8952 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8953 continue;
8954 if (i_eq != -1)
8955 return 0;
8956 i_eq = i;
8958 for (i = 0; i < bset->n_ineq; ++i) {
8959 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8960 continue;
8961 if (i_eq != -1 || i_ineq != -1)
8962 return 0;
8963 i_ineq = i;
8965 if (i_eq == -1 && i_ineq == -1)
8966 return 0;
8967 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8968 /* The coefficient should always be one due to normalization. */
8969 if (!isl_int_is_one(c[1+nparam+dim]))
8970 return 0;
8971 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8972 return 0;
8973 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8974 total - nparam - dim - 1) != -1)
8975 return 0;
8976 if (val)
8977 isl_int_neg(*val, c[0]);
8978 return 1;
8981 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8982 unsigned dim, isl_int *val)
8984 int i;
8985 isl_int v;
8986 isl_int tmp;
8987 int fixed;
8989 if (!set)
8990 return -1;
8991 if (set->n == 0)
8992 return 0;
8993 if (set->n == 1)
8994 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8995 dim, val);
8996 isl_int_init(v);
8997 isl_int_init(tmp);
8998 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8999 dim, &v);
9000 for (i = 1; fixed == 1 && i < set->n; ++i) {
9001 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
9002 dim, &tmp);
9003 if (fixed == 1 && isl_int_ne(tmp, v))
9004 fixed = 0;
9006 if (val)
9007 isl_int_set(*val, v);
9008 isl_int_clear(tmp);
9009 isl_int_clear(v);
9010 return fixed;
9013 /* Return -1 if the constraint "c1" should be sorted before "c2"
9014 * and 1 if it should be sorted after "c2".
9015 * Return 0 if the two constraints are the same (up to the constant term).
9017 * In particular, if a constraint involves later variables than another
9018 * then it is sorted after this other constraint.
9019 * uset_gist depends on constraints without existentially quantified
9020 * variables sorting first.
9022 * For constraints that have the same latest variable, those
9023 * with the same coefficient for this latest variable (first in absolute value
9024 * and then in actual value) are grouped together.
9025 * This is useful for detecting pairs of constraints that can
9026 * be chained in their printed representation.
9028 * Finally, within a group, constraints are sorted according to
9029 * their coefficients (excluding the constant term).
9031 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9033 isl_int **c1 = (isl_int **) p1;
9034 isl_int **c2 = (isl_int **) p2;
9035 int l1, l2;
9036 unsigned size = *(unsigned *) arg;
9037 int cmp;
9039 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9040 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9042 if (l1 != l2)
9043 return l1 - l2;
9045 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9046 if (cmp != 0)
9047 return cmp;
9048 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9049 if (cmp != 0)
9050 return -cmp;
9052 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9055 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9056 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9057 * and 0 if the two constraints are the same (up to the constant term).
9059 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9060 isl_int *c1, isl_int *c2)
9062 unsigned total;
9064 if (!bmap)
9065 return -2;
9066 total = isl_basic_map_total_dim(bmap);
9067 return sort_constraint_cmp(&c1, &c2, &total);
9070 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9071 __isl_take isl_basic_map *bmap)
9073 unsigned total;
9075 if (!bmap)
9076 return NULL;
9077 if (bmap->n_ineq == 0)
9078 return bmap;
9079 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9080 return bmap;
9081 total = isl_basic_map_total_dim(bmap);
9082 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9083 &sort_constraint_cmp, &total) < 0)
9084 return isl_basic_map_free(bmap);
9085 return bmap;
9088 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9089 __isl_take isl_basic_set *bset)
9091 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9092 (struct isl_basic_map *)bset);
9095 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9097 if (!bmap)
9098 return NULL;
9099 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9100 return bmap;
9101 bmap = isl_basic_map_remove_redundancies(bmap);
9102 bmap = isl_basic_map_sort_constraints(bmap);
9103 if (bmap)
9104 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9105 return bmap;
9108 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9110 return (struct isl_basic_set *)isl_basic_map_normalize(
9111 (struct isl_basic_map *)bset);
9114 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9115 const __isl_keep isl_basic_map *bmap2)
9117 int i, cmp;
9118 unsigned total;
9120 if (!bmap1 || !bmap2)
9121 return -1;
9123 if (bmap1 == bmap2)
9124 return 0;
9125 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9126 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9127 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9128 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9129 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9130 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9131 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9132 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9133 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9134 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9135 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9136 return 0;
9137 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9138 return 1;
9139 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9140 return -1;
9141 if (bmap1->n_eq != bmap2->n_eq)
9142 return bmap1->n_eq - bmap2->n_eq;
9143 if (bmap1->n_ineq != bmap2->n_ineq)
9144 return bmap1->n_ineq - bmap2->n_ineq;
9145 if (bmap1->n_div != bmap2->n_div)
9146 return bmap1->n_div - bmap2->n_div;
9147 total = isl_basic_map_total_dim(bmap1);
9148 for (i = 0; i < bmap1->n_eq; ++i) {
9149 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9150 if (cmp)
9151 return cmp;
9153 for (i = 0; i < bmap1->n_ineq; ++i) {
9154 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9155 if (cmp)
9156 return cmp;
9158 for (i = 0; i < bmap1->n_div; ++i) {
9159 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9160 if (cmp)
9161 return cmp;
9163 return 0;
9166 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9167 const __isl_keep isl_basic_set *bset2)
9169 return isl_basic_map_plain_cmp(bset1, bset2);
9172 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9174 int i, cmp;
9176 if (set1 == set2)
9177 return 0;
9178 if (set1->n != set2->n)
9179 return set1->n - set2->n;
9181 for (i = 0; i < set1->n; ++i) {
9182 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9183 if (cmp)
9184 return cmp;
9187 return 0;
9190 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9191 __isl_keep isl_basic_map *bmap2)
9193 if (!bmap1 || !bmap2)
9194 return isl_bool_error;
9195 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9198 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9199 __isl_keep isl_basic_set *bset2)
9201 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9202 (isl_basic_map *)bset2);
9205 static int qsort_bmap_cmp(const void *p1, const void *p2)
9207 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9208 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9210 return isl_basic_map_plain_cmp(bmap1, bmap2);
9213 /* Sort the basic maps of "map" and remove duplicate basic maps.
9215 * While removing basic maps, we make sure that the basic maps remain
9216 * sorted because isl_map_normalize expects the basic maps of the result
9217 * to be sorted.
9219 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9221 int i, j;
9223 map = isl_map_remove_empty_parts(map);
9224 if (!map)
9225 return NULL;
9226 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9227 for (i = map->n - 1; i >= 1; --i) {
9228 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9229 continue;
9230 isl_basic_map_free(map->p[i-1]);
9231 for (j = i; j < map->n; ++j)
9232 map->p[j - 1] = map->p[j];
9233 map->n--;
9236 return map;
9239 /* Remove obvious duplicates among the basic maps of "map".
9241 * Unlike isl_map_normalize, this function does not remove redundant
9242 * constraints and only removes duplicates that have exactly the same
9243 * constraints in the input. It does sort the constraints and
9244 * the basic maps to ease the detection of duplicates.
9246 * If "map" has already been normalized or if the basic maps are
9247 * disjoint, then there can be no duplicates.
9249 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9251 int i;
9252 isl_basic_map *bmap;
9254 if (!map)
9255 return NULL;
9256 if (map->n <= 1)
9257 return map;
9258 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9259 return map;
9260 for (i = 0; i < map->n; ++i) {
9261 bmap = isl_basic_map_copy(map->p[i]);
9262 bmap = isl_basic_map_sort_constraints(bmap);
9263 if (!bmap)
9264 return isl_map_free(map);
9265 isl_basic_map_free(map->p[i]);
9266 map->p[i] = bmap;
9269 map = sort_and_remove_duplicates(map);
9270 return map;
9273 /* We normalize in place, but if anything goes wrong we need
9274 * to return NULL, so we need to make sure we don't change the
9275 * meaning of any possible other copies of map.
9277 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9279 int i;
9280 struct isl_basic_map *bmap;
9282 if (!map)
9283 return NULL;
9284 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9285 return map;
9286 for (i = 0; i < map->n; ++i) {
9287 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9288 if (!bmap)
9289 goto error;
9290 isl_basic_map_free(map->p[i]);
9291 map->p[i] = bmap;
9294 map = sort_and_remove_duplicates(map);
9295 if (map)
9296 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9297 return map;
9298 error:
9299 isl_map_free(map);
9300 return NULL;
9303 struct isl_set *isl_set_normalize(struct isl_set *set)
9305 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9308 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9309 __isl_keep isl_map *map2)
9311 int i;
9312 isl_bool equal;
9314 if (!map1 || !map2)
9315 return isl_bool_error;
9317 if (map1 == map2)
9318 return isl_bool_true;
9319 if (!isl_space_is_equal(map1->dim, map2->dim))
9320 return isl_bool_false;
9322 map1 = isl_map_copy(map1);
9323 map2 = isl_map_copy(map2);
9324 map1 = isl_map_normalize(map1);
9325 map2 = isl_map_normalize(map2);
9326 if (!map1 || !map2)
9327 goto error;
9328 equal = map1->n == map2->n;
9329 for (i = 0; equal && i < map1->n; ++i) {
9330 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9331 if (equal < 0)
9332 goto error;
9334 isl_map_free(map1);
9335 isl_map_free(map2);
9336 return equal;
9337 error:
9338 isl_map_free(map1);
9339 isl_map_free(map2);
9340 return isl_bool_error;
9343 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9344 __isl_keep isl_set *set2)
9346 return isl_map_plain_is_equal((struct isl_map *)set1,
9347 (struct isl_map *)set2);
9350 /* Return an interval that ranges from min to max (inclusive)
9352 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9353 isl_int min, isl_int max)
9355 int k;
9356 struct isl_basic_set *bset = NULL;
9358 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9359 if (!bset)
9360 goto error;
9362 k = isl_basic_set_alloc_inequality(bset);
9363 if (k < 0)
9364 goto error;
9365 isl_int_set_si(bset->ineq[k][1], 1);
9366 isl_int_neg(bset->ineq[k][0], min);
9368 k = isl_basic_set_alloc_inequality(bset);
9369 if (k < 0)
9370 goto error;
9371 isl_int_set_si(bset->ineq[k][1], -1);
9372 isl_int_set(bset->ineq[k][0], max);
9374 return bset;
9375 error:
9376 isl_basic_set_free(bset);
9377 return NULL;
9380 /* Return the basic maps in "map" as a list.
9382 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9383 __isl_keep isl_map *map)
9385 int i;
9386 isl_ctx *ctx;
9387 isl_basic_map_list *list;
9389 if (!map)
9390 return NULL;
9391 ctx = isl_map_get_ctx(map);
9392 list = isl_basic_map_list_alloc(ctx, map->n);
9394 for (i = 0; i < map->n; ++i) {
9395 isl_basic_map *bmap;
9397 bmap = isl_basic_map_copy(map->p[i]);
9398 list = isl_basic_map_list_add(list, bmap);
9401 return list;
9404 /* Return the intersection of the elements in the non-empty list "list".
9405 * All elements are assumed to live in the same space.
9407 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9408 __isl_take isl_basic_map_list *list)
9410 int i, n;
9411 isl_basic_map *bmap;
9413 if (!list)
9414 return NULL;
9415 n = isl_basic_map_list_n_basic_map(list);
9416 if (n < 1)
9417 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9418 "expecting non-empty list", goto error);
9420 bmap = isl_basic_map_list_get_basic_map(list, 0);
9421 for (i = 1; i < n; ++i) {
9422 isl_basic_map *bmap_i;
9424 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9425 bmap = isl_basic_map_intersect(bmap, bmap_i);
9428 isl_basic_map_list_free(list);
9429 return bmap;
9430 error:
9431 isl_basic_map_list_free(list);
9432 return NULL;
9435 /* Return the intersection of the elements in the non-empty list "list".
9436 * All elements are assumed to live in the same space.
9438 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9439 __isl_take isl_basic_set_list *list)
9441 return isl_basic_map_list_intersect(list);
9444 /* Return the union of the elements in the non-empty list "list".
9445 * All elements are assumed to live in the same space.
9447 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9449 int i, n;
9450 isl_set *set;
9452 if (!list)
9453 return NULL;
9454 n = isl_set_list_n_set(list);
9455 if (n < 1)
9456 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9457 "expecting non-empty list", goto error);
9459 set = isl_set_list_get_set(list, 0);
9460 for (i = 1; i < n; ++i) {
9461 isl_set *set_i;
9463 set_i = isl_set_list_get_set(list, i);
9464 set = isl_set_union(set, set_i);
9467 isl_set_list_free(list);
9468 return set;
9469 error:
9470 isl_set_list_free(list);
9471 return NULL;
9474 /* Return the Cartesian product of the basic sets in list (in the given order).
9476 __isl_give isl_basic_set *isl_basic_set_list_product(
9477 __isl_take struct isl_basic_set_list *list)
9479 int i;
9480 unsigned dim;
9481 unsigned nparam;
9482 unsigned extra;
9483 unsigned n_eq;
9484 unsigned n_ineq;
9485 struct isl_basic_set *product = NULL;
9487 if (!list)
9488 goto error;
9489 isl_assert(list->ctx, list->n > 0, goto error);
9490 isl_assert(list->ctx, list->p[0], goto error);
9491 nparam = isl_basic_set_n_param(list->p[0]);
9492 dim = isl_basic_set_n_dim(list->p[0]);
9493 extra = list->p[0]->n_div;
9494 n_eq = list->p[0]->n_eq;
9495 n_ineq = list->p[0]->n_ineq;
9496 for (i = 1; i < list->n; ++i) {
9497 isl_assert(list->ctx, list->p[i], goto error);
9498 isl_assert(list->ctx,
9499 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9500 dim += isl_basic_set_n_dim(list->p[i]);
9501 extra += list->p[i]->n_div;
9502 n_eq += list->p[i]->n_eq;
9503 n_ineq += list->p[i]->n_ineq;
9505 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9506 n_eq, n_ineq);
9507 if (!product)
9508 goto error;
9509 dim = 0;
9510 for (i = 0; i < list->n; ++i) {
9511 isl_basic_set_add_constraints(product,
9512 isl_basic_set_copy(list->p[i]), dim);
9513 dim += isl_basic_set_n_dim(list->p[i]);
9515 isl_basic_set_list_free(list);
9516 return product;
9517 error:
9518 isl_basic_set_free(product);
9519 isl_basic_set_list_free(list);
9520 return NULL;
9523 struct isl_basic_map *isl_basic_map_product(
9524 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9526 isl_space *dim_result = NULL;
9527 struct isl_basic_map *bmap;
9528 unsigned in1, in2, out1, out2, nparam, total, pos;
9529 struct isl_dim_map *dim_map1, *dim_map2;
9531 if (!bmap1 || !bmap2)
9532 goto error;
9534 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9535 bmap2->dim, isl_dim_param), goto error);
9536 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9537 isl_space_copy(bmap2->dim));
9539 in1 = isl_basic_map_n_in(bmap1);
9540 in2 = isl_basic_map_n_in(bmap2);
9541 out1 = isl_basic_map_n_out(bmap1);
9542 out2 = isl_basic_map_n_out(bmap2);
9543 nparam = isl_basic_map_n_param(bmap1);
9545 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9546 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9547 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9548 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9549 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9550 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9551 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9552 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9553 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9554 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9555 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9557 bmap = isl_basic_map_alloc_space(dim_result,
9558 bmap1->n_div + bmap2->n_div,
9559 bmap1->n_eq + bmap2->n_eq,
9560 bmap1->n_ineq + bmap2->n_ineq);
9561 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9562 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9563 bmap = isl_basic_map_simplify(bmap);
9564 return isl_basic_map_finalize(bmap);
9565 error:
9566 isl_basic_map_free(bmap1);
9567 isl_basic_map_free(bmap2);
9568 return NULL;
9571 __isl_give isl_basic_map *isl_basic_map_flat_product(
9572 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9574 isl_basic_map *prod;
9576 prod = isl_basic_map_product(bmap1, bmap2);
9577 prod = isl_basic_map_flatten(prod);
9578 return prod;
9581 __isl_give isl_basic_set *isl_basic_set_flat_product(
9582 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9584 return isl_basic_map_flat_range_product(bset1, bset2);
9587 __isl_give isl_basic_map *isl_basic_map_domain_product(
9588 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9590 isl_space *space_result = NULL;
9591 isl_basic_map *bmap;
9592 unsigned in1, in2, out, nparam, total, pos;
9593 struct isl_dim_map *dim_map1, *dim_map2;
9595 if (!bmap1 || !bmap2)
9596 goto error;
9598 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9599 isl_space_copy(bmap2->dim));
9601 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9602 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9603 out = isl_basic_map_dim(bmap1, isl_dim_out);
9604 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9606 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9607 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9608 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9609 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9610 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9611 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9612 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9613 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9614 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9615 isl_dim_map_div(dim_map1, bmap1, pos += out);
9616 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9618 bmap = isl_basic_map_alloc_space(space_result,
9619 bmap1->n_div + bmap2->n_div,
9620 bmap1->n_eq + bmap2->n_eq,
9621 bmap1->n_ineq + bmap2->n_ineq);
9622 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9623 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9624 bmap = isl_basic_map_simplify(bmap);
9625 return isl_basic_map_finalize(bmap);
9626 error:
9627 isl_basic_map_free(bmap1);
9628 isl_basic_map_free(bmap2);
9629 return NULL;
9632 __isl_give isl_basic_map *isl_basic_map_range_product(
9633 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9635 isl_space *dim_result = NULL;
9636 isl_basic_map *bmap;
9637 unsigned in, out1, out2, nparam, total, pos;
9638 struct isl_dim_map *dim_map1, *dim_map2;
9640 if (!bmap1 || !bmap2)
9641 goto error;
9643 if (!isl_space_match(bmap1->dim, isl_dim_param,
9644 bmap2->dim, isl_dim_param))
9645 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9646 "parameters don't match", goto error);
9648 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9649 isl_space_copy(bmap2->dim));
9651 in = isl_basic_map_dim(bmap1, isl_dim_in);
9652 out1 = isl_basic_map_n_out(bmap1);
9653 out2 = isl_basic_map_n_out(bmap2);
9654 nparam = isl_basic_map_n_param(bmap1);
9656 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9657 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9658 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9659 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9660 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9661 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9662 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9663 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9664 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9665 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9666 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9668 bmap = isl_basic_map_alloc_space(dim_result,
9669 bmap1->n_div + bmap2->n_div,
9670 bmap1->n_eq + bmap2->n_eq,
9671 bmap1->n_ineq + bmap2->n_ineq);
9672 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9673 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9674 bmap = isl_basic_map_simplify(bmap);
9675 return isl_basic_map_finalize(bmap);
9676 error:
9677 isl_basic_map_free(bmap1);
9678 isl_basic_map_free(bmap2);
9679 return NULL;
9682 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9683 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9685 isl_basic_map *prod;
9687 prod = isl_basic_map_range_product(bmap1, bmap2);
9688 prod = isl_basic_map_flatten_range(prod);
9689 return prod;
9692 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9693 * and collect the results.
9694 * The result live in the space obtained by calling "space_product"
9695 * on the spaces of "map1" and "map2".
9696 * If "remove_duplicates" is set then the result may contain duplicates
9697 * (even if the inputs do not) and so we try and remove the obvious
9698 * duplicates.
9700 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9701 __isl_take isl_map *map2,
9702 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9703 __isl_take isl_space *right),
9704 __isl_give isl_basic_map *(*basic_map_product)(
9705 __isl_take isl_basic_map *left,
9706 __isl_take isl_basic_map *right),
9707 int remove_duplicates)
9709 unsigned flags = 0;
9710 struct isl_map *result;
9711 int i, j;
9713 if (!map1 || !map2)
9714 goto error;
9716 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9717 map2->dim, isl_dim_param), goto error);
9719 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9720 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9721 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9723 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9724 isl_space_copy(map2->dim)),
9725 map1->n * map2->n, flags);
9726 if (!result)
9727 goto error;
9728 for (i = 0; i < map1->n; ++i)
9729 for (j = 0; j < map2->n; ++j) {
9730 struct isl_basic_map *part;
9731 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9732 isl_basic_map_copy(map2->p[j]));
9733 if (isl_basic_map_is_empty(part))
9734 isl_basic_map_free(part);
9735 else
9736 result = isl_map_add_basic_map(result, part);
9737 if (!result)
9738 goto error;
9740 if (remove_duplicates)
9741 result = isl_map_remove_obvious_duplicates(result);
9742 isl_map_free(map1);
9743 isl_map_free(map2);
9744 return result;
9745 error:
9746 isl_map_free(map1);
9747 isl_map_free(map2);
9748 return NULL;
9751 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9753 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9754 __isl_take isl_map *map2)
9756 return map_product(map1, map2, &isl_space_product,
9757 &isl_basic_map_product, 0);
9760 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9761 __isl_take isl_map *map2)
9763 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9766 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9768 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9769 __isl_take isl_map *map2)
9771 isl_map *prod;
9773 prod = isl_map_product(map1, map2);
9774 prod = isl_map_flatten(prod);
9775 return prod;
9778 /* Given two set A and B, construct its Cartesian product A x B.
9780 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9782 return isl_map_range_product(set1, set2);
9785 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9786 __isl_take isl_set *set2)
9788 return isl_map_flat_range_product(set1, set2);
9791 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9793 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9794 __isl_take isl_map *map2)
9796 return map_product(map1, map2, &isl_space_domain_product,
9797 &isl_basic_map_domain_product, 1);
9800 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9802 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9803 __isl_take isl_map *map2)
9805 return map_product(map1, map2, &isl_space_range_product,
9806 &isl_basic_map_range_product, 1);
9809 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9810 __isl_take isl_map *map2)
9812 return isl_map_align_params_map_map_and(map1, map2,
9813 &map_domain_product_aligned);
9816 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9817 __isl_take isl_map *map2)
9819 return isl_map_align_params_map_map_and(map1, map2,
9820 &map_range_product_aligned);
9823 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9825 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9827 isl_space *space;
9828 int total1, keep1, total2, keep2;
9830 if (!map)
9831 return NULL;
9832 if (!isl_space_domain_is_wrapping(map->dim) ||
9833 !isl_space_range_is_wrapping(map->dim))
9834 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9835 "not a product", return isl_map_free(map));
9837 space = isl_map_get_space(map);
9838 total1 = isl_space_dim(space, isl_dim_in);
9839 total2 = isl_space_dim(space, isl_dim_out);
9840 space = isl_space_factor_domain(space);
9841 keep1 = isl_space_dim(space, isl_dim_in);
9842 keep2 = isl_space_dim(space, isl_dim_out);
9843 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9844 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9845 map = isl_map_reset_space(map, space);
9847 return map;
9850 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9852 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9854 isl_space *space;
9855 int total1, keep1, total2, keep2;
9857 if (!map)
9858 return NULL;
9859 if (!isl_space_domain_is_wrapping(map->dim) ||
9860 !isl_space_range_is_wrapping(map->dim))
9861 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9862 "not a product", return isl_map_free(map));
9864 space = isl_map_get_space(map);
9865 total1 = isl_space_dim(space, isl_dim_in);
9866 total2 = isl_space_dim(space, isl_dim_out);
9867 space = isl_space_factor_range(space);
9868 keep1 = isl_space_dim(space, isl_dim_in);
9869 keep2 = isl_space_dim(space, isl_dim_out);
9870 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9871 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9872 map = isl_map_reset_space(map, space);
9874 return map;
9877 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9879 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9881 isl_space *space;
9882 int total, keep;
9884 if (!map)
9885 return NULL;
9886 if (!isl_space_domain_is_wrapping(map->dim))
9887 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9888 "domain is not a product", return isl_map_free(map));
9890 space = isl_map_get_space(map);
9891 total = isl_space_dim(space, isl_dim_in);
9892 space = isl_space_domain_factor_domain(space);
9893 keep = isl_space_dim(space, isl_dim_in);
9894 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9895 map = isl_map_reset_space(map, space);
9897 return map;
9900 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9902 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9904 isl_space *space;
9905 int total, keep;
9907 if (!map)
9908 return NULL;
9909 if (!isl_space_domain_is_wrapping(map->dim))
9910 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9911 "domain is not a product", return isl_map_free(map));
9913 space = isl_map_get_space(map);
9914 total = isl_space_dim(space, isl_dim_in);
9915 space = isl_space_domain_factor_range(space);
9916 keep = isl_space_dim(space, isl_dim_in);
9917 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9918 map = isl_map_reset_space(map, space);
9920 return map;
9923 /* Given a map A -> [B -> C], extract the map A -> B.
9925 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9927 isl_space *space;
9928 int total, keep;
9930 if (!map)
9931 return NULL;
9932 if (!isl_space_range_is_wrapping(map->dim))
9933 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9934 "range is not a product", return isl_map_free(map));
9936 space = isl_map_get_space(map);
9937 total = isl_space_dim(space, isl_dim_out);
9938 space = isl_space_range_factor_domain(space);
9939 keep = isl_space_dim(space, isl_dim_out);
9940 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9941 map = isl_map_reset_space(map, space);
9943 return map;
9946 /* Given a map A -> [B -> C], extract the map A -> C.
9948 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9950 isl_space *space;
9951 int total, keep;
9953 if (!map)
9954 return NULL;
9955 if (!isl_space_range_is_wrapping(map->dim))
9956 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9957 "range is not a product", return isl_map_free(map));
9959 space = isl_map_get_space(map);
9960 total = isl_space_dim(space, isl_dim_out);
9961 space = isl_space_range_factor_range(space);
9962 keep = isl_space_dim(space, isl_dim_out);
9963 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9964 map = isl_map_reset_space(map, space);
9966 return map;
9969 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9971 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9972 __isl_take isl_map *map2)
9974 isl_map *prod;
9976 prod = isl_map_domain_product(map1, map2);
9977 prod = isl_map_flatten_domain(prod);
9978 return prod;
9981 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9983 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9984 __isl_take isl_map *map2)
9986 isl_map *prod;
9988 prod = isl_map_range_product(map1, map2);
9989 prod = isl_map_flatten_range(prod);
9990 return prod;
9993 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9995 int i;
9996 uint32_t hash = isl_hash_init();
9997 unsigned total;
9999 if (!bmap)
10000 return 0;
10001 bmap = isl_basic_map_copy(bmap);
10002 bmap = isl_basic_map_normalize(bmap);
10003 if (!bmap)
10004 return 0;
10005 total = isl_basic_map_total_dim(bmap);
10006 isl_hash_byte(hash, bmap->n_eq & 0xFF);
10007 for (i = 0; i < bmap->n_eq; ++i) {
10008 uint32_t c_hash;
10009 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
10010 isl_hash_hash(hash, c_hash);
10012 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10013 for (i = 0; i < bmap->n_ineq; ++i) {
10014 uint32_t c_hash;
10015 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10016 isl_hash_hash(hash, c_hash);
10018 isl_hash_byte(hash, bmap->n_div & 0xFF);
10019 for (i = 0; i < bmap->n_div; ++i) {
10020 uint32_t c_hash;
10021 if (isl_int_is_zero(bmap->div[i][0]))
10022 continue;
10023 isl_hash_byte(hash, i & 0xFF);
10024 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10025 isl_hash_hash(hash, c_hash);
10027 isl_basic_map_free(bmap);
10028 return hash;
10031 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10033 return isl_basic_map_get_hash((isl_basic_map *)bset);
10036 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10038 int i;
10039 uint32_t hash;
10041 if (!map)
10042 return 0;
10043 map = isl_map_copy(map);
10044 map = isl_map_normalize(map);
10045 if (!map)
10046 return 0;
10048 hash = isl_hash_init();
10049 for (i = 0; i < map->n; ++i) {
10050 uint32_t bmap_hash;
10051 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10052 isl_hash_hash(hash, bmap_hash);
10055 isl_map_free(map);
10057 return hash;
10060 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10062 return isl_map_get_hash((isl_map *)set);
10065 /* Check if the value for dimension dim is completely determined
10066 * by the values of the other parameters and variables.
10067 * That is, check if dimension dim is involved in an equality.
10069 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10071 int i;
10072 unsigned nparam;
10074 if (!bset)
10075 return -1;
10076 nparam = isl_basic_set_n_param(bset);
10077 for (i = 0; i < bset->n_eq; ++i)
10078 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10079 return 1;
10080 return 0;
10083 /* Check if the value for dimension dim is completely determined
10084 * by the values of the other parameters and variables.
10085 * That is, check if dimension dim is involved in an equality
10086 * for each of the subsets.
10088 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10090 int i;
10092 if (!set)
10093 return -1;
10094 for (i = 0; i < set->n; ++i) {
10095 int unique;
10096 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10097 if (unique != 1)
10098 return unique;
10100 return 1;
10103 /* Return the number of basic maps in the (current) representation of "map".
10105 int isl_map_n_basic_map(__isl_keep isl_map *map)
10107 return map ? map->n : 0;
10110 int isl_set_n_basic_set(__isl_keep isl_set *set)
10112 return set ? set->n : 0;
10115 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10116 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10118 int i;
10120 if (!map)
10121 return isl_stat_error;
10123 for (i = 0; i < map->n; ++i)
10124 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10125 return isl_stat_error;
10127 return isl_stat_ok;
10130 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10131 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10133 int i;
10135 if (!set)
10136 return isl_stat_error;
10138 for (i = 0; i < set->n; ++i)
10139 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10140 return isl_stat_error;
10142 return isl_stat_ok;
10145 /* Return a list of basic sets, the union of which is equal to "set".
10147 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10148 __isl_keep isl_set *set)
10150 int i;
10151 isl_basic_set_list *list;
10153 if (!set)
10154 return NULL;
10156 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10157 for (i = 0; i < set->n; ++i) {
10158 isl_basic_set *bset;
10160 bset = isl_basic_set_copy(set->p[i]);
10161 list = isl_basic_set_list_add(list, bset);
10164 return list;
10167 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10169 isl_space *dim;
10171 if (!bset)
10172 return NULL;
10174 bset = isl_basic_set_cow(bset);
10175 if (!bset)
10176 return NULL;
10178 dim = isl_basic_set_get_space(bset);
10179 dim = isl_space_lift(dim, bset->n_div);
10180 if (!dim)
10181 goto error;
10182 isl_space_free(bset->dim);
10183 bset->dim = dim;
10184 bset->extra -= bset->n_div;
10185 bset->n_div = 0;
10187 bset = isl_basic_set_finalize(bset);
10189 return bset;
10190 error:
10191 isl_basic_set_free(bset);
10192 return NULL;
10195 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10197 int i;
10198 isl_space *dim;
10199 unsigned n_div;
10201 set = isl_set_align_divs(set);
10203 if (!set)
10204 return NULL;
10206 set = isl_set_cow(set);
10207 if (!set)
10208 return NULL;
10210 n_div = set->p[0]->n_div;
10211 dim = isl_set_get_space(set);
10212 dim = isl_space_lift(dim, n_div);
10213 if (!dim)
10214 goto error;
10215 isl_space_free(set->dim);
10216 set->dim = dim;
10218 for (i = 0; i < set->n; ++i) {
10219 set->p[i] = isl_basic_set_lift(set->p[i]);
10220 if (!set->p[i])
10221 goto error;
10224 return set;
10225 error:
10226 isl_set_free(set);
10227 return NULL;
10230 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10232 isl_space *dim;
10233 struct isl_basic_map *bmap;
10234 unsigned n_set;
10235 unsigned n_div;
10236 unsigned n_param;
10237 unsigned total;
10238 int i, k, l;
10240 set = isl_set_align_divs(set);
10242 if (!set)
10243 return NULL;
10245 dim = isl_set_get_space(set);
10246 if (set->n == 0 || set->p[0]->n_div == 0) {
10247 isl_set_free(set);
10248 return isl_map_identity(isl_space_map_from_set(dim));
10251 n_div = set->p[0]->n_div;
10252 dim = isl_space_map_from_set(dim);
10253 n_param = isl_space_dim(dim, isl_dim_param);
10254 n_set = isl_space_dim(dim, isl_dim_in);
10255 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10256 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10257 for (i = 0; i < n_set; ++i)
10258 bmap = var_equal(bmap, i);
10260 total = n_param + n_set + n_set + n_div;
10261 for (i = 0; i < n_div; ++i) {
10262 k = isl_basic_map_alloc_inequality(bmap);
10263 if (k < 0)
10264 goto error;
10265 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10266 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10267 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10268 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10269 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10270 set->p[0]->div[i][0]);
10272 l = isl_basic_map_alloc_inequality(bmap);
10273 if (l < 0)
10274 goto error;
10275 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10276 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10277 set->p[0]->div[i][0]);
10278 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10281 isl_set_free(set);
10282 bmap = isl_basic_map_simplify(bmap);
10283 bmap = isl_basic_map_finalize(bmap);
10284 return isl_map_from_basic_map(bmap);
10285 error:
10286 isl_set_free(set);
10287 isl_basic_map_free(bmap);
10288 return NULL;
10291 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10293 unsigned dim;
10294 int size = 0;
10296 if (!bset)
10297 return -1;
10299 dim = isl_basic_set_total_dim(bset);
10300 size += bset->n_eq * (1 + dim);
10301 size += bset->n_ineq * (1 + dim);
10302 size += bset->n_div * (2 + dim);
10304 return size;
10307 int isl_set_size(__isl_keep isl_set *set)
10309 int i;
10310 int size = 0;
10312 if (!set)
10313 return -1;
10315 for (i = 0; i < set->n; ++i)
10316 size += isl_basic_set_size(set->p[i]);
10318 return size;
10321 /* Check if there is any lower bound (if lower == 0) and/or upper
10322 * bound (if upper == 0) on the specified dim.
10324 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10325 enum isl_dim_type type, unsigned pos, int lower, int upper)
10327 int i;
10329 if (!bmap)
10330 return isl_bool_error;
10332 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10333 return isl_bool_error);
10335 pos += isl_basic_map_offset(bmap, type);
10337 for (i = 0; i < bmap->n_div; ++i) {
10338 if (isl_int_is_zero(bmap->div[i][0]))
10339 continue;
10340 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10341 return isl_bool_true;
10344 for (i = 0; i < bmap->n_eq; ++i)
10345 if (!isl_int_is_zero(bmap->eq[i][pos]))
10346 return isl_bool_true;
10348 for (i = 0; i < bmap->n_ineq; ++i) {
10349 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10350 if (sgn > 0)
10351 lower = 1;
10352 if (sgn < 0)
10353 upper = 1;
10356 return lower && upper;
10359 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10360 enum isl_dim_type type, unsigned pos)
10362 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10365 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10366 enum isl_dim_type type, unsigned pos)
10368 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10371 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10372 enum isl_dim_type type, unsigned pos)
10374 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10377 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10378 enum isl_dim_type type, unsigned pos)
10380 int i;
10382 if (!map)
10383 return -1;
10385 for (i = 0; i < map->n; ++i) {
10386 int bounded;
10387 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10388 if (bounded < 0 || !bounded)
10389 return bounded;
10392 return 1;
10395 /* Return 1 if the specified dim is involved in both an upper bound
10396 * and a lower bound.
10398 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10399 enum isl_dim_type type, unsigned pos)
10401 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10404 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10406 static isl_bool has_any_bound(__isl_keep isl_map *map,
10407 enum isl_dim_type type, unsigned pos,
10408 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10409 enum isl_dim_type type, unsigned pos))
10411 int i;
10413 if (!map)
10414 return isl_bool_error;
10416 for (i = 0; i < map->n; ++i) {
10417 isl_bool bounded;
10418 bounded = fn(map->p[i], type, pos);
10419 if (bounded < 0 || bounded)
10420 return bounded;
10423 return isl_bool_false;
10426 /* Return 1 if the specified dim is involved in any lower bound.
10428 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10429 enum isl_dim_type type, unsigned pos)
10431 return has_any_bound(set, type, pos,
10432 &isl_basic_map_dim_has_lower_bound);
10435 /* Return 1 if the specified dim is involved in any upper bound.
10437 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10438 enum isl_dim_type type, unsigned pos)
10440 return has_any_bound(set, type, pos,
10441 &isl_basic_map_dim_has_upper_bound);
10444 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10446 static isl_bool has_bound(__isl_keep isl_map *map,
10447 enum isl_dim_type type, unsigned pos,
10448 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10449 enum isl_dim_type type, unsigned pos))
10451 int i;
10453 if (!map)
10454 return isl_bool_error;
10456 for (i = 0; i < map->n; ++i) {
10457 isl_bool bounded;
10458 bounded = fn(map->p[i], type, pos);
10459 if (bounded < 0 || !bounded)
10460 return bounded;
10463 return isl_bool_true;
10466 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10468 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10469 enum isl_dim_type type, unsigned pos)
10471 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10474 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10476 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10477 enum isl_dim_type type, unsigned pos)
10479 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10482 /* For each of the "n" variables starting at "first", determine
10483 * the sign of the variable and put the results in the first "n"
10484 * elements of the array "signs".
10485 * Sign
10486 * 1 means that the variable is non-negative
10487 * -1 means that the variable is non-positive
10488 * 0 means the variable attains both positive and negative values.
10490 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10491 unsigned first, unsigned n, int *signs)
10493 isl_vec *bound = NULL;
10494 struct isl_tab *tab = NULL;
10495 struct isl_tab_undo *snap;
10496 int i;
10498 if (!bset || !signs)
10499 return -1;
10501 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10502 tab = isl_tab_from_basic_set(bset, 0);
10503 if (!bound || !tab)
10504 goto error;
10506 isl_seq_clr(bound->el, bound->size);
10507 isl_int_set_si(bound->el[0], -1);
10509 snap = isl_tab_snap(tab);
10510 for (i = 0; i < n; ++i) {
10511 int empty;
10513 isl_int_set_si(bound->el[1 + first + i], -1);
10514 if (isl_tab_add_ineq(tab, bound->el) < 0)
10515 goto error;
10516 empty = tab->empty;
10517 isl_int_set_si(bound->el[1 + first + i], 0);
10518 if (isl_tab_rollback(tab, snap) < 0)
10519 goto error;
10521 if (empty) {
10522 signs[i] = 1;
10523 continue;
10526 isl_int_set_si(bound->el[1 + first + i], 1);
10527 if (isl_tab_add_ineq(tab, bound->el) < 0)
10528 goto error;
10529 empty = tab->empty;
10530 isl_int_set_si(bound->el[1 + first + i], 0);
10531 if (isl_tab_rollback(tab, snap) < 0)
10532 goto error;
10534 signs[i] = empty ? -1 : 0;
10537 isl_tab_free(tab);
10538 isl_vec_free(bound);
10539 return 0;
10540 error:
10541 isl_tab_free(tab);
10542 isl_vec_free(bound);
10543 return -1;
10546 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10547 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10549 if (!bset || !signs)
10550 return -1;
10551 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10552 return -1);
10554 first += pos(bset->dim, type) - 1;
10555 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10558 /* Is it possible for the integer division "div" to depend (possibly
10559 * indirectly) on any output dimensions?
10561 * If the div is undefined, then we conservatively assume that it
10562 * may depend on them.
10563 * Otherwise, we check if it actually depends on them or on any integer
10564 * divisions that may depend on them.
10566 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10568 int i;
10569 unsigned n_out, o_out;
10570 unsigned n_div, o_div;
10572 if (isl_int_is_zero(bmap->div[div][0]))
10573 return 1;
10575 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10576 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10578 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10579 return 1;
10581 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10582 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10584 for (i = 0; i < n_div; ++i) {
10585 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10586 continue;
10587 if (div_may_involve_output(bmap, i))
10588 return 1;
10591 return 0;
10594 /* Return the first integer division of "bmap" in the range
10595 * [first, first + n[ that may depend on any output dimensions and
10596 * that has a non-zero coefficient in "c" (where the first coefficient
10597 * in "c" corresponds to integer division "first").
10599 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10600 isl_int *c, int first, int n)
10602 int k;
10604 if (!bmap)
10605 return -1;
10607 for (k = first; k < first + n; ++k) {
10608 if (isl_int_is_zero(c[k]))
10609 continue;
10610 if (div_may_involve_output(bmap, k))
10611 return k;
10614 return first + n;
10617 /* Look for a pair of inequality constraints in "bmap" of the form
10619 * -l + i >= 0 or i >= l
10620 * and
10621 * n + l - i >= 0 or i <= l + n
10623 * with n < "m" and i the output dimension at position "pos".
10624 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10625 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10626 * and earlier output dimensions, as well as integer divisions that do
10627 * not involve any of the output dimensions.
10629 * Return the index of the first inequality constraint or bmap->n_ineq
10630 * if no such pair can be found.
10632 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10633 int pos, isl_int m)
10635 int i, j;
10636 isl_ctx *ctx;
10637 unsigned total;
10638 unsigned n_div, o_div;
10639 unsigned n_out, o_out;
10640 int less;
10642 if (!bmap)
10643 return -1;
10645 ctx = isl_basic_map_get_ctx(bmap);
10646 total = isl_basic_map_total_dim(bmap);
10647 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10648 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10649 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10650 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10651 for (i = 0; i < bmap->n_ineq; ++i) {
10652 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10653 continue;
10654 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10655 n_out - (pos + 1)) != -1)
10656 continue;
10657 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10658 0, n_div) < n_div)
10659 continue;
10660 for (j = i + 1; j < bmap->n_ineq; ++j) {
10661 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10662 ctx->one))
10663 continue;
10664 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10665 bmap->ineq[j] + 1, total))
10666 continue;
10667 break;
10669 if (j >= bmap->n_ineq)
10670 continue;
10671 isl_int_add(bmap->ineq[i][0],
10672 bmap->ineq[i][0], bmap->ineq[j][0]);
10673 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10674 isl_int_sub(bmap->ineq[i][0],
10675 bmap->ineq[i][0], bmap->ineq[j][0]);
10676 if (!less)
10677 continue;
10678 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10679 return i;
10680 else
10681 return j;
10684 return bmap->n_ineq;
10687 /* Return the index of the equality of "bmap" that defines
10688 * the output dimension "pos" in terms of earlier dimensions.
10689 * The equality may also involve integer divisions, as long
10690 * as those integer divisions are defined in terms of
10691 * parameters or input dimensions.
10692 * In this case, *div is set to the number of integer divisions and
10693 * *ineq is set to the number of inequality constraints (provided
10694 * div and ineq are not NULL).
10696 * The equality may also involve a single integer division involving
10697 * the output dimensions (typically only output dimension "pos") as
10698 * long as the coefficient of output dimension "pos" is 1 or -1 and
10699 * there is a pair of constraints i >= l and i <= l + n, with i referring
10700 * to output dimension "pos", l an expression involving only earlier
10701 * dimensions and n smaller than the coefficient of the integer division
10702 * in the equality. In this case, the output dimension can be defined
10703 * in terms of a modulo expression that does not involve the integer division.
10704 * *div is then set to this single integer division and
10705 * *ineq is set to the index of constraint i >= l.
10707 * Return bmap->n_eq if there is no such equality.
10708 * Return -1 on error.
10710 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10711 int pos, int *div, int *ineq)
10713 int j, k, l;
10714 unsigned n_out, o_out;
10715 unsigned n_div, o_div;
10717 if (!bmap)
10718 return -1;
10720 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10721 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10722 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10723 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10725 if (ineq)
10726 *ineq = bmap->n_ineq;
10727 if (div)
10728 *div = n_div;
10729 for (j = 0; j < bmap->n_eq; ++j) {
10730 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10731 continue;
10732 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10733 n_out - (pos + 1)) != -1)
10734 continue;
10735 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10736 0, n_div);
10737 if (k >= n_div)
10738 return j;
10739 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10740 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10741 continue;
10742 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10743 k + 1, n_div - (k+1)) < n_div)
10744 continue;
10745 l = find_modulo_constraint_pair(bmap, pos,
10746 bmap->eq[j][o_div + k]);
10747 if (l < 0)
10748 return -1;
10749 if (l >= bmap->n_ineq)
10750 continue;
10751 if (div)
10752 *div = k;
10753 if (ineq)
10754 *ineq = l;
10755 return j;
10758 return bmap->n_eq;
10761 /* Check if the given basic map is obviously single-valued.
10762 * In particular, for each output dimension, check that there is
10763 * an equality that defines the output dimension in terms of
10764 * earlier dimensions.
10766 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10768 int i;
10769 unsigned n_out;
10771 if (!bmap)
10772 return isl_bool_error;
10774 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10776 for (i = 0; i < n_out; ++i) {
10777 int eq;
10779 eq = isl_basic_map_output_defining_equality(bmap, i,
10780 NULL, NULL);
10781 if (eq < 0)
10782 return isl_bool_error;
10783 if (eq >= bmap->n_eq)
10784 return isl_bool_false;
10787 return isl_bool_true;
10790 /* Check if the given basic map is single-valued.
10791 * We simply compute
10793 * M \circ M^-1
10795 * and check if the result is a subset of the identity mapping.
10797 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10799 isl_space *space;
10800 isl_basic_map *test;
10801 isl_basic_map *id;
10802 isl_bool sv;
10804 sv = isl_basic_map_plain_is_single_valued(bmap);
10805 if (sv < 0 || sv)
10806 return sv;
10808 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10809 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10811 space = isl_basic_map_get_space(bmap);
10812 space = isl_space_map_from_set(isl_space_range(space));
10813 id = isl_basic_map_identity(space);
10815 sv = isl_basic_map_is_subset(test, id);
10817 isl_basic_map_free(test);
10818 isl_basic_map_free(id);
10820 return sv;
10823 /* Check if the given map is obviously single-valued.
10825 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10827 if (!map)
10828 return isl_bool_error;
10829 if (map->n == 0)
10830 return isl_bool_true;
10831 if (map->n >= 2)
10832 return isl_bool_false;
10834 return isl_basic_map_plain_is_single_valued(map->p[0]);
10837 /* Check if the given map is single-valued.
10838 * We simply compute
10840 * M \circ M^-1
10842 * and check if the result is a subset of the identity mapping.
10844 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10846 isl_space *dim;
10847 isl_map *test;
10848 isl_map *id;
10849 isl_bool sv;
10851 sv = isl_map_plain_is_single_valued(map);
10852 if (sv < 0 || sv)
10853 return sv;
10855 test = isl_map_reverse(isl_map_copy(map));
10856 test = isl_map_apply_range(test, isl_map_copy(map));
10858 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10859 id = isl_map_identity(dim);
10861 sv = isl_map_is_subset(test, id);
10863 isl_map_free(test);
10864 isl_map_free(id);
10866 return sv;
10869 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10871 isl_bool in;
10873 map = isl_map_copy(map);
10874 map = isl_map_reverse(map);
10875 in = isl_map_is_single_valued(map);
10876 isl_map_free(map);
10878 return in;
10881 /* Check if the given map is obviously injective.
10883 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10885 isl_bool in;
10887 map = isl_map_copy(map);
10888 map = isl_map_reverse(map);
10889 in = isl_map_plain_is_single_valued(map);
10890 isl_map_free(map);
10892 return in;
10895 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10897 isl_bool sv;
10899 sv = isl_map_is_single_valued(map);
10900 if (sv < 0 || !sv)
10901 return sv;
10903 return isl_map_is_injective(map);
10906 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10908 return isl_map_is_single_valued((isl_map *)set);
10911 /* Does "map" only map elements to themselves?
10913 * If the domain and range spaces are different, then "map"
10914 * is considered not to be an identity relation, even if it is empty.
10915 * Otherwise, construct the maximal identity relation and
10916 * check whether "map" is a subset of this relation.
10918 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10920 isl_space *space;
10921 isl_map *id;
10922 isl_bool equal, is_identity;
10924 space = isl_map_get_space(map);
10925 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10926 isl_space_free(space);
10927 if (equal < 0 || !equal)
10928 return equal;
10930 id = isl_map_identity(isl_map_get_space(map));
10931 is_identity = isl_map_is_subset(map, id);
10932 isl_map_free(id);
10934 return is_identity;
10937 int isl_map_is_translation(__isl_keep isl_map *map)
10939 int ok;
10940 isl_set *delta;
10942 delta = isl_map_deltas(isl_map_copy(map));
10943 ok = isl_set_is_singleton(delta);
10944 isl_set_free(delta);
10946 return ok;
10949 static int unique(isl_int *p, unsigned pos, unsigned len)
10951 if (isl_seq_first_non_zero(p, pos) != -1)
10952 return 0;
10953 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10954 return 0;
10955 return 1;
10958 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10960 int i, j;
10961 unsigned nvar;
10962 unsigned ovar;
10964 if (!bset)
10965 return -1;
10967 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10968 return 0;
10970 nvar = isl_basic_set_dim(bset, isl_dim_set);
10971 ovar = isl_space_offset(bset->dim, isl_dim_set);
10972 for (j = 0; j < nvar; ++j) {
10973 int lower = 0, upper = 0;
10974 for (i = 0; i < bset->n_eq; ++i) {
10975 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10976 continue;
10977 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10978 return 0;
10979 break;
10981 if (i < bset->n_eq)
10982 continue;
10983 for (i = 0; i < bset->n_ineq; ++i) {
10984 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10985 continue;
10986 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10987 return 0;
10988 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10989 lower = 1;
10990 else
10991 upper = 1;
10993 if (!lower || !upper)
10994 return 0;
10997 return 1;
11000 int isl_set_is_box(__isl_keep isl_set *set)
11002 if (!set)
11003 return -1;
11004 if (set->n != 1)
11005 return 0;
11007 return isl_basic_set_is_box(set->p[0]);
11010 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
11012 if (!bset)
11013 return isl_bool_error;
11015 return isl_space_is_wrapping(bset->dim);
11018 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
11020 if (!set)
11021 return isl_bool_error;
11023 return isl_space_is_wrapping(set->dim);
11026 /* Modify the space of "map" through a call to "change".
11027 * If "can_change" is set (not NULL), then first call it to check
11028 * if the modification is allowed, printing the error message "cannot_change"
11029 * if it is not.
11031 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11032 isl_bool (*can_change)(__isl_keep isl_map *map),
11033 const char *cannot_change,
11034 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11036 isl_bool ok;
11037 isl_space *space;
11039 if (!map)
11040 return NULL;
11042 ok = can_change ? can_change(map) : isl_bool_true;
11043 if (ok < 0)
11044 return isl_map_free(map);
11045 if (!ok)
11046 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11047 return isl_map_free(map));
11049 space = change(isl_map_get_space(map));
11050 map = isl_map_reset_space(map, space);
11052 return map;
11055 /* Is the domain of "map" a wrapped relation?
11057 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11059 if (!map)
11060 return isl_bool_error;
11062 return isl_space_domain_is_wrapping(map->dim);
11065 /* Is the range of "map" a wrapped relation?
11067 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11069 if (!map)
11070 return isl_bool_error;
11072 return isl_space_range_is_wrapping(map->dim);
11075 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11077 bmap = isl_basic_map_cow(bmap);
11078 if (!bmap)
11079 return NULL;
11081 bmap->dim = isl_space_wrap(bmap->dim);
11082 if (!bmap->dim)
11083 goto error;
11085 bmap = isl_basic_map_finalize(bmap);
11087 return (isl_basic_set *)bmap;
11088 error:
11089 isl_basic_map_free(bmap);
11090 return NULL;
11093 /* Given a map A -> B, return the set (A -> B).
11095 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11097 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11100 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11102 bset = isl_basic_set_cow(bset);
11103 if (!bset)
11104 return NULL;
11106 bset->dim = isl_space_unwrap(bset->dim);
11107 if (!bset->dim)
11108 goto error;
11110 bset = isl_basic_set_finalize(bset);
11112 return (isl_basic_map *)bset;
11113 error:
11114 isl_basic_set_free(bset);
11115 return NULL;
11118 /* Given a set (A -> B), return the map A -> B.
11119 * Error out if "set" is not of the form (A -> B).
11121 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11123 return isl_map_change_space(set, &isl_set_is_wrapping,
11124 "not a wrapping set", &isl_space_unwrap);
11127 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11128 enum isl_dim_type type)
11130 if (!bmap)
11131 return NULL;
11133 if (!isl_space_is_named_or_nested(bmap->dim, type))
11134 return bmap;
11136 bmap = isl_basic_map_cow(bmap);
11137 if (!bmap)
11138 return NULL;
11140 bmap->dim = isl_space_reset(bmap->dim, type);
11141 if (!bmap->dim)
11142 goto error;
11144 bmap = isl_basic_map_finalize(bmap);
11146 return bmap;
11147 error:
11148 isl_basic_map_free(bmap);
11149 return NULL;
11152 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11153 enum isl_dim_type type)
11155 int i;
11157 if (!map)
11158 return NULL;
11160 if (!isl_space_is_named_or_nested(map->dim, type))
11161 return map;
11163 map = isl_map_cow(map);
11164 if (!map)
11165 return NULL;
11167 for (i = 0; i < map->n; ++i) {
11168 map->p[i] = isl_basic_map_reset(map->p[i], type);
11169 if (!map->p[i])
11170 goto error;
11172 map->dim = isl_space_reset(map->dim, type);
11173 if (!map->dim)
11174 goto error;
11176 return map;
11177 error:
11178 isl_map_free(map);
11179 return NULL;
11182 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11184 if (!bmap)
11185 return NULL;
11187 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11188 return bmap;
11190 bmap = isl_basic_map_cow(bmap);
11191 if (!bmap)
11192 return NULL;
11194 bmap->dim = isl_space_flatten(bmap->dim);
11195 if (!bmap->dim)
11196 goto error;
11198 bmap = isl_basic_map_finalize(bmap);
11200 return bmap;
11201 error:
11202 isl_basic_map_free(bmap);
11203 return NULL;
11206 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11208 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
11211 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11212 __isl_take isl_basic_map *bmap)
11214 if (!bmap)
11215 return NULL;
11217 if (!bmap->dim->nested[0])
11218 return bmap;
11220 bmap = isl_basic_map_cow(bmap);
11221 if (!bmap)
11222 return NULL;
11224 bmap->dim = isl_space_flatten_domain(bmap->dim);
11225 if (!bmap->dim)
11226 goto error;
11228 bmap = isl_basic_map_finalize(bmap);
11230 return bmap;
11231 error:
11232 isl_basic_map_free(bmap);
11233 return NULL;
11236 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11237 __isl_take isl_basic_map *bmap)
11239 if (!bmap)
11240 return NULL;
11242 if (!bmap->dim->nested[1])
11243 return bmap;
11245 bmap = isl_basic_map_cow(bmap);
11246 if (!bmap)
11247 return NULL;
11249 bmap->dim = isl_space_flatten_range(bmap->dim);
11250 if (!bmap->dim)
11251 goto error;
11253 bmap = isl_basic_map_finalize(bmap);
11255 return bmap;
11256 error:
11257 isl_basic_map_free(bmap);
11258 return NULL;
11261 /* Remove any internal structure from the spaces of domain and range of "map".
11263 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11265 if (!map)
11266 return NULL;
11268 if (!map->dim->nested[0] && !map->dim->nested[1])
11269 return map;
11271 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11274 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11276 return (isl_set *)isl_map_flatten((isl_map *)set);
11279 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11281 isl_space *dim, *flat_dim;
11282 isl_map *map;
11284 dim = isl_set_get_space(set);
11285 flat_dim = isl_space_flatten(isl_space_copy(dim));
11286 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11287 map = isl_map_intersect_domain(map, set);
11289 return map;
11292 /* Remove any internal structure from the space of the domain of "map".
11294 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11296 if (!map)
11297 return NULL;
11299 if (!map->dim->nested[0])
11300 return map;
11302 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11305 /* Remove any internal structure from the space of the range of "map".
11307 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11309 if (!map)
11310 return NULL;
11312 if (!map->dim->nested[1])
11313 return map;
11315 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11318 /* Reorder the dimensions of "bmap" according to the given dim_map
11319 * and set the dimension specification to "dim" and
11320 * perform Gaussian elimination on the result.
11322 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11323 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11325 isl_basic_map *res;
11326 unsigned flags;
11328 bmap = isl_basic_map_cow(bmap);
11329 if (!bmap || !dim || !dim_map)
11330 goto error;
11332 flags = bmap->flags;
11333 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11334 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11335 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11336 res = isl_basic_map_alloc_space(dim,
11337 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11338 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11339 if (res)
11340 res->flags = flags;
11341 res = isl_basic_map_gauss(res, NULL);
11342 res = isl_basic_map_finalize(res);
11343 return res;
11344 error:
11345 free(dim_map);
11346 isl_basic_map_free(bmap);
11347 isl_space_free(dim);
11348 return NULL;
11351 /* Reorder the dimensions of "map" according to given reordering.
11353 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11354 __isl_take isl_reordering *r)
11356 int i;
11357 struct isl_dim_map *dim_map;
11359 map = isl_map_cow(map);
11360 dim_map = isl_dim_map_from_reordering(r);
11361 if (!map || !r || !dim_map)
11362 goto error;
11364 for (i = 0; i < map->n; ++i) {
11365 struct isl_dim_map *dim_map_i;
11367 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11369 map->p[i] = isl_basic_map_realign(map->p[i],
11370 isl_space_copy(r->dim), dim_map_i);
11372 if (!map->p[i])
11373 goto error;
11376 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11378 isl_reordering_free(r);
11379 free(dim_map);
11380 return map;
11381 error:
11382 free(dim_map);
11383 isl_map_free(map);
11384 isl_reordering_free(r);
11385 return NULL;
11388 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11389 __isl_take isl_reordering *r)
11391 return (isl_set *)isl_map_realign((isl_map *)set, r);
11394 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11395 __isl_take isl_space *model)
11397 isl_ctx *ctx;
11399 if (!map || !model)
11400 goto error;
11402 ctx = isl_space_get_ctx(model);
11403 if (!isl_space_has_named_params(model))
11404 isl_die(ctx, isl_error_invalid,
11405 "model has unnamed parameters", goto error);
11406 if (!isl_space_has_named_params(map->dim))
11407 isl_die(ctx, isl_error_invalid,
11408 "relation has unnamed parameters", goto error);
11409 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11410 isl_reordering *exp;
11412 model = isl_space_drop_dims(model, isl_dim_in,
11413 0, isl_space_dim(model, isl_dim_in));
11414 model = isl_space_drop_dims(model, isl_dim_out,
11415 0, isl_space_dim(model, isl_dim_out));
11416 exp = isl_parameter_alignment_reordering(map->dim, model);
11417 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11418 map = isl_map_realign(map, exp);
11421 isl_space_free(model);
11422 return map;
11423 error:
11424 isl_space_free(model);
11425 isl_map_free(map);
11426 return NULL;
11429 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11430 __isl_take isl_space *model)
11432 return isl_map_align_params(set, model);
11435 /* Align the parameters of "bmap" to those of "model", introducing
11436 * additional parameters if needed.
11438 __isl_give isl_basic_map *isl_basic_map_align_params(
11439 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11441 isl_ctx *ctx;
11443 if (!bmap || !model)
11444 goto error;
11446 ctx = isl_space_get_ctx(model);
11447 if (!isl_space_has_named_params(model))
11448 isl_die(ctx, isl_error_invalid,
11449 "model has unnamed parameters", goto error);
11450 if (!isl_space_has_named_params(bmap->dim))
11451 isl_die(ctx, isl_error_invalid,
11452 "relation has unnamed parameters", goto error);
11453 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11454 isl_reordering *exp;
11455 struct isl_dim_map *dim_map;
11457 model = isl_space_drop_dims(model, isl_dim_in,
11458 0, isl_space_dim(model, isl_dim_in));
11459 model = isl_space_drop_dims(model, isl_dim_out,
11460 0, isl_space_dim(model, isl_dim_out));
11461 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11462 exp = isl_reordering_extend_space(exp,
11463 isl_basic_map_get_space(bmap));
11464 dim_map = isl_dim_map_from_reordering(exp);
11465 bmap = isl_basic_map_realign(bmap,
11466 exp ? isl_space_copy(exp->dim) : NULL,
11467 isl_dim_map_extend(dim_map, bmap));
11468 isl_reordering_free(exp);
11469 free(dim_map);
11472 isl_space_free(model);
11473 return bmap;
11474 error:
11475 isl_space_free(model);
11476 isl_basic_map_free(bmap);
11477 return NULL;
11480 /* Align the parameters of "bset" to those of "model", introducing
11481 * additional parameters if needed.
11483 __isl_give isl_basic_set *isl_basic_set_align_params(
11484 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11486 return isl_basic_map_align_params(bset, model);
11489 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11490 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11491 enum isl_dim_type c2, enum isl_dim_type c3,
11492 enum isl_dim_type c4, enum isl_dim_type c5)
11494 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11495 struct isl_mat *mat;
11496 int i, j, k;
11497 int pos;
11499 if (!bmap)
11500 return NULL;
11501 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11502 isl_basic_map_total_dim(bmap) + 1);
11503 if (!mat)
11504 return NULL;
11505 for (i = 0; i < bmap->n_eq; ++i)
11506 for (j = 0, pos = 0; j < 5; ++j) {
11507 int off = isl_basic_map_offset(bmap, c[j]);
11508 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11509 isl_int_set(mat->row[i][pos],
11510 bmap->eq[i][off + k]);
11511 ++pos;
11515 return mat;
11518 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11519 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11520 enum isl_dim_type c2, enum isl_dim_type c3,
11521 enum isl_dim_type c4, enum isl_dim_type c5)
11523 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11524 struct isl_mat *mat;
11525 int i, j, k;
11526 int pos;
11528 if (!bmap)
11529 return NULL;
11530 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11531 isl_basic_map_total_dim(bmap) + 1);
11532 if (!mat)
11533 return NULL;
11534 for (i = 0; i < bmap->n_ineq; ++i)
11535 for (j = 0, pos = 0; j < 5; ++j) {
11536 int off = isl_basic_map_offset(bmap, c[j]);
11537 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11538 isl_int_set(mat->row[i][pos],
11539 bmap->ineq[i][off + k]);
11540 ++pos;
11544 return mat;
11547 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11548 __isl_take isl_space *dim,
11549 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11550 enum isl_dim_type c2, enum isl_dim_type c3,
11551 enum isl_dim_type c4, enum isl_dim_type c5)
11553 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11554 isl_basic_map *bmap;
11555 unsigned total;
11556 unsigned extra;
11557 int i, j, k, l;
11558 int pos;
11560 if (!dim || !eq || !ineq)
11561 goto error;
11563 if (eq->n_col != ineq->n_col)
11564 isl_die(dim->ctx, isl_error_invalid,
11565 "equalities and inequalities matrices should have "
11566 "same number of columns", goto error);
11568 total = 1 + isl_space_dim(dim, isl_dim_all);
11570 if (eq->n_col < total)
11571 isl_die(dim->ctx, isl_error_invalid,
11572 "number of columns too small", goto error);
11574 extra = eq->n_col - total;
11576 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11577 eq->n_row, ineq->n_row);
11578 if (!bmap)
11579 goto error;
11580 for (i = 0; i < extra; ++i) {
11581 k = isl_basic_map_alloc_div(bmap);
11582 if (k < 0)
11583 goto error;
11584 isl_int_set_si(bmap->div[k][0], 0);
11586 for (i = 0; i < eq->n_row; ++i) {
11587 l = isl_basic_map_alloc_equality(bmap);
11588 if (l < 0)
11589 goto error;
11590 for (j = 0, pos = 0; j < 5; ++j) {
11591 int off = isl_basic_map_offset(bmap, c[j]);
11592 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11593 isl_int_set(bmap->eq[l][off + k],
11594 eq->row[i][pos]);
11595 ++pos;
11599 for (i = 0; i < ineq->n_row; ++i) {
11600 l = isl_basic_map_alloc_inequality(bmap);
11601 if (l < 0)
11602 goto error;
11603 for (j = 0, pos = 0; j < 5; ++j) {
11604 int off = isl_basic_map_offset(bmap, c[j]);
11605 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11606 isl_int_set(bmap->ineq[l][off + k],
11607 ineq->row[i][pos]);
11608 ++pos;
11613 isl_space_free(dim);
11614 isl_mat_free(eq);
11615 isl_mat_free(ineq);
11617 bmap = isl_basic_map_simplify(bmap);
11618 return isl_basic_map_finalize(bmap);
11619 error:
11620 isl_space_free(dim);
11621 isl_mat_free(eq);
11622 isl_mat_free(ineq);
11623 return NULL;
11626 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11627 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11628 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11630 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11631 c1, c2, c3, c4, isl_dim_in);
11634 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11635 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11636 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11638 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11639 c1, c2, c3, c4, isl_dim_in);
11642 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11643 __isl_take isl_space *dim,
11644 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11645 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11647 return (isl_basic_set*)
11648 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11649 c1, c2, c3, c4, isl_dim_in);
11652 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11654 if (!bmap)
11655 return isl_bool_error;
11657 return isl_space_can_zip(bmap->dim);
11660 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11662 if (!map)
11663 return isl_bool_error;
11665 return isl_space_can_zip(map->dim);
11668 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11669 * (A -> C) -> (B -> D).
11671 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11673 unsigned pos;
11674 unsigned n1;
11675 unsigned n2;
11677 if (!bmap)
11678 return NULL;
11680 if (!isl_basic_map_can_zip(bmap))
11681 isl_die(bmap->ctx, isl_error_invalid,
11682 "basic map cannot be zipped", goto error);
11683 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11684 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11685 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11686 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11687 bmap = isl_basic_map_cow(bmap);
11688 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11689 if (!bmap)
11690 return NULL;
11691 bmap->dim = isl_space_zip(bmap->dim);
11692 if (!bmap->dim)
11693 goto error;
11694 bmap = isl_basic_map_mark_final(bmap);
11695 return bmap;
11696 error:
11697 isl_basic_map_free(bmap);
11698 return NULL;
11701 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11702 * (A -> C) -> (B -> D).
11704 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11706 int i;
11708 if (!map)
11709 return NULL;
11711 if (!isl_map_can_zip(map))
11712 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11713 goto error);
11715 map = isl_map_cow(map);
11716 if (!map)
11717 return NULL;
11719 for (i = 0; i < map->n; ++i) {
11720 map->p[i] = isl_basic_map_zip(map->p[i]);
11721 if (!map->p[i])
11722 goto error;
11725 map->dim = isl_space_zip(map->dim);
11726 if (!map->dim)
11727 goto error;
11729 return map;
11730 error:
11731 isl_map_free(map);
11732 return NULL;
11735 /* Can we apply isl_basic_map_curry to "bmap"?
11736 * That is, does it have a nested relation in its domain?
11738 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11740 if (!bmap)
11741 return isl_bool_error;
11743 return isl_space_can_curry(bmap->dim);
11746 /* Can we apply isl_map_curry to "map"?
11747 * That is, does it have a nested relation in its domain?
11749 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11751 if (!map)
11752 return isl_bool_error;
11754 return isl_space_can_curry(map->dim);
11757 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11758 * A -> (B -> C).
11760 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11763 if (!bmap)
11764 return NULL;
11766 if (!isl_basic_map_can_curry(bmap))
11767 isl_die(bmap->ctx, isl_error_invalid,
11768 "basic map cannot be curried", goto error);
11769 bmap = isl_basic_map_cow(bmap);
11770 if (!bmap)
11771 return NULL;
11772 bmap->dim = isl_space_curry(bmap->dim);
11773 if (!bmap->dim)
11774 goto error;
11775 bmap = isl_basic_map_mark_final(bmap);
11776 return bmap;
11777 error:
11778 isl_basic_map_free(bmap);
11779 return NULL;
11782 /* Given a map (A -> B) -> C, return the corresponding map
11783 * A -> (B -> C).
11785 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11787 return isl_map_change_space(map, &isl_map_can_curry,
11788 "map cannot be curried", &isl_space_curry);
11791 /* Can isl_map_range_curry be applied to "map"?
11792 * That is, does it have a nested relation in its range,
11793 * the domain of which is itself a nested relation?
11795 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11797 if (!map)
11798 return isl_bool_error;
11800 return isl_space_can_range_curry(map->dim);
11803 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11804 * A -> (B -> (C -> D)).
11806 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11808 return isl_map_change_space(map, &isl_map_can_range_curry,
11809 "map range cannot be curried",
11810 &isl_space_range_curry);
11813 /* Can we apply isl_basic_map_uncurry to "bmap"?
11814 * That is, does it have a nested relation in its domain?
11816 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11818 if (!bmap)
11819 return isl_bool_error;
11821 return isl_space_can_uncurry(bmap->dim);
11824 /* Can we apply isl_map_uncurry to "map"?
11825 * That is, does it have a nested relation in its domain?
11827 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11829 if (!map)
11830 return isl_bool_error;
11832 return isl_space_can_uncurry(map->dim);
11835 /* Given a basic map A -> (B -> C), return the corresponding basic map
11836 * (A -> B) -> C.
11838 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11841 if (!bmap)
11842 return NULL;
11844 if (!isl_basic_map_can_uncurry(bmap))
11845 isl_die(bmap->ctx, isl_error_invalid,
11846 "basic map cannot be uncurried",
11847 return isl_basic_map_free(bmap));
11848 bmap = isl_basic_map_cow(bmap);
11849 if (!bmap)
11850 return NULL;
11851 bmap->dim = isl_space_uncurry(bmap->dim);
11852 if (!bmap->dim)
11853 return isl_basic_map_free(bmap);
11854 bmap = isl_basic_map_mark_final(bmap);
11855 return bmap;
11858 /* Given a map A -> (B -> C), return the corresponding map
11859 * (A -> B) -> C.
11861 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11863 return isl_map_change_space(map, &isl_map_can_uncurry,
11864 "map cannot be uncurried", &isl_space_uncurry);
11867 /* Construct a basic map mapping the domain of the affine expression
11868 * to a one-dimensional range prescribed by the affine expression.
11870 * A NaN affine expression cannot be converted to a basic map.
11872 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11874 int k;
11875 int pos;
11876 isl_bool is_nan;
11877 isl_local_space *ls;
11878 isl_basic_map *bmap = NULL;
11880 if (!aff)
11881 return NULL;
11882 is_nan = isl_aff_is_nan(aff);
11883 if (is_nan < 0)
11884 goto error;
11885 if (is_nan)
11886 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11887 "cannot convert NaN", goto error);
11889 ls = isl_aff_get_local_space(aff);
11890 bmap = isl_basic_map_from_local_space(ls);
11891 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11892 k = isl_basic_map_alloc_equality(bmap);
11893 if (k < 0)
11894 goto error;
11896 pos = isl_basic_map_offset(bmap, isl_dim_out);
11897 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11898 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11899 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11900 aff->v->size - (pos + 1));
11902 isl_aff_free(aff);
11903 bmap = isl_basic_map_finalize(bmap);
11904 return bmap;
11905 error:
11906 isl_aff_free(aff);
11907 isl_basic_map_free(bmap);
11908 return NULL;
11911 /* Construct a map mapping the domain of the affine expression
11912 * to a one-dimensional range prescribed by the affine expression.
11914 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11916 isl_basic_map *bmap;
11918 bmap = isl_basic_map_from_aff(aff);
11919 return isl_map_from_basic_map(bmap);
11922 /* Construct a basic map mapping the domain the multi-affine expression
11923 * to its range, with each dimension in the range equated to the
11924 * corresponding affine expression.
11926 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11927 __isl_take isl_multi_aff *maff)
11929 int i;
11930 isl_space *space;
11931 isl_basic_map *bmap;
11933 if (!maff)
11934 return NULL;
11936 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11937 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11938 "invalid space", goto error);
11940 space = isl_space_domain(isl_multi_aff_get_space(maff));
11941 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11943 for (i = 0; i < maff->n; ++i) {
11944 isl_aff *aff;
11945 isl_basic_map *bmap_i;
11947 aff = isl_aff_copy(maff->p[i]);
11948 bmap_i = isl_basic_map_from_aff(aff);
11950 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11953 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11955 isl_multi_aff_free(maff);
11956 return bmap;
11957 error:
11958 isl_multi_aff_free(maff);
11959 return NULL;
11962 /* Construct a map mapping the domain the multi-affine expression
11963 * to its range, with each dimension in the range equated to the
11964 * corresponding affine expression.
11966 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11968 isl_basic_map *bmap;
11970 bmap = isl_basic_map_from_multi_aff(maff);
11971 return isl_map_from_basic_map(bmap);
11974 /* Construct a basic map mapping a domain in the given space to
11975 * to an n-dimensional range, with n the number of elements in the list,
11976 * where each coordinate in the range is prescribed by the
11977 * corresponding affine expression.
11978 * The domains of all affine expressions in the list are assumed to match
11979 * domain_dim.
11981 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11982 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11984 int i;
11985 isl_space *dim;
11986 isl_basic_map *bmap;
11988 if (!list)
11989 return NULL;
11991 dim = isl_space_from_domain(domain_dim);
11992 bmap = isl_basic_map_universe(dim);
11994 for (i = 0; i < list->n; ++i) {
11995 isl_aff *aff;
11996 isl_basic_map *bmap_i;
11998 aff = isl_aff_copy(list->p[i]);
11999 bmap_i = isl_basic_map_from_aff(aff);
12001 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
12004 isl_aff_list_free(list);
12005 return bmap;
12008 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
12009 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12011 return isl_map_equate(set, type1, pos1, type2, pos2);
12014 /* Construct a basic map where the given dimensions are equal to each other.
12016 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
12017 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12019 isl_basic_map *bmap = NULL;
12020 int i;
12022 if (!space)
12023 return NULL;
12025 if (pos1 >= isl_space_dim(space, type1))
12026 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12027 "index out of bounds", goto error);
12028 if (pos2 >= isl_space_dim(space, type2))
12029 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12030 "index out of bounds", goto error);
12032 if (type1 == type2 && pos1 == pos2)
12033 return isl_basic_map_universe(space);
12035 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12036 i = isl_basic_map_alloc_equality(bmap);
12037 if (i < 0)
12038 goto error;
12039 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12040 pos1 += isl_basic_map_offset(bmap, type1);
12041 pos2 += isl_basic_map_offset(bmap, type2);
12042 isl_int_set_si(bmap->eq[i][pos1], -1);
12043 isl_int_set_si(bmap->eq[i][pos2], 1);
12044 bmap = isl_basic_map_finalize(bmap);
12045 isl_space_free(space);
12046 return bmap;
12047 error:
12048 isl_space_free(space);
12049 isl_basic_map_free(bmap);
12050 return NULL;
12053 /* Add a constraint imposing that the given two dimensions are equal.
12055 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12056 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12058 isl_basic_map *eq;
12060 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12062 bmap = isl_basic_map_intersect(bmap, eq);
12064 return bmap;
12067 /* Add a constraint imposing that the given two dimensions are equal.
12069 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12070 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12072 isl_basic_map *bmap;
12074 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12076 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12078 return map;
12081 /* Add a constraint imposing that the given two dimensions have opposite values.
12083 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12084 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12086 isl_basic_map *bmap = NULL;
12087 int i;
12089 if (!map)
12090 return NULL;
12092 if (pos1 >= isl_map_dim(map, type1))
12093 isl_die(map->ctx, isl_error_invalid,
12094 "index out of bounds", goto error);
12095 if (pos2 >= isl_map_dim(map, type2))
12096 isl_die(map->ctx, isl_error_invalid,
12097 "index out of bounds", goto error);
12099 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12100 i = isl_basic_map_alloc_equality(bmap);
12101 if (i < 0)
12102 goto error;
12103 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12104 pos1 += isl_basic_map_offset(bmap, type1);
12105 pos2 += isl_basic_map_offset(bmap, type2);
12106 isl_int_set_si(bmap->eq[i][pos1], 1);
12107 isl_int_set_si(bmap->eq[i][pos2], 1);
12108 bmap = isl_basic_map_finalize(bmap);
12110 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12112 return map;
12113 error:
12114 isl_basic_map_free(bmap);
12115 isl_map_free(map);
12116 return NULL;
12119 /* Construct a constraint imposing that the value of the first dimension is
12120 * greater than or equal to that of the second.
12122 static __isl_give isl_constraint *constraint_order_ge(
12123 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12124 enum isl_dim_type type2, int pos2)
12126 isl_constraint *c;
12128 if (!space)
12129 return NULL;
12131 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12133 if (pos1 >= isl_constraint_dim(c, type1))
12134 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12135 "index out of bounds", return isl_constraint_free(c));
12136 if (pos2 >= isl_constraint_dim(c, type2))
12137 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12138 "index out of bounds", return isl_constraint_free(c));
12140 if (type1 == type2 && pos1 == pos2)
12141 return c;
12143 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12144 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12146 return c;
12149 /* Add a constraint imposing that the value of the first dimension is
12150 * greater than or equal to that of the second.
12152 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12153 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12155 isl_constraint *c;
12156 isl_space *space;
12158 if (type1 == type2 && pos1 == pos2)
12159 return bmap;
12160 space = isl_basic_map_get_space(bmap);
12161 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12162 bmap = isl_basic_map_add_constraint(bmap, c);
12164 return bmap;
12167 /* Add a constraint imposing that the value of the first dimension is
12168 * greater than or equal to that of the second.
12170 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12171 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12173 isl_constraint *c;
12174 isl_space *space;
12176 if (type1 == type2 && pos1 == pos2)
12177 return map;
12178 space = isl_map_get_space(map);
12179 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12180 map = isl_map_add_constraint(map, c);
12182 return map;
12185 /* Add a constraint imposing that the value of the first dimension is
12186 * less than or equal to that of the second.
12188 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12189 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12191 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12194 /* Construct a basic map where the value of the first dimension is
12195 * greater than that of the second.
12197 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12198 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12200 isl_basic_map *bmap = NULL;
12201 int i;
12203 if (!space)
12204 return NULL;
12206 if (pos1 >= isl_space_dim(space, type1))
12207 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12208 "index out of bounds", goto error);
12209 if (pos2 >= isl_space_dim(space, type2))
12210 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12211 "index out of bounds", goto error);
12213 if (type1 == type2 && pos1 == pos2)
12214 return isl_basic_map_empty(space);
12216 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12217 i = isl_basic_map_alloc_inequality(bmap);
12218 if (i < 0)
12219 return isl_basic_map_free(bmap);
12220 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12221 pos1 += isl_basic_map_offset(bmap, type1);
12222 pos2 += isl_basic_map_offset(bmap, type2);
12223 isl_int_set_si(bmap->ineq[i][pos1], 1);
12224 isl_int_set_si(bmap->ineq[i][pos2], -1);
12225 isl_int_set_si(bmap->ineq[i][0], -1);
12226 bmap = isl_basic_map_finalize(bmap);
12228 return bmap;
12229 error:
12230 isl_space_free(space);
12231 isl_basic_map_free(bmap);
12232 return NULL;
12235 /* Add a constraint imposing that the value of the first dimension is
12236 * greater than that of the second.
12238 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12239 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12241 isl_basic_map *gt;
12243 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12245 bmap = isl_basic_map_intersect(bmap, gt);
12247 return bmap;
12250 /* Add a constraint imposing that the value of the first dimension is
12251 * greater than that of the second.
12253 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12254 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12256 isl_basic_map *bmap;
12258 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12260 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12262 return map;
12265 /* Add a constraint imposing that the value of the first dimension is
12266 * smaller than that of the second.
12268 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12269 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12271 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12274 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12275 int pos)
12277 isl_aff *div;
12278 isl_local_space *ls;
12280 if (!bmap)
12281 return NULL;
12283 if (!isl_basic_map_divs_known(bmap))
12284 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12285 "some divs are unknown", return NULL);
12287 ls = isl_basic_map_get_local_space(bmap);
12288 div = isl_local_space_get_div(ls, pos);
12289 isl_local_space_free(ls);
12291 return div;
12294 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12295 int pos)
12297 return isl_basic_map_get_div(bset, pos);
12300 /* Plug in "subs" for dimension "type", "pos" of "bset".
12302 * Let i be the dimension to replace and let "subs" be of the form
12304 * f/d
12306 * Any integer division with a non-zero coefficient for i,
12308 * floor((a i + g)/m)
12310 * is replaced by
12312 * floor((a f + d g)/(m d))
12314 * Constraints of the form
12316 * a i + g
12318 * are replaced by
12320 * a f + d g
12322 * We currently require that "subs" is an integral expression.
12323 * Handling rational expressions may require us to add stride constraints
12324 * as we do in isl_basic_set_preimage_multi_aff.
12326 __isl_give isl_basic_set *isl_basic_set_substitute(
12327 __isl_take isl_basic_set *bset,
12328 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12330 int i;
12331 isl_int v;
12332 isl_ctx *ctx;
12334 if (bset && isl_basic_set_plain_is_empty(bset))
12335 return bset;
12337 bset = isl_basic_set_cow(bset);
12338 if (!bset || !subs)
12339 goto error;
12341 ctx = isl_basic_set_get_ctx(bset);
12342 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12343 isl_die(ctx, isl_error_invalid,
12344 "spaces don't match", goto error);
12345 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12346 isl_die(ctx, isl_error_unsupported,
12347 "cannot handle divs yet", goto error);
12348 if (!isl_int_is_one(subs->v->el[0]))
12349 isl_die(ctx, isl_error_invalid,
12350 "can only substitute integer expressions", goto error);
12352 pos += isl_basic_set_offset(bset, type);
12354 isl_int_init(v);
12356 for (i = 0; i < bset->n_eq; ++i) {
12357 if (isl_int_is_zero(bset->eq[i][pos]))
12358 continue;
12359 isl_int_set(v, bset->eq[i][pos]);
12360 isl_int_set_si(bset->eq[i][pos], 0);
12361 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12362 v, subs->v->el + 1, subs->v->size - 1);
12365 for (i = 0; i < bset->n_ineq; ++i) {
12366 if (isl_int_is_zero(bset->ineq[i][pos]))
12367 continue;
12368 isl_int_set(v, bset->ineq[i][pos]);
12369 isl_int_set_si(bset->ineq[i][pos], 0);
12370 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12371 v, subs->v->el + 1, subs->v->size - 1);
12374 for (i = 0; i < bset->n_div; ++i) {
12375 if (isl_int_is_zero(bset->div[i][1 + pos]))
12376 continue;
12377 isl_int_set(v, bset->div[i][1 + pos]);
12378 isl_int_set_si(bset->div[i][1 + pos], 0);
12379 isl_seq_combine(bset->div[i] + 1,
12380 subs->v->el[0], bset->div[i] + 1,
12381 v, subs->v->el + 1, subs->v->size - 1);
12382 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12385 isl_int_clear(v);
12387 bset = isl_basic_set_simplify(bset);
12388 return isl_basic_set_finalize(bset);
12389 error:
12390 isl_basic_set_free(bset);
12391 return NULL;
12394 /* Plug in "subs" for dimension "type", "pos" of "set".
12396 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12397 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12399 int i;
12401 if (set && isl_set_plain_is_empty(set))
12402 return set;
12404 set = isl_set_cow(set);
12405 if (!set || !subs)
12406 goto error;
12408 for (i = set->n - 1; i >= 0; --i) {
12409 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12410 if (remove_if_empty(set, i) < 0)
12411 goto error;
12414 return set;
12415 error:
12416 isl_set_free(set);
12417 return NULL;
12420 /* Check if the range of "ma" is compatible with the domain or range
12421 * (depending on "type") of "bmap".
12422 * Return -1 if anything is wrong.
12424 static int check_basic_map_compatible_range_multi_aff(
12425 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12426 __isl_keep isl_multi_aff *ma)
12428 int m;
12429 isl_space *ma_space;
12431 ma_space = isl_multi_aff_get_space(ma);
12433 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12434 if (m < 0)
12435 goto error;
12436 if (!m)
12437 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12438 "parameters don't match", goto error);
12439 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12440 if (m < 0)
12441 goto error;
12442 if (!m)
12443 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12444 "spaces don't match", goto error);
12446 isl_space_free(ma_space);
12447 return m;
12448 error:
12449 isl_space_free(ma_space);
12450 return -1;
12453 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12454 * coefficients before the transformed range of dimensions,
12455 * the "n_after" coefficients after the transformed range of dimensions
12456 * and the coefficients of the other divs in "bmap".
12458 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12459 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12461 int i;
12462 int n_param;
12463 int n_set;
12464 isl_local_space *ls;
12466 if (n_div == 0)
12467 return 0;
12469 ls = isl_aff_get_domain_local_space(ma->p[0]);
12470 if (!ls)
12471 return -1;
12473 n_param = isl_local_space_dim(ls, isl_dim_param);
12474 n_set = isl_local_space_dim(ls, isl_dim_set);
12475 for (i = 0; i < n_div; ++i) {
12476 int o_bmap = 0, o_ls = 0;
12478 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12479 o_bmap += 1 + 1 + n_param;
12480 o_ls += 1 + 1 + n_param;
12481 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12482 o_bmap += n_before;
12483 isl_seq_cpy(bmap->div[i] + o_bmap,
12484 ls->div->row[i] + o_ls, n_set);
12485 o_bmap += n_set;
12486 o_ls += n_set;
12487 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12488 o_bmap += n_after;
12489 isl_seq_cpy(bmap->div[i] + o_bmap,
12490 ls->div->row[i] + o_ls, n_div);
12491 o_bmap += n_div;
12492 o_ls += n_div;
12493 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12494 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12495 goto error;
12498 isl_local_space_free(ls);
12499 return 0;
12500 error:
12501 isl_local_space_free(ls);
12502 return -1;
12505 /* How many stride constraints does "ma" enforce?
12506 * That is, how many of the affine expressions have a denominator
12507 * different from one?
12509 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12511 int i;
12512 int strides = 0;
12514 for (i = 0; i < ma->n; ++i)
12515 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12516 strides++;
12518 return strides;
12521 /* For each affine expression in ma of the form
12523 * x_i = (f_i y + h_i)/m_i
12525 * with m_i different from one, add a constraint to "bmap"
12526 * of the form
12528 * f_i y + h_i = m_i alpha_i
12530 * with alpha_i an additional existentially quantified variable.
12532 * The input variables of "ma" correspond to a subset of the variables
12533 * of "bmap". There are "n_before" variables in "bmap" before this
12534 * subset and "n_after" variables after this subset.
12535 * The integer divisions of the affine expressions in "ma" are assumed
12536 * to have been aligned. There are "n_div_ma" of them and
12537 * they appear first in "bmap", straight after the "n_after" variables.
12539 static __isl_give isl_basic_map *add_ma_strides(
12540 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12541 int n_before, int n_after, int n_div_ma)
12543 int i, k;
12544 int div;
12545 int total;
12546 int n_param;
12547 int n_in;
12549 total = isl_basic_map_total_dim(bmap);
12550 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12551 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12552 for (i = 0; i < ma->n; ++i) {
12553 int o_bmap = 0, o_ma = 1;
12555 if (isl_int_is_one(ma->p[i]->v->el[0]))
12556 continue;
12557 div = isl_basic_map_alloc_div(bmap);
12558 k = isl_basic_map_alloc_equality(bmap);
12559 if (div < 0 || k < 0)
12560 goto error;
12561 isl_int_set_si(bmap->div[div][0], 0);
12562 isl_seq_cpy(bmap->eq[k] + o_bmap,
12563 ma->p[i]->v->el + o_ma, 1 + n_param);
12564 o_bmap += 1 + n_param;
12565 o_ma += 1 + n_param;
12566 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12567 o_bmap += n_before;
12568 isl_seq_cpy(bmap->eq[k] + o_bmap,
12569 ma->p[i]->v->el + o_ma, n_in);
12570 o_bmap += n_in;
12571 o_ma += n_in;
12572 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12573 o_bmap += n_after;
12574 isl_seq_cpy(bmap->eq[k] + o_bmap,
12575 ma->p[i]->v->el + o_ma, n_div_ma);
12576 o_bmap += n_div_ma;
12577 o_ma += n_div_ma;
12578 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12579 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12580 total++;
12583 return bmap;
12584 error:
12585 isl_basic_map_free(bmap);
12586 return NULL;
12589 /* Replace the domain or range space (depending on "type) of "space" by "set".
12591 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12592 enum isl_dim_type type, __isl_take isl_space *set)
12594 if (type == isl_dim_in) {
12595 space = isl_space_range(space);
12596 space = isl_space_map_from_domain_and_range(set, space);
12597 } else {
12598 space = isl_space_domain(space);
12599 space = isl_space_map_from_domain_and_range(space, set);
12602 return space;
12605 /* Compute the preimage of the domain or range (depending on "type")
12606 * of "bmap" under the function represented by "ma".
12607 * In other words, plug in "ma" in the domain or range of "bmap".
12608 * The result is a basic map that lives in the same space as "bmap"
12609 * except that the domain or range has been replaced by
12610 * the domain space of "ma".
12612 * If bmap is represented by
12614 * A(p) + S u + B x + T v + C(divs) >= 0,
12616 * where u and x are input and output dimensions if type == isl_dim_out
12617 * while x and v are input and output dimensions if type == isl_dim_in,
12618 * and ma is represented by
12620 * x = D(p) + F(y) + G(divs')
12622 * then the result is
12624 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12626 * The divs in the input set are similarly adjusted.
12627 * In particular
12629 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12631 * becomes
12633 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12634 * B_i G(divs') + c_i(divs))/n_i)
12636 * If bmap is not a rational map and if F(y) involves any denominators
12638 * x_i = (f_i y + h_i)/m_i
12640 * then additional constraints are added to ensure that we only
12641 * map back integer points. That is we enforce
12643 * f_i y + h_i = m_i alpha_i
12645 * with alpha_i an additional existentially quantified variable.
12647 * We first copy over the divs from "ma".
12648 * Then we add the modified constraints and divs from "bmap".
12649 * Finally, we add the stride constraints, if needed.
12651 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12652 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12653 __isl_take isl_multi_aff *ma)
12655 int i, k;
12656 isl_space *space;
12657 isl_basic_map *res = NULL;
12658 int n_before, n_after, n_div_bmap, n_div_ma;
12659 isl_int f, c1, c2, g;
12660 int rational, strides;
12662 isl_int_init(f);
12663 isl_int_init(c1);
12664 isl_int_init(c2);
12665 isl_int_init(g);
12667 ma = isl_multi_aff_align_divs(ma);
12668 if (!bmap || !ma)
12669 goto error;
12670 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12671 goto error;
12673 if (type == isl_dim_in) {
12674 n_before = 0;
12675 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12676 } else {
12677 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12678 n_after = 0;
12680 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12681 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12683 space = isl_multi_aff_get_domain_space(ma);
12684 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12685 rational = isl_basic_map_is_rational(bmap);
12686 strides = rational ? 0 : multi_aff_strides(ma);
12687 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12688 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12689 if (rational)
12690 res = isl_basic_map_set_rational(res);
12692 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12693 if (isl_basic_map_alloc_div(res) < 0)
12694 goto error;
12696 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12697 goto error;
12699 for (i = 0; i < bmap->n_eq; ++i) {
12700 k = isl_basic_map_alloc_equality(res);
12701 if (k < 0)
12702 goto error;
12703 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12704 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12707 for (i = 0; i < bmap->n_ineq; ++i) {
12708 k = isl_basic_map_alloc_inequality(res);
12709 if (k < 0)
12710 goto error;
12711 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12712 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12715 for (i = 0; i < bmap->n_div; ++i) {
12716 if (isl_int_is_zero(bmap->div[i][0])) {
12717 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12718 continue;
12720 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12721 n_before, n_after, n_div_ma, n_div_bmap,
12722 f, c1, c2, g, 1);
12725 if (strides)
12726 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12728 isl_int_clear(f);
12729 isl_int_clear(c1);
12730 isl_int_clear(c2);
12731 isl_int_clear(g);
12732 isl_basic_map_free(bmap);
12733 isl_multi_aff_free(ma);
12734 res = isl_basic_set_simplify(res);
12735 return isl_basic_map_finalize(res);
12736 error:
12737 isl_int_clear(f);
12738 isl_int_clear(c1);
12739 isl_int_clear(c2);
12740 isl_int_clear(g);
12741 isl_basic_map_free(bmap);
12742 isl_multi_aff_free(ma);
12743 isl_basic_map_free(res);
12744 return NULL;
12747 /* Compute the preimage of "bset" under the function represented by "ma".
12748 * In other words, plug in "ma" in "bset". The result is a basic set
12749 * that lives in the domain space of "ma".
12751 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12752 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12754 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12757 /* Compute the preimage of the domain of "bmap" under the function
12758 * represented by "ma".
12759 * In other words, plug in "ma" in the domain of "bmap".
12760 * The result is a basic map that lives in the same space as "bmap"
12761 * except that the domain has been replaced by the domain space of "ma".
12763 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12764 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12766 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12769 /* Compute the preimage of the range of "bmap" under the function
12770 * represented by "ma".
12771 * In other words, plug in "ma" in the range of "bmap".
12772 * The result is a basic map that lives in the same space as "bmap"
12773 * except that the range has been replaced by the domain space of "ma".
12775 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12776 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12778 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12781 /* Check if the range of "ma" is compatible with the domain or range
12782 * (depending on "type") of "map".
12783 * Return -1 if anything is wrong.
12785 static int check_map_compatible_range_multi_aff(
12786 __isl_keep isl_map *map, enum isl_dim_type type,
12787 __isl_keep isl_multi_aff *ma)
12789 int m;
12790 isl_space *ma_space;
12792 ma_space = isl_multi_aff_get_space(ma);
12793 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12794 isl_space_free(ma_space);
12795 if (m >= 0 && !m)
12796 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12797 "spaces don't match", return -1);
12798 return m;
12801 /* Compute the preimage of the domain or range (depending on "type")
12802 * of "map" under the function represented by "ma".
12803 * In other words, plug in "ma" in the domain or range of "map".
12804 * The result is a map that lives in the same space as "map"
12805 * except that the domain or range has been replaced by
12806 * the domain space of "ma".
12808 * The parameters are assumed to have been aligned.
12810 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12811 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12813 int i;
12814 isl_space *space;
12816 map = isl_map_cow(map);
12817 ma = isl_multi_aff_align_divs(ma);
12818 if (!map || !ma)
12819 goto error;
12820 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12821 goto error;
12823 for (i = 0; i < map->n; ++i) {
12824 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12825 isl_multi_aff_copy(ma));
12826 if (!map->p[i])
12827 goto error;
12830 space = isl_multi_aff_get_domain_space(ma);
12831 space = isl_space_set(isl_map_get_space(map), type, space);
12833 isl_space_free(map->dim);
12834 map->dim = space;
12835 if (!map->dim)
12836 goto error;
12838 isl_multi_aff_free(ma);
12839 if (map->n > 1)
12840 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12841 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12842 return map;
12843 error:
12844 isl_multi_aff_free(ma);
12845 isl_map_free(map);
12846 return NULL;
12849 /* Compute the preimage of the domain or range (depending on "type")
12850 * of "map" under the function represented by "ma".
12851 * In other words, plug in "ma" in the domain or range of "map".
12852 * The result is a map that lives in the same space as "map"
12853 * except that the domain or range has been replaced by
12854 * the domain space of "ma".
12856 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12857 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12859 if (!map || !ma)
12860 goto error;
12862 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12863 return map_preimage_multi_aff(map, type, ma);
12865 if (!isl_space_has_named_params(map->dim) ||
12866 !isl_space_has_named_params(ma->space))
12867 isl_die(map->ctx, isl_error_invalid,
12868 "unaligned unnamed parameters", goto error);
12869 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12870 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12872 return map_preimage_multi_aff(map, type, ma);
12873 error:
12874 isl_multi_aff_free(ma);
12875 return isl_map_free(map);
12878 /* Compute the preimage of "set" under the function represented by "ma".
12879 * In other words, plug in "ma" in "set". The result is a set
12880 * that lives in the domain space of "ma".
12882 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12883 __isl_take isl_multi_aff *ma)
12885 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12888 /* Compute the preimage of the domain of "map" under the function
12889 * represented by "ma".
12890 * In other words, plug in "ma" in the domain of "map".
12891 * The result is a map that lives in the same space as "map"
12892 * except that the domain has been replaced by the domain space of "ma".
12894 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12895 __isl_take isl_multi_aff *ma)
12897 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12900 /* Compute the preimage of the range of "map" under the function
12901 * represented by "ma".
12902 * In other words, plug in "ma" in the range of "map".
12903 * The result is a map that lives in the same space as "map"
12904 * except that the range has been replaced by the domain space of "ma".
12906 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12907 __isl_take isl_multi_aff *ma)
12909 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12912 /* Compute the preimage of "map" under the function represented by "pma".
12913 * In other words, plug in "pma" in the domain or range of "map".
12914 * The result is a map that lives in the same space as "map",
12915 * except that the space of type "type" has been replaced by
12916 * the domain space of "pma".
12918 * The parameters of "map" and "pma" are assumed to have been aligned.
12920 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12921 __isl_take isl_map *map, enum isl_dim_type type,
12922 __isl_take isl_pw_multi_aff *pma)
12924 int i;
12925 isl_map *res;
12927 if (!pma)
12928 goto error;
12930 if (pma->n == 0) {
12931 isl_pw_multi_aff_free(pma);
12932 res = isl_map_empty(isl_map_get_space(map));
12933 isl_map_free(map);
12934 return res;
12937 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12938 isl_multi_aff_copy(pma->p[0].maff));
12939 if (type == isl_dim_in)
12940 res = isl_map_intersect_domain(res,
12941 isl_map_copy(pma->p[0].set));
12942 else
12943 res = isl_map_intersect_range(res,
12944 isl_map_copy(pma->p[0].set));
12946 for (i = 1; i < pma->n; ++i) {
12947 isl_map *res_i;
12949 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12950 isl_multi_aff_copy(pma->p[i].maff));
12951 if (type == isl_dim_in)
12952 res_i = isl_map_intersect_domain(res_i,
12953 isl_map_copy(pma->p[i].set));
12954 else
12955 res_i = isl_map_intersect_range(res_i,
12956 isl_map_copy(pma->p[i].set));
12957 res = isl_map_union(res, res_i);
12960 isl_pw_multi_aff_free(pma);
12961 isl_map_free(map);
12962 return res;
12963 error:
12964 isl_pw_multi_aff_free(pma);
12965 isl_map_free(map);
12966 return NULL;
12969 /* Compute the preimage of "map" under the function represented by "pma".
12970 * In other words, plug in "pma" in the domain or range of "map".
12971 * The result is a map that lives in the same space as "map",
12972 * except that the space of type "type" has been replaced by
12973 * the domain space of "pma".
12975 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12976 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12978 if (!map || !pma)
12979 goto error;
12981 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12982 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12984 if (!isl_space_has_named_params(map->dim) ||
12985 !isl_space_has_named_params(pma->dim))
12986 isl_die(map->ctx, isl_error_invalid,
12987 "unaligned unnamed parameters", goto error);
12988 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12989 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12991 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12992 error:
12993 isl_pw_multi_aff_free(pma);
12994 return isl_map_free(map);
12997 /* Compute the preimage of "set" under the function represented by "pma".
12998 * In other words, plug in "pma" in "set". The result is a set
12999 * that lives in the domain space of "pma".
13001 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
13002 __isl_take isl_pw_multi_aff *pma)
13004 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
13007 /* Compute the preimage of the domain of "map" under the function
13008 * represented by "pma".
13009 * In other words, plug in "pma" in the domain of "map".
13010 * The result is a map that lives in the same space as "map",
13011 * except that domain space has been replaced by the domain space of "pma".
13013 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
13014 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13016 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
13019 /* Compute the preimage of the range of "map" under the function
13020 * represented by "pma".
13021 * In other words, plug in "pma" in the range of "map".
13022 * The result is a map that lives in the same space as "map",
13023 * except that range space has been replaced by the domain space of "pma".
13025 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
13026 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
13028 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13031 /* Compute the preimage of "map" under the function represented by "mpa".
13032 * In other words, plug in "mpa" in the domain or range of "map".
13033 * The result is a map that lives in the same space as "map",
13034 * except that the space of type "type" has been replaced by
13035 * the domain space of "mpa".
13037 * If the map does not involve any constraints that refer to the
13038 * dimensions of the substituted space, then the only possible
13039 * effect of "mpa" on the map is to map the space to a different space.
13040 * We create a separate isl_multi_aff to effectuate this change
13041 * in order to avoid spurious splitting of the map along the pieces
13042 * of "mpa".
13044 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13045 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13047 int n;
13048 isl_pw_multi_aff *pma;
13050 if (!map || !mpa)
13051 goto error;
13053 n = isl_map_dim(map, type);
13054 if (!isl_map_involves_dims(map, type, 0, n)) {
13055 isl_space *space;
13056 isl_multi_aff *ma;
13058 space = isl_multi_pw_aff_get_space(mpa);
13059 isl_multi_pw_aff_free(mpa);
13060 ma = isl_multi_aff_zero(space);
13061 return isl_map_preimage_multi_aff(map, type, ma);
13064 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13065 return isl_map_preimage_pw_multi_aff(map, type, pma);
13066 error:
13067 isl_map_free(map);
13068 isl_multi_pw_aff_free(mpa);
13069 return NULL;
13072 /* Compute the preimage of "map" under the function represented by "mpa".
13073 * In other words, plug in "mpa" in the domain "map".
13074 * The result is a map that lives in the same space as "map",
13075 * except that domain space has been replaced by the domain space of "mpa".
13077 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13078 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13080 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13083 /* Compute the preimage of "set" by the function represented by "mpa".
13084 * In other words, plug in "mpa" in "set".
13086 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13087 __isl_take isl_multi_pw_aff *mpa)
13089 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);