isl_tab_pip.c: isl_context_op.add_div: generalize to insert_div
[isl.git] / isl_map.c
blob31c751a3e9efb4e47509e9f782b7b261379d0d64
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, K.U.Leuven, Departement
11 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
12 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
13 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
19 #include <string.h>
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_blk.h>
23 #include <isl/constraint.h>
24 #include "isl_space_private.h"
25 #include "isl_equalities.h"
26 #include <isl_lp_private.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl/map.h>
30 #include <isl_reordering.h>
31 #include "isl_sample.h"
32 #include <isl_sort.h>
33 #include "isl_tab.h"
34 #include <isl/vec.h>
35 #include <isl_mat_private.h>
36 #include <isl_vec_private.h>
37 #include <isl_dim_map.h>
38 #include <isl_local_space_private.h>
39 #include <isl_aff_private.h>
40 #include <isl_options_private.h>
41 #include <isl_morph.h>
42 #include <isl_val_private.h>
43 #include <isl/deprecated/map_int.h>
44 #include <isl/deprecated/set_int.h>
46 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
48 switch (type) {
49 case isl_dim_param: return dim->nparam;
50 case isl_dim_in: return dim->n_in;
51 case isl_dim_out: return dim->n_out;
52 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
53 default: return 0;
57 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
59 switch (type) {
60 case isl_dim_param: return 1;
61 case isl_dim_in: return 1 + dim->nparam;
62 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
63 default: return 0;
67 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
68 enum isl_dim_type type)
70 if (!bmap)
71 return 0;
72 switch (type) {
73 case isl_dim_cst: return 1;
74 case isl_dim_param:
75 case isl_dim_in:
76 case isl_dim_out: return isl_space_dim(bmap->dim, type);
77 case isl_dim_div: return bmap->n_div;
78 case isl_dim_all: return isl_basic_map_total_dim(bmap);
79 default: return 0;
83 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
85 return map ? n(map->dim, type) : 0;
88 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
90 return set ? n(set->dim, type) : 0;
93 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
94 enum isl_dim_type type)
96 isl_space *dim = bmap->dim;
97 switch (type) {
98 case isl_dim_cst: return 0;
99 case isl_dim_param: return 1;
100 case isl_dim_in: return 1 + dim->nparam;
101 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
102 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
103 default: return 0;
107 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
108 enum isl_dim_type type)
110 return isl_basic_map_offset(bset, type);
113 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
115 return pos(map->dim, type);
118 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
119 enum isl_dim_type type)
121 return isl_basic_map_dim(bset, type);
124 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
126 return isl_basic_set_dim(bset, isl_dim_set);
129 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
131 return isl_basic_set_dim(bset, isl_dim_param);
134 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
136 if (!bset)
137 return 0;
138 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
141 unsigned isl_set_n_dim(__isl_keep isl_set *set)
143 return isl_set_dim(set, isl_dim_set);
146 unsigned isl_set_n_param(__isl_keep isl_set *set)
148 return isl_set_dim(set, isl_dim_param);
151 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
153 return bmap ? bmap->dim->n_in : 0;
156 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
158 return bmap ? bmap->dim->n_out : 0;
161 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
163 return bmap ? bmap->dim->nparam : 0;
166 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
168 return bmap ? bmap->n_div : 0;
171 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
173 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
176 unsigned isl_map_n_in(const struct isl_map *map)
178 return map ? map->dim->n_in : 0;
181 unsigned isl_map_n_out(const struct isl_map *map)
183 return map ? map->dim->n_out : 0;
186 unsigned isl_map_n_param(const struct isl_map *map)
188 return map ? map->dim->nparam : 0;
191 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
193 int m;
194 if (!map || !set)
195 return -1;
196 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
197 if (m < 0 || !m)
198 return m;
199 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
200 set->dim, isl_dim_set);
203 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
204 __isl_keep isl_basic_set *bset)
206 isl_bool m;
207 if (!bmap || !bset)
208 return isl_bool_error;
209 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
210 if (m < 0 || !m)
211 return m;
212 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
213 bset->dim, isl_dim_set);
216 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
218 int m;
219 if (!map || !set)
220 return -1;
221 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
222 if (m < 0 || !m)
223 return m;
224 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
225 set->dim, isl_dim_set);
228 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
229 struct isl_basic_set *bset)
231 int m;
232 if (!bmap || !bset)
233 return -1;
234 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
235 if (m < 0 || !m)
236 return m;
237 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
238 bset->dim, isl_dim_set);
241 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
243 return bmap ? bmap->ctx : NULL;
246 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
248 return bset ? bset->ctx : NULL;
251 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
253 return map ? map->ctx : NULL;
256 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
258 return set ? set->ctx : NULL;
261 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
263 if (!bmap)
264 return NULL;
265 return isl_space_copy(bmap->dim);
268 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
270 if (!bset)
271 return NULL;
272 return isl_space_copy(bset->dim);
275 /* Extract the divs in "bmap" as a matrix.
277 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
279 int i;
280 isl_ctx *ctx;
281 isl_mat *div;
282 unsigned total;
283 unsigned cols;
285 if (!bmap)
286 return NULL;
288 ctx = isl_basic_map_get_ctx(bmap);
289 total = isl_space_dim(bmap->dim, isl_dim_all);
290 cols = 1 + 1 + total + bmap->n_div;
291 div = isl_mat_alloc(ctx, bmap->n_div, cols);
292 if (!div)
293 return NULL;
295 for (i = 0; i < bmap->n_div; ++i)
296 isl_seq_cpy(div->row[i], bmap->div[i], cols);
298 return div;
301 /* Extract the divs in "bset" as a matrix.
303 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
305 return isl_basic_map_get_divs(bset);
308 __isl_give isl_local_space *isl_basic_map_get_local_space(
309 __isl_keep isl_basic_map *bmap)
311 isl_mat *div;
313 if (!bmap)
314 return NULL;
316 div = isl_basic_map_get_divs(bmap);
317 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
320 __isl_give isl_local_space *isl_basic_set_get_local_space(
321 __isl_keep isl_basic_set *bset)
323 return isl_basic_map_get_local_space(bset);
326 /* For each known div d = floor(f/m), add the constraints
328 * f - m d >= 0
329 * -(f-(m-1)) + m d >= 0
331 * Do not finalize the result.
333 static __isl_give isl_basic_map *add_known_div_constraints(
334 __isl_take isl_basic_map *bmap)
336 int i;
337 unsigned n_div;
339 if (!bmap)
340 return NULL;
341 n_div = isl_basic_map_dim(bmap, isl_dim_div);
342 if (n_div == 0)
343 return bmap;
344 bmap = isl_basic_map_cow(bmap);
345 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
346 if (!bmap)
347 return NULL;
348 for (i = 0; i < n_div; ++i) {
349 if (isl_int_is_zero(bmap->div[i][0]))
350 continue;
351 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
352 return isl_basic_map_free(bmap);
355 return bmap;
358 __isl_give isl_basic_map *isl_basic_map_from_local_space(
359 __isl_take isl_local_space *ls)
361 int i;
362 int n_div;
363 isl_basic_map *bmap;
365 if (!ls)
366 return NULL;
368 n_div = isl_local_space_dim(ls, isl_dim_div);
369 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
370 n_div, 0, 2 * n_div);
372 for (i = 0; i < n_div; ++i)
373 if (isl_basic_map_alloc_div(bmap) < 0)
374 goto error;
376 for (i = 0; i < n_div; ++i)
377 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
378 bmap = add_known_div_constraints(bmap);
380 isl_local_space_free(ls);
381 return bmap;
382 error:
383 isl_local_space_free(ls);
384 isl_basic_map_free(bmap);
385 return NULL;
388 __isl_give isl_basic_set *isl_basic_set_from_local_space(
389 __isl_take isl_local_space *ls)
391 return isl_basic_map_from_local_space(ls);
394 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
396 if (!map)
397 return NULL;
398 return isl_space_copy(map->dim);
401 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
403 if (!set)
404 return NULL;
405 return isl_space_copy(set->dim);
408 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
409 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
411 bmap = isl_basic_map_cow(bmap);
412 if (!bmap)
413 return NULL;
414 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
415 if (!bmap->dim)
416 goto error;
417 bmap = isl_basic_map_finalize(bmap);
418 return bmap;
419 error:
420 isl_basic_map_free(bmap);
421 return NULL;
424 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
425 __isl_take isl_basic_set *bset, const char *s)
427 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
430 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
431 enum isl_dim_type type)
433 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
436 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
437 enum isl_dim_type type, const char *s)
439 int i;
441 map = isl_map_cow(map);
442 if (!map)
443 return NULL;
445 map->dim = isl_space_set_tuple_name(map->dim, type, s);
446 if (!map->dim)
447 goto error;
449 for (i = 0; i < map->n; ++i) {
450 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
451 if (!map->p[i])
452 goto error;
455 return map;
456 error:
457 isl_map_free(map);
458 return NULL;
461 /* Replace the identifier of the tuple of type "type" by "id".
463 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
464 __isl_take isl_basic_map *bmap,
465 enum isl_dim_type type, __isl_take isl_id *id)
467 bmap = isl_basic_map_cow(bmap);
468 if (!bmap)
469 goto error;
470 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
471 if (!bmap->dim)
472 return isl_basic_map_free(bmap);
473 bmap = isl_basic_map_finalize(bmap);
474 return bmap;
475 error:
476 isl_id_free(id);
477 return NULL;
480 /* Replace the identifier of the tuple by "id".
482 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
483 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
485 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
488 /* Does the input or output tuple have a name?
490 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
492 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
495 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
496 enum isl_dim_type type)
498 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
501 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
502 const char *s)
504 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
507 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
508 enum isl_dim_type type, __isl_take isl_id *id)
510 map = isl_map_cow(map);
511 if (!map)
512 goto error;
514 map->dim = isl_space_set_tuple_id(map->dim, type, id);
516 return isl_map_reset_space(map, isl_space_copy(map->dim));
517 error:
518 isl_id_free(id);
519 return NULL;
522 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
523 __isl_take isl_id *id)
525 return isl_map_set_tuple_id(set, isl_dim_set, id);
528 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
529 enum isl_dim_type type)
531 map = isl_map_cow(map);
532 if (!map)
533 return NULL;
535 map->dim = isl_space_reset_tuple_id(map->dim, type);
537 return isl_map_reset_space(map, isl_space_copy(map->dim));
540 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
542 return isl_map_reset_tuple_id(set, isl_dim_set);
545 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
547 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
550 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
551 enum isl_dim_type type)
553 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
556 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
558 return isl_map_has_tuple_id(set, isl_dim_set);
561 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
563 return isl_map_get_tuple_id(set, isl_dim_set);
566 /* Does the set tuple have a name?
568 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
570 if (!set)
571 return isl_bool_error;
572 return isl_space_has_tuple_name(set->dim, isl_dim_set);
576 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
578 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
581 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
583 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
586 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
587 enum isl_dim_type type, unsigned pos)
589 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
592 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
593 enum isl_dim_type type, unsigned pos)
595 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
598 /* Does the given dimension have a name?
600 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
601 enum isl_dim_type type, unsigned pos)
603 if (!map)
604 return isl_bool_error;
605 return isl_space_has_dim_name(map->dim, type, pos);
608 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
609 enum isl_dim_type type, unsigned pos)
611 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
614 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
615 enum isl_dim_type type, unsigned pos)
617 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
620 /* Does the given dimension have a name?
622 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
623 enum isl_dim_type type, unsigned pos)
625 if (!set)
626 return isl_bool_error;
627 return isl_space_has_dim_name(set->dim, type, pos);
630 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
631 __isl_take isl_basic_map *bmap,
632 enum isl_dim_type type, unsigned pos, const char *s)
634 bmap = isl_basic_map_cow(bmap);
635 if (!bmap)
636 return NULL;
637 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
638 if (!bmap->dim)
639 goto error;
640 return isl_basic_map_finalize(bmap);
641 error:
642 isl_basic_map_free(bmap);
643 return NULL;
646 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
647 enum isl_dim_type type, unsigned pos, const char *s)
649 int i;
651 map = isl_map_cow(map);
652 if (!map)
653 return NULL;
655 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
656 if (!map->dim)
657 goto error;
659 for (i = 0; i < map->n; ++i) {
660 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
661 if (!map->p[i])
662 goto error;
665 return map;
666 error:
667 isl_map_free(map);
668 return NULL;
671 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
672 __isl_take isl_basic_set *bset,
673 enum isl_dim_type type, unsigned pos, const char *s)
675 return (isl_basic_set *)isl_basic_map_set_dim_name(
676 (isl_basic_map *)bset, type, pos, s);
679 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
680 enum isl_dim_type type, unsigned pos, const char *s)
682 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
685 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
686 enum isl_dim_type type, unsigned pos)
688 if (!bmap)
689 return isl_bool_error;
690 return isl_space_has_dim_id(bmap->dim, type, pos);
693 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
694 enum isl_dim_type type, unsigned pos)
696 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
699 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
700 enum isl_dim_type type, unsigned pos)
702 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
705 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
706 enum isl_dim_type type, unsigned pos)
708 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
711 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
712 enum isl_dim_type type, unsigned pos)
714 return isl_map_has_dim_id(set, type, pos);
717 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
718 enum isl_dim_type type, unsigned pos)
720 return isl_map_get_dim_id(set, type, pos);
723 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
724 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
726 map = isl_map_cow(map);
727 if (!map)
728 goto error;
730 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
732 return isl_map_reset_space(map, isl_space_copy(map->dim));
733 error:
734 isl_id_free(id);
735 return NULL;
738 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
739 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
741 return isl_map_set_dim_id(set, type, pos, id);
744 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
745 __isl_keep isl_id *id)
747 if (!map)
748 return -1;
749 return isl_space_find_dim_by_id(map->dim, type, id);
752 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
753 __isl_keep isl_id *id)
755 return isl_map_find_dim_by_id(set, type, id);
758 /* Return the position of the dimension of the given type and name
759 * in "bmap".
760 * Return -1 if no such dimension can be found.
762 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
763 enum isl_dim_type type, const char *name)
765 if (!bmap)
766 return -1;
767 return isl_space_find_dim_by_name(bmap->dim, type, name);
770 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
771 const char *name)
773 if (!map)
774 return -1;
775 return isl_space_find_dim_by_name(map->dim, type, name);
778 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
779 const char *name)
781 return isl_map_find_dim_by_name(set, type, name);
784 /* Reset the user pointer on all identifiers of parameters and tuples
785 * of the space of "map".
787 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
789 isl_space *space;
791 space = isl_map_get_space(map);
792 space = isl_space_reset_user(space);
793 map = isl_map_reset_space(map, space);
795 return map;
798 /* Reset the user pointer on all identifiers of parameters and tuples
799 * of the space of "set".
801 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
803 return isl_map_reset_user(set);
806 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
808 if (!bmap)
809 return -1;
810 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
813 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
815 return isl_basic_map_is_rational(bset);
818 /* Does "bmap" contain any rational points?
820 * If "bmap" has an equality for each dimension, equating the dimension
821 * to an integer constant, then it has no rational points, even if it
822 * is marked as rational.
824 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
826 int has_rational = 1;
827 unsigned total;
829 if (!bmap)
830 return -1;
831 if (isl_basic_map_plain_is_empty(bmap))
832 return 0;
833 if (!isl_basic_map_is_rational(bmap))
834 return 0;
835 bmap = isl_basic_map_copy(bmap);
836 bmap = isl_basic_map_implicit_equalities(bmap);
837 if (!bmap)
838 return -1;
839 total = isl_basic_map_total_dim(bmap);
840 if (bmap->n_eq == total) {
841 int i, j;
842 for (i = 0; i < bmap->n_eq; ++i) {
843 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
844 if (j < 0)
845 break;
846 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
847 !isl_int_is_negone(bmap->eq[i][1 + j]))
848 break;
849 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
850 total - j - 1);
851 if (j >= 0)
852 break;
854 if (i == bmap->n_eq)
855 has_rational = 0;
857 isl_basic_map_free(bmap);
859 return has_rational;
862 /* Does "map" contain any rational points?
864 int isl_map_has_rational(__isl_keep isl_map *map)
866 int i;
867 int has_rational;
869 if (!map)
870 return -1;
871 for (i = 0; i < map->n; ++i) {
872 has_rational = isl_basic_map_has_rational(map->p[i]);
873 if (has_rational < 0)
874 return -1;
875 if (has_rational)
876 return 1;
878 return 0;
881 /* Does "set" contain any rational points?
883 int isl_set_has_rational(__isl_keep isl_set *set)
885 return isl_map_has_rational(set);
888 /* Is this basic set a parameter domain?
890 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
892 if (!bset)
893 return -1;
894 return isl_space_is_params(bset->dim);
897 /* Is this set a parameter domain?
899 isl_bool isl_set_is_params(__isl_keep isl_set *set)
901 if (!set)
902 return isl_bool_error;
903 return isl_space_is_params(set->dim);
906 /* Is this map actually a parameter domain?
907 * Users should never call this function. Outside of isl,
908 * a map can never be a parameter domain.
910 int isl_map_is_params(__isl_keep isl_map *map)
912 if (!map)
913 return -1;
914 return isl_space_is_params(map->dim);
917 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
918 struct isl_basic_map *bmap, unsigned extra,
919 unsigned n_eq, unsigned n_ineq)
921 int i;
922 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
924 bmap->ctx = ctx;
925 isl_ctx_ref(ctx);
927 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
928 if (isl_blk_is_error(bmap->block))
929 goto error;
931 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
932 if ((n_ineq + n_eq) && !bmap->ineq)
933 goto error;
935 if (extra == 0) {
936 bmap->block2 = isl_blk_empty();
937 bmap->div = NULL;
938 } else {
939 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
940 if (isl_blk_is_error(bmap->block2))
941 goto error;
943 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
944 if (!bmap->div)
945 goto error;
948 for (i = 0; i < n_ineq + n_eq; ++i)
949 bmap->ineq[i] = bmap->block.data + i * row_size;
951 for (i = 0; i < extra; ++i)
952 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
954 bmap->ref = 1;
955 bmap->flags = 0;
956 bmap->c_size = n_eq + n_ineq;
957 bmap->eq = bmap->ineq + n_ineq;
958 bmap->extra = extra;
959 bmap->n_eq = 0;
960 bmap->n_ineq = 0;
961 bmap->n_div = 0;
962 bmap->sample = NULL;
964 return bmap;
965 error:
966 isl_basic_map_free(bmap);
967 return NULL;
970 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
971 unsigned nparam, unsigned dim, unsigned extra,
972 unsigned n_eq, unsigned n_ineq)
974 struct isl_basic_map *bmap;
975 isl_space *space;
977 space = isl_space_set_alloc(ctx, nparam, dim);
978 if (!space)
979 return NULL;
981 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
982 return (struct isl_basic_set *)bmap;
985 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
986 unsigned extra, unsigned n_eq, unsigned n_ineq)
988 struct isl_basic_map *bmap;
989 if (!dim)
990 return NULL;
991 isl_assert(dim->ctx, dim->n_in == 0, goto error);
992 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
993 return (struct isl_basic_set *)bmap;
994 error:
995 isl_space_free(dim);
996 return NULL;
999 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1000 unsigned extra, unsigned n_eq, unsigned n_ineq)
1002 struct isl_basic_map *bmap;
1004 if (!dim)
1005 return NULL;
1006 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1007 if (!bmap)
1008 goto error;
1009 bmap->dim = dim;
1011 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1012 error:
1013 isl_space_free(dim);
1014 return NULL;
1017 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1018 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1019 unsigned n_eq, unsigned n_ineq)
1021 struct isl_basic_map *bmap;
1022 isl_space *dim;
1024 dim = isl_space_alloc(ctx, nparam, in, out);
1025 if (!dim)
1026 return NULL;
1028 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1029 return bmap;
1032 static void dup_constraints(
1033 struct isl_basic_map *dst, struct isl_basic_map *src)
1035 int i;
1036 unsigned total = isl_basic_map_total_dim(src);
1038 for (i = 0; i < src->n_eq; ++i) {
1039 int j = isl_basic_map_alloc_equality(dst);
1040 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1043 for (i = 0; i < src->n_ineq; ++i) {
1044 int j = isl_basic_map_alloc_inequality(dst);
1045 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1048 for (i = 0; i < src->n_div; ++i) {
1049 int j = isl_basic_map_alloc_div(dst);
1050 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1052 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1055 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1057 struct isl_basic_map *dup;
1059 if (!bmap)
1060 return NULL;
1061 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1062 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1063 if (!dup)
1064 return NULL;
1065 dup_constraints(dup, bmap);
1066 dup->flags = bmap->flags;
1067 dup->sample = isl_vec_copy(bmap->sample);
1068 return dup;
1071 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1073 struct isl_basic_map *dup;
1075 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1076 return (struct isl_basic_set *)dup;
1079 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1081 if (!bset)
1082 return NULL;
1084 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1085 bset->ref++;
1086 return bset;
1088 return isl_basic_set_dup(bset);
1091 struct isl_set *isl_set_copy(struct isl_set *set)
1093 if (!set)
1094 return NULL;
1096 set->ref++;
1097 return set;
1100 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1102 if (!bmap)
1103 return NULL;
1105 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1106 bmap->ref++;
1107 return bmap;
1109 bmap = isl_basic_map_dup(bmap);
1110 if (bmap)
1111 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1112 return bmap;
1115 struct isl_map *isl_map_copy(struct isl_map *map)
1117 if (!map)
1118 return NULL;
1120 map->ref++;
1121 return map;
1124 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1126 if (!bmap)
1127 return NULL;
1129 if (--bmap->ref > 0)
1130 return NULL;
1132 isl_ctx_deref(bmap->ctx);
1133 free(bmap->div);
1134 isl_blk_free(bmap->ctx, bmap->block2);
1135 free(bmap->ineq);
1136 isl_blk_free(bmap->ctx, bmap->block);
1137 isl_vec_free(bmap->sample);
1138 isl_space_free(bmap->dim);
1139 free(bmap);
1141 return NULL;
1144 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1146 return isl_basic_map_free((struct isl_basic_map *)bset);
1149 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1151 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1154 __isl_give isl_map *isl_map_align_params_map_map_and(
1155 __isl_take isl_map *map1, __isl_take isl_map *map2,
1156 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1157 __isl_take isl_map *map2))
1159 if (!map1 || !map2)
1160 goto error;
1161 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1162 return fn(map1, map2);
1163 if (!isl_space_has_named_params(map1->dim) ||
1164 !isl_space_has_named_params(map2->dim))
1165 isl_die(map1->ctx, isl_error_invalid,
1166 "unaligned unnamed parameters", goto error);
1167 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1168 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1169 return fn(map1, map2);
1170 error:
1171 isl_map_free(map1);
1172 isl_map_free(map2);
1173 return NULL;
1176 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1177 __isl_keep isl_map *map2,
1178 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1180 isl_bool r;
1182 if (!map1 || !map2)
1183 return isl_bool_error;
1184 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1185 return fn(map1, map2);
1186 if (!isl_space_has_named_params(map1->dim) ||
1187 !isl_space_has_named_params(map2->dim))
1188 isl_die(map1->ctx, isl_error_invalid,
1189 "unaligned unnamed parameters", return isl_bool_error);
1190 map1 = isl_map_copy(map1);
1191 map2 = isl_map_copy(map2);
1192 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1193 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1194 r = fn(map1, map2);
1195 isl_map_free(map1);
1196 isl_map_free(map2);
1197 return r;
1200 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1202 struct isl_ctx *ctx;
1203 if (!bmap)
1204 return -1;
1205 ctx = bmap->ctx;
1206 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1207 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1208 return -1);
1209 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1210 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1211 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1212 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1213 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1214 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1215 isl_int *t;
1216 int j = isl_basic_map_alloc_inequality(bmap);
1217 if (j < 0)
1218 return -1;
1219 t = bmap->ineq[j];
1220 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1221 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1222 bmap->eq[-1] = t;
1223 bmap->n_eq++;
1224 bmap->n_ineq--;
1225 bmap->eq--;
1226 return 0;
1228 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1229 bmap->extra - bmap->n_div);
1230 return bmap->n_eq++;
1233 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1235 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1238 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1240 if (!bmap)
1241 return -1;
1242 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1243 bmap->n_eq -= n;
1244 return 0;
1247 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1249 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1252 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1254 isl_int *t;
1255 if (!bmap)
1256 return -1;
1257 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1259 if (pos != bmap->n_eq - 1) {
1260 t = bmap->eq[pos];
1261 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1262 bmap->eq[bmap->n_eq - 1] = t;
1264 bmap->n_eq--;
1265 return 0;
1268 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1270 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1273 /* Turn inequality "pos" of "bmap" into an equality.
1275 * In particular, we move the inequality in front of the equalities
1276 * and move the last inequality in the position of the moved inequality.
1277 * Note that isl_tab_make_equalities_explicit depends on this particular
1278 * change in the ordering of the constraints.
1280 void isl_basic_map_inequality_to_equality(
1281 struct isl_basic_map *bmap, unsigned pos)
1283 isl_int *t;
1285 t = bmap->ineq[pos];
1286 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1287 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1288 bmap->eq[-1] = t;
1289 bmap->n_eq++;
1290 bmap->n_ineq--;
1291 bmap->eq--;
1292 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1293 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1294 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1295 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1298 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1300 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1303 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1305 struct isl_ctx *ctx;
1306 if (!bmap)
1307 return -1;
1308 ctx = bmap->ctx;
1309 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1310 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1311 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1312 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1313 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1314 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1315 1 + isl_basic_map_total_dim(bmap),
1316 bmap->extra - bmap->n_div);
1317 return bmap->n_ineq++;
1320 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1322 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1325 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1327 if (!bmap)
1328 return -1;
1329 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1330 bmap->n_ineq -= n;
1331 return 0;
1334 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1336 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1339 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1341 isl_int *t;
1342 if (!bmap)
1343 return -1;
1344 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1346 if (pos != bmap->n_ineq - 1) {
1347 t = bmap->ineq[pos];
1348 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1349 bmap->ineq[bmap->n_ineq - 1] = t;
1350 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1352 bmap->n_ineq--;
1353 return 0;
1356 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1358 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1361 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1362 isl_int *eq)
1364 int k;
1366 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1367 if (!bmap)
1368 return NULL;
1369 k = isl_basic_map_alloc_equality(bmap);
1370 if (k < 0)
1371 goto error;
1372 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1373 return bmap;
1374 error:
1375 isl_basic_map_free(bmap);
1376 return NULL;
1379 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1380 isl_int *eq)
1382 return (isl_basic_set *)
1383 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1386 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1387 isl_int *ineq)
1389 int k;
1391 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1392 if (!bmap)
1393 return NULL;
1394 k = isl_basic_map_alloc_inequality(bmap);
1395 if (k < 0)
1396 goto error;
1397 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1398 return bmap;
1399 error:
1400 isl_basic_map_free(bmap);
1401 return NULL;
1404 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1405 isl_int *ineq)
1407 return (isl_basic_set *)
1408 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1411 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1413 if (!bmap)
1414 return -1;
1415 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1416 isl_seq_clr(bmap->div[bmap->n_div] +
1417 1 + 1 + isl_basic_map_total_dim(bmap),
1418 bmap->extra - bmap->n_div);
1419 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1420 return bmap->n_div++;
1423 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1425 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1428 /* Insert an extra integer division, prescribed by "div", to "bmap"
1429 * at (integer division) position "pos".
1431 * The integer division is first added at the end and then moved
1432 * into the right position.
1434 __isl_give isl_basic_map *isl_basic_map_insert_div(
1435 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1437 int i, k, n_div;
1439 bmap = isl_basic_map_cow(bmap);
1440 if (!bmap || !div)
1441 return isl_basic_map_free(bmap);
1443 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1444 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1445 "unexpected size", return isl_basic_map_free(bmap));
1446 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1447 if (pos < 0 || pos > n_div)
1448 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1449 "invalid position", return isl_basic_map_free(bmap));
1451 bmap = isl_basic_map_extend_space(bmap,
1452 isl_basic_map_get_space(bmap), 1, 0, 2);
1453 k = isl_basic_map_alloc_div(bmap);
1454 if (k < 0)
1455 return isl_basic_map_free(bmap);
1456 isl_seq_cpy(bmap->div[k], div->el, div->size);
1457 isl_int_set_si(bmap->div[k][div->size], 0);
1459 for (i = k; i > pos; --i)
1460 isl_basic_map_swap_div(bmap, i, i - 1);
1462 return bmap;
1465 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1467 if (!bmap)
1468 return -1;
1469 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1470 bmap->n_div -= n;
1471 return 0;
1474 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1476 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1479 /* Copy constraint from src to dst, putting the vars of src at offset
1480 * dim_off in dst and the divs of src at offset div_off in dst.
1481 * If both sets are actually map, then dim_off applies to the input
1482 * variables.
1484 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1485 struct isl_basic_map *src_map, isl_int *src,
1486 unsigned in_off, unsigned out_off, unsigned div_off)
1488 unsigned src_nparam = isl_basic_map_n_param(src_map);
1489 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1490 unsigned src_in = isl_basic_map_n_in(src_map);
1491 unsigned dst_in = isl_basic_map_n_in(dst_map);
1492 unsigned src_out = isl_basic_map_n_out(src_map);
1493 unsigned dst_out = isl_basic_map_n_out(dst_map);
1494 isl_int_set(dst[0], src[0]);
1495 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1496 if (dst_nparam > src_nparam)
1497 isl_seq_clr(dst+1+src_nparam,
1498 dst_nparam - src_nparam);
1499 isl_seq_clr(dst+1+dst_nparam, in_off);
1500 isl_seq_cpy(dst+1+dst_nparam+in_off,
1501 src+1+src_nparam,
1502 isl_min(dst_in-in_off, src_in));
1503 if (dst_in-in_off > src_in)
1504 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1505 dst_in - in_off - src_in);
1506 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1507 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1508 src+1+src_nparam+src_in,
1509 isl_min(dst_out-out_off, src_out));
1510 if (dst_out-out_off > src_out)
1511 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1512 dst_out - out_off - src_out);
1513 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1514 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1515 src+1+src_nparam+src_in+src_out,
1516 isl_min(dst_map->extra-div_off, src_map->n_div));
1517 if (dst_map->n_div-div_off > src_map->n_div)
1518 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1519 div_off+src_map->n_div,
1520 dst_map->n_div - div_off - src_map->n_div);
1523 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1524 struct isl_basic_map *src_map, isl_int *src,
1525 unsigned in_off, unsigned out_off, unsigned div_off)
1527 isl_int_set(dst[0], src[0]);
1528 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1531 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1532 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1534 int i;
1535 unsigned div_off;
1537 if (!bmap1 || !bmap2)
1538 goto error;
1540 div_off = bmap1->n_div;
1542 for (i = 0; i < bmap2->n_eq; ++i) {
1543 int i1 = isl_basic_map_alloc_equality(bmap1);
1544 if (i1 < 0)
1545 goto error;
1546 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1547 i_pos, o_pos, div_off);
1550 for (i = 0; i < bmap2->n_ineq; ++i) {
1551 int i1 = isl_basic_map_alloc_inequality(bmap1);
1552 if (i1 < 0)
1553 goto error;
1554 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1555 i_pos, o_pos, div_off);
1558 for (i = 0; i < bmap2->n_div; ++i) {
1559 int i1 = isl_basic_map_alloc_div(bmap1);
1560 if (i1 < 0)
1561 goto error;
1562 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1563 i_pos, o_pos, div_off);
1566 isl_basic_map_free(bmap2);
1568 return bmap1;
1570 error:
1571 isl_basic_map_free(bmap1);
1572 isl_basic_map_free(bmap2);
1573 return NULL;
1576 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1577 struct isl_basic_set *bset2, unsigned pos)
1579 return (struct isl_basic_set *)
1580 add_constraints((struct isl_basic_map *)bset1,
1581 (struct isl_basic_map *)bset2, 0, pos);
1584 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1585 __isl_take isl_space *dim, unsigned extra,
1586 unsigned n_eq, unsigned n_ineq)
1588 struct isl_basic_map *ext;
1589 unsigned flags;
1590 int dims_ok;
1592 if (!dim)
1593 goto error;
1595 if (!base)
1596 goto error;
1598 dims_ok = isl_space_is_equal(base->dim, dim) &&
1599 base->extra >= base->n_div + extra;
1601 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1602 room_for_ineq(base, n_ineq)) {
1603 isl_space_free(dim);
1604 return base;
1607 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1608 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1609 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1610 extra += base->extra;
1611 n_eq += base->n_eq;
1612 n_ineq += base->n_ineq;
1614 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1615 dim = NULL;
1616 if (!ext)
1617 goto error;
1619 if (dims_ok)
1620 ext->sample = isl_vec_copy(base->sample);
1621 flags = base->flags;
1622 ext = add_constraints(ext, base, 0, 0);
1623 if (ext) {
1624 ext->flags = flags;
1625 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1628 return ext;
1630 error:
1631 isl_space_free(dim);
1632 isl_basic_map_free(base);
1633 return NULL;
1636 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1637 __isl_take isl_space *dim, unsigned extra,
1638 unsigned n_eq, unsigned n_ineq)
1640 return (struct isl_basic_set *)
1641 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1642 extra, n_eq, n_ineq);
1645 struct isl_basic_map *isl_basic_map_extend_constraints(
1646 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1648 if (!base)
1649 return NULL;
1650 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1651 0, n_eq, n_ineq);
1654 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1655 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1656 unsigned n_eq, unsigned n_ineq)
1658 struct isl_basic_map *bmap;
1659 isl_space *dim;
1661 if (!base)
1662 return NULL;
1663 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1664 if (!dim)
1665 goto error;
1667 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1668 return bmap;
1669 error:
1670 isl_basic_map_free(base);
1671 return NULL;
1674 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1675 unsigned nparam, unsigned dim, unsigned extra,
1676 unsigned n_eq, unsigned n_ineq)
1678 return (struct isl_basic_set *)
1679 isl_basic_map_extend((struct isl_basic_map *)base,
1680 nparam, 0, dim, extra, n_eq, n_ineq);
1683 struct isl_basic_set *isl_basic_set_extend_constraints(
1684 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1686 return (struct isl_basic_set *)
1687 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1688 n_eq, n_ineq);
1691 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1693 return (struct isl_basic_set *)
1694 isl_basic_map_cow((struct isl_basic_map *)bset);
1697 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1699 if (!bmap)
1700 return NULL;
1702 if (bmap->ref > 1) {
1703 bmap->ref--;
1704 bmap = isl_basic_map_dup(bmap);
1706 if (bmap) {
1707 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1708 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1710 return bmap;
1713 /* Clear all cached information in "map", either because it is about
1714 * to be modified or because it is being freed.
1715 * Always return the same pointer that is passed in.
1716 * This is needed for the use in isl_map_free.
1718 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1720 isl_basic_map_free(map->cached_simple_hull[0]);
1721 isl_basic_map_free(map->cached_simple_hull[1]);
1722 map->cached_simple_hull[0] = NULL;
1723 map->cached_simple_hull[1] = NULL;
1724 return map;
1727 struct isl_set *isl_set_cow(struct isl_set *set)
1729 return isl_map_cow(set);
1732 /* Return an isl_map that is equal to "map" and that has only
1733 * a single reference.
1735 * If the original input already has only one reference, then
1736 * simply return it, but clear all cached information, since
1737 * it may be rendered invalid by the operations that will be
1738 * performed on the result.
1740 * Otherwise, create a duplicate (without any cached information).
1742 struct isl_map *isl_map_cow(struct isl_map *map)
1744 if (!map)
1745 return NULL;
1747 if (map->ref == 1)
1748 return clear_caches(map);
1749 map->ref--;
1750 return isl_map_dup(map);
1753 static void swap_vars(struct isl_blk blk, isl_int *a,
1754 unsigned a_len, unsigned b_len)
1756 isl_seq_cpy(blk.data, a+a_len, b_len);
1757 isl_seq_cpy(blk.data+b_len, a, a_len);
1758 isl_seq_cpy(a, blk.data, b_len+a_len);
1761 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1762 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1764 int i;
1765 struct isl_blk blk;
1767 if (!bmap)
1768 goto error;
1770 isl_assert(bmap->ctx,
1771 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1773 if (n1 == 0 || n2 == 0)
1774 return bmap;
1776 bmap = isl_basic_map_cow(bmap);
1777 if (!bmap)
1778 return NULL;
1780 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1781 if (isl_blk_is_error(blk))
1782 goto error;
1784 for (i = 0; i < bmap->n_eq; ++i)
1785 swap_vars(blk,
1786 bmap->eq[i] + pos, n1, n2);
1788 for (i = 0; i < bmap->n_ineq; ++i)
1789 swap_vars(blk,
1790 bmap->ineq[i] + pos, n1, n2);
1792 for (i = 0; i < bmap->n_div; ++i)
1793 swap_vars(blk,
1794 bmap->div[i]+1 + pos, n1, n2);
1796 isl_blk_free(bmap->ctx, blk);
1798 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1799 bmap = isl_basic_map_gauss(bmap, NULL);
1800 return isl_basic_map_finalize(bmap);
1801 error:
1802 isl_basic_map_free(bmap);
1803 return NULL;
1806 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1808 int i = 0;
1809 unsigned total;
1810 if (!bmap)
1811 goto error;
1812 total = isl_basic_map_total_dim(bmap);
1813 isl_basic_map_free_div(bmap, bmap->n_div);
1814 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1815 if (bmap->n_eq > 0)
1816 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1817 else {
1818 i = isl_basic_map_alloc_equality(bmap);
1819 if (i < 0)
1820 goto error;
1822 isl_int_set_si(bmap->eq[i][0], 1);
1823 isl_seq_clr(bmap->eq[i]+1, total);
1824 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1825 isl_vec_free(bmap->sample);
1826 bmap->sample = NULL;
1827 return isl_basic_map_finalize(bmap);
1828 error:
1829 isl_basic_map_free(bmap);
1830 return NULL;
1833 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1835 return (struct isl_basic_set *)
1836 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1839 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1840 * of "bmap").
1842 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1844 isl_int *t = bmap->div[a];
1845 bmap->div[a] = bmap->div[b];
1846 bmap->div[b] = t;
1849 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1850 * div definitions accordingly.
1852 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1854 int i;
1855 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1857 swap_div(bmap, a, b);
1859 for (i = 0; i < bmap->n_eq; ++i)
1860 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1862 for (i = 0; i < bmap->n_ineq; ++i)
1863 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1865 for (i = 0; i < bmap->n_div; ++i)
1866 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1867 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1870 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1871 * div definitions accordingly.
1873 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1875 isl_basic_map_swap_div(bset, a, b);
1878 /* Eliminate the specified n dimensions starting at first from the
1879 * constraints, without removing the dimensions from the space.
1880 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1882 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1883 enum isl_dim_type type, unsigned first, unsigned n)
1885 int i;
1887 if (!map)
1888 return NULL;
1889 if (n == 0)
1890 return map;
1892 if (first + n > isl_map_dim(map, type) || first + n < first)
1893 isl_die(map->ctx, isl_error_invalid,
1894 "index out of bounds", goto error);
1896 map = isl_map_cow(map);
1897 if (!map)
1898 return NULL;
1900 for (i = 0; i < map->n; ++i) {
1901 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1902 if (!map->p[i])
1903 goto error;
1905 return map;
1906 error:
1907 isl_map_free(map);
1908 return NULL;
1911 /* Eliminate the specified n dimensions starting at first from the
1912 * constraints, without removing the dimensions from the space.
1913 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1915 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1916 enum isl_dim_type type, unsigned first, unsigned n)
1918 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1921 /* Eliminate the specified n dimensions starting at first from the
1922 * constraints, without removing the dimensions from the space.
1923 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1925 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1926 unsigned first, unsigned n)
1928 return isl_set_eliminate(set, isl_dim_set, first, n);
1931 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1932 __isl_take isl_basic_map *bmap)
1934 if (!bmap)
1935 return NULL;
1936 bmap = isl_basic_map_eliminate_vars(bmap,
1937 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1938 if (!bmap)
1939 return NULL;
1940 bmap->n_div = 0;
1941 return isl_basic_map_finalize(bmap);
1944 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1945 __isl_take isl_basic_set *bset)
1947 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1948 (struct isl_basic_map *)bset);
1951 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1953 int i;
1955 if (!map)
1956 return NULL;
1957 if (map->n == 0)
1958 return map;
1960 map = isl_map_cow(map);
1961 if (!map)
1962 return NULL;
1964 for (i = 0; i < map->n; ++i) {
1965 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1966 if (!map->p[i])
1967 goto error;
1969 return map;
1970 error:
1971 isl_map_free(map);
1972 return NULL;
1975 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1977 return isl_map_remove_divs(set);
1980 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1981 enum isl_dim_type type, unsigned first, unsigned n)
1983 if (!bmap)
1984 return NULL;
1985 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1986 goto error);
1987 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1988 return bmap;
1989 bmap = isl_basic_map_eliminate_vars(bmap,
1990 isl_basic_map_offset(bmap, type) - 1 + first, n);
1991 if (!bmap)
1992 return bmap;
1993 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1994 return bmap;
1995 bmap = isl_basic_map_drop(bmap, type, first, n);
1996 return bmap;
1997 error:
1998 isl_basic_map_free(bmap);
1999 return NULL;
2002 /* Return true if the definition of the given div (recursively) involves
2003 * any of the given variables.
2005 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2006 unsigned first, unsigned n)
2008 int i;
2009 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2011 if (isl_int_is_zero(bmap->div[div][0]))
2012 return 0;
2013 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2014 return 1;
2016 for (i = bmap->n_div - 1; i >= 0; --i) {
2017 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2018 continue;
2019 if (div_involves_vars(bmap, i, first, n))
2020 return 1;
2023 return 0;
2026 /* Try and add a lower and/or upper bound on "div" to "bmap"
2027 * based on inequality "i".
2028 * "total" is the total number of variables (excluding the divs).
2029 * "v" is a temporary object that can be used during the calculations.
2030 * If "lb" is set, then a lower bound should be constructed.
2031 * If "ub" is set, then an upper bound should be constructed.
2033 * The calling function has already checked that the inequality does not
2034 * reference "div", but we still need to check that the inequality is
2035 * of the right form. We'll consider the case where we want to construct
2036 * a lower bound. The construction of upper bounds is similar.
2038 * Let "div" be of the form
2040 * q = floor((a + f(x))/d)
2042 * We essentially check if constraint "i" is of the form
2044 * b + f(x) >= 0
2046 * so that we can use it to derive a lower bound on "div".
2047 * However, we allow a slightly more general form
2049 * b + g(x) >= 0
2051 * with the condition that the coefficients of g(x) - f(x) are all
2052 * divisible by d.
2053 * Rewriting this constraint as
2055 * 0 >= -b - g(x)
2057 * adding a + f(x) to both sides and dividing by d, we obtain
2059 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2061 * Taking the floor on both sides, we obtain
2063 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2065 * or
2067 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2069 * In the case of an upper bound, we construct the constraint
2071 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2074 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2075 __isl_take isl_basic_map *bmap, int div, int i,
2076 unsigned total, isl_int v, int lb, int ub)
2078 int j;
2080 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2081 if (lb) {
2082 isl_int_sub(v, bmap->ineq[i][1 + j],
2083 bmap->div[div][1 + 1 + j]);
2084 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2086 if (ub) {
2087 isl_int_add(v, bmap->ineq[i][1 + j],
2088 bmap->div[div][1 + 1 + j]);
2089 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2092 if (!lb && !ub)
2093 return bmap;
2095 bmap = isl_basic_map_cow(bmap);
2096 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2097 if (lb) {
2098 int k = isl_basic_map_alloc_inequality(bmap);
2099 if (k < 0)
2100 goto error;
2101 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2102 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2103 bmap->div[div][1 + j]);
2104 isl_int_cdiv_q(bmap->ineq[k][j],
2105 bmap->ineq[k][j], bmap->div[div][0]);
2107 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2109 if (ub) {
2110 int k = isl_basic_map_alloc_inequality(bmap);
2111 if (k < 0)
2112 goto error;
2113 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2114 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2115 bmap->div[div][1 + j]);
2116 isl_int_fdiv_q(bmap->ineq[k][j],
2117 bmap->ineq[k][j], bmap->div[div][0]);
2119 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2122 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2123 return bmap;
2124 error:
2125 isl_basic_map_free(bmap);
2126 return NULL;
2129 /* This function is called right before "div" is eliminated from "bmap"
2130 * using Fourier-Motzkin.
2131 * Look through the constraints of "bmap" for constraints on the argument
2132 * of the integer division and use them to construct constraints on the
2133 * integer division itself. These constraints can then be combined
2134 * during the Fourier-Motzkin elimination.
2135 * Note that it is only useful to introduce lower bounds on "div"
2136 * if "bmap" already contains upper bounds on "div" as the newly
2137 * introduce lower bounds can then be combined with the pre-existing
2138 * upper bounds. Similarly for upper bounds.
2139 * We therefore first check if "bmap" contains any lower and/or upper bounds
2140 * on "div".
2142 * It is interesting to note that the introduction of these constraints
2143 * can indeed lead to more accurate results, even when compared to
2144 * deriving constraints on the argument of "div" from constraints on "div".
2145 * Consider, for example, the set
2147 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2149 * The second constraint can be rewritten as
2151 * 2 * [(-i-2j+3)/4] + k >= 0
2153 * from which we can derive
2155 * -i - 2j + 3 >= -2k
2157 * or
2159 * i + 2j <= 3 + 2k
2161 * Combined with the first constraint, we obtain
2163 * -3 <= 3 + 2k or k >= -3
2165 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2166 * the first constraint, we obtain
2168 * [(i + 2j)/4] >= [-3/4] = -1
2170 * Combining this constraint with the second constraint, we obtain
2172 * k >= -2
2174 static __isl_give isl_basic_map *insert_bounds_on_div(
2175 __isl_take isl_basic_map *bmap, int div)
2177 int i;
2178 int check_lb, check_ub;
2179 isl_int v;
2180 unsigned total;
2182 if (!bmap)
2183 return NULL;
2185 if (isl_int_is_zero(bmap->div[div][0]))
2186 return bmap;
2188 total = isl_space_dim(bmap->dim, isl_dim_all);
2190 check_lb = 0;
2191 check_ub = 0;
2192 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2193 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2194 if (s > 0)
2195 check_ub = 1;
2196 if (s < 0)
2197 check_lb = 1;
2200 if (!check_lb && !check_ub)
2201 return bmap;
2203 isl_int_init(v);
2205 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2206 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2207 continue;
2209 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2210 check_lb, check_ub);
2213 isl_int_clear(v);
2215 return bmap;
2218 /* Remove all divs (recursively) involving any of the given dimensions
2219 * in their definitions.
2221 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2222 __isl_take isl_basic_map *bmap,
2223 enum isl_dim_type type, unsigned first, unsigned n)
2225 int i;
2227 if (!bmap)
2228 return NULL;
2229 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2230 goto error);
2231 first += isl_basic_map_offset(bmap, type);
2233 for (i = bmap->n_div - 1; i >= 0; --i) {
2234 if (!div_involves_vars(bmap, i, first, n))
2235 continue;
2236 bmap = insert_bounds_on_div(bmap, i);
2237 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2238 if (!bmap)
2239 return NULL;
2240 i = bmap->n_div;
2243 return bmap;
2244 error:
2245 isl_basic_map_free(bmap);
2246 return NULL;
2249 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2250 __isl_take isl_basic_set *bset,
2251 enum isl_dim_type type, unsigned first, unsigned n)
2253 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2256 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2257 enum isl_dim_type type, unsigned first, unsigned n)
2259 int i;
2261 if (!map)
2262 return NULL;
2263 if (map->n == 0)
2264 return map;
2266 map = isl_map_cow(map);
2267 if (!map)
2268 return NULL;
2270 for (i = 0; i < map->n; ++i) {
2271 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2272 type, first, n);
2273 if (!map->p[i])
2274 goto error;
2276 return map;
2277 error:
2278 isl_map_free(map);
2279 return NULL;
2282 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2283 enum isl_dim_type type, unsigned first, unsigned n)
2285 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2286 type, first, n);
2289 /* Does the desciption of "bmap" depend on the specified dimensions?
2290 * We also check whether the dimensions appear in any of the div definitions.
2291 * In principle there is no need for this check. If the dimensions appear
2292 * in a div definition, they also appear in the defining constraints of that
2293 * div.
2295 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2296 enum isl_dim_type type, unsigned first, unsigned n)
2298 int i;
2300 if (!bmap)
2301 return isl_bool_error;
2303 if (first + n > isl_basic_map_dim(bmap, type))
2304 isl_die(bmap->ctx, isl_error_invalid,
2305 "index out of bounds", return isl_bool_error);
2307 first += isl_basic_map_offset(bmap, type);
2308 for (i = 0; i < bmap->n_eq; ++i)
2309 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2310 return isl_bool_true;
2311 for (i = 0; i < bmap->n_ineq; ++i)
2312 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2313 return isl_bool_true;
2314 for (i = 0; i < bmap->n_div; ++i) {
2315 if (isl_int_is_zero(bmap->div[i][0]))
2316 continue;
2317 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2318 return isl_bool_true;
2321 return isl_bool_false;
2324 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2325 enum isl_dim_type type, unsigned first, unsigned n)
2327 int i;
2329 if (!map)
2330 return isl_bool_error;
2332 if (first + n > isl_map_dim(map, type))
2333 isl_die(map->ctx, isl_error_invalid,
2334 "index out of bounds", return isl_bool_error);
2336 for (i = 0; i < map->n; ++i) {
2337 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2338 type, first, n);
2339 if (involves < 0 || involves)
2340 return involves;
2343 return isl_bool_false;
2346 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2347 enum isl_dim_type type, unsigned first, unsigned n)
2349 return isl_basic_map_involves_dims(bset, type, first, n);
2352 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2353 enum isl_dim_type type, unsigned first, unsigned n)
2355 return isl_map_involves_dims(set, type, first, n);
2358 /* Does local variable "div" of "bmap" have a complete explicit representation?
2359 * Having a complete explicit representation requires not only
2360 * an explicit representation, but also that all local variables
2361 * that appear in this explicit representation in turn have
2362 * a complete explicit representation.
2364 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2366 int i;
2367 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2368 isl_bool marked;
2370 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2371 if (marked < 0 || marked)
2372 return isl_bool_not(marked);
2374 for (i = bmap->n_div - 1; i >= 0; --i) {
2375 isl_bool known;
2377 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2378 continue;
2379 known = isl_basic_map_div_is_known(bmap, i);
2380 if (known < 0 || !known)
2381 return known;
2384 return isl_bool_true;
2387 /* Does local variable "div" of "bset" have a complete explicit representation?
2389 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2391 return isl_basic_map_div_is_known(bset, div);
2394 /* Remove all divs that are unknown or defined in terms of unknown divs.
2396 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2397 __isl_take isl_basic_map *bmap)
2399 int i;
2401 if (!bmap)
2402 return NULL;
2404 for (i = bmap->n_div - 1; i >= 0; --i) {
2405 if (isl_basic_map_div_is_known(bmap, i))
2406 continue;
2407 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2408 if (!bmap)
2409 return NULL;
2410 i = bmap->n_div;
2413 return bmap;
2416 /* Remove all divs that are unknown or defined in terms of unknown divs.
2418 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2419 __isl_take isl_basic_set *bset)
2421 return isl_basic_map_remove_unknown_divs(bset);
2424 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2426 int i;
2428 if (!map)
2429 return NULL;
2430 if (map->n == 0)
2431 return map;
2433 map = isl_map_cow(map);
2434 if (!map)
2435 return NULL;
2437 for (i = 0; i < map->n; ++i) {
2438 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2439 if (!map->p[i])
2440 goto error;
2442 return map;
2443 error:
2444 isl_map_free(map);
2445 return NULL;
2448 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2450 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2453 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2454 __isl_take isl_basic_set *bset,
2455 enum isl_dim_type type, unsigned first, unsigned n)
2457 return (isl_basic_set *)
2458 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2461 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2462 enum isl_dim_type type, unsigned first, unsigned n)
2464 int i;
2466 if (n == 0)
2467 return map;
2469 map = isl_map_cow(map);
2470 if (!map)
2471 return NULL;
2472 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2474 for (i = 0; i < map->n; ++i) {
2475 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2476 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2477 if (!map->p[i])
2478 goto error;
2480 map = isl_map_drop(map, type, first, n);
2481 return map;
2482 error:
2483 isl_map_free(map);
2484 return NULL;
2487 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2488 enum isl_dim_type type, unsigned first, unsigned n)
2490 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2493 /* Project out n inputs starting at first using Fourier-Motzkin */
2494 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2495 unsigned first, unsigned n)
2497 return isl_map_remove_dims(map, isl_dim_in, first, n);
2500 static void dump_term(struct isl_basic_map *bmap,
2501 isl_int c, int pos, FILE *out)
2503 const char *name;
2504 unsigned in = isl_basic_map_n_in(bmap);
2505 unsigned dim = in + isl_basic_map_n_out(bmap);
2506 unsigned nparam = isl_basic_map_n_param(bmap);
2507 if (!pos)
2508 isl_int_print(out, c, 0);
2509 else {
2510 if (!isl_int_is_one(c))
2511 isl_int_print(out, c, 0);
2512 if (pos < 1 + nparam) {
2513 name = isl_space_get_dim_name(bmap->dim,
2514 isl_dim_param, pos - 1);
2515 if (name)
2516 fprintf(out, "%s", name);
2517 else
2518 fprintf(out, "p%d", pos - 1);
2519 } else if (pos < 1 + nparam + in)
2520 fprintf(out, "i%d", pos - 1 - nparam);
2521 else if (pos < 1 + nparam + dim)
2522 fprintf(out, "o%d", pos - 1 - nparam - in);
2523 else
2524 fprintf(out, "e%d", pos - 1 - nparam - dim);
2528 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2529 int sign, FILE *out)
2531 int i;
2532 int first;
2533 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2534 isl_int v;
2536 isl_int_init(v);
2537 for (i = 0, first = 1; i < len; ++i) {
2538 if (isl_int_sgn(c[i]) * sign <= 0)
2539 continue;
2540 if (!first)
2541 fprintf(out, " + ");
2542 first = 0;
2543 isl_int_abs(v, c[i]);
2544 dump_term(bmap, v, i, out);
2546 isl_int_clear(v);
2547 if (first)
2548 fprintf(out, "0");
2551 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2552 const char *op, FILE *out, int indent)
2554 int i;
2556 fprintf(out, "%*s", indent, "");
2558 dump_constraint_sign(bmap, c, 1, out);
2559 fprintf(out, " %s ", op);
2560 dump_constraint_sign(bmap, c, -1, out);
2562 fprintf(out, "\n");
2564 for (i = bmap->n_div; i < bmap->extra; ++i) {
2565 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2566 continue;
2567 fprintf(out, "%*s", indent, "");
2568 fprintf(out, "ERROR: unused div coefficient not zero\n");
2569 abort();
2573 static void dump_constraints(struct isl_basic_map *bmap,
2574 isl_int **c, unsigned n,
2575 const char *op, FILE *out, int indent)
2577 int i;
2579 for (i = 0; i < n; ++i)
2580 dump_constraint(bmap, c[i], op, out, indent);
2583 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2585 int j;
2586 int first = 1;
2587 unsigned total = isl_basic_map_total_dim(bmap);
2589 for (j = 0; j < 1 + total; ++j) {
2590 if (isl_int_is_zero(exp[j]))
2591 continue;
2592 if (!first && isl_int_is_pos(exp[j]))
2593 fprintf(out, "+");
2594 dump_term(bmap, exp[j], j, out);
2595 first = 0;
2599 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2601 int i;
2603 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2604 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2606 for (i = 0; i < bmap->n_div; ++i) {
2607 fprintf(out, "%*s", indent, "");
2608 fprintf(out, "e%d = [(", i);
2609 dump_affine(bmap, bmap->div[i]+1, out);
2610 fprintf(out, ")/");
2611 isl_int_print(out, bmap->div[i][0], 0);
2612 fprintf(out, "]\n");
2616 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2617 FILE *out, int indent)
2619 if (!bset) {
2620 fprintf(out, "null basic set\n");
2621 return;
2624 fprintf(out, "%*s", indent, "");
2625 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2626 bset->ref, bset->dim->nparam, bset->dim->n_out,
2627 bset->extra, bset->flags);
2628 dump((struct isl_basic_map *)bset, out, indent);
2631 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2632 FILE *out, int indent)
2634 if (!bmap) {
2635 fprintf(out, "null basic map\n");
2636 return;
2639 fprintf(out, "%*s", indent, "");
2640 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2641 "flags: %x, n_name: %d\n",
2642 bmap->ref,
2643 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2644 bmap->extra, bmap->flags, bmap->dim->n_id);
2645 dump(bmap, out, indent);
2648 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2650 unsigned total;
2651 if (!bmap)
2652 return -1;
2653 total = isl_basic_map_total_dim(bmap);
2654 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2655 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2656 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2657 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2658 return 0;
2661 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2662 unsigned flags)
2664 if (!space)
2665 return NULL;
2666 if (isl_space_dim(space, isl_dim_in) != 0)
2667 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2668 "set cannot have input dimensions", goto error);
2669 return isl_map_alloc_space(space, n, flags);
2670 error:
2671 isl_space_free(space);
2672 return NULL;
2675 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2676 unsigned nparam, unsigned dim, int n, unsigned flags)
2678 struct isl_set *set;
2679 isl_space *dims;
2681 dims = isl_space_alloc(ctx, nparam, 0, dim);
2682 if (!dims)
2683 return NULL;
2685 set = isl_set_alloc_space(dims, n, flags);
2686 return set;
2689 /* Make sure "map" has room for at least "n" more basic maps.
2691 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2693 int i;
2694 struct isl_map *grown = NULL;
2696 if (!map)
2697 return NULL;
2698 isl_assert(map->ctx, n >= 0, goto error);
2699 if (map->n + n <= map->size)
2700 return map;
2701 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2702 if (!grown)
2703 goto error;
2704 for (i = 0; i < map->n; ++i) {
2705 grown->p[i] = isl_basic_map_copy(map->p[i]);
2706 if (!grown->p[i])
2707 goto error;
2708 grown->n++;
2710 isl_map_free(map);
2711 return grown;
2712 error:
2713 isl_map_free(grown);
2714 isl_map_free(map);
2715 return NULL;
2718 /* Make sure "set" has room for at least "n" more basic sets.
2720 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2722 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2725 struct isl_set *isl_set_dup(struct isl_set *set)
2727 int i;
2728 struct isl_set *dup;
2730 if (!set)
2731 return NULL;
2733 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2734 if (!dup)
2735 return NULL;
2736 for (i = 0; i < set->n; ++i)
2737 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2738 return dup;
2741 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2743 return isl_map_from_basic_map(bset);
2746 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2748 struct isl_map *map;
2750 if (!bmap)
2751 return NULL;
2753 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2754 return isl_map_add_basic_map(map, bmap);
2757 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2758 __isl_take isl_basic_set *bset)
2760 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2761 (struct isl_basic_map *)bset);
2764 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2766 return isl_map_free(set);
2769 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2771 int i;
2773 if (!set) {
2774 fprintf(out, "null set\n");
2775 return;
2778 fprintf(out, "%*s", indent, "");
2779 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2780 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2781 set->flags);
2782 for (i = 0; i < set->n; ++i) {
2783 fprintf(out, "%*s", indent, "");
2784 fprintf(out, "basic set %d:\n", i);
2785 isl_basic_set_print_internal(set->p[i], out, indent+4);
2789 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2791 int i;
2793 if (!map) {
2794 fprintf(out, "null map\n");
2795 return;
2798 fprintf(out, "%*s", indent, "");
2799 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2800 "flags: %x, n_name: %d\n",
2801 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2802 map->dim->n_out, map->flags, map->dim->n_id);
2803 for (i = 0; i < map->n; ++i) {
2804 fprintf(out, "%*s", indent, "");
2805 fprintf(out, "basic map %d:\n", i);
2806 isl_basic_map_print_internal(map->p[i], out, indent+4);
2810 struct isl_basic_map *isl_basic_map_intersect_domain(
2811 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2813 struct isl_basic_map *bmap_domain;
2815 if (!bmap || !bset)
2816 goto error;
2818 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2819 bset->dim, isl_dim_param), goto error);
2821 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2822 isl_assert(bset->ctx,
2823 isl_basic_map_compatible_domain(bmap, bset), goto error);
2825 bmap = isl_basic_map_cow(bmap);
2826 if (!bmap)
2827 goto error;
2828 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2829 bset->n_div, bset->n_eq, bset->n_ineq);
2830 bmap_domain = isl_basic_map_from_domain(bset);
2831 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2833 bmap = isl_basic_map_simplify(bmap);
2834 return isl_basic_map_finalize(bmap);
2835 error:
2836 isl_basic_map_free(bmap);
2837 isl_basic_set_free(bset);
2838 return NULL;
2841 struct isl_basic_map *isl_basic_map_intersect_range(
2842 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2844 struct isl_basic_map *bmap_range;
2846 if (!bmap || !bset)
2847 goto error;
2849 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2850 bset->dim, isl_dim_param), goto error);
2852 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2853 isl_assert(bset->ctx,
2854 isl_basic_map_compatible_range(bmap, bset), goto error);
2856 if (isl_basic_set_plain_is_universe(bset)) {
2857 isl_basic_set_free(bset);
2858 return bmap;
2861 bmap = isl_basic_map_cow(bmap);
2862 if (!bmap)
2863 goto error;
2864 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2865 bset->n_div, bset->n_eq, bset->n_ineq);
2866 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2867 bmap = add_constraints(bmap, bmap_range, 0, 0);
2869 bmap = isl_basic_map_simplify(bmap);
2870 return isl_basic_map_finalize(bmap);
2871 error:
2872 isl_basic_map_free(bmap);
2873 isl_basic_set_free(bset);
2874 return NULL;
2877 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2878 __isl_keep isl_vec *vec)
2880 int i;
2881 unsigned total;
2882 isl_int s;
2884 if (!bmap || !vec)
2885 return isl_bool_error;
2887 total = 1 + isl_basic_map_total_dim(bmap);
2888 if (total != vec->size)
2889 return isl_bool_error;
2891 isl_int_init(s);
2893 for (i = 0; i < bmap->n_eq; ++i) {
2894 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2895 if (!isl_int_is_zero(s)) {
2896 isl_int_clear(s);
2897 return isl_bool_false;
2901 for (i = 0; i < bmap->n_ineq; ++i) {
2902 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2903 if (isl_int_is_neg(s)) {
2904 isl_int_clear(s);
2905 return isl_bool_false;
2909 isl_int_clear(s);
2911 return isl_bool_true;
2914 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2915 __isl_keep isl_vec *vec)
2917 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2920 struct isl_basic_map *isl_basic_map_intersect(
2921 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2923 struct isl_vec *sample = NULL;
2925 if (!bmap1 || !bmap2)
2926 goto error;
2928 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2929 bmap2->dim, isl_dim_param), goto error);
2930 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2931 isl_space_dim(bmap1->dim, isl_dim_param) &&
2932 isl_space_dim(bmap2->dim, isl_dim_all) !=
2933 isl_space_dim(bmap2->dim, isl_dim_param))
2934 return isl_basic_map_intersect(bmap2, bmap1);
2936 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2937 isl_space_dim(bmap2->dim, isl_dim_param))
2938 isl_assert(bmap1->ctx,
2939 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2941 if (isl_basic_map_plain_is_empty(bmap1)) {
2942 isl_basic_map_free(bmap2);
2943 return bmap1;
2945 if (isl_basic_map_plain_is_empty(bmap2)) {
2946 isl_basic_map_free(bmap1);
2947 return bmap2;
2950 if (bmap1->sample &&
2951 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2952 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2953 sample = isl_vec_copy(bmap1->sample);
2954 else if (bmap2->sample &&
2955 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2956 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2957 sample = isl_vec_copy(bmap2->sample);
2959 bmap1 = isl_basic_map_cow(bmap1);
2960 if (!bmap1)
2961 goto error;
2962 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2963 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2964 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2966 if (!bmap1)
2967 isl_vec_free(sample);
2968 else if (sample) {
2969 isl_vec_free(bmap1->sample);
2970 bmap1->sample = sample;
2973 bmap1 = isl_basic_map_simplify(bmap1);
2974 return isl_basic_map_finalize(bmap1);
2975 error:
2976 if (sample)
2977 isl_vec_free(sample);
2978 isl_basic_map_free(bmap1);
2979 isl_basic_map_free(bmap2);
2980 return NULL;
2983 struct isl_basic_set *isl_basic_set_intersect(
2984 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2986 return (struct isl_basic_set *)
2987 isl_basic_map_intersect(
2988 (struct isl_basic_map *)bset1,
2989 (struct isl_basic_map *)bset2);
2992 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2993 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2995 return isl_basic_set_intersect(bset1, bset2);
2998 /* Special case of isl_map_intersect, where both map1 and map2
2999 * are convex, without any divs and such that either map1 or map2
3000 * contains a single constraint. This constraint is then simply
3001 * added to the other map.
3003 static __isl_give isl_map *map_intersect_add_constraint(
3004 __isl_take isl_map *map1, __isl_take isl_map *map2)
3006 isl_assert(map1->ctx, map1->n == 1, goto error);
3007 isl_assert(map2->ctx, map1->n == 1, goto error);
3008 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3009 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3011 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3012 return isl_map_intersect(map2, map1);
3014 isl_assert(map2->ctx,
3015 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3017 map1 = isl_map_cow(map1);
3018 if (!map1)
3019 goto error;
3020 if (isl_map_plain_is_empty(map1)) {
3021 isl_map_free(map2);
3022 return map1;
3024 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3025 if (map2->p[0]->n_eq == 1)
3026 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3027 else
3028 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3029 map2->p[0]->ineq[0]);
3031 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3032 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3033 if (!map1->p[0])
3034 goto error;
3036 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3037 isl_basic_map_free(map1->p[0]);
3038 map1->n = 0;
3041 isl_map_free(map2);
3043 return map1;
3044 error:
3045 isl_map_free(map1);
3046 isl_map_free(map2);
3047 return NULL;
3050 /* map2 may be either a parameter domain or a map living in the same
3051 * space as map1.
3053 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3054 __isl_take isl_map *map2)
3056 unsigned flags = 0;
3057 isl_map *result;
3058 int i, j;
3060 if (!map1 || !map2)
3061 goto error;
3063 if ((isl_map_plain_is_empty(map1) ||
3064 isl_map_plain_is_universe(map2)) &&
3065 isl_space_is_equal(map1->dim, map2->dim)) {
3066 isl_map_free(map2);
3067 return map1;
3069 if ((isl_map_plain_is_empty(map2) ||
3070 isl_map_plain_is_universe(map1)) &&
3071 isl_space_is_equal(map1->dim, map2->dim)) {
3072 isl_map_free(map1);
3073 return map2;
3076 if (map1->n == 1 && map2->n == 1 &&
3077 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3078 isl_space_is_equal(map1->dim, map2->dim) &&
3079 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3080 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3081 return map_intersect_add_constraint(map1, map2);
3083 if (isl_space_dim(map2->dim, isl_dim_all) !=
3084 isl_space_dim(map2->dim, isl_dim_param))
3085 isl_assert(map1->ctx,
3086 isl_space_is_equal(map1->dim, map2->dim), goto error);
3088 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3089 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3090 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3092 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3093 map1->n * map2->n, flags);
3094 if (!result)
3095 goto error;
3096 for (i = 0; i < map1->n; ++i)
3097 for (j = 0; j < map2->n; ++j) {
3098 struct isl_basic_map *part;
3099 part = isl_basic_map_intersect(
3100 isl_basic_map_copy(map1->p[i]),
3101 isl_basic_map_copy(map2->p[j]));
3102 if (isl_basic_map_is_empty(part) < 0)
3103 part = isl_basic_map_free(part);
3104 result = isl_map_add_basic_map(result, part);
3105 if (!result)
3106 goto error;
3108 isl_map_free(map1);
3109 isl_map_free(map2);
3110 return result;
3111 error:
3112 isl_map_free(map1);
3113 isl_map_free(map2);
3114 return NULL;
3117 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3118 __isl_take isl_map *map2)
3120 if (!map1 || !map2)
3121 goto error;
3122 if (!isl_space_is_equal(map1->dim, map2->dim))
3123 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3124 "spaces don't match", goto error);
3125 return map_intersect_internal(map1, map2);
3126 error:
3127 isl_map_free(map1);
3128 isl_map_free(map2);
3129 return NULL;
3132 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3133 __isl_take isl_map *map2)
3135 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3138 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3140 return (struct isl_set *)
3141 isl_map_intersect((struct isl_map *)set1,
3142 (struct isl_map *)set2);
3145 /* map_intersect_internal accepts intersections
3146 * with parameter domains, so we can just call that function.
3148 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3149 __isl_take isl_set *params)
3151 return map_intersect_internal(map, params);
3154 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3155 __isl_take isl_map *map2)
3157 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3160 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3161 __isl_take isl_set *params)
3163 return isl_map_intersect_params(set, params);
3166 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3168 isl_space *space;
3169 unsigned pos, n1, n2;
3171 if (!bmap)
3172 return NULL;
3173 bmap = isl_basic_map_cow(bmap);
3174 if (!bmap)
3175 return NULL;
3176 space = isl_space_reverse(isl_space_copy(bmap->dim));
3177 pos = isl_basic_map_offset(bmap, isl_dim_in);
3178 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3179 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3180 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3181 return isl_basic_map_reset_space(bmap, space);
3184 static __isl_give isl_basic_map *basic_map_space_reset(
3185 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3187 isl_space *space;
3189 if (!bmap)
3190 return NULL;
3191 if (!isl_space_is_named_or_nested(bmap->dim, type))
3192 return bmap;
3194 space = isl_basic_map_get_space(bmap);
3195 space = isl_space_reset(space, type);
3196 bmap = isl_basic_map_reset_space(bmap, space);
3197 return bmap;
3200 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3201 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3202 unsigned pos, unsigned n)
3204 isl_space *res_dim;
3205 struct isl_basic_map *res;
3206 struct isl_dim_map *dim_map;
3207 unsigned total, off;
3208 enum isl_dim_type t;
3210 if (n == 0)
3211 return basic_map_space_reset(bmap, type);
3213 if (!bmap)
3214 return NULL;
3216 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3218 total = isl_basic_map_total_dim(bmap) + n;
3219 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3220 off = 0;
3221 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3222 if (t != type) {
3223 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3224 } else {
3225 unsigned size = isl_basic_map_dim(bmap, t);
3226 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3227 0, pos, off);
3228 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3229 pos, size - pos, off + pos + n);
3231 off += isl_space_dim(res_dim, t);
3233 isl_dim_map_div(dim_map, bmap, off);
3235 res = isl_basic_map_alloc_space(res_dim,
3236 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3237 if (isl_basic_map_is_rational(bmap))
3238 res = isl_basic_map_set_rational(res);
3239 if (isl_basic_map_plain_is_empty(bmap)) {
3240 isl_basic_map_free(bmap);
3241 free(dim_map);
3242 return isl_basic_map_set_to_empty(res);
3244 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3245 return isl_basic_map_finalize(res);
3248 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3249 __isl_take isl_basic_set *bset,
3250 enum isl_dim_type type, unsigned pos, unsigned n)
3252 return isl_basic_map_insert_dims(bset, type, pos, n);
3255 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3256 enum isl_dim_type type, unsigned n)
3258 if (!bmap)
3259 return NULL;
3260 return isl_basic_map_insert_dims(bmap, type,
3261 isl_basic_map_dim(bmap, type), n);
3264 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3265 enum isl_dim_type type, unsigned n)
3267 if (!bset)
3268 return NULL;
3269 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3270 return isl_basic_map_add_dims(bset, type, n);
3271 error:
3272 isl_basic_set_free(bset);
3273 return NULL;
3276 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3277 enum isl_dim_type type)
3279 isl_space *space;
3281 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3282 return map;
3284 space = isl_map_get_space(map);
3285 space = isl_space_reset(space, type);
3286 map = isl_map_reset_space(map, space);
3287 return map;
3290 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3291 enum isl_dim_type type, unsigned pos, unsigned n)
3293 int i;
3295 if (n == 0)
3296 return map_space_reset(map, type);
3298 map = isl_map_cow(map);
3299 if (!map)
3300 return NULL;
3302 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3303 if (!map->dim)
3304 goto error;
3306 for (i = 0; i < map->n; ++i) {
3307 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3308 if (!map->p[i])
3309 goto error;
3312 return map;
3313 error:
3314 isl_map_free(map);
3315 return NULL;
3318 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3319 enum isl_dim_type type, unsigned pos, unsigned n)
3321 return isl_map_insert_dims(set, type, pos, n);
3324 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3325 enum isl_dim_type type, unsigned n)
3327 if (!map)
3328 return NULL;
3329 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3332 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3333 enum isl_dim_type type, unsigned n)
3335 if (!set)
3336 return NULL;
3337 isl_assert(set->ctx, type != isl_dim_in, goto error);
3338 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3339 error:
3340 isl_set_free(set);
3341 return NULL;
3344 __isl_give isl_basic_map *isl_basic_map_move_dims(
3345 __isl_take isl_basic_map *bmap,
3346 enum isl_dim_type dst_type, unsigned dst_pos,
3347 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3349 struct isl_dim_map *dim_map;
3350 struct isl_basic_map *res;
3351 enum isl_dim_type t;
3352 unsigned total, off;
3354 if (!bmap)
3355 return NULL;
3356 if (n == 0)
3357 return bmap;
3359 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3360 goto error);
3362 if (dst_type == src_type && dst_pos == src_pos)
3363 return bmap;
3365 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3367 if (pos(bmap->dim, dst_type) + dst_pos ==
3368 pos(bmap->dim, src_type) + src_pos +
3369 ((src_type < dst_type) ? n : 0)) {
3370 bmap = isl_basic_map_cow(bmap);
3371 if (!bmap)
3372 return NULL;
3374 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3375 src_type, src_pos, n);
3376 if (!bmap->dim)
3377 goto error;
3379 bmap = isl_basic_map_finalize(bmap);
3381 return bmap;
3384 total = isl_basic_map_total_dim(bmap);
3385 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3387 off = 0;
3388 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3389 unsigned size = isl_space_dim(bmap->dim, t);
3390 if (t == dst_type) {
3391 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3392 0, dst_pos, off);
3393 off += dst_pos;
3394 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3395 src_pos, n, off);
3396 off += n;
3397 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3398 dst_pos, size - dst_pos, off);
3399 off += size - dst_pos;
3400 } else if (t == src_type) {
3401 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3402 0, src_pos, off);
3403 off += src_pos;
3404 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3405 src_pos + n, size - src_pos - n, off);
3406 off += size - src_pos - n;
3407 } else {
3408 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3409 off += size;
3412 isl_dim_map_div(dim_map, bmap, off);
3414 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3415 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3416 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3417 if (!bmap)
3418 goto error;
3420 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3421 src_type, src_pos, n);
3422 if (!bmap->dim)
3423 goto error;
3425 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3426 bmap = isl_basic_map_gauss(bmap, NULL);
3427 bmap = isl_basic_map_finalize(bmap);
3429 return bmap;
3430 error:
3431 isl_basic_map_free(bmap);
3432 return NULL;
3435 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3436 enum isl_dim_type dst_type, unsigned dst_pos,
3437 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3439 return (isl_basic_set *)isl_basic_map_move_dims(
3440 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3443 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3444 enum isl_dim_type dst_type, unsigned dst_pos,
3445 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3447 if (!set)
3448 return NULL;
3449 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3450 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3451 src_type, src_pos, n);
3452 error:
3453 isl_set_free(set);
3454 return NULL;
3457 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3458 enum isl_dim_type dst_type, unsigned dst_pos,
3459 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3461 int i;
3463 if (!map)
3464 return NULL;
3465 if (n == 0)
3466 return map;
3468 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3469 goto error);
3471 if (dst_type == src_type && dst_pos == src_pos)
3472 return map;
3474 isl_assert(map->ctx, dst_type != src_type, goto error);
3476 map = isl_map_cow(map);
3477 if (!map)
3478 return NULL;
3480 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3481 if (!map->dim)
3482 goto error;
3484 for (i = 0; i < map->n; ++i) {
3485 map->p[i] = isl_basic_map_move_dims(map->p[i],
3486 dst_type, dst_pos,
3487 src_type, src_pos, n);
3488 if (!map->p[i])
3489 goto error;
3492 return map;
3493 error:
3494 isl_map_free(map);
3495 return NULL;
3498 /* Move the specified dimensions to the last columns right before
3499 * the divs. Don't change the dimension specification of bmap.
3500 * That's the responsibility of the caller.
3502 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3503 enum isl_dim_type type, unsigned first, unsigned n)
3505 struct isl_dim_map *dim_map;
3506 struct isl_basic_map *res;
3507 enum isl_dim_type t;
3508 unsigned total, off;
3510 if (!bmap)
3511 return NULL;
3512 if (pos(bmap->dim, type) + first + n ==
3513 1 + isl_space_dim(bmap->dim, isl_dim_all))
3514 return bmap;
3516 total = isl_basic_map_total_dim(bmap);
3517 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3519 off = 0;
3520 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3521 unsigned size = isl_space_dim(bmap->dim, t);
3522 if (t == type) {
3523 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3524 0, first, off);
3525 off += first;
3526 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3527 first, n, total - bmap->n_div - n);
3528 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3529 first + n, size - (first + n), off);
3530 off += size - (first + n);
3531 } else {
3532 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3533 off += size;
3536 isl_dim_map_div(dim_map, bmap, off + n);
3538 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3539 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3540 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3541 return res;
3544 /* Insert "n" rows in the divs of "bmap".
3546 * The number of columns is not changed, which means that the last
3547 * dimensions of "bmap" are being reintepreted as the new divs.
3548 * The space of "bmap" is not adjusted, however, which means
3549 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3550 * from the space of "bmap" is the responsibility of the caller.
3552 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3553 int n)
3555 int i;
3556 size_t row_size;
3557 isl_int **new_div;
3558 isl_int *old;
3560 bmap = isl_basic_map_cow(bmap);
3561 if (!bmap)
3562 return NULL;
3564 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3565 old = bmap->block2.data;
3566 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3567 (bmap->extra + n) * (1 + row_size));
3568 if (!bmap->block2.data)
3569 return isl_basic_map_free(bmap);
3570 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3571 if (!new_div)
3572 return isl_basic_map_free(bmap);
3573 for (i = 0; i < n; ++i) {
3574 new_div[i] = bmap->block2.data +
3575 (bmap->extra + i) * (1 + row_size);
3576 isl_seq_clr(new_div[i], 1 + row_size);
3578 for (i = 0; i < bmap->extra; ++i)
3579 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3580 free(bmap->div);
3581 bmap->div = new_div;
3582 bmap->n_div += n;
3583 bmap->extra += n;
3585 return bmap;
3588 /* Drop constraints from "bmap" that only involve the variables
3589 * of "type" in the range [first, first + n] that are not related
3590 * to any of the variables outside that interval.
3591 * These constraints cannot influence the values for the variables
3592 * outside the interval, except in case they cause "bmap" to be empty.
3593 * Only drop the constraints if "bmap" is known to be non-empty.
3595 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3596 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3597 unsigned first, unsigned n)
3599 int i;
3600 int *groups;
3601 unsigned dim, n_div;
3602 isl_bool non_empty;
3604 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3605 if (non_empty < 0)
3606 return isl_basic_map_free(bmap);
3607 if (!non_empty)
3608 return bmap;
3610 dim = isl_basic_map_dim(bmap, isl_dim_all);
3611 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3612 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3613 if (!groups)
3614 return isl_basic_map_free(bmap);
3615 first += isl_basic_map_offset(bmap, type) - 1;
3616 for (i = 0; i < first; ++i)
3617 groups[i] = -1;
3618 for (i = first + n; i < dim - n_div; ++i)
3619 groups[i] = -1;
3621 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3623 return bmap;
3626 /* Turn the n dimensions of type type, starting at first
3627 * into existentially quantified variables.
3629 * If a subset of the projected out variables are unrelated
3630 * to any of the variables that remain, then the constraints
3631 * involving this subset are simply dropped first.
3633 __isl_give isl_basic_map *isl_basic_map_project_out(
3634 __isl_take isl_basic_map *bmap,
3635 enum isl_dim_type type, unsigned first, unsigned n)
3637 if (n == 0)
3638 return basic_map_space_reset(bmap, type);
3639 if (type == isl_dim_div)
3640 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3641 "cannot project out existentially quantified variables",
3642 return isl_basic_map_free(bmap));
3644 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3645 if (!bmap)
3646 return NULL;
3648 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3649 return isl_basic_map_remove_dims(bmap, type, first, n);
3651 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3652 goto error);
3654 bmap = move_last(bmap, type, first, n);
3655 bmap = isl_basic_map_cow(bmap);
3656 bmap = insert_div_rows(bmap, n);
3657 if (!bmap)
3658 return NULL;
3660 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3661 if (!bmap->dim)
3662 goto error;
3663 bmap = isl_basic_map_simplify(bmap);
3664 bmap = isl_basic_map_drop_redundant_divs(bmap);
3665 return isl_basic_map_finalize(bmap);
3666 error:
3667 isl_basic_map_free(bmap);
3668 return NULL;
3671 /* Turn the n dimensions of type type, starting at first
3672 * into existentially quantified variables.
3674 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3675 enum isl_dim_type type, unsigned first, unsigned n)
3677 return (isl_basic_set *)isl_basic_map_project_out(
3678 (isl_basic_map *)bset, type, first, n);
3681 /* Turn the n dimensions of type type, starting at first
3682 * into existentially quantified variables.
3684 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3685 enum isl_dim_type type, unsigned first, unsigned n)
3687 int i;
3689 if (!map)
3690 return NULL;
3692 if (n == 0)
3693 return map_space_reset(map, type);
3695 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3697 map = isl_map_cow(map);
3698 if (!map)
3699 return NULL;
3701 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3702 if (!map->dim)
3703 goto error;
3705 for (i = 0; i < map->n; ++i) {
3706 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3707 if (!map->p[i])
3708 goto error;
3711 return map;
3712 error:
3713 isl_map_free(map);
3714 return NULL;
3717 /* Turn the n dimensions of type type, starting at first
3718 * into existentially quantified variables.
3720 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3721 enum isl_dim_type type, unsigned first, unsigned n)
3723 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3726 /* Return a map that projects the elements in "set" onto their
3727 * "n" set dimensions starting at "first".
3728 * "type" should be equal to isl_dim_set.
3730 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3731 enum isl_dim_type type, unsigned first, unsigned n)
3733 int i;
3734 int dim;
3735 isl_map *map;
3737 if (!set)
3738 return NULL;
3739 if (type != isl_dim_set)
3740 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3741 "only set dimensions can be projected out", goto error);
3742 dim = isl_set_dim(set, isl_dim_set);
3743 if (first + n > dim || first + n < first)
3744 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3745 "index out of bounds", goto error);
3747 map = isl_map_from_domain(set);
3748 map = isl_map_add_dims(map, isl_dim_out, n);
3749 for (i = 0; i < n; ++i)
3750 map = isl_map_equate(map, isl_dim_in, first + i,
3751 isl_dim_out, i);
3752 return map;
3753 error:
3754 isl_set_free(set);
3755 return NULL;
3758 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3760 int i, j;
3762 for (i = 0; i < n; ++i) {
3763 j = isl_basic_map_alloc_div(bmap);
3764 if (j < 0)
3765 goto error;
3766 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3768 return bmap;
3769 error:
3770 isl_basic_map_free(bmap);
3771 return NULL;
3774 struct isl_basic_map *isl_basic_map_apply_range(
3775 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3777 isl_space *dim_result = NULL;
3778 struct isl_basic_map *bmap;
3779 unsigned n_in, n_out, n, nparam, total, pos;
3780 struct isl_dim_map *dim_map1, *dim_map2;
3782 if (!bmap1 || !bmap2)
3783 goto error;
3784 if (!isl_space_match(bmap1->dim, isl_dim_param,
3785 bmap2->dim, isl_dim_param))
3786 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3787 "parameters don't match", goto error);
3788 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3789 bmap2->dim, isl_dim_in))
3790 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3791 "spaces don't match", goto error);
3793 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3794 isl_space_copy(bmap2->dim));
3796 n_in = isl_basic_map_n_in(bmap1);
3797 n_out = isl_basic_map_n_out(bmap2);
3798 n = isl_basic_map_n_out(bmap1);
3799 nparam = isl_basic_map_n_param(bmap1);
3801 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3802 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3803 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3804 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3805 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3806 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3807 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3808 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3809 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3810 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3811 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3813 bmap = isl_basic_map_alloc_space(dim_result,
3814 bmap1->n_div + bmap2->n_div + n,
3815 bmap1->n_eq + bmap2->n_eq,
3816 bmap1->n_ineq + bmap2->n_ineq);
3817 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3818 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3819 bmap = add_divs(bmap, n);
3820 bmap = isl_basic_map_simplify(bmap);
3821 bmap = isl_basic_map_drop_redundant_divs(bmap);
3822 return isl_basic_map_finalize(bmap);
3823 error:
3824 isl_basic_map_free(bmap1);
3825 isl_basic_map_free(bmap2);
3826 return NULL;
3829 struct isl_basic_set *isl_basic_set_apply(
3830 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3832 if (!bset || !bmap)
3833 goto error;
3835 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3836 goto error);
3838 return (struct isl_basic_set *)
3839 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3840 error:
3841 isl_basic_set_free(bset);
3842 isl_basic_map_free(bmap);
3843 return NULL;
3846 struct isl_basic_map *isl_basic_map_apply_domain(
3847 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3849 if (!bmap1 || !bmap2)
3850 goto error;
3852 isl_assert(bmap1->ctx,
3853 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3854 isl_assert(bmap1->ctx,
3855 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3856 goto error);
3858 bmap1 = isl_basic_map_reverse(bmap1);
3859 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3860 return isl_basic_map_reverse(bmap1);
3861 error:
3862 isl_basic_map_free(bmap1);
3863 isl_basic_map_free(bmap2);
3864 return NULL;
3867 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3868 * A \cap B -> f(A) + f(B)
3870 struct isl_basic_map *isl_basic_map_sum(
3871 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3873 unsigned n_in, n_out, nparam, total, pos;
3874 struct isl_basic_map *bmap = NULL;
3875 struct isl_dim_map *dim_map1, *dim_map2;
3876 int i;
3878 if (!bmap1 || !bmap2)
3879 goto error;
3881 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3882 goto error);
3884 nparam = isl_basic_map_n_param(bmap1);
3885 n_in = isl_basic_map_n_in(bmap1);
3886 n_out = isl_basic_map_n_out(bmap1);
3888 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3889 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3890 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3891 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3892 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3893 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3894 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3895 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3896 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3897 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3898 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3900 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3901 bmap1->n_div + bmap2->n_div + 2 * n_out,
3902 bmap1->n_eq + bmap2->n_eq + n_out,
3903 bmap1->n_ineq + bmap2->n_ineq);
3904 for (i = 0; i < n_out; ++i) {
3905 int j = isl_basic_map_alloc_equality(bmap);
3906 if (j < 0)
3907 goto error;
3908 isl_seq_clr(bmap->eq[j], 1+total);
3909 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3910 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3911 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3913 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3914 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3915 bmap = add_divs(bmap, 2 * n_out);
3917 bmap = isl_basic_map_simplify(bmap);
3918 return isl_basic_map_finalize(bmap);
3919 error:
3920 isl_basic_map_free(bmap);
3921 isl_basic_map_free(bmap1);
3922 isl_basic_map_free(bmap2);
3923 return NULL;
3926 /* Given two maps A -> f(A) and B -> g(B), construct a map
3927 * A \cap B -> f(A) + f(B)
3929 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3931 struct isl_map *result;
3932 int i, j;
3934 if (!map1 || !map2)
3935 goto error;
3937 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3939 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3940 map1->n * map2->n, 0);
3941 if (!result)
3942 goto error;
3943 for (i = 0; i < map1->n; ++i)
3944 for (j = 0; j < map2->n; ++j) {
3945 struct isl_basic_map *part;
3946 part = isl_basic_map_sum(
3947 isl_basic_map_copy(map1->p[i]),
3948 isl_basic_map_copy(map2->p[j]));
3949 if (isl_basic_map_is_empty(part))
3950 isl_basic_map_free(part);
3951 else
3952 result = isl_map_add_basic_map(result, part);
3953 if (!result)
3954 goto error;
3956 isl_map_free(map1);
3957 isl_map_free(map2);
3958 return result;
3959 error:
3960 isl_map_free(map1);
3961 isl_map_free(map2);
3962 return NULL;
3965 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3966 __isl_take isl_set *set2)
3968 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3971 /* Given a basic map A -> f(A), construct A -> -f(A).
3973 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3975 int i, j;
3976 unsigned off, n;
3978 bmap = isl_basic_map_cow(bmap);
3979 if (!bmap)
3980 return NULL;
3982 n = isl_basic_map_dim(bmap, isl_dim_out);
3983 off = isl_basic_map_offset(bmap, isl_dim_out);
3984 for (i = 0; i < bmap->n_eq; ++i)
3985 for (j = 0; j < n; ++j)
3986 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3987 for (i = 0; i < bmap->n_ineq; ++i)
3988 for (j = 0; j < n; ++j)
3989 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3990 for (i = 0; i < bmap->n_div; ++i)
3991 for (j = 0; j < n; ++j)
3992 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3993 bmap = isl_basic_map_gauss(bmap, NULL);
3994 return isl_basic_map_finalize(bmap);
3997 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3999 return isl_basic_map_neg(bset);
4002 /* Given a map A -> f(A), construct A -> -f(A).
4004 struct isl_map *isl_map_neg(struct isl_map *map)
4006 int i;
4008 map = isl_map_cow(map);
4009 if (!map)
4010 return NULL;
4012 for (i = 0; i < map->n; ++i) {
4013 map->p[i] = isl_basic_map_neg(map->p[i]);
4014 if (!map->p[i])
4015 goto error;
4018 return map;
4019 error:
4020 isl_map_free(map);
4021 return NULL;
4024 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4026 return (isl_set *)isl_map_neg((isl_map *)set);
4029 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4030 * A -> floor(f(A)/d).
4032 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4033 isl_int d)
4035 unsigned n_in, n_out, nparam, total, pos;
4036 struct isl_basic_map *result = NULL;
4037 struct isl_dim_map *dim_map;
4038 int i;
4040 if (!bmap)
4041 return NULL;
4043 nparam = isl_basic_map_n_param(bmap);
4044 n_in = isl_basic_map_n_in(bmap);
4045 n_out = isl_basic_map_n_out(bmap);
4047 total = nparam + n_in + n_out + bmap->n_div + n_out;
4048 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4049 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4050 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4051 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4052 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4054 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4055 bmap->n_div + n_out,
4056 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4057 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4058 result = add_divs(result, n_out);
4059 for (i = 0; i < n_out; ++i) {
4060 int j;
4061 j = isl_basic_map_alloc_inequality(result);
4062 if (j < 0)
4063 goto error;
4064 isl_seq_clr(result->ineq[j], 1+total);
4065 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
4066 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4067 j = isl_basic_map_alloc_inequality(result);
4068 if (j < 0)
4069 goto error;
4070 isl_seq_clr(result->ineq[j], 1+total);
4071 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4072 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4073 isl_int_sub_ui(result->ineq[j][0], d, 1);
4076 result = isl_basic_map_simplify(result);
4077 return isl_basic_map_finalize(result);
4078 error:
4079 isl_basic_map_free(result);
4080 return NULL;
4083 /* Given a map A -> f(A) and an integer d, construct a map
4084 * A -> floor(f(A)/d).
4086 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4088 int i;
4090 map = isl_map_cow(map);
4091 if (!map)
4092 return NULL;
4094 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4095 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4096 for (i = 0; i < map->n; ++i) {
4097 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4098 if (!map->p[i])
4099 goto error;
4102 return map;
4103 error:
4104 isl_map_free(map);
4105 return NULL;
4108 /* Given a map A -> f(A) and an integer d, construct a map
4109 * A -> floor(f(A)/d).
4111 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4112 __isl_take isl_val *d)
4114 if (!map || !d)
4115 goto error;
4116 if (!isl_val_is_int(d))
4117 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4118 "expecting integer denominator", goto error);
4119 map = isl_map_floordiv(map, d->n);
4120 isl_val_free(d);
4121 return map;
4122 error:
4123 isl_map_free(map);
4124 isl_val_free(d);
4125 return NULL;
4128 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4130 int i;
4131 unsigned nparam;
4132 unsigned n_in;
4134 i = isl_basic_map_alloc_equality(bmap);
4135 if (i < 0)
4136 goto error;
4137 nparam = isl_basic_map_n_param(bmap);
4138 n_in = isl_basic_map_n_in(bmap);
4139 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4140 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4141 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4142 return isl_basic_map_finalize(bmap);
4143 error:
4144 isl_basic_map_free(bmap);
4145 return NULL;
4148 /* Add a constraints to "bmap" expressing i_pos < o_pos
4150 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4152 int i;
4153 unsigned nparam;
4154 unsigned n_in;
4156 i = isl_basic_map_alloc_inequality(bmap);
4157 if (i < 0)
4158 goto error;
4159 nparam = isl_basic_map_n_param(bmap);
4160 n_in = isl_basic_map_n_in(bmap);
4161 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4162 isl_int_set_si(bmap->ineq[i][0], -1);
4163 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4164 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4165 return isl_basic_map_finalize(bmap);
4166 error:
4167 isl_basic_map_free(bmap);
4168 return NULL;
4171 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4173 static __isl_give isl_basic_map *var_less_or_equal(
4174 __isl_take isl_basic_map *bmap, unsigned pos)
4176 int i;
4177 unsigned nparam;
4178 unsigned n_in;
4180 i = isl_basic_map_alloc_inequality(bmap);
4181 if (i < 0)
4182 goto error;
4183 nparam = isl_basic_map_n_param(bmap);
4184 n_in = isl_basic_map_n_in(bmap);
4185 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4186 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4187 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4188 return isl_basic_map_finalize(bmap);
4189 error:
4190 isl_basic_map_free(bmap);
4191 return NULL;
4194 /* Add a constraints to "bmap" expressing i_pos > o_pos
4196 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4198 int i;
4199 unsigned nparam;
4200 unsigned n_in;
4202 i = isl_basic_map_alloc_inequality(bmap);
4203 if (i < 0)
4204 goto error;
4205 nparam = isl_basic_map_n_param(bmap);
4206 n_in = isl_basic_map_n_in(bmap);
4207 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4208 isl_int_set_si(bmap->ineq[i][0], -1);
4209 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4210 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4211 return isl_basic_map_finalize(bmap);
4212 error:
4213 isl_basic_map_free(bmap);
4214 return NULL;
4217 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4219 static __isl_give isl_basic_map *var_more_or_equal(
4220 __isl_take isl_basic_map *bmap, unsigned pos)
4222 int i;
4223 unsigned nparam;
4224 unsigned n_in;
4226 i = isl_basic_map_alloc_inequality(bmap);
4227 if (i < 0)
4228 goto error;
4229 nparam = isl_basic_map_n_param(bmap);
4230 n_in = isl_basic_map_n_in(bmap);
4231 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4232 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4233 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4234 return isl_basic_map_finalize(bmap);
4235 error:
4236 isl_basic_map_free(bmap);
4237 return NULL;
4240 __isl_give isl_basic_map *isl_basic_map_equal(
4241 __isl_take isl_space *dim, unsigned n_equal)
4243 int i;
4244 struct isl_basic_map *bmap;
4245 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4246 if (!bmap)
4247 return NULL;
4248 for (i = 0; i < n_equal && bmap; ++i)
4249 bmap = var_equal(bmap, i);
4250 return isl_basic_map_finalize(bmap);
4253 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4255 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4256 unsigned pos)
4258 int i;
4259 struct isl_basic_map *bmap;
4260 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4261 if (!bmap)
4262 return NULL;
4263 for (i = 0; i < pos && bmap; ++i)
4264 bmap = var_equal(bmap, i);
4265 if (bmap)
4266 bmap = var_less(bmap, pos);
4267 return isl_basic_map_finalize(bmap);
4270 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4272 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4273 __isl_take isl_space *dim, unsigned pos)
4275 int i;
4276 isl_basic_map *bmap;
4278 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4279 for (i = 0; i < pos; ++i)
4280 bmap = var_equal(bmap, i);
4281 bmap = var_less_or_equal(bmap, pos);
4282 return isl_basic_map_finalize(bmap);
4285 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4287 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4288 unsigned pos)
4290 int i;
4291 struct isl_basic_map *bmap;
4292 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4293 if (!bmap)
4294 return NULL;
4295 for (i = 0; i < pos && bmap; ++i)
4296 bmap = var_equal(bmap, i);
4297 if (bmap)
4298 bmap = var_more(bmap, pos);
4299 return isl_basic_map_finalize(bmap);
4302 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4304 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4305 __isl_take isl_space *dim, unsigned pos)
4307 int i;
4308 isl_basic_map *bmap;
4310 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4311 for (i = 0; i < pos; ++i)
4312 bmap = var_equal(bmap, i);
4313 bmap = var_more_or_equal(bmap, pos);
4314 return isl_basic_map_finalize(bmap);
4317 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4318 unsigned n, int equal)
4320 struct isl_map *map;
4321 int i;
4323 if (n == 0 && equal)
4324 return isl_map_universe(dims);
4326 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4328 for (i = 0; i + 1 < n; ++i)
4329 map = isl_map_add_basic_map(map,
4330 isl_basic_map_less_at(isl_space_copy(dims), i));
4331 if (n > 0) {
4332 if (equal)
4333 map = isl_map_add_basic_map(map,
4334 isl_basic_map_less_or_equal_at(dims, n - 1));
4335 else
4336 map = isl_map_add_basic_map(map,
4337 isl_basic_map_less_at(dims, n - 1));
4338 } else
4339 isl_space_free(dims);
4341 return map;
4344 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4346 if (!dims)
4347 return NULL;
4348 return map_lex_lte_first(dims, dims->n_out, equal);
4351 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4353 return map_lex_lte_first(dim, n, 0);
4356 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4358 return map_lex_lte_first(dim, n, 1);
4361 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4363 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4366 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4368 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4371 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4372 unsigned n, int equal)
4374 struct isl_map *map;
4375 int i;
4377 if (n == 0 && equal)
4378 return isl_map_universe(dims);
4380 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4382 for (i = 0; i + 1 < n; ++i)
4383 map = isl_map_add_basic_map(map,
4384 isl_basic_map_more_at(isl_space_copy(dims), i));
4385 if (n > 0) {
4386 if (equal)
4387 map = isl_map_add_basic_map(map,
4388 isl_basic_map_more_or_equal_at(dims, n - 1));
4389 else
4390 map = isl_map_add_basic_map(map,
4391 isl_basic_map_more_at(dims, n - 1));
4392 } else
4393 isl_space_free(dims);
4395 return map;
4398 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4400 if (!dims)
4401 return NULL;
4402 return map_lex_gte_first(dims, dims->n_out, equal);
4405 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4407 return map_lex_gte_first(dim, n, 0);
4410 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4412 return map_lex_gte_first(dim, n, 1);
4415 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4417 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4420 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4422 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4425 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4426 __isl_take isl_set *set2)
4428 isl_map *map;
4429 map = isl_map_lex_le(isl_set_get_space(set1));
4430 map = isl_map_intersect_domain(map, set1);
4431 map = isl_map_intersect_range(map, set2);
4432 return map;
4435 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4436 __isl_take isl_set *set2)
4438 isl_map *map;
4439 map = isl_map_lex_lt(isl_set_get_space(set1));
4440 map = isl_map_intersect_domain(map, set1);
4441 map = isl_map_intersect_range(map, set2);
4442 return map;
4445 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4446 __isl_take isl_set *set2)
4448 isl_map *map;
4449 map = isl_map_lex_ge(isl_set_get_space(set1));
4450 map = isl_map_intersect_domain(map, set1);
4451 map = isl_map_intersect_range(map, set2);
4452 return map;
4455 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4456 __isl_take isl_set *set2)
4458 isl_map *map;
4459 map = isl_map_lex_gt(isl_set_get_space(set1));
4460 map = isl_map_intersect_domain(map, set1);
4461 map = isl_map_intersect_range(map, set2);
4462 return map;
4465 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4466 __isl_take isl_map *map2)
4468 isl_map *map;
4469 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4470 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4471 map = isl_map_apply_range(map, isl_map_reverse(map2));
4472 return map;
4475 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4476 __isl_take isl_map *map2)
4478 isl_map *map;
4479 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4480 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4481 map = isl_map_apply_range(map, isl_map_reverse(map2));
4482 return map;
4485 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4486 __isl_take isl_map *map2)
4488 isl_map *map;
4489 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4490 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4491 map = isl_map_apply_range(map, isl_map_reverse(map2));
4492 return map;
4495 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4496 __isl_take isl_map *map2)
4498 isl_map *map;
4499 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4500 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4501 map = isl_map_apply_range(map, isl_map_reverse(map2));
4502 return map;
4505 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4506 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4508 struct isl_basic_map *bmap;
4510 bset = isl_basic_set_cow(bset);
4511 if (!bset || !dim)
4512 goto error;
4514 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4515 isl_space_free(bset->dim);
4516 bmap = (struct isl_basic_map *) bset;
4517 bmap->dim = dim;
4518 return isl_basic_map_finalize(bmap);
4519 error:
4520 isl_basic_set_free(bset);
4521 isl_space_free(dim);
4522 return NULL;
4525 /* For a div d = floor(f/m), add the constraint
4527 * f - m d >= 0
4529 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4530 unsigned pos, isl_int *div)
4532 int i;
4533 unsigned total = isl_basic_map_total_dim(bmap);
4535 i = isl_basic_map_alloc_inequality(bmap);
4536 if (i < 0)
4537 return -1;
4538 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4539 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4541 return 0;
4544 /* For a div d = floor(f/m), add the constraint
4546 * -(f-(m-1)) + m d >= 0
4548 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4549 unsigned pos, isl_int *div)
4551 int i;
4552 unsigned total = isl_basic_map_total_dim(bmap);
4554 i = isl_basic_map_alloc_inequality(bmap);
4555 if (i < 0)
4556 return -1;
4557 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4558 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4559 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4560 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4562 return 0;
4565 /* For a div d = floor(f/m), add the constraints
4567 * f - m d >= 0
4568 * -(f-(m-1)) + m d >= 0
4570 * Note that the second constraint is the negation of
4572 * f - m d >= m
4574 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4575 unsigned pos, isl_int *div)
4577 if (add_upper_div_constraint(bmap, pos, div) < 0)
4578 return -1;
4579 if (add_lower_div_constraint(bmap, pos, div) < 0)
4580 return -1;
4581 return 0;
4584 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4585 unsigned pos, isl_int *div)
4587 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4588 pos, div);
4591 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4593 unsigned total = isl_basic_map_total_dim(bmap);
4594 unsigned div_pos = total - bmap->n_div + div;
4596 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4597 bmap->div[div]);
4600 /* For each known div d = floor(f/m), add the constraints
4602 * f - m d >= 0
4603 * -(f-(m-1)) + m d >= 0
4605 * Remove duplicate constraints in case of some these div constraints
4606 * already appear in "bmap".
4608 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4609 __isl_take isl_basic_map *bmap)
4611 unsigned n_div;
4613 if (!bmap)
4614 return NULL;
4615 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4616 if (n_div == 0)
4617 return bmap;
4619 bmap = add_known_div_constraints(bmap);
4620 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4621 bmap = isl_basic_map_finalize(bmap);
4622 return bmap;
4625 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4627 * In particular, if this div is of the form d = floor(f/m),
4628 * then add the constraint
4630 * f - m d >= 0
4632 * if sign < 0 or the constraint
4634 * -(f-(m-1)) + m d >= 0
4636 * if sign > 0.
4638 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4639 unsigned div, int sign)
4641 unsigned total;
4642 unsigned div_pos;
4644 if (!bmap)
4645 return -1;
4647 total = isl_basic_map_total_dim(bmap);
4648 div_pos = total - bmap->n_div + div;
4650 if (sign < 0)
4651 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4652 else
4653 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4656 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4658 return isl_basic_map_add_div_constraints(bset, div);
4661 struct isl_basic_set *isl_basic_map_underlying_set(
4662 struct isl_basic_map *bmap)
4664 if (!bmap)
4665 goto error;
4666 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4667 bmap->n_div == 0 &&
4668 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4669 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4670 return (struct isl_basic_set *)bmap;
4671 bmap = isl_basic_map_cow(bmap);
4672 if (!bmap)
4673 goto error;
4674 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4675 if (!bmap->dim)
4676 goto error;
4677 bmap->extra -= bmap->n_div;
4678 bmap->n_div = 0;
4679 bmap = isl_basic_map_finalize(bmap);
4680 return (struct isl_basic_set *)bmap;
4681 error:
4682 isl_basic_map_free(bmap);
4683 return NULL;
4686 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4687 __isl_take isl_basic_set *bset)
4689 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4692 /* Replace each element in "list" by the result of applying
4693 * isl_basic_map_underlying_set to the element.
4695 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4696 __isl_take isl_basic_map_list *list)
4698 int i, n;
4700 if (!list)
4701 return NULL;
4703 n = isl_basic_map_list_n_basic_map(list);
4704 for (i = 0; i < n; ++i) {
4705 isl_basic_map *bmap;
4706 isl_basic_set *bset;
4708 bmap = isl_basic_map_list_get_basic_map(list, i);
4709 bset = isl_basic_set_underlying_set(bmap);
4710 list = isl_basic_set_list_set_basic_set(list, i, bset);
4713 return list;
4716 struct isl_basic_map *isl_basic_map_overlying_set(
4717 struct isl_basic_set *bset, struct isl_basic_map *like)
4719 struct isl_basic_map *bmap;
4720 struct isl_ctx *ctx;
4721 unsigned total;
4722 int i;
4724 if (!bset || !like)
4725 goto error;
4726 ctx = bset->ctx;
4727 isl_assert(ctx, bset->n_div == 0, goto error);
4728 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4729 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4730 goto error);
4731 if (like->n_div == 0) {
4732 isl_space *space = isl_basic_map_get_space(like);
4733 isl_basic_map_free(like);
4734 return isl_basic_map_reset_space(bset, space);
4736 bset = isl_basic_set_cow(bset);
4737 if (!bset)
4738 goto error;
4739 total = bset->dim->n_out + bset->extra;
4740 bmap = (struct isl_basic_map *)bset;
4741 isl_space_free(bmap->dim);
4742 bmap->dim = isl_space_copy(like->dim);
4743 if (!bmap->dim)
4744 goto error;
4745 bmap->n_div = like->n_div;
4746 bmap->extra += like->n_div;
4747 if (bmap->extra) {
4748 unsigned ltotal;
4749 isl_int **div;
4750 ltotal = total - bmap->extra + like->extra;
4751 if (ltotal > total)
4752 ltotal = total;
4753 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4754 bmap->extra * (1 + 1 + total));
4755 if (isl_blk_is_error(bmap->block2))
4756 goto error;
4757 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4758 if (!div)
4759 goto error;
4760 bmap->div = div;
4761 for (i = 0; i < bmap->extra; ++i)
4762 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4763 for (i = 0; i < like->n_div; ++i) {
4764 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4765 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4767 bmap = isl_basic_map_add_known_div_constraints(bmap);
4769 isl_basic_map_free(like);
4770 bmap = isl_basic_map_simplify(bmap);
4771 bmap = isl_basic_map_finalize(bmap);
4772 return bmap;
4773 error:
4774 isl_basic_map_free(like);
4775 isl_basic_set_free(bset);
4776 return NULL;
4779 struct isl_basic_set *isl_basic_set_from_underlying_set(
4780 struct isl_basic_set *bset, struct isl_basic_set *like)
4782 return (struct isl_basic_set *)
4783 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4786 struct isl_set *isl_set_from_underlying_set(
4787 struct isl_set *set, struct isl_basic_set *like)
4789 int i;
4791 if (!set || !like)
4792 goto error;
4793 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4794 goto error);
4795 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4796 isl_basic_set_free(like);
4797 return set;
4799 set = isl_set_cow(set);
4800 if (!set)
4801 goto error;
4802 for (i = 0; i < set->n; ++i) {
4803 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4804 isl_basic_set_copy(like));
4805 if (!set->p[i])
4806 goto error;
4808 isl_space_free(set->dim);
4809 set->dim = isl_space_copy(like->dim);
4810 if (!set->dim)
4811 goto error;
4812 isl_basic_set_free(like);
4813 return set;
4814 error:
4815 isl_basic_set_free(like);
4816 isl_set_free(set);
4817 return NULL;
4820 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4822 int i;
4824 map = isl_map_cow(map);
4825 if (!map)
4826 return NULL;
4827 map->dim = isl_space_cow(map->dim);
4828 if (!map->dim)
4829 goto error;
4831 for (i = 1; i < map->n; ++i)
4832 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4833 goto error);
4834 for (i = 0; i < map->n; ++i) {
4835 map->p[i] = (struct isl_basic_map *)
4836 isl_basic_map_underlying_set(map->p[i]);
4837 if (!map->p[i])
4838 goto error;
4840 if (map->n == 0)
4841 map->dim = isl_space_underlying(map->dim, 0);
4842 else {
4843 isl_space_free(map->dim);
4844 map->dim = isl_space_copy(map->p[0]->dim);
4846 if (!map->dim)
4847 goto error;
4848 return (struct isl_set *)map;
4849 error:
4850 isl_map_free(map);
4851 return NULL;
4854 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4856 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4859 /* Replace the space of "bmap" by "space".
4861 * If the space of "bmap" is identical to "space" (including the identifiers
4862 * of the input and output dimensions), then simply return the original input.
4864 __isl_give isl_basic_map *isl_basic_map_reset_space(
4865 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4867 isl_bool equal;
4869 if (!bmap)
4870 goto error;
4871 equal = isl_space_is_equal(bmap->dim, space);
4872 if (equal >= 0 && equal)
4873 equal = isl_space_match(bmap->dim, isl_dim_in,
4874 space, isl_dim_in);
4875 if (equal >= 0 && equal)
4876 equal = isl_space_match(bmap->dim, isl_dim_out,
4877 space, isl_dim_out);
4878 if (equal < 0)
4879 goto error;
4880 if (equal) {
4881 isl_space_free(space);
4882 return bmap;
4884 bmap = isl_basic_map_cow(bmap);
4885 if (!bmap || !space)
4886 goto error;
4888 isl_space_free(bmap->dim);
4889 bmap->dim = space;
4891 bmap = isl_basic_map_finalize(bmap);
4893 return bmap;
4894 error:
4895 isl_basic_map_free(bmap);
4896 isl_space_free(space);
4897 return NULL;
4900 __isl_give isl_basic_set *isl_basic_set_reset_space(
4901 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4903 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4904 dim);
4907 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4908 __isl_take isl_space *dim)
4910 int i;
4912 map = isl_map_cow(map);
4913 if (!map || !dim)
4914 goto error;
4916 for (i = 0; i < map->n; ++i) {
4917 map->p[i] = isl_basic_map_reset_space(map->p[i],
4918 isl_space_copy(dim));
4919 if (!map->p[i])
4920 goto error;
4922 isl_space_free(map->dim);
4923 map->dim = dim;
4925 return map;
4926 error:
4927 isl_map_free(map);
4928 isl_space_free(dim);
4929 return NULL;
4932 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4933 __isl_take isl_space *dim)
4935 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4938 /* Compute the parameter domain of the given basic set.
4940 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4942 isl_space *space;
4943 unsigned n;
4945 if (isl_basic_set_is_params(bset))
4946 return bset;
4948 n = isl_basic_set_dim(bset, isl_dim_set);
4949 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4950 space = isl_basic_set_get_space(bset);
4951 space = isl_space_params(space);
4952 bset = isl_basic_set_reset_space(bset, space);
4953 return bset;
4956 /* Construct a zero-dimensional basic set with the given parameter domain.
4958 __isl_give isl_basic_set *isl_basic_set_from_params(
4959 __isl_take isl_basic_set *bset)
4961 isl_space *space;
4962 space = isl_basic_set_get_space(bset);
4963 space = isl_space_set_from_params(space);
4964 bset = isl_basic_set_reset_space(bset, space);
4965 return bset;
4968 /* Compute the parameter domain of the given set.
4970 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4972 isl_space *space;
4973 unsigned n;
4975 if (isl_set_is_params(set))
4976 return set;
4978 n = isl_set_dim(set, isl_dim_set);
4979 set = isl_set_project_out(set, isl_dim_set, 0, n);
4980 space = isl_set_get_space(set);
4981 space = isl_space_params(space);
4982 set = isl_set_reset_space(set, space);
4983 return set;
4986 /* Construct a zero-dimensional set with the given parameter domain.
4988 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4990 isl_space *space;
4991 space = isl_set_get_space(set);
4992 space = isl_space_set_from_params(space);
4993 set = isl_set_reset_space(set, space);
4994 return set;
4997 /* Compute the parameter domain of the given map.
4999 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5001 isl_space *space;
5002 unsigned n;
5004 n = isl_map_dim(map, isl_dim_in);
5005 map = isl_map_project_out(map, isl_dim_in, 0, n);
5006 n = isl_map_dim(map, isl_dim_out);
5007 map = isl_map_project_out(map, isl_dim_out, 0, n);
5008 space = isl_map_get_space(map);
5009 space = isl_space_params(space);
5010 map = isl_map_reset_space(map, space);
5011 return map;
5014 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5016 isl_space *space;
5017 unsigned n_out;
5019 if (!bmap)
5020 return NULL;
5021 space = isl_space_domain(isl_basic_map_get_space(bmap));
5023 n_out = isl_basic_map_n_out(bmap);
5024 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5026 return isl_basic_map_reset_space(bmap, space);
5029 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5031 if (!bmap)
5032 return -1;
5033 return isl_space_may_be_set(bmap->dim);
5036 /* Is this basic map actually a set?
5037 * Users should never call this function. Outside of isl,
5038 * the type should indicate whether something is a set or a map.
5040 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5042 if (!bmap)
5043 return -1;
5044 return isl_space_is_set(bmap->dim);
5047 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5049 if (!bmap)
5050 return NULL;
5051 if (isl_basic_map_is_set(bmap))
5052 return bmap;
5053 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5056 __isl_give isl_basic_map *isl_basic_map_domain_map(
5057 __isl_take isl_basic_map *bmap)
5059 int i, k;
5060 isl_space *dim;
5061 isl_basic_map *domain;
5062 int nparam, n_in, n_out;
5063 unsigned total;
5065 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5066 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5067 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5069 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5070 domain = isl_basic_map_universe(dim);
5072 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5073 bmap = isl_basic_map_apply_range(bmap, domain);
5074 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5076 total = isl_basic_map_total_dim(bmap);
5078 for (i = 0; i < n_in; ++i) {
5079 k = isl_basic_map_alloc_equality(bmap);
5080 if (k < 0)
5081 goto error;
5082 isl_seq_clr(bmap->eq[k], 1 + total);
5083 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5084 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5087 bmap = isl_basic_map_gauss(bmap, NULL);
5088 return isl_basic_map_finalize(bmap);
5089 error:
5090 isl_basic_map_free(bmap);
5091 return NULL;
5094 __isl_give isl_basic_map *isl_basic_map_range_map(
5095 __isl_take isl_basic_map *bmap)
5097 int i, k;
5098 isl_space *dim;
5099 isl_basic_map *range;
5100 int nparam, n_in, n_out;
5101 unsigned total;
5103 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5104 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5105 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5107 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5108 range = isl_basic_map_universe(dim);
5110 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5111 bmap = isl_basic_map_apply_range(bmap, range);
5112 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5114 total = isl_basic_map_total_dim(bmap);
5116 for (i = 0; i < n_out; ++i) {
5117 k = isl_basic_map_alloc_equality(bmap);
5118 if (k < 0)
5119 goto error;
5120 isl_seq_clr(bmap->eq[k], 1 + total);
5121 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5122 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5125 bmap = isl_basic_map_gauss(bmap, NULL);
5126 return isl_basic_map_finalize(bmap);
5127 error:
5128 isl_basic_map_free(bmap);
5129 return NULL;
5132 int isl_map_may_be_set(__isl_keep isl_map *map)
5134 if (!map)
5135 return -1;
5136 return isl_space_may_be_set(map->dim);
5139 /* Is this map actually a set?
5140 * Users should never call this function. Outside of isl,
5141 * the type should indicate whether something is a set or a map.
5143 int isl_map_is_set(__isl_keep isl_map *map)
5145 if (!map)
5146 return -1;
5147 return isl_space_is_set(map->dim);
5150 struct isl_set *isl_map_range(struct isl_map *map)
5152 int i;
5153 struct isl_set *set;
5155 if (!map)
5156 goto error;
5157 if (isl_map_is_set(map))
5158 return (isl_set *)map;
5160 map = isl_map_cow(map);
5161 if (!map)
5162 goto error;
5164 set = (struct isl_set *) map;
5165 set->dim = isl_space_range(set->dim);
5166 if (!set->dim)
5167 goto error;
5168 for (i = 0; i < map->n; ++i) {
5169 set->p[i] = isl_basic_map_range(map->p[i]);
5170 if (!set->p[i])
5171 goto error;
5173 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5174 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5175 return set;
5176 error:
5177 isl_map_free(map);
5178 return NULL;
5181 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5183 int i;
5185 map = isl_map_cow(map);
5186 if (!map)
5187 return NULL;
5189 map->dim = isl_space_domain_map(map->dim);
5190 if (!map->dim)
5191 goto error;
5192 for (i = 0; i < map->n; ++i) {
5193 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5194 if (!map->p[i])
5195 goto error;
5197 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5198 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5199 return map;
5200 error:
5201 isl_map_free(map);
5202 return NULL;
5205 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5207 int i;
5208 isl_space *range_dim;
5210 map = isl_map_cow(map);
5211 if (!map)
5212 return NULL;
5214 range_dim = isl_space_range(isl_map_get_space(map));
5215 range_dim = isl_space_from_range(range_dim);
5216 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5217 map->dim = isl_space_join(map->dim, range_dim);
5218 if (!map->dim)
5219 goto error;
5220 for (i = 0; i < map->n; ++i) {
5221 map->p[i] = isl_basic_map_range_map(map->p[i]);
5222 if (!map->p[i])
5223 goto error;
5225 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5226 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5227 return map;
5228 error:
5229 isl_map_free(map);
5230 return NULL;
5233 /* Given a wrapped map of the form A[B -> C],
5234 * return the map A[B -> C] -> B.
5236 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5238 isl_id *id;
5239 isl_map *map;
5241 if (!set)
5242 return NULL;
5243 if (!isl_set_has_tuple_id(set))
5244 return isl_map_domain_map(isl_set_unwrap(set));
5246 id = isl_set_get_tuple_id(set);
5247 map = isl_map_domain_map(isl_set_unwrap(set));
5248 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5250 return map;
5253 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5254 __isl_take isl_space *dim)
5256 int i;
5257 struct isl_map *map = NULL;
5259 set = isl_set_cow(set);
5260 if (!set || !dim)
5261 goto error;
5262 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5263 map = (struct isl_map *)set;
5264 for (i = 0; i < set->n; ++i) {
5265 map->p[i] = isl_basic_map_from_basic_set(
5266 set->p[i], isl_space_copy(dim));
5267 if (!map->p[i])
5268 goto error;
5270 isl_space_free(map->dim);
5271 map->dim = dim;
5272 return map;
5273 error:
5274 isl_space_free(dim);
5275 isl_set_free(set);
5276 return NULL;
5279 __isl_give isl_basic_map *isl_basic_map_from_domain(
5280 __isl_take isl_basic_set *bset)
5282 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5285 __isl_give isl_basic_map *isl_basic_map_from_range(
5286 __isl_take isl_basic_set *bset)
5288 isl_space *space;
5289 space = isl_basic_set_get_space(bset);
5290 space = isl_space_from_range(space);
5291 bset = isl_basic_set_reset_space(bset, space);
5292 return (isl_basic_map *)bset;
5295 /* Create a relation with the given set as range.
5296 * The domain of the created relation is a zero-dimensional
5297 * flat anonymous space.
5299 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5301 isl_space *space;
5302 space = isl_set_get_space(set);
5303 space = isl_space_from_range(space);
5304 set = isl_set_reset_space(set, space);
5305 return (struct isl_map *)set;
5308 /* Create a relation with the given set as domain.
5309 * The range of the created relation is a zero-dimensional
5310 * flat anonymous space.
5312 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5314 return isl_map_reverse(isl_map_from_range(set));
5317 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5318 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5320 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5323 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5324 __isl_take isl_set *range)
5326 return isl_map_apply_range(isl_map_reverse(domain), range);
5329 /* Return a newly allocated isl_map with given space and flags and
5330 * room for "n" basic maps.
5331 * Make sure that all cached information is cleared.
5333 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5334 unsigned flags)
5336 struct isl_map *map;
5338 if (!space)
5339 return NULL;
5340 if (n < 0)
5341 isl_die(space->ctx, isl_error_internal,
5342 "negative number of basic maps", goto error);
5343 map = isl_calloc(space->ctx, struct isl_map,
5344 sizeof(struct isl_map) +
5345 (n - 1) * sizeof(struct isl_basic_map *));
5346 if (!map)
5347 goto error;
5349 map->ctx = space->ctx;
5350 isl_ctx_ref(map->ctx);
5351 map->ref = 1;
5352 map->size = n;
5353 map->n = 0;
5354 map->dim = space;
5355 map->flags = flags;
5356 return map;
5357 error:
5358 isl_space_free(space);
5359 return NULL;
5362 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5363 unsigned nparam, unsigned in, unsigned out, int n,
5364 unsigned flags)
5366 struct isl_map *map;
5367 isl_space *dims;
5369 dims = isl_space_alloc(ctx, nparam, in, out);
5370 if (!dims)
5371 return NULL;
5373 map = isl_map_alloc_space(dims, n, flags);
5374 return map;
5377 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5379 struct isl_basic_map *bmap;
5380 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5381 bmap = isl_basic_map_set_to_empty(bmap);
5382 return bmap;
5385 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5387 struct isl_basic_set *bset;
5388 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5389 bset = isl_basic_set_set_to_empty(bset);
5390 return bset;
5393 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5395 struct isl_basic_map *bmap;
5396 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5397 bmap = isl_basic_map_finalize(bmap);
5398 return bmap;
5401 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5403 struct isl_basic_set *bset;
5404 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5405 bset = isl_basic_set_finalize(bset);
5406 return bset;
5409 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5411 int i;
5412 unsigned total = isl_space_dim(dim, isl_dim_all);
5413 isl_basic_map *bmap;
5415 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5416 for (i = 0; i < total; ++i) {
5417 int k = isl_basic_map_alloc_inequality(bmap);
5418 if (k < 0)
5419 goto error;
5420 isl_seq_clr(bmap->ineq[k], 1 + total);
5421 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5423 return bmap;
5424 error:
5425 isl_basic_map_free(bmap);
5426 return NULL;
5429 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5431 return isl_basic_map_nat_universe(dim);
5434 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5436 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5439 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5441 return isl_map_nat_universe(dim);
5444 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5446 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5449 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5451 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5454 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5456 struct isl_map *map;
5457 if (!dim)
5458 return NULL;
5459 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5460 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5461 return map;
5464 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5466 struct isl_set *set;
5467 if (!dim)
5468 return NULL;
5469 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5470 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5471 return set;
5474 struct isl_map *isl_map_dup(struct isl_map *map)
5476 int i;
5477 struct isl_map *dup;
5479 if (!map)
5480 return NULL;
5481 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5482 for (i = 0; i < map->n; ++i)
5483 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5484 return dup;
5487 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5488 __isl_take isl_basic_map *bmap)
5490 if (!bmap || !map)
5491 goto error;
5492 if (isl_basic_map_plain_is_empty(bmap)) {
5493 isl_basic_map_free(bmap);
5494 return map;
5496 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5497 isl_assert(map->ctx, map->n < map->size, goto error);
5498 map->p[map->n] = bmap;
5499 map->n++;
5500 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5501 return map;
5502 error:
5503 if (map)
5504 isl_map_free(map);
5505 if (bmap)
5506 isl_basic_map_free(bmap);
5507 return NULL;
5510 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5512 int i;
5514 if (!map)
5515 return NULL;
5517 if (--map->ref > 0)
5518 return NULL;
5520 clear_caches(map);
5521 isl_ctx_deref(map->ctx);
5522 for (i = 0; i < map->n; ++i)
5523 isl_basic_map_free(map->p[i]);
5524 isl_space_free(map->dim);
5525 free(map);
5527 return NULL;
5530 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5531 struct isl_basic_map *bmap, unsigned pos, int value)
5533 int j;
5535 bmap = isl_basic_map_cow(bmap);
5536 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5537 j = isl_basic_map_alloc_equality(bmap);
5538 if (j < 0)
5539 goto error;
5540 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5541 isl_int_set_si(bmap->eq[j][pos], -1);
5542 isl_int_set_si(bmap->eq[j][0], value);
5543 bmap = isl_basic_map_simplify(bmap);
5544 return isl_basic_map_finalize(bmap);
5545 error:
5546 isl_basic_map_free(bmap);
5547 return NULL;
5550 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5551 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5553 int j;
5555 bmap = isl_basic_map_cow(bmap);
5556 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5557 j = isl_basic_map_alloc_equality(bmap);
5558 if (j < 0)
5559 goto error;
5560 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5561 isl_int_set_si(bmap->eq[j][pos], -1);
5562 isl_int_set(bmap->eq[j][0], value);
5563 bmap = isl_basic_map_simplify(bmap);
5564 return isl_basic_map_finalize(bmap);
5565 error:
5566 isl_basic_map_free(bmap);
5567 return NULL;
5570 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5571 enum isl_dim_type type, unsigned pos, int value)
5573 if (!bmap)
5574 return NULL;
5575 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5576 return isl_basic_map_fix_pos_si(bmap,
5577 isl_basic_map_offset(bmap, type) + pos, value);
5578 error:
5579 isl_basic_map_free(bmap);
5580 return NULL;
5583 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5584 enum isl_dim_type type, unsigned pos, isl_int value)
5586 if (!bmap)
5587 return NULL;
5588 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5589 return isl_basic_map_fix_pos(bmap,
5590 isl_basic_map_offset(bmap, type) + pos, value);
5591 error:
5592 isl_basic_map_free(bmap);
5593 return NULL;
5596 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5597 * to be equal to "v".
5599 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5600 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5602 if (!bmap || !v)
5603 goto error;
5604 if (!isl_val_is_int(v))
5605 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5606 "expecting integer value", goto error);
5607 if (pos >= isl_basic_map_dim(bmap, type))
5608 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5609 "index out of bounds", goto error);
5610 pos += isl_basic_map_offset(bmap, type);
5611 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5612 isl_val_free(v);
5613 return bmap;
5614 error:
5615 isl_basic_map_free(bmap);
5616 isl_val_free(v);
5617 return NULL;
5620 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5621 * to be equal to "v".
5623 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5624 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5626 return isl_basic_map_fix_val(bset, type, pos, v);
5629 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5630 enum isl_dim_type type, unsigned pos, int value)
5632 return (struct isl_basic_set *)
5633 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5634 type, pos, value);
5637 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5638 enum isl_dim_type type, unsigned pos, isl_int value)
5640 return (struct isl_basic_set *)
5641 isl_basic_map_fix((struct isl_basic_map *)bset,
5642 type, pos, value);
5645 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5646 unsigned input, int value)
5648 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5651 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5652 unsigned dim, int value)
5654 return (struct isl_basic_set *)
5655 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5656 isl_dim_set, dim, value);
5659 static int remove_if_empty(__isl_keep isl_map *map, int i)
5661 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5663 if (empty < 0)
5664 return -1;
5665 if (!empty)
5666 return 0;
5668 isl_basic_map_free(map->p[i]);
5669 if (i != map->n - 1) {
5670 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5671 map->p[i] = map->p[map->n - 1];
5673 map->n--;
5675 return 0;
5678 /* Perform "fn" on each basic map of "map", where we may not be holding
5679 * the only reference to "map".
5680 * In particular, "fn" should be a semantics preserving operation
5681 * that we want to apply to all copies of "map". We therefore need
5682 * to be careful not to modify "map" in a way that breaks "map"
5683 * in case anything goes wrong.
5685 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5686 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5688 struct isl_basic_map *bmap;
5689 int i;
5691 if (!map)
5692 return NULL;
5694 for (i = map->n - 1; i >= 0; --i) {
5695 bmap = isl_basic_map_copy(map->p[i]);
5696 bmap = fn(bmap);
5697 if (!bmap)
5698 goto error;
5699 isl_basic_map_free(map->p[i]);
5700 map->p[i] = bmap;
5701 if (remove_if_empty(map, i) < 0)
5702 goto error;
5705 return map;
5706 error:
5707 isl_map_free(map);
5708 return NULL;
5711 struct isl_map *isl_map_fix_si(struct isl_map *map,
5712 enum isl_dim_type type, unsigned pos, int value)
5714 int i;
5716 map = isl_map_cow(map);
5717 if (!map)
5718 return NULL;
5720 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5721 for (i = map->n - 1; i >= 0; --i) {
5722 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5723 if (remove_if_empty(map, i) < 0)
5724 goto error;
5726 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5727 return map;
5728 error:
5729 isl_map_free(map);
5730 return NULL;
5733 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5734 enum isl_dim_type type, unsigned pos, int value)
5736 return (struct isl_set *)
5737 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5740 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5741 enum isl_dim_type type, unsigned pos, isl_int value)
5743 int i;
5745 map = isl_map_cow(map);
5746 if (!map)
5747 return NULL;
5749 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5750 for (i = 0; i < map->n; ++i) {
5751 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5752 if (!map->p[i])
5753 goto error;
5755 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5756 return map;
5757 error:
5758 isl_map_free(map);
5759 return NULL;
5762 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5763 enum isl_dim_type type, unsigned pos, isl_int value)
5765 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5768 /* Fix the value of the variable at position "pos" of type "type" of "map"
5769 * to be equal to "v".
5771 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5772 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5774 int i;
5776 map = isl_map_cow(map);
5777 if (!map || !v)
5778 goto error;
5780 if (!isl_val_is_int(v))
5781 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5782 "expecting integer value", goto error);
5783 if (pos >= isl_map_dim(map, type))
5784 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5785 "index out of bounds", goto error);
5786 for (i = map->n - 1; i >= 0; --i) {
5787 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5788 isl_val_copy(v));
5789 if (remove_if_empty(map, i) < 0)
5790 goto error;
5792 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5793 isl_val_free(v);
5794 return map;
5795 error:
5796 isl_map_free(map);
5797 isl_val_free(v);
5798 return NULL;
5801 /* Fix the value of the variable at position "pos" of type "type" of "set"
5802 * to be equal to "v".
5804 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5805 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5807 return isl_map_fix_val(set, type, pos, v);
5810 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5811 unsigned input, int value)
5813 return isl_map_fix_si(map, isl_dim_in, input, value);
5816 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5818 return (struct isl_set *)
5819 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5822 static __isl_give isl_basic_map *basic_map_bound_si(
5823 __isl_take isl_basic_map *bmap,
5824 enum isl_dim_type type, unsigned pos, int value, int upper)
5826 int j;
5828 if (!bmap)
5829 return NULL;
5830 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5831 pos += isl_basic_map_offset(bmap, type);
5832 bmap = isl_basic_map_cow(bmap);
5833 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5834 j = isl_basic_map_alloc_inequality(bmap);
5835 if (j < 0)
5836 goto error;
5837 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5838 if (upper) {
5839 isl_int_set_si(bmap->ineq[j][pos], -1);
5840 isl_int_set_si(bmap->ineq[j][0], value);
5841 } else {
5842 isl_int_set_si(bmap->ineq[j][pos], 1);
5843 isl_int_set_si(bmap->ineq[j][0], -value);
5845 bmap = isl_basic_map_simplify(bmap);
5846 return isl_basic_map_finalize(bmap);
5847 error:
5848 isl_basic_map_free(bmap);
5849 return NULL;
5852 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5853 __isl_take isl_basic_map *bmap,
5854 enum isl_dim_type type, unsigned pos, int value)
5856 return basic_map_bound_si(bmap, type, pos, value, 0);
5859 /* Constrain the values of the given dimension to be no greater than "value".
5861 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5862 __isl_take isl_basic_map *bmap,
5863 enum isl_dim_type type, unsigned pos, int value)
5865 return basic_map_bound_si(bmap, type, pos, value, 1);
5868 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5869 unsigned dim, isl_int value)
5871 int j;
5873 bset = isl_basic_set_cow(bset);
5874 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5875 j = isl_basic_set_alloc_inequality(bset);
5876 if (j < 0)
5877 goto error;
5878 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5879 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5880 isl_int_neg(bset->ineq[j][0], value);
5881 bset = isl_basic_set_simplify(bset);
5882 return isl_basic_set_finalize(bset);
5883 error:
5884 isl_basic_set_free(bset);
5885 return NULL;
5888 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5889 enum isl_dim_type type, unsigned pos, int value, int upper)
5891 int i;
5893 map = isl_map_cow(map);
5894 if (!map)
5895 return NULL;
5897 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5898 for (i = 0; i < map->n; ++i) {
5899 map->p[i] = basic_map_bound_si(map->p[i],
5900 type, pos, value, upper);
5901 if (!map->p[i])
5902 goto error;
5904 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5905 return map;
5906 error:
5907 isl_map_free(map);
5908 return NULL;
5911 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5912 enum isl_dim_type type, unsigned pos, int value)
5914 return map_bound_si(map, type, pos, value, 0);
5917 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5918 enum isl_dim_type type, unsigned pos, int value)
5920 return map_bound_si(map, type, pos, value, 1);
5923 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5924 enum isl_dim_type type, unsigned pos, int value)
5926 return (struct isl_set *)
5927 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5930 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5931 enum isl_dim_type type, unsigned pos, int value)
5933 return isl_map_upper_bound_si(set, type, pos, value);
5936 /* Bound the given variable of "bmap" from below (or above is "upper"
5937 * is set) to "value".
5939 static __isl_give isl_basic_map *basic_map_bound(
5940 __isl_take isl_basic_map *bmap,
5941 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5943 int j;
5945 if (!bmap)
5946 return NULL;
5947 if (pos >= isl_basic_map_dim(bmap, type))
5948 isl_die(bmap->ctx, isl_error_invalid,
5949 "index out of bounds", goto error);
5950 pos += isl_basic_map_offset(bmap, type);
5951 bmap = isl_basic_map_cow(bmap);
5952 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5953 j = isl_basic_map_alloc_inequality(bmap);
5954 if (j < 0)
5955 goto error;
5956 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5957 if (upper) {
5958 isl_int_set_si(bmap->ineq[j][pos], -1);
5959 isl_int_set(bmap->ineq[j][0], value);
5960 } else {
5961 isl_int_set_si(bmap->ineq[j][pos], 1);
5962 isl_int_neg(bmap->ineq[j][0], value);
5964 bmap = isl_basic_map_simplify(bmap);
5965 return isl_basic_map_finalize(bmap);
5966 error:
5967 isl_basic_map_free(bmap);
5968 return NULL;
5971 /* Bound the given variable of "map" from below (or above is "upper"
5972 * is set) to "value".
5974 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5975 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5977 int i;
5979 map = isl_map_cow(map);
5980 if (!map)
5981 return NULL;
5983 if (pos >= isl_map_dim(map, type))
5984 isl_die(map->ctx, isl_error_invalid,
5985 "index out of bounds", goto error);
5986 for (i = map->n - 1; i >= 0; --i) {
5987 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5988 if (remove_if_empty(map, i) < 0)
5989 goto error;
5991 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5992 return map;
5993 error:
5994 isl_map_free(map);
5995 return NULL;
5998 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5999 enum isl_dim_type type, unsigned pos, isl_int value)
6001 return map_bound(map, type, pos, value, 0);
6004 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6005 enum isl_dim_type type, unsigned pos, isl_int value)
6007 return map_bound(map, type, pos, value, 1);
6010 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6011 enum isl_dim_type type, unsigned pos, isl_int value)
6013 return isl_map_lower_bound(set, type, pos, value);
6016 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6017 enum isl_dim_type type, unsigned pos, isl_int value)
6019 return isl_map_upper_bound(set, type, pos, value);
6022 /* Force the values of the variable at position "pos" of type "type" of "set"
6023 * to be no smaller than "value".
6025 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6026 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6028 if (!value)
6029 goto error;
6030 if (!isl_val_is_int(value))
6031 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6032 "expecting integer value", goto error);
6033 set = isl_set_lower_bound(set, type, pos, value->n);
6034 isl_val_free(value);
6035 return set;
6036 error:
6037 isl_val_free(value);
6038 isl_set_free(set);
6039 return NULL;
6042 /* Force the values of the variable at position "pos" of type "type" of "set"
6043 * to be no greater than "value".
6045 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6046 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6048 if (!value)
6049 goto error;
6050 if (!isl_val_is_int(value))
6051 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6052 "expecting integer value", goto error);
6053 set = isl_set_upper_bound(set, type, pos, value->n);
6054 isl_val_free(value);
6055 return set;
6056 error:
6057 isl_val_free(value);
6058 isl_set_free(set);
6059 return NULL;
6062 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6063 isl_int value)
6065 int i;
6067 set = isl_set_cow(set);
6068 if (!set)
6069 return NULL;
6071 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6072 for (i = 0; i < set->n; ++i) {
6073 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6074 if (!set->p[i])
6075 goto error;
6077 return set;
6078 error:
6079 isl_set_free(set);
6080 return NULL;
6083 struct isl_map *isl_map_reverse(struct isl_map *map)
6085 int i;
6087 map = isl_map_cow(map);
6088 if (!map)
6089 return NULL;
6091 map->dim = isl_space_reverse(map->dim);
6092 if (!map->dim)
6093 goto error;
6094 for (i = 0; i < map->n; ++i) {
6095 map->p[i] = isl_basic_map_reverse(map->p[i]);
6096 if (!map->p[i])
6097 goto error;
6099 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6100 return map;
6101 error:
6102 isl_map_free(map);
6103 return NULL;
6106 #undef TYPE
6107 #define TYPE isl_pw_multi_aff
6108 #undef SUFFIX
6109 #define SUFFIX _pw_multi_aff
6110 #undef EMPTY
6111 #define EMPTY isl_pw_multi_aff_empty
6112 #undef ADD
6113 #define ADD isl_pw_multi_aff_union_add
6114 #include "isl_map_lexopt_templ.c"
6116 /* Given a map "map", compute the lexicographically minimal
6117 * (or maximal) image element for each domain element in dom,
6118 * in the form of an isl_pw_multi_aff.
6119 * If "empty" is not NULL, then set *empty to those elements in dom that
6120 * do not have an image element.
6121 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6122 * should be computed over the domain of "map". "empty" is also NULL
6123 * in this case.
6125 * We first compute the lexicographically minimal or maximal element
6126 * in the first basic map. This results in a partial solution "res"
6127 * and a subset "todo" of dom that still need to be handled.
6128 * We then consider each of the remaining maps in "map" and successively
6129 * update both "res" and "todo".
6130 * If "empty" is NULL, then the todo sets are not needed and therefore
6131 * also not computed.
6133 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6134 __isl_take isl_map *map, __isl_take isl_set *dom,
6135 __isl_give isl_set **empty, unsigned flags)
6137 int i;
6138 int full;
6139 isl_pw_multi_aff *res;
6140 isl_set *todo;
6142 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6143 if (!map || (!full && !dom))
6144 goto error;
6146 if (isl_map_plain_is_empty(map)) {
6147 if (empty)
6148 *empty = dom;
6149 else
6150 isl_set_free(dom);
6151 return isl_pw_multi_aff_from_map(map);
6154 res = basic_map_partial_lexopt_pw_multi_aff(
6155 isl_basic_map_copy(map->p[0]),
6156 isl_set_copy(dom), empty, flags);
6158 if (empty)
6159 todo = *empty;
6160 for (i = 1; i < map->n; ++i) {
6161 isl_pw_multi_aff *res_i;
6163 res_i = basic_map_partial_lexopt_pw_multi_aff(
6164 isl_basic_map_copy(map->p[i]),
6165 isl_set_copy(dom), empty, flags);
6167 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6168 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6169 else
6170 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6172 if (empty)
6173 todo = isl_set_intersect(todo, *empty);
6176 isl_set_free(dom);
6177 isl_map_free(map);
6179 if (empty)
6180 *empty = todo;
6182 return res;
6183 error:
6184 if (empty)
6185 *empty = NULL;
6186 isl_set_free(dom);
6187 isl_map_free(map);
6188 return NULL;
6191 #undef TYPE
6192 #define TYPE isl_map
6193 #undef SUFFIX
6194 #define SUFFIX
6195 #undef EMPTY
6196 #define EMPTY isl_map_empty
6197 #undef ADD
6198 #define ADD isl_map_union_disjoint
6199 #include "isl_map_lexopt_templ.c"
6201 /* Given a map "map", compute the lexicographically minimal
6202 * (or maximal) image element for each domain element in "dom",
6203 * in the form of an isl_map.
6204 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6205 * do not have an image element.
6206 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6207 * should be computed over the domain of "map". "empty" is also NULL
6208 * in this case.
6210 * If the input consists of more than one disjunct, then first
6211 * compute the desired result in the form of an isl_pw_multi_aff and
6212 * then convert that into an isl_map.
6214 * This function used to have an explicit implementation in terms
6215 * of isl_maps, but it would continually intersect the domains of
6216 * partial results with the complement of the domain of the next
6217 * partial solution, potentially leading to an explosion in the number
6218 * of disjuncts if there are several disjuncts in the input.
6219 * An even earlier implementation of this function would look for
6220 * better results in the domain of the partial result and for extra
6221 * results in the complement of this domain, which would lead to
6222 * even more splintering.
6224 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6225 __isl_take isl_map *map, __isl_take isl_set *dom,
6226 __isl_give isl_set **empty, unsigned flags)
6228 int full;
6229 struct isl_map *res;
6230 isl_pw_multi_aff *pma;
6232 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6233 if (!map || (!full && !dom))
6234 goto error;
6236 if (isl_map_plain_is_empty(map)) {
6237 if (empty)
6238 *empty = dom;
6239 else
6240 isl_set_free(dom);
6241 return map;
6244 if (map->n == 1) {
6245 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6246 dom, empty, flags);
6247 isl_map_free(map);
6248 return res;
6251 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6252 flags);
6253 return isl_map_from_pw_multi_aff(pma);
6254 error:
6255 if (empty)
6256 *empty = NULL;
6257 isl_set_free(dom);
6258 isl_map_free(map);
6259 return NULL;
6262 __isl_give isl_map *isl_map_partial_lexmax(
6263 __isl_take isl_map *map, __isl_take isl_set *dom,
6264 __isl_give isl_set **empty)
6266 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6269 __isl_give isl_map *isl_map_partial_lexmin(
6270 __isl_take isl_map *map, __isl_take isl_set *dom,
6271 __isl_give isl_set **empty)
6273 return isl_map_partial_lexopt(map, dom, empty, 0);
6276 __isl_give isl_set *isl_set_partial_lexmin(
6277 __isl_take isl_set *set, __isl_take isl_set *dom,
6278 __isl_give isl_set **empty)
6280 return (struct isl_set *)
6281 isl_map_partial_lexmin((struct isl_map *)set,
6282 dom, empty);
6285 __isl_give isl_set *isl_set_partial_lexmax(
6286 __isl_take isl_set *set, __isl_take isl_set *dom,
6287 __isl_give isl_set **empty)
6289 return (struct isl_set *)
6290 isl_map_partial_lexmax((struct isl_map *)set,
6291 dom, empty);
6294 /* Compute the lexicographic minimum (or maximum if "flags" includes
6295 * ISL_OPT_MAX) of "bset" over its parametric domain.
6297 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6298 unsigned flags)
6300 return isl_basic_map_lexopt(bset, flags);
6303 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6305 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6308 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6310 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6313 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6315 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6318 /* Compute the lexicographic minimum of "bset" over its parametric domain
6319 * for the purpose of quantifier elimination.
6320 * That is, find an explicit representation for all the existentially
6321 * quantified variables in "bset" by computing their lexicographic
6322 * minimum.
6324 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6325 __isl_take isl_basic_set *bset)
6327 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6330 /* Extract the first and only affine expression from list
6331 * and then add it to *pwaff with the given dom.
6332 * This domain is known to be disjoint from other domains
6333 * because of the way isl_basic_map_foreach_lexmax works.
6335 static int update_dim_opt(__isl_take isl_basic_set *dom,
6336 __isl_take isl_aff_list *list, void *user)
6338 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6339 isl_aff *aff;
6340 isl_pw_aff **pwaff = user;
6341 isl_pw_aff *pwaff_i;
6343 if (!list)
6344 goto error;
6345 if (isl_aff_list_n_aff(list) != 1)
6346 isl_die(ctx, isl_error_internal,
6347 "expecting single element list", goto error);
6349 aff = isl_aff_list_get_aff(list, 0);
6350 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6352 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6354 isl_aff_list_free(list);
6356 return 0;
6357 error:
6358 isl_basic_set_free(dom);
6359 isl_aff_list_free(list);
6360 return -1;
6363 /* Given a basic map with one output dimension, compute the minimum or
6364 * maximum of that dimension as an isl_pw_aff.
6366 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6367 * call update_dim_opt on each leaf of the result.
6369 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6370 int max)
6372 isl_space *dim = isl_basic_map_get_space(bmap);
6373 isl_pw_aff *pwaff;
6374 int r;
6376 dim = isl_space_from_domain(isl_space_domain(dim));
6377 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6378 pwaff = isl_pw_aff_empty(dim);
6380 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6381 if (r < 0)
6382 return isl_pw_aff_free(pwaff);
6384 return pwaff;
6387 /* Compute the minimum or maximum of the given output dimension
6388 * as a function of the parameters and the input dimensions,
6389 * but independently of the other output dimensions.
6391 * We first project out the other output dimension and then compute
6392 * the "lexicographic" maximum in each basic map, combining the results
6393 * using isl_pw_aff_union_max.
6395 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6396 int max)
6398 int i;
6399 isl_pw_aff *pwaff;
6400 unsigned n_out;
6402 n_out = isl_map_dim(map, isl_dim_out);
6403 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6404 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6405 if (!map)
6406 return NULL;
6408 if (map->n == 0) {
6409 isl_space *dim = isl_map_get_space(map);
6410 isl_map_free(map);
6411 return isl_pw_aff_empty(dim);
6414 pwaff = basic_map_dim_opt(map->p[0], max);
6415 for (i = 1; i < map->n; ++i) {
6416 isl_pw_aff *pwaff_i;
6418 pwaff_i = basic_map_dim_opt(map->p[i], max);
6419 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6422 isl_map_free(map);
6424 return pwaff;
6427 /* Compute the minimum of the given output dimension as a function of the
6428 * parameters and input dimensions, but independently of
6429 * the other output dimensions.
6431 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6433 return map_dim_opt(map, pos, 0);
6436 /* Compute the maximum of the given output dimension as a function of the
6437 * parameters and input dimensions, but independently of
6438 * the other output dimensions.
6440 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6442 return map_dim_opt(map, pos, 1);
6445 /* Compute the minimum or maximum of the given set dimension
6446 * as a function of the parameters,
6447 * but independently of the other set dimensions.
6449 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6450 int max)
6452 return map_dim_opt(set, pos, max);
6455 /* Compute the maximum of the given set dimension as a function of the
6456 * parameters, but independently of the other set dimensions.
6458 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6460 return set_dim_opt(set, pos, 1);
6463 /* Compute the minimum of the given set dimension as a function of the
6464 * parameters, but independently of the other set dimensions.
6466 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6468 return set_dim_opt(set, pos, 0);
6471 /* Apply a preimage specified by "mat" on the parameters of "bset".
6472 * bset is assumed to have only parameters and divs.
6474 static struct isl_basic_set *basic_set_parameter_preimage(
6475 struct isl_basic_set *bset, struct isl_mat *mat)
6477 unsigned nparam;
6479 if (!bset || !mat)
6480 goto error;
6482 bset->dim = isl_space_cow(bset->dim);
6483 if (!bset->dim)
6484 goto error;
6486 nparam = isl_basic_set_dim(bset, isl_dim_param);
6488 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6490 bset->dim->nparam = 0;
6491 bset->dim->n_out = nparam;
6492 bset = isl_basic_set_preimage(bset, mat);
6493 if (bset) {
6494 bset->dim->nparam = bset->dim->n_out;
6495 bset->dim->n_out = 0;
6497 return bset;
6498 error:
6499 isl_mat_free(mat);
6500 isl_basic_set_free(bset);
6501 return NULL;
6504 /* Apply a preimage specified by "mat" on the parameters of "set".
6505 * set is assumed to have only parameters and divs.
6507 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6508 __isl_take isl_mat *mat)
6510 isl_space *space;
6511 unsigned nparam;
6513 if (!set || !mat)
6514 goto error;
6516 nparam = isl_set_dim(set, isl_dim_param);
6518 if (mat->n_row != 1 + nparam)
6519 isl_die(isl_set_get_ctx(set), isl_error_internal,
6520 "unexpected number of rows", goto error);
6522 space = isl_set_get_space(set);
6523 space = isl_space_move_dims(space, isl_dim_set, 0,
6524 isl_dim_param, 0, nparam);
6525 set = isl_set_reset_space(set, space);
6526 set = isl_set_preimage(set, mat);
6527 nparam = isl_set_dim(set, isl_dim_out);
6528 space = isl_set_get_space(set);
6529 space = isl_space_move_dims(space, isl_dim_param, 0,
6530 isl_dim_out, 0, nparam);
6531 set = isl_set_reset_space(set, space);
6532 return set;
6533 error:
6534 isl_mat_free(mat);
6535 isl_set_free(set);
6536 return NULL;
6539 /* Intersect the basic set "bset" with the affine space specified by the
6540 * equalities in "eq".
6542 static struct isl_basic_set *basic_set_append_equalities(
6543 struct isl_basic_set *bset, struct isl_mat *eq)
6545 int i, k;
6546 unsigned len;
6548 if (!bset || !eq)
6549 goto error;
6551 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6552 eq->n_row, 0);
6553 if (!bset)
6554 goto error;
6556 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6557 for (i = 0; i < eq->n_row; ++i) {
6558 k = isl_basic_set_alloc_equality(bset);
6559 if (k < 0)
6560 goto error;
6561 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6562 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6564 isl_mat_free(eq);
6566 bset = isl_basic_set_gauss(bset, NULL);
6567 bset = isl_basic_set_finalize(bset);
6569 return bset;
6570 error:
6571 isl_mat_free(eq);
6572 isl_basic_set_free(bset);
6573 return NULL;
6576 /* Intersect the set "set" with the affine space specified by the
6577 * equalities in "eq".
6579 static struct isl_set *set_append_equalities(struct isl_set *set,
6580 struct isl_mat *eq)
6582 int i;
6584 if (!set || !eq)
6585 goto error;
6587 for (i = 0; i < set->n; ++i) {
6588 set->p[i] = basic_set_append_equalities(set->p[i],
6589 isl_mat_copy(eq));
6590 if (!set->p[i])
6591 goto error;
6593 isl_mat_free(eq);
6594 return set;
6595 error:
6596 isl_mat_free(eq);
6597 isl_set_free(set);
6598 return NULL;
6601 /* Given a basic set "bset" that only involves parameters and existentially
6602 * quantified variables, return the index of the first equality
6603 * that only involves parameters. If there is no such equality then
6604 * return bset->n_eq.
6606 * This function assumes that isl_basic_set_gauss has been called on "bset".
6608 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6610 int i, j;
6611 unsigned nparam, n_div;
6613 if (!bset)
6614 return -1;
6616 nparam = isl_basic_set_dim(bset, isl_dim_param);
6617 n_div = isl_basic_set_dim(bset, isl_dim_div);
6619 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6620 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6621 ++i;
6624 return i;
6627 /* Compute an explicit representation for the existentially quantified
6628 * variables in "bset" by computing the "minimal value" of the set
6629 * variables. Since there are no set variables, the computation of
6630 * the minimal value essentially computes an explicit representation
6631 * of the non-empty part(s) of "bset".
6633 * The input only involves parameters and existentially quantified variables.
6634 * All equalities among parameters have been removed.
6636 * Since the existentially quantified variables in the result are in general
6637 * going to be different from those in the input, we first replace
6638 * them by the minimal number of variables based on their equalities.
6639 * This should simplify the parametric integer programming.
6641 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6643 isl_morph *morph1, *morph2;
6644 isl_set *set;
6645 unsigned n;
6647 if (!bset)
6648 return NULL;
6649 if (bset->n_eq == 0)
6650 return isl_basic_set_lexmin_compute_divs(bset);
6652 morph1 = isl_basic_set_parameter_compression(bset);
6653 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6654 bset = isl_basic_set_lift(bset);
6655 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6656 bset = isl_morph_basic_set(morph2, bset);
6657 n = isl_basic_set_dim(bset, isl_dim_set);
6658 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6660 set = isl_basic_set_lexmin_compute_divs(bset);
6662 set = isl_morph_set(isl_morph_inverse(morph1), set);
6664 return set;
6667 /* Project the given basic set onto its parameter domain, possibly introducing
6668 * new, explicit, existential variables in the constraints.
6669 * The input has parameters and (possibly implicit) existential variables.
6670 * The output has the same parameters, but only
6671 * explicit existentially quantified variables.
6673 * The actual projection is performed by pip, but pip doesn't seem
6674 * to like equalities very much, so we first remove the equalities
6675 * among the parameters by performing a variable compression on
6676 * the parameters. Afterward, an inverse transformation is performed
6677 * and the equalities among the parameters are inserted back in.
6679 * The variable compression on the parameters may uncover additional
6680 * equalities that were only implicit before. We therefore check
6681 * if there are any new parameter equalities in the result and
6682 * if so recurse. The removal of parameter equalities is required
6683 * for the parameter compression performed by base_compute_divs.
6685 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6687 int i;
6688 struct isl_mat *eq;
6689 struct isl_mat *T, *T2;
6690 struct isl_set *set;
6691 unsigned nparam;
6693 bset = isl_basic_set_cow(bset);
6694 if (!bset)
6695 return NULL;
6697 if (bset->n_eq == 0)
6698 return base_compute_divs(bset);
6700 bset = isl_basic_set_gauss(bset, NULL);
6701 if (!bset)
6702 return NULL;
6703 if (isl_basic_set_plain_is_empty(bset))
6704 return isl_set_from_basic_set(bset);
6706 i = first_parameter_equality(bset);
6707 if (i == bset->n_eq)
6708 return base_compute_divs(bset);
6710 nparam = isl_basic_set_dim(bset, isl_dim_param);
6711 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6712 0, 1 + nparam);
6713 eq = isl_mat_cow(eq);
6714 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6715 if (T && T->n_col == 0) {
6716 isl_mat_free(T);
6717 isl_mat_free(T2);
6718 isl_mat_free(eq);
6719 bset = isl_basic_set_set_to_empty(bset);
6720 return isl_set_from_basic_set(bset);
6722 bset = basic_set_parameter_preimage(bset, T);
6724 i = first_parameter_equality(bset);
6725 if (!bset)
6726 set = NULL;
6727 else if (i == bset->n_eq)
6728 set = base_compute_divs(bset);
6729 else
6730 set = parameter_compute_divs(bset);
6731 set = set_parameter_preimage(set, T2);
6732 set = set_append_equalities(set, eq);
6733 return set;
6736 /* Insert the divs from "ls" before those of "bmap".
6738 * The number of columns is not changed, which means that the last
6739 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6740 * The caller is responsible for removing the same number of dimensions
6741 * from the space of "bmap".
6743 static __isl_give isl_basic_map *insert_divs_from_local_space(
6744 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6746 int i;
6747 int n_div;
6748 int old_n_div;
6750 n_div = isl_local_space_dim(ls, isl_dim_div);
6751 if (n_div == 0)
6752 return bmap;
6754 old_n_div = bmap->n_div;
6755 bmap = insert_div_rows(bmap, n_div);
6756 if (!bmap)
6757 return NULL;
6759 for (i = 0; i < n_div; ++i) {
6760 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6761 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6764 return bmap;
6767 /* Replace the space of "bmap" by the space and divs of "ls".
6769 * If "ls" has any divs, then we simplify the result since we may
6770 * have discovered some additional equalities that could simplify
6771 * the div expressions.
6773 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6774 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6776 int n_div;
6778 bmap = isl_basic_map_cow(bmap);
6779 if (!bmap || !ls)
6780 goto error;
6782 n_div = isl_local_space_dim(ls, isl_dim_div);
6783 bmap = insert_divs_from_local_space(bmap, ls);
6784 if (!bmap)
6785 goto error;
6787 isl_space_free(bmap->dim);
6788 bmap->dim = isl_local_space_get_space(ls);
6789 if (!bmap->dim)
6790 goto error;
6792 isl_local_space_free(ls);
6793 if (n_div > 0)
6794 bmap = isl_basic_map_simplify(bmap);
6795 bmap = isl_basic_map_finalize(bmap);
6796 return bmap;
6797 error:
6798 isl_basic_map_free(bmap);
6799 isl_local_space_free(ls);
6800 return NULL;
6803 /* Replace the space of "map" by the space and divs of "ls".
6805 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6806 __isl_take isl_local_space *ls)
6808 int i;
6810 map = isl_map_cow(map);
6811 if (!map || !ls)
6812 goto error;
6814 for (i = 0; i < map->n; ++i) {
6815 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6816 isl_local_space_copy(ls));
6817 if (!map->p[i])
6818 goto error;
6820 isl_space_free(map->dim);
6821 map->dim = isl_local_space_get_space(ls);
6822 if (!map->dim)
6823 goto error;
6825 isl_local_space_free(ls);
6826 return map;
6827 error:
6828 isl_local_space_free(ls);
6829 isl_map_free(map);
6830 return NULL;
6833 /* Compute an explicit representation for the existentially
6834 * quantified variables for which do not know any explicit representation yet.
6836 * We first sort the existentially quantified variables so that the
6837 * existentially quantified variables for which we already have an explicit
6838 * representation are placed before those for which we do not.
6839 * The input dimensions, the output dimensions and the existentially
6840 * quantified variables for which we already have an explicit
6841 * representation are then turned into parameters.
6842 * compute_divs returns a map with the same parameters and
6843 * no input or output dimensions and the dimension specification
6844 * is reset to that of the input, including the existentially quantified
6845 * variables for which we already had an explicit representation.
6847 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6849 struct isl_basic_set *bset;
6850 struct isl_set *set;
6851 struct isl_map *map;
6852 isl_space *dim;
6853 isl_local_space *ls;
6854 unsigned nparam;
6855 unsigned n_in;
6856 unsigned n_out;
6857 int n_known;
6858 int i;
6860 bmap = isl_basic_map_sort_divs(bmap);
6861 bmap = isl_basic_map_cow(bmap);
6862 if (!bmap)
6863 return NULL;
6865 n_known = isl_basic_map_first_unknown_div(bmap);
6866 if (n_known < 0)
6867 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6869 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6870 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6871 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6872 dim = isl_space_set_alloc(bmap->ctx,
6873 nparam + n_in + n_out + n_known, 0);
6874 if (!dim)
6875 goto error;
6877 ls = isl_basic_map_get_local_space(bmap);
6878 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6879 n_known, bmap->n_div - n_known);
6880 if (n_known > 0) {
6881 for (i = n_known; i < bmap->n_div; ++i)
6882 swap_div(bmap, i - n_known, i);
6883 bmap->n_div -= n_known;
6884 bmap->extra -= n_known;
6886 bmap = isl_basic_map_reset_space(bmap, dim);
6887 bset = (struct isl_basic_set *)bmap;
6889 set = parameter_compute_divs(bset);
6890 map = (struct isl_map *)set;
6891 map = replace_space_by_local_space(map, ls);
6893 return map;
6894 error:
6895 isl_basic_map_free(bmap);
6896 return NULL;
6899 /* Remove the explicit representation of local variable "div",
6900 * if there is any.
6902 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6903 __isl_take isl_basic_map *bmap, int div)
6905 isl_bool unknown;
6907 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6908 if (unknown < 0)
6909 return isl_basic_map_free(bmap);
6910 if (unknown)
6911 return bmap;
6913 bmap = isl_basic_map_cow(bmap);
6914 if (!bmap)
6915 return NULL;
6916 isl_int_set_si(bmap->div[div][0], 0);
6917 return bmap;
6920 /* Is local variable "div" of "bmap" marked as not having an explicit
6921 * representation?
6922 * Note that even if "div" is not marked in this way and therefore
6923 * has an explicit representation, this representation may still
6924 * depend (indirectly) on other local variables that do not
6925 * have an explicit representation.
6927 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
6928 int div)
6930 if (!bmap)
6931 return isl_bool_error;
6932 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6933 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6934 "position out of bounds", return isl_bool_error);
6935 return isl_int_is_zero(bmap->div[div][0]);
6938 /* Return the position of the first local variable that does not
6939 * have an explicit representation.
6940 * Return the total number of local variables if they all have
6941 * an explicit representation.
6942 * Return -1 on error.
6944 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
6946 int i;
6948 if (!bmap)
6949 return -1;
6951 for (i = 0; i < bmap->n_div; ++i) {
6952 if (!isl_basic_map_div_is_known(bmap, i))
6953 return i;
6955 return bmap->n_div;
6958 /* Does "bmap" have an explicit representation for all local variables?
6960 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6962 int first, n;
6964 n = isl_basic_map_dim(bmap, isl_dim_div);
6965 first = isl_basic_map_first_unknown_div(bmap);
6966 if (first < 0)
6967 return isl_bool_error;
6968 return first == n;
6971 /* Do all basic maps in "map" have an explicit representation
6972 * for all local variables?
6974 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
6976 int i;
6978 if (!map)
6979 return isl_bool_error;
6981 for (i = 0; i < map->n; ++i) {
6982 int known = isl_basic_map_divs_known(map->p[i]);
6983 if (known <= 0)
6984 return known;
6987 return isl_bool_true;
6990 /* If bmap contains any unknown divs, then compute explicit
6991 * expressions for them. However, this computation may be
6992 * quite expensive, so first try to remove divs that aren't
6993 * strictly needed.
6995 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6997 int known;
6998 struct isl_map *map;
7000 known = isl_basic_map_divs_known(bmap);
7001 if (known < 0)
7002 goto error;
7003 if (known)
7004 return isl_map_from_basic_map(bmap);
7006 bmap = isl_basic_map_drop_redundant_divs(bmap);
7008 known = isl_basic_map_divs_known(bmap);
7009 if (known < 0)
7010 goto error;
7011 if (known)
7012 return isl_map_from_basic_map(bmap);
7014 map = compute_divs(bmap);
7015 return map;
7016 error:
7017 isl_basic_map_free(bmap);
7018 return NULL;
7021 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7023 int i;
7024 int known;
7025 struct isl_map *res;
7027 if (!map)
7028 return NULL;
7029 if (map->n == 0)
7030 return map;
7032 known = isl_map_divs_known(map);
7033 if (known < 0) {
7034 isl_map_free(map);
7035 return NULL;
7037 if (known)
7038 return map;
7040 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7041 for (i = 1 ; i < map->n; ++i) {
7042 struct isl_map *r2;
7043 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7044 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7045 res = isl_map_union_disjoint(res, r2);
7046 else
7047 res = isl_map_union(res, r2);
7049 isl_map_free(map);
7051 return res;
7054 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7056 return (struct isl_set *)
7057 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7060 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7062 return (struct isl_set *)
7063 isl_map_compute_divs((struct isl_map *)set);
7066 struct isl_set *isl_map_domain(struct isl_map *map)
7068 int i;
7069 struct isl_set *set;
7071 if (!map)
7072 goto error;
7074 map = isl_map_cow(map);
7075 if (!map)
7076 return NULL;
7078 set = (struct isl_set *)map;
7079 set->dim = isl_space_domain(set->dim);
7080 if (!set->dim)
7081 goto error;
7082 for (i = 0; i < map->n; ++i) {
7083 set->p[i] = isl_basic_map_domain(map->p[i]);
7084 if (!set->p[i])
7085 goto error;
7087 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7088 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7089 return set;
7090 error:
7091 isl_map_free(map);
7092 return NULL;
7095 /* Return the union of "map1" and "map2", where we assume for now that
7096 * "map1" and "map2" are disjoint. Note that the basic maps inside
7097 * "map1" or "map2" may not be disjoint from each other.
7098 * Also note that this function is also called from isl_map_union,
7099 * which takes care of handling the situation where "map1" and "map2"
7100 * may not be disjoint.
7102 * If one of the inputs is empty, we can simply return the other input.
7103 * Similarly, if one of the inputs is universal, then it is equal to the union.
7105 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7106 __isl_take isl_map *map2)
7108 int i;
7109 unsigned flags = 0;
7110 struct isl_map *map = NULL;
7111 int is_universe;
7113 if (!map1 || !map2)
7114 goto error;
7116 if (!isl_space_is_equal(map1->dim, map2->dim))
7117 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7118 "spaces don't match", goto error);
7120 if (map1->n == 0) {
7121 isl_map_free(map1);
7122 return map2;
7124 if (map2->n == 0) {
7125 isl_map_free(map2);
7126 return map1;
7129 is_universe = isl_map_plain_is_universe(map1);
7130 if (is_universe < 0)
7131 goto error;
7132 if (is_universe) {
7133 isl_map_free(map2);
7134 return map1;
7137 is_universe = isl_map_plain_is_universe(map2);
7138 if (is_universe < 0)
7139 goto error;
7140 if (is_universe) {
7141 isl_map_free(map1);
7142 return map2;
7145 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7146 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7147 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7149 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7150 map1->n + map2->n, flags);
7151 if (!map)
7152 goto error;
7153 for (i = 0; i < map1->n; ++i) {
7154 map = isl_map_add_basic_map(map,
7155 isl_basic_map_copy(map1->p[i]));
7156 if (!map)
7157 goto error;
7159 for (i = 0; i < map2->n; ++i) {
7160 map = isl_map_add_basic_map(map,
7161 isl_basic_map_copy(map2->p[i]));
7162 if (!map)
7163 goto error;
7165 isl_map_free(map1);
7166 isl_map_free(map2);
7167 return map;
7168 error:
7169 isl_map_free(map);
7170 isl_map_free(map1);
7171 isl_map_free(map2);
7172 return NULL;
7175 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7176 * guaranteed to be disjoint by the caller.
7178 * Note that this functions is called from within isl_map_make_disjoint,
7179 * so we have to be careful not to touch the constraints of the inputs
7180 * in any way.
7182 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7183 __isl_take isl_map *map2)
7185 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7188 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7189 * not be disjoint. The parameters are assumed to have been aligned.
7191 * We currently simply call map_union_disjoint, the internal operation
7192 * of which does not really depend on the inputs being disjoint.
7193 * If the result contains more than one basic map, then we clear
7194 * the disjoint flag since the result may contain basic maps from
7195 * both inputs and these are not guaranteed to be disjoint.
7197 * As a special case, if "map1" and "map2" are obviously equal,
7198 * then we simply return "map1".
7200 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7201 __isl_take isl_map *map2)
7203 int equal;
7205 if (!map1 || !map2)
7206 goto error;
7208 equal = isl_map_plain_is_equal(map1, map2);
7209 if (equal < 0)
7210 goto error;
7211 if (equal) {
7212 isl_map_free(map2);
7213 return map1;
7216 map1 = map_union_disjoint(map1, map2);
7217 if (!map1)
7218 return NULL;
7219 if (map1->n > 1)
7220 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7221 return map1;
7222 error:
7223 isl_map_free(map1);
7224 isl_map_free(map2);
7225 return NULL;
7228 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7229 * not be disjoint.
7231 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7232 __isl_take isl_map *map2)
7234 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7237 struct isl_set *isl_set_union_disjoint(
7238 struct isl_set *set1, struct isl_set *set2)
7240 return (struct isl_set *)
7241 isl_map_union_disjoint(
7242 (struct isl_map *)set1, (struct isl_map *)set2);
7245 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7247 return (struct isl_set *)
7248 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7251 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7252 * the results.
7254 * "map" and "set" are assumed to be compatible and non-NULL.
7256 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7257 __isl_take isl_set *set,
7258 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7259 __isl_take isl_basic_set *bset))
7261 unsigned flags = 0;
7262 struct isl_map *result;
7263 int i, j;
7265 if (isl_set_plain_is_universe(set)) {
7266 isl_set_free(set);
7267 return map;
7270 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7271 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7272 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7274 result = isl_map_alloc_space(isl_space_copy(map->dim),
7275 map->n * set->n, flags);
7276 for (i = 0; result && i < map->n; ++i)
7277 for (j = 0; j < set->n; ++j) {
7278 result = isl_map_add_basic_map(result,
7279 fn(isl_basic_map_copy(map->p[i]),
7280 isl_basic_set_copy(set->p[j])));
7281 if (!result)
7282 break;
7285 isl_map_free(map);
7286 isl_set_free(set);
7287 return result;
7290 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7291 __isl_take isl_set *set)
7293 if (!map || !set)
7294 goto error;
7296 if (!isl_map_compatible_range(map, set))
7297 isl_die(set->ctx, isl_error_invalid,
7298 "incompatible spaces", goto error);
7300 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7301 error:
7302 isl_map_free(map);
7303 isl_set_free(set);
7304 return NULL;
7307 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7308 __isl_take isl_set *set)
7310 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7313 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7314 __isl_take isl_set *set)
7316 if (!map || !set)
7317 goto error;
7319 if (!isl_map_compatible_domain(map, set))
7320 isl_die(set->ctx, isl_error_invalid,
7321 "incompatible spaces", goto error);
7323 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7324 error:
7325 isl_map_free(map);
7326 isl_set_free(set);
7327 return NULL;
7330 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7331 __isl_take isl_set *set)
7333 return isl_map_align_params_map_map_and(map, set,
7334 &map_intersect_domain);
7337 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7338 __isl_take isl_map *map2)
7340 if (!map1 || !map2)
7341 goto error;
7342 map1 = isl_map_reverse(map1);
7343 map1 = isl_map_apply_range(map1, map2);
7344 return isl_map_reverse(map1);
7345 error:
7346 isl_map_free(map1);
7347 isl_map_free(map2);
7348 return NULL;
7351 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7352 __isl_take isl_map *map2)
7354 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7357 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7358 __isl_take isl_map *map2)
7360 isl_space *dim_result;
7361 struct isl_map *result;
7362 int i, j;
7364 if (!map1 || !map2)
7365 goto error;
7367 dim_result = isl_space_join(isl_space_copy(map1->dim),
7368 isl_space_copy(map2->dim));
7370 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7371 if (!result)
7372 goto error;
7373 for (i = 0; i < map1->n; ++i)
7374 for (j = 0; j < map2->n; ++j) {
7375 result = isl_map_add_basic_map(result,
7376 isl_basic_map_apply_range(
7377 isl_basic_map_copy(map1->p[i]),
7378 isl_basic_map_copy(map2->p[j])));
7379 if (!result)
7380 goto error;
7382 isl_map_free(map1);
7383 isl_map_free(map2);
7384 if (result && result->n <= 1)
7385 ISL_F_SET(result, ISL_MAP_DISJOINT);
7386 return result;
7387 error:
7388 isl_map_free(map1);
7389 isl_map_free(map2);
7390 return NULL;
7393 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7394 __isl_take isl_map *map2)
7396 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7400 * returns range - domain
7402 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7404 isl_space *target_space;
7405 struct isl_basic_set *bset;
7406 unsigned dim;
7407 unsigned nparam;
7408 int i;
7410 if (!bmap)
7411 goto error;
7412 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7413 bmap->dim, isl_dim_out),
7414 goto error);
7415 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7416 dim = isl_basic_map_n_in(bmap);
7417 nparam = isl_basic_map_n_param(bmap);
7418 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7419 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7420 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7421 for (i = 0; i < dim; ++i) {
7422 int j = isl_basic_map_alloc_equality(bmap);
7423 if (j < 0) {
7424 bmap = isl_basic_map_free(bmap);
7425 break;
7427 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7428 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7429 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7430 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7432 bset = isl_basic_map_domain(bmap);
7433 bset = isl_basic_set_reset_space(bset, target_space);
7434 return bset;
7435 error:
7436 isl_basic_map_free(bmap);
7437 return NULL;
7441 * returns range - domain
7443 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7445 int i;
7446 isl_space *dim;
7447 struct isl_set *result;
7449 if (!map)
7450 return NULL;
7452 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7453 map->dim, isl_dim_out),
7454 goto error);
7455 dim = isl_map_get_space(map);
7456 dim = isl_space_domain(dim);
7457 result = isl_set_alloc_space(dim, map->n, 0);
7458 if (!result)
7459 goto error;
7460 for (i = 0; i < map->n; ++i)
7461 result = isl_set_add_basic_set(result,
7462 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7463 isl_map_free(map);
7464 return result;
7465 error:
7466 isl_map_free(map);
7467 return NULL;
7471 * returns [domain -> range] -> range - domain
7473 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7474 __isl_take isl_basic_map *bmap)
7476 int i, k;
7477 isl_space *dim;
7478 isl_basic_map *domain;
7479 int nparam, n;
7480 unsigned total;
7482 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7483 bmap->dim, isl_dim_out))
7484 isl_die(bmap->ctx, isl_error_invalid,
7485 "domain and range don't match", goto error);
7487 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7488 n = isl_basic_map_dim(bmap, isl_dim_in);
7490 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7491 domain = isl_basic_map_universe(dim);
7493 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7494 bmap = isl_basic_map_apply_range(bmap, domain);
7495 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7497 total = isl_basic_map_total_dim(bmap);
7499 for (i = 0; i < n; ++i) {
7500 k = isl_basic_map_alloc_equality(bmap);
7501 if (k < 0)
7502 goto error;
7503 isl_seq_clr(bmap->eq[k], 1 + total);
7504 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7505 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7506 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7509 bmap = isl_basic_map_gauss(bmap, NULL);
7510 return isl_basic_map_finalize(bmap);
7511 error:
7512 isl_basic_map_free(bmap);
7513 return NULL;
7517 * returns [domain -> range] -> range - domain
7519 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7521 int i;
7522 isl_space *domain_dim;
7524 if (!map)
7525 return NULL;
7527 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7528 map->dim, isl_dim_out))
7529 isl_die(map->ctx, isl_error_invalid,
7530 "domain and range don't match", goto error);
7532 map = isl_map_cow(map);
7533 if (!map)
7534 return NULL;
7536 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7537 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7538 map->dim = isl_space_join(map->dim, domain_dim);
7539 if (!map->dim)
7540 goto error;
7541 for (i = 0; i < map->n; ++i) {
7542 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7543 if (!map->p[i])
7544 goto error;
7546 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7547 return map;
7548 error:
7549 isl_map_free(map);
7550 return NULL;
7553 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7555 struct isl_basic_map *bmap;
7556 unsigned nparam;
7557 unsigned dim;
7558 int i;
7560 if (!dims)
7561 return NULL;
7563 nparam = dims->nparam;
7564 dim = dims->n_out;
7565 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7566 if (!bmap)
7567 goto error;
7569 for (i = 0; i < dim; ++i) {
7570 int j = isl_basic_map_alloc_equality(bmap);
7571 if (j < 0)
7572 goto error;
7573 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7574 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7575 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7577 return isl_basic_map_finalize(bmap);
7578 error:
7579 isl_basic_map_free(bmap);
7580 return NULL;
7583 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7585 if (!dim)
7586 return NULL;
7587 if (dim->n_in != dim->n_out)
7588 isl_die(dim->ctx, isl_error_invalid,
7589 "number of input and output dimensions needs to be "
7590 "the same", goto error);
7591 return basic_map_identity(dim);
7592 error:
7593 isl_space_free(dim);
7594 return NULL;
7597 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7599 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7602 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7604 isl_space *dim = isl_set_get_space(set);
7605 isl_map *id;
7606 id = isl_map_identity(isl_space_map_from_set(dim));
7607 return isl_map_intersect_range(id, set);
7610 /* Construct a basic set with all set dimensions having only non-negative
7611 * values.
7613 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7614 __isl_take isl_space *space)
7616 int i;
7617 unsigned nparam;
7618 unsigned dim;
7619 struct isl_basic_set *bset;
7621 if (!space)
7622 return NULL;
7623 nparam = space->nparam;
7624 dim = space->n_out;
7625 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7626 if (!bset)
7627 return NULL;
7628 for (i = 0; i < dim; ++i) {
7629 int k = isl_basic_set_alloc_inequality(bset);
7630 if (k < 0)
7631 goto error;
7632 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7633 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7635 return bset;
7636 error:
7637 isl_basic_set_free(bset);
7638 return NULL;
7641 /* Construct the half-space x_pos >= 0.
7643 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7644 int pos)
7646 int k;
7647 isl_basic_set *nonneg;
7649 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7650 k = isl_basic_set_alloc_inequality(nonneg);
7651 if (k < 0)
7652 goto error;
7653 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7654 isl_int_set_si(nonneg->ineq[k][pos], 1);
7656 return isl_basic_set_finalize(nonneg);
7657 error:
7658 isl_basic_set_free(nonneg);
7659 return NULL;
7662 /* Construct the half-space x_pos <= -1.
7664 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7666 int k;
7667 isl_basic_set *neg;
7669 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7670 k = isl_basic_set_alloc_inequality(neg);
7671 if (k < 0)
7672 goto error;
7673 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7674 isl_int_set_si(neg->ineq[k][0], -1);
7675 isl_int_set_si(neg->ineq[k][pos], -1);
7677 return isl_basic_set_finalize(neg);
7678 error:
7679 isl_basic_set_free(neg);
7680 return NULL;
7683 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7684 enum isl_dim_type type, unsigned first, unsigned n)
7686 int i;
7687 unsigned offset;
7688 isl_basic_set *nonneg;
7689 isl_basic_set *neg;
7691 if (!set)
7692 return NULL;
7693 if (n == 0)
7694 return set;
7696 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7698 offset = pos(set->dim, type);
7699 for (i = 0; i < n; ++i) {
7700 nonneg = nonneg_halfspace(isl_set_get_space(set),
7701 offset + first + i);
7702 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7704 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7707 return set;
7708 error:
7709 isl_set_free(set);
7710 return NULL;
7713 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7714 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7715 void *user)
7717 isl_set *half;
7719 if (!set)
7720 return -1;
7721 if (isl_set_plain_is_empty(set)) {
7722 isl_set_free(set);
7723 return 0;
7725 if (first == len)
7726 return fn(set, signs, user);
7728 signs[first] = 1;
7729 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7730 1 + first));
7731 half = isl_set_intersect(half, isl_set_copy(set));
7732 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7733 goto error;
7735 signs[first] = -1;
7736 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7737 1 + first));
7738 half = isl_set_intersect(half, set);
7739 return foreach_orthant(half, signs, first + 1, len, fn, user);
7740 error:
7741 isl_set_free(set);
7742 return -1;
7745 /* Call "fn" on the intersections of "set" with each of the orthants
7746 * (except for obviously empty intersections). The orthant is identified
7747 * by the signs array, with each entry having value 1 or -1 according
7748 * to the sign of the corresponding variable.
7750 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7751 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7752 void *user)
7754 unsigned nparam;
7755 unsigned nvar;
7756 int *signs;
7757 int r;
7759 if (!set)
7760 return -1;
7761 if (isl_set_plain_is_empty(set))
7762 return 0;
7764 nparam = isl_set_dim(set, isl_dim_param);
7765 nvar = isl_set_dim(set, isl_dim_set);
7767 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7769 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7770 fn, user);
7772 free(signs);
7774 return r;
7777 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7779 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7782 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7783 __isl_keep isl_basic_map *bmap2)
7785 int is_subset;
7786 struct isl_map *map1;
7787 struct isl_map *map2;
7789 if (!bmap1 || !bmap2)
7790 return isl_bool_error;
7792 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7793 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7795 is_subset = isl_map_is_subset(map1, map2);
7797 isl_map_free(map1);
7798 isl_map_free(map2);
7800 return is_subset;
7803 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7804 __isl_keep isl_basic_set *bset2)
7806 return isl_basic_map_is_subset(bset1, bset2);
7809 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7810 __isl_keep isl_basic_map *bmap2)
7812 isl_bool is_subset;
7814 if (!bmap1 || !bmap2)
7815 return isl_bool_error;
7816 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7817 if (is_subset != isl_bool_true)
7818 return is_subset;
7819 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7820 return is_subset;
7823 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7824 __isl_keep isl_basic_set *bset2)
7826 return isl_basic_map_is_equal(
7827 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7830 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7832 int i;
7833 int is_empty;
7835 if (!map)
7836 return isl_bool_error;
7837 for (i = 0; i < map->n; ++i) {
7838 is_empty = isl_basic_map_is_empty(map->p[i]);
7839 if (is_empty < 0)
7840 return isl_bool_error;
7841 if (!is_empty)
7842 return isl_bool_false;
7844 return isl_bool_true;
7847 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7849 return map ? map->n == 0 : isl_bool_error;
7852 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7854 return set ? set->n == 0 : isl_bool_error;
7857 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7859 return isl_map_is_empty((struct isl_map *)set);
7862 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7864 if (!map1 || !map2)
7865 return -1;
7867 return isl_space_is_equal(map1->dim, map2->dim);
7870 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7872 if (!set1 || !set2)
7873 return -1;
7875 return isl_space_is_equal(set1->dim, set2->dim);
7878 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7880 isl_bool is_subset;
7882 if (!map1 || !map2)
7883 return isl_bool_error;
7884 is_subset = isl_map_is_subset(map1, map2);
7885 if (is_subset != isl_bool_true)
7886 return is_subset;
7887 is_subset = isl_map_is_subset(map2, map1);
7888 return is_subset;
7891 /* Is "map1" equal to "map2"?
7893 * First check if they are obviously equal.
7894 * If not, then perform a more detailed analysis.
7896 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7898 isl_bool equal;
7900 equal = isl_map_plain_is_equal(map1, map2);
7901 if (equal < 0 || equal)
7902 return equal;
7903 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7906 isl_bool isl_basic_map_is_strict_subset(
7907 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7909 isl_bool is_subset;
7911 if (!bmap1 || !bmap2)
7912 return isl_bool_error;
7913 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7914 if (is_subset != isl_bool_true)
7915 return is_subset;
7916 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7917 if (is_subset == isl_bool_error)
7918 return is_subset;
7919 return !is_subset;
7922 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7923 __isl_keep isl_map *map2)
7925 isl_bool is_subset;
7927 if (!map1 || !map2)
7928 return isl_bool_error;
7929 is_subset = isl_map_is_subset(map1, map2);
7930 if (is_subset != isl_bool_true)
7931 return is_subset;
7932 is_subset = isl_map_is_subset(map2, map1);
7933 if (is_subset == isl_bool_error)
7934 return is_subset;
7935 return !is_subset;
7938 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7939 __isl_keep isl_set *set2)
7941 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7944 /* Is "bmap" obviously equal to the universe with the same space?
7946 * That is, does it not have any constraints?
7948 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
7950 if (!bmap)
7951 return isl_bool_error;
7952 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7955 /* Is "bset" obviously equal to the universe with the same space?
7957 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
7959 return isl_basic_map_plain_is_universe(bset);
7962 /* If "c" does not involve any existentially quantified variables,
7963 * then set *univ to false and abort
7965 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
7967 isl_bool *univ = user;
7968 unsigned n;
7970 n = isl_constraint_dim(c, isl_dim_div);
7971 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
7972 isl_constraint_free(c);
7973 if (*univ < 0 || !*univ)
7974 return isl_stat_error;
7975 return isl_stat_ok;
7978 /* Is "bmap" equal to the universe with the same space?
7980 * First check if it is obviously equal to the universe.
7981 * If not and if there are any constraints not involving
7982 * existentially quantified variables, then it is certainly
7983 * not equal to the universe.
7984 * Otherwise, check if the universe is a subset of "bmap".
7986 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
7988 isl_bool univ;
7989 isl_basic_map *test;
7991 univ = isl_basic_map_plain_is_universe(bmap);
7992 if (univ < 0 || univ)
7993 return univ;
7994 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
7995 return isl_bool_false;
7996 univ = isl_bool_true;
7997 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
7998 univ)
7999 return isl_bool_error;
8000 if (univ < 0 || !univ)
8001 return univ;
8002 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8003 univ = isl_basic_map_is_subset(test, bmap);
8004 isl_basic_map_free(test);
8005 return univ;
8008 /* Is "bset" equal to the universe with the same space?
8010 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8012 return isl_basic_map_is_universe(bset);
8015 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8017 int i;
8019 if (!map)
8020 return isl_bool_error;
8022 for (i = 0; i < map->n; ++i) {
8023 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8024 if (r < 0 || r)
8025 return r;
8028 return isl_bool_false;
8031 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8033 return isl_map_plain_is_universe((isl_map *) set);
8036 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8038 struct isl_basic_set *bset = NULL;
8039 struct isl_vec *sample = NULL;
8040 isl_bool empty, non_empty;
8042 if (!bmap)
8043 return isl_bool_error;
8045 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8046 return isl_bool_true;
8048 if (isl_basic_map_plain_is_universe(bmap))
8049 return isl_bool_false;
8051 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8052 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8053 copy = isl_basic_map_remove_redundancies(copy);
8054 empty = isl_basic_map_plain_is_empty(copy);
8055 isl_basic_map_free(copy);
8056 return empty;
8059 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8060 if (non_empty < 0)
8061 return isl_bool_error;
8062 if (non_empty)
8063 return isl_bool_false;
8064 isl_vec_free(bmap->sample);
8065 bmap->sample = NULL;
8066 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8067 if (!bset)
8068 return isl_bool_error;
8069 sample = isl_basic_set_sample_vec(bset);
8070 if (!sample)
8071 return isl_bool_error;
8072 empty = sample->size == 0;
8073 isl_vec_free(bmap->sample);
8074 bmap->sample = sample;
8075 if (empty)
8076 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8078 return empty;
8081 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8083 if (!bmap)
8084 return isl_bool_error;
8085 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8088 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8090 if (!bset)
8091 return isl_bool_error;
8092 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8095 /* Is "bmap" known to be non-empty?
8097 * That is, is the cached sample still valid?
8099 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8101 unsigned total;
8103 if (!bmap)
8104 return isl_bool_error;
8105 if (!bmap->sample)
8106 return isl_bool_false;
8107 total = 1 + isl_basic_map_total_dim(bmap);
8108 if (bmap->sample->size != total)
8109 return isl_bool_false;
8110 return isl_basic_map_contains(bmap, bmap->sample);
8113 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8115 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8118 struct isl_map *isl_basic_map_union(
8119 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8121 struct isl_map *map;
8122 if (!bmap1 || !bmap2)
8123 goto error;
8125 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8127 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8128 if (!map)
8129 goto error;
8130 map = isl_map_add_basic_map(map, bmap1);
8131 map = isl_map_add_basic_map(map, bmap2);
8132 return map;
8133 error:
8134 isl_basic_map_free(bmap1);
8135 isl_basic_map_free(bmap2);
8136 return NULL;
8139 struct isl_set *isl_basic_set_union(
8140 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8142 return (struct isl_set *)isl_basic_map_union(
8143 (struct isl_basic_map *)bset1,
8144 (struct isl_basic_map *)bset2);
8147 /* Order divs such that any div only depends on previous divs */
8148 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8150 int i;
8151 unsigned off;
8153 if (!bmap)
8154 return NULL;
8156 off = isl_space_dim(bmap->dim, isl_dim_all);
8158 for (i = 0; i < bmap->n_div; ++i) {
8159 int pos;
8160 if (isl_int_is_zero(bmap->div[i][0]))
8161 continue;
8162 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8163 bmap->n_div-i);
8164 if (pos == -1)
8165 continue;
8166 if (pos == 0)
8167 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8168 "integer division depends on itself",
8169 return isl_basic_map_free(bmap));
8170 isl_basic_map_swap_div(bmap, i, i + pos);
8171 --i;
8173 return bmap;
8176 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8178 return (struct isl_basic_set *)
8179 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8182 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8184 int i;
8186 if (!map)
8187 return 0;
8189 for (i = 0; i < map->n; ++i) {
8190 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8191 if (!map->p[i])
8192 goto error;
8195 return map;
8196 error:
8197 isl_map_free(map);
8198 return NULL;
8201 /* Apply the expansion computed by isl_merge_divs.
8202 * The expansion itself is given by "exp" while the resulting
8203 * list of divs is given by "div".
8205 * Move the integer divisions of "bmap" into the right position
8206 * according to "exp" and then introduce the additional integer
8207 * divisions, adding div constraints.
8208 * The moving should be done first to avoid moving coefficients
8209 * in the definitions of the extra integer divisions.
8211 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8212 __isl_take isl_basic_set *bmap, __isl_take isl_mat *div, int *exp)
8214 int i, j;
8215 int n_div;
8217 bmap = isl_basic_map_cow(bmap);
8218 if (!bmap || !div)
8219 goto error;
8221 if (div->n_row < bmap->n_div)
8222 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8223 "not an expansion", goto error);
8225 n_div = bmap->n_div;
8226 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8227 div->n_row - n_div, 0,
8228 2 * (div->n_row - n_div));
8230 for (i = n_div; i < div->n_row; ++i)
8231 if (isl_basic_map_alloc_div(bmap) < 0)
8232 goto error;
8234 for (j = n_div - 1; j >= 0; --j) {
8235 if (exp[j] == j)
8236 break;
8237 isl_basic_map_swap_div(bmap, j, exp[j]);
8239 j = 0;
8240 for (i = 0; i < div->n_row; ++i) {
8241 if (j < n_div && exp[j] == i) {
8242 j++;
8243 } else {
8244 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8245 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8246 continue;
8247 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8248 goto error;
8252 isl_mat_free(div);
8253 return bmap;
8254 error:
8255 isl_basic_map_free(bmap);
8256 isl_mat_free(div);
8257 return NULL;
8260 /* Apply the expansion computed by isl_merge_divs.
8261 * The expansion itself is given by "exp" while the resulting
8262 * list of divs is given by "div".
8264 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8265 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8267 return isl_basic_map_expand_divs(bset, div, exp);
8270 /* Look for a div in dst that corresponds to the div "div" in src.
8271 * The divs before "div" in src and dst are assumed to be the same.
8273 * Returns -1 if no corresponding div was found and the position
8274 * of the corresponding div in dst otherwise.
8276 static int find_div(struct isl_basic_map *dst,
8277 struct isl_basic_map *src, unsigned div)
8279 int i;
8281 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8283 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8284 for (i = div; i < dst->n_div; ++i)
8285 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8286 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8287 dst->n_div - div) == -1)
8288 return i;
8289 return -1;
8292 /* Align the divs of "dst" to those of "src", adding divs from "src"
8293 * if needed. That is, make sure that the first src->n_div divs
8294 * of the result are equal to those of src.
8296 * The result is not finalized as by design it will have redundant
8297 * divs if any divs from "src" were copied.
8299 __isl_give isl_basic_map *isl_basic_map_align_divs(
8300 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8302 int i;
8303 int known, extended;
8304 unsigned total;
8306 if (!dst || !src)
8307 return isl_basic_map_free(dst);
8309 if (src->n_div == 0)
8310 return dst;
8312 known = isl_basic_map_divs_known(src);
8313 if (known < 0)
8314 return isl_basic_map_free(dst);
8315 if (!known)
8316 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8317 "some src divs are unknown",
8318 return isl_basic_map_free(dst));
8320 src = isl_basic_map_order_divs(src);
8322 extended = 0;
8323 total = isl_space_dim(src->dim, isl_dim_all);
8324 for (i = 0; i < src->n_div; ++i) {
8325 int j = find_div(dst, src, i);
8326 if (j < 0) {
8327 if (!extended) {
8328 int extra = src->n_div - i;
8329 dst = isl_basic_map_cow(dst);
8330 if (!dst)
8331 return NULL;
8332 dst = isl_basic_map_extend_space(dst,
8333 isl_space_copy(dst->dim),
8334 extra, 0, 2 * extra);
8335 extended = 1;
8337 j = isl_basic_map_alloc_div(dst);
8338 if (j < 0)
8339 return isl_basic_map_free(dst);
8340 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8341 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8342 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8343 return isl_basic_map_free(dst);
8345 if (j != i)
8346 isl_basic_map_swap_div(dst, i, j);
8348 return dst;
8351 struct isl_basic_set *isl_basic_set_align_divs(
8352 struct isl_basic_set *dst, struct isl_basic_set *src)
8354 return (struct isl_basic_set *)isl_basic_map_align_divs(
8355 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8358 struct isl_map *isl_map_align_divs(struct isl_map *map)
8360 int i;
8362 if (!map)
8363 return NULL;
8364 if (map->n == 0)
8365 return map;
8366 map = isl_map_compute_divs(map);
8367 map = isl_map_cow(map);
8368 if (!map)
8369 return NULL;
8371 for (i = 1; i < map->n; ++i)
8372 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8373 for (i = 1; i < map->n; ++i) {
8374 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8375 if (!map->p[i])
8376 return isl_map_free(map);
8379 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8380 return map;
8383 struct isl_set *isl_set_align_divs(struct isl_set *set)
8385 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8388 /* Align the divs of the basic maps in "map" to those
8389 * of the basic maps in "list", as well as to the other basic maps in "map".
8390 * The elements in "list" are assumed to have known divs.
8392 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8393 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8395 int i, n;
8397 map = isl_map_compute_divs(map);
8398 map = isl_map_cow(map);
8399 if (!map || !list)
8400 return isl_map_free(map);
8401 if (map->n == 0)
8402 return map;
8404 n = isl_basic_map_list_n_basic_map(list);
8405 for (i = 0; i < n; ++i) {
8406 isl_basic_map *bmap;
8408 bmap = isl_basic_map_list_get_basic_map(list, i);
8409 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8410 isl_basic_map_free(bmap);
8412 if (!map->p[0])
8413 return isl_map_free(map);
8415 return isl_map_align_divs(map);
8418 /* Align the divs of each element of "list" to those of "bmap".
8419 * Both "bmap" and the elements of "list" are assumed to have known divs.
8421 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8422 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8424 int i, n;
8426 if (!list || !bmap)
8427 return isl_basic_map_list_free(list);
8429 n = isl_basic_map_list_n_basic_map(list);
8430 for (i = 0; i < n; ++i) {
8431 isl_basic_map *bmap_i;
8433 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8434 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8435 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8438 return list;
8441 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8442 __isl_take isl_map *map)
8444 if (!set || !map)
8445 goto error;
8446 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8447 map = isl_map_intersect_domain(map, set);
8448 set = isl_map_range(map);
8449 return set;
8450 error:
8451 isl_set_free(set);
8452 isl_map_free(map);
8453 return NULL;
8456 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8457 __isl_take isl_map *map)
8459 return isl_map_align_params_map_map_and(set, map, &set_apply);
8462 /* There is no need to cow as removing empty parts doesn't change
8463 * the meaning of the set.
8465 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8467 int i;
8469 if (!map)
8470 return NULL;
8472 for (i = map->n - 1; i >= 0; --i)
8473 remove_if_empty(map, i);
8475 return map;
8478 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8480 return (struct isl_set *)
8481 isl_map_remove_empty_parts((struct isl_map *)set);
8484 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8486 struct isl_basic_map *bmap;
8487 if (!map || map->n == 0)
8488 return NULL;
8489 bmap = map->p[map->n-1];
8490 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8491 return isl_basic_map_copy(bmap);
8494 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8496 return (struct isl_basic_set *)
8497 isl_map_copy_basic_map((struct isl_map *)set);
8500 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8501 __isl_keep isl_basic_map *bmap)
8503 int i;
8505 if (!map || !bmap)
8506 goto error;
8507 for (i = map->n-1; i >= 0; --i) {
8508 if (map->p[i] != bmap)
8509 continue;
8510 map = isl_map_cow(map);
8511 if (!map)
8512 goto error;
8513 isl_basic_map_free(map->p[i]);
8514 if (i != map->n-1) {
8515 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8516 map->p[i] = map->p[map->n-1];
8518 map->n--;
8519 return map;
8521 return map;
8522 error:
8523 isl_map_free(map);
8524 return NULL;
8527 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8528 struct isl_basic_set *bset)
8530 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8531 (struct isl_basic_map *)bset);
8534 /* Given two basic sets bset1 and bset2, compute the maximal difference
8535 * between the values of dimension pos in bset1 and those in bset2
8536 * for any common value of the parameters and dimensions preceding pos.
8538 static enum isl_lp_result basic_set_maximal_difference_at(
8539 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8540 int pos, isl_int *opt)
8542 isl_space *dims;
8543 struct isl_basic_map *bmap1 = NULL;
8544 struct isl_basic_map *bmap2 = NULL;
8545 struct isl_ctx *ctx;
8546 struct isl_vec *obj;
8547 unsigned total;
8548 unsigned nparam;
8549 unsigned dim1, dim2;
8550 enum isl_lp_result res;
8552 if (!bset1 || !bset2)
8553 return isl_lp_error;
8555 nparam = isl_basic_set_n_param(bset1);
8556 dim1 = isl_basic_set_n_dim(bset1);
8557 dim2 = isl_basic_set_n_dim(bset2);
8558 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8559 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8560 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8561 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8562 if (!bmap1 || !bmap2)
8563 goto error;
8564 bmap1 = isl_basic_map_cow(bmap1);
8565 bmap1 = isl_basic_map_extend(bmap1, nparam,
8566 pos, (dim1 - pos) + (dim2 - pos),
8567 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8568 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8569 if (!bmap1)
8570 goto error2;
8571 total = isl_basic_map_total_dim(bmap1);
8572 ctx = bmap1->ctx;
8573 obj = isl_vec_alloc(ctx, 1 + total);
8574 if (!obj)
8575 goto error2;
8576 isl_seq_clr(obj->block.data, 1 + total);
8577 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8578 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8579 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8580 opt, NULL, NULL);
8581 isl_basic_map_free(bmap1);
8582 isl_vec_free(obj);
8583 return res;
8584 error:
8585 isl_basic_map_free(bmap2);
8586 error2:
8587 isl_basic_map_free(bmap1);
8588 return isl_lp_error;
8591 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8592 * for any common value of the parameters and dimensions preceding pos
8593 * in both basic sets, the values of dimension pos in bset1 are
8594 * smaller or larger than those in bset2.
8596 * Returns
8597 * 1 if bset1 follows bset2
8598 * -1 if bset1 precedes bset2
8599 * 0 if bset1 and bset2 are incomparable
8600 * -2 if some error occurred.
8602 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8603 struct isl_basic_set *bset2, int pos)
8605 isl_int opt;
8606 enum isl_lp_result res;
8607 int cmp;
8609 isl_int_init(opt);
8611 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8613 if (res == isl_lp_empty)
8614 cmp = 0;
8615 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8616 res == isl_lp_unbounded)
8617 cmp = 1;
8618 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8619 cmp = -1;
8620 else
8621 cmp = -2;
8623 isl_int_clear(opt);
8624 return cmp;
8627 /* Given two basic sets bset1 and bset2, check whether
8628 * for any common value of the parameters and dimensions preceding pos
8629 * there is a value of dimension pos in bset1 that is larger
8630 * than a value of the same dimension in bset2.
8632 * Return
8633 * 1 if there exists such a pair
8634 * 0 if there is no such pair, but there is a pair of equal values
8635 * -1 otherwise
8636 * -2 if some error occurred.
8638 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8639 __isl_keep isl_basic_set *bset2, int pos)
8641 isl_int opt;
8642 enum isl_lp_result res;
8643 int cmp;
8645 isl_int_init(opt);
8647 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8649 if (res == isl_lp_empty)
8650 cmp = -1;
8651 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8652 res == isl_lp_unbounded)
8653 cmp = 1;
8654 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8655 cmp = -1;
8656 else if (res == isl_lp_ok)
8657 cmp = 0;
8658 else
8659 cmp = -2;
8661 isl_int_clear(opt);
8662 return cmp;
8665 /* Given two sets set1 and set2, check whether
8666 * for any common value of the parameters and dimensions preceding pos
8667 * there is a value of dimension pos in set1 that is larger
8668 * than a value of the same dimension in set2.
8670 * Return
8671 * 1 if there exists such a pair
8672 * 0 if there is no such pair, but there is a pair of equal values
8673 * -1 otherwise
8674 * -2 if some error occurred.
8676 int isl_set_follows_at(__isl_keep isl_set *set1,
8677 __isl_keep isl_set *set2, int pos)
8679 int i, j;
8680 int follows = -1;
8682 if (!set1 || !set2)
8683 return -2;
8685 for (i = 0; i < set1->n; ++i)
8686 for (j = 0; j < set2->n; ++j) {
8687 int f;
8688 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8689 if (f == 1 || f == -2)
8690 return f;
8691 if (f > follows)
8692 follows = f;
8695 return follows;
8698 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8699 unsigned pos, isl_int *val)
8701 int i;
8702 int d;
8703 unsigned total;
8705 if (!bmap)
8706 return -1;
8707 total = isl_basic_map_total_dim(bmap);
8708 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8709 for (; d+1 > pos; --d)
8710 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8711 break;
8712 if (d != pos)
8713 continue;
8714 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8715 return 0;
8716 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8717 return 0;
8718 if (!isl_int_is_one(bmap->eq[i][1+d]))
8719 return 0;
8720 if (val)
8721 isl_int_neg(*val, bmap->eq[i][0]);
8722 return 1;
8724 return 0;
8727 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8728 unsigned pos, isl_int *val)
8730 int i;
8731 isl_int v;
8732 isl_int tmp;
8733 int fixed;
8735 if (!map)
8736 return -1;
8737 if (map->n == 0)
8738 return 0;
8739 if (map->n == 1)
8740 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8741 isl_int_init(v);
8742 isl_int_init(tmp);
8743 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8744 for (i = 1; fixed == 1 && i < map->n; ++i) {
8745 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8746 if (fixed == 1 && isl_int_ne(tmp, v))
8747 fixed = 0;
8749 if (val)
8750 isl_int_set(*val, v);
8751 isl_int_clear(tmp);
8752 isl_int_clear(v);
8753 return fixed;
8756 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8757 unsigned pos, isl_int *val)
8759 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8760 pos, val);
8763 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8764 isl_int *val)
8766 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8769 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8770 enum isl_dim_type type, unsigned pos, isl_int *val)
8772 if (pos >= isl_basic_map_dim(bmap, type))
8773 return -1;
8774 return isl_basic_map_plain_has_fixed_var(bmap,
8775 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8778 /* If "bmap" obviously lies on a hyperplane where the given dimension
8779 * has a fixed value, then return that value.
8780 * Otherwise return NaN.
8782 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8783 __isl_keep isl_basic_map *bmap,
8784 enum isl_dim_type type, unsigned pos)
8786 isl_ctx *ctx;
8787 isl_val *v;
8788 int fixed;
8790 if (!bmap)
8791 return NULL;
8792 ctx = isl_basic_map_get_ctx(bmap);
8793 v = isl_val_alloc(ctx);
8794 if (!v)
8795 return NULL;
8796 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8797 if (fixed < 0)
8798 return isl_val_free(v);
8799 if (fixed) {
8800 isl_int_set_si(v->d, 1);
8801 return v;
8803 isl_val_free(v);
8804 return isl_val_nan(ctx);
8807 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8808 enum isl_dim_type type, unsigned pos, isl_int *val)
8810 if (pos >= isl_map_dim(map, type))
8811 return -1;
8812 return isl_map_plain_has_fixed_var(map,
8813 map_offset(map, type) - 1 + pos, val);
8816 /* If "map" obviously lies on a hyperplane where the given dimension
8817 * has a fixed value, then return that value.
8818 * Otherwise return NaN.
8820 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8821 enum isl_dim_type type, unsigned pos)
8823 isl_ctx *ctx;
8824 isl_val *v;
8825 int fixed;
8827 if (!map)
8828 return NULL;
8829 ctx = isl_map_get_ctx(map);
8830 v = isl_val_alloc(ctx);
8831 if (!v)
8832 return NULL;
8833 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8834 if (fixed < 0)
8835 return isl_val_free(v);
8836 if (fixed) {
8837 isl_int_set_si(v->d, 1);
8838 return v;
8840 isl_val_free(v);
8841 return isl_val_nan(ctx);
8844 /* If "set" obviously lies on a hyperplane where the given dimension
8845 * has a fixed value, then return that value.
8846 * Otherwise return NaN.
8848 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8849 enum isl_dim_type type, unsigned pos)
8851 return isl_map_plain_get_val_if_fixed(set, type, pos);
8854 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8855 enum isl_dim_type type, unsigned pos, isl_int *val)
8857 return isl_map_plain_is_fixed(set, type, pos, val);
8860 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8861 * then return this fixed value in *val.
8863 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8864 unsigned dim, isl_int *val)
8866 return isl_basic_set_plain_has_fixed_var(bset,
8867 isl_basic_set_n_param(bset) + dim, val);
8870 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8871 * then return this fixed value in *val.
8873 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8874 unsigned dim, isl_int *val)
8876 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8879 /* Check if input variable in has fixed value and if so and if val is not NULL,
8880 * then return this fixed value in *val.
8882 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8883 unsigned in, isl_int *val)
8885 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8888 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8889 * and if val is not NULL, then return this lower bound in *val.
8891 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8892 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8894 int i, i_eq = -1, i_ineq = -1;
8895 isl_int *c;
8896 unsigned total;
8897 unsigned nparam;
8899 if (!bset)
8900 return -1;
8901 total = isl_basic_set_total_dim(bset);
8902 nparam = isl_basic_set_n_param(bset);
8903 for (i = 0; i < bset->n_eq; ++i) {
8904 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8905 continue;
8906 if (i_eq != -1)
8907 return 0;
8908 i_eq = i;
8910 for (i = 0; i < bset->n_ineq; ++i) {
8911 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8912 continue;
8913 if (i_eq != -1 || i_ineq != -1)
8914 return 0;
8915 i_ineq = i;
8917 if (i_eq == -1 && i_ineq == -1)
8918 return 0;
8919 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8920 /* The coefficient should always be one due to normalization. */
8921 if (!isl_int_is_one(c[1+nparam+dim]))
8922 return 0;
8923 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8924 return 0;
8925 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8926 total - nparam - dim - 1) != -1)
8927 return 0;
8928 if (val)
8929 isl_int_neg(*val, c[0]);
8930 return 1;
8933 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8934 unsigned dim, isl_int *val)
8936 int i;
8937 isl_int v;
8938 isl_int tmp;
8939 int fixed;
8941 if (!set)
8942 return -1;
8943 if (set->n == 0)
8944 return 0;
8945 if (set->n == 1)
8946 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8947 dim, val);
8948 isl_int_init(v);
8949 isl_int_init(tmp);
8950 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8951 dim, &v);
8952 for (i = 1; fixed == 1 && i < set->n; ++i) {
8953 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8954 dim, &tmp);
8955 if (fixed == 1 && isl_int_ne(tmp, v))
8956 fixed = 0;
8958 if (val)
8959 isl_int_set(*val, v);
8960 isl_int_clear(tmp);
8961 isl_int_clear(v);
8962 return fixed;
8965 /* Return -1 if the constraint "c1" should be sorted before "c2"
8966 * and 1 if it should be sorted after "c2".
8967 * Return 0 if the two constraints are the same (up to the constant term).
8969 * In particular, if a constraint involves later variables than another
8970 * then it is sorted after this other constraint.
8971 * uset_gist depends on constraints without existentially quantified
8972 * variables sorting first.
8974 * For constraints that have the same latest variable, those
8975 * with the same coefficient for this latest variable (first in absolute value
8976 * and then in actual value) are grouped together.
8977 * This is useful for detecting pairs of constraints that can
8978 * be chained in their printed representation.
8980 * Finally, within a group, constraints are sorted according to
8981 * their coefficients (excluding the constant term).
8983 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
8985 isl_int **c1 = (isl_int **) p1;
8986 isl_int **c2 = (isl_int **) p2;
8987 int l1, l2;
8988 unsigned size = *(unsigned *) arg;
8989 int cmp;
8991 l1 = isl_seq_last_non_zero(*c1 + 1, size);
8992 l2 = isl_seq_last_non_zero(*c2 + 1, size);
8994 if (l1 != l2)
8995 return l1 - l2;
8997 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
8998 if (cmp != 0)
8999 return cmp;
9000 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9001 if (cmp != 0)
9002 return -cmp;
9004 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9007 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9008 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9009 * and 0 if the two constraints are the same (up to the constant term).
9011 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9012 isl_int *c1, isl_int *c2)
9014 unsigned total;
9016 if (!bmap)
9017 return -2;
9018 total = isl_basic_map_total_dim(bmap);
9019 return sort_constraint_cmp(&c1, &c2, &total);
9022 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9023 __isl_take isl_basic_map *bmap)
9025 unsigned total;
9027 if (!bmap)
9028 return NULL;
9029 if (bmap->n_ineq == 0)
9030 return bmap;
9031 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9032 return bmap;
9033 total = isl_basic_map_total_dim(bmap);
9034 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9035 &sort_constraint_cmp, &total) < 0)
9036 return isl_basic_map_free(bmap);
9037 return bmap;
9040 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9041 __isl_take isl_basic_set *bset)
9043 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9044 (struct isl_basic_map *)bset);
9047 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9049 if (!bmap)
9050 return NULL;
9051 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9052 return bmap;
9053 bmap = isl_basic_map_remove_redundancies(bmap);
9054 bmap = isl_basic_map_sort_constraints(bmap);
9055 if (bmap)
9056 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9057 return bmap;
9060 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9062 return (struct isl_basic_set *)isl_basic_map_normalize(
9063 (struct isl_basic_map *)bset);
9066 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9067 const __isl_keep isl_basic_map *bmap2)
9069 int i, cmp;
9070 unsigned total;
9072 if (!bmap1 || !bmap2)
9073 return -1;
9075 if (bmap1 == bmap2)
9076 return 0;
9077 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9078 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9079 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9080 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9081 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9082 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9083 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9084 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9085 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9086 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9087 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9088 return 0;
9089 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9090 return 1;
9091 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9092 return -1;
9093 if (bmap1->n_eq != bmap2->n_eq)
9094 return bmap1->n_eq - bmap2->n_eq;
9095 if (bmap1->n_ineq != bmap2->n_ineq)
9096 return bmap1->n_ineq - bmap2->n_ineq;
9097 if (bmap1->n_div != bmap2->n_div)
9098 return bmap1->n_div - bmap2->n_div;
9099 total = isl_basic_map_total_dim(bmap1);
9100 for (i = 0; i < bmap1->n_eq; ++i) {
9101 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9102 if (cmp)
9103 return cmp;
9105 for (i = 0; i < bmap1->n_ineq; ++i) {
9106 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9107 if (cmp)
9108 return cmp;
9110 for (i = 0; i < bmap1->n_div; ++i) {
9111 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9112 if (cmp)
9113 return cmp;
9115 return 0;
9118 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9119 const __isl_keep isl_basic_set *bset2)
9121 return isl_basic_map_plain_cmp(bset1, bset2);
9124 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9126 int i, cmp;
9128 if (set1 == set2)
9129 return 0;
9130 if (set1->n != set2->n)
9131 return set1->n - set2->n;
9133 for (i = 0; i < set1->n; ++i) {
9134 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9135 if (cmp)
9136 return cmp;
9139 return 0;
9142 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9143 __isl_keep isl_basic_map *bmap2)
9145 if (!bmap1 || !bmap2)
9146 return isl_bool_error;
9147 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9150 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9151 __isl_keep isl_basic_set *bset2)
9153 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9154 (isl_basic_map *)bset2);
9157 static int qsort_bmap_cmp(const void *p1, const void *p2)
9159 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9160 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9162 return isl_basic_map_plain_cmp(bmap1, bmap2);
9165 /* Sort the basic maps of "map" and remove duplicate basic maps.
9167 * While removing basic maps, we make sure that the basic maps remain
9168 * sorted because isl_map_normalize expects the basic maps of the result
9169 * to be sorted.
9171 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9173 int i, j;
9175 map = isl_map_remove_empty_parts(map);
9176 if (!map)
9177 return NULL;
9178 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9179 for (i = map->n - 1; i >= 1; --i) {
9180 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9181 continue;
9182 isl_basic_map_free(map->p[i-1]);
9183 for (j = i; j < map->n; ++j)
9184 map->p[j - 1] = map->p[j];
9185 map->n--;
9188 return map;
9191 /* Remove obvious duplicates among the basic maps of "map".
9193 * Unlike isl_map_normalize, this function does not remove redundant
9194 * constraints and only removes duplicates that have exactly the same
9195 * constraints in the input. It does sort the constraints and
9196 * the basic maps to ease the detection of duplicates.
9198 * If "map" has already been normalized or if the basic maps are
9199 * disjoint, then there can be no duplicates.
9201 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9203 int i;
9204 isl_basic_map *bmap;
9206 if (!map)
9207 return NULL;
9208 if (map->n <= 1)
9209 return map;
9210 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9211 return map;
9212 for (i = 0; i < map->n; ++i) {
9213 bmap = isl_basic_map_copy(map->p[i]);
9214 bmap = isl_basic_map_sort_constraints(bmap);
9215 if (!bmap)
9216 return isl_map_free(map);
9217 isl_basic_map_free(map->p[i]);
9218 map->p[i] = bmap;
9221 map = sort_and_remove_duplicates(map);
9222 return map;
9225 /* We normalize in place, but if anything goes wrong we need
9226 * to return NULL, so we need to make sure we don't change the
9227 * meaning of any possible other copies of map.
9229 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9231 int i;
9232 struct isl_basic_map *bmap;
9234 if (!map)
9235 return NULL;
9236 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9237 return map;
9238 for (i = 0; i < map->n; ++i) {
9239 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9240 if (!bmap)
9241 goto error;
9242 isl_basic_map_free(map->p[i]);
9243 map->p[i] = bmap;
9246 map = sort_and_remove_duplicates(map);
9247 if (map)
9248 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9249 return map;
9250 error:
9251 isl_map_free(map);
9252 return NULL;
9255 struct isl_set *isl_set_normalize(struct isl_set *set)
9257 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9260 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9261 __isl_keep isl_map *map2)
9263 int i;
9264 isl_bool equal;
9266 if (!map1 || !map2)
9267 return isl_bool_error;
9269 if (map1 == map2)
9270 return isl_bool_true;
9271 if (!isl_space_is_equal(map1->dim, map2->dim))
9272 return isl_bool_false;
9274 map1 = isl_map_copy(map1);
9275 map2 = isl_map_copy(map2);
9276 map1 = isl_map_normalize(map1);
9277 map2 = isl_map_normalize(map2);
9278 if (!map1 || !map2)
9279 goto error;
9280 equal = map1->n == map2->n;
9281 for (i = 0; equal && i < map1->n; ++i) {
9282 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9283 if (equal < 0)
9284 goto error;
9286 isl_map_free(map1);
9287 isl_map_free(map2);
9288 return equal;
9289 error:
9290 isl_map_free(map1);
9291 isl_map_free(map2);
9292 return isl_bool_error;
9295 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9296 __isl_keep isl_set *set2)
9298 return isl_map_plain_is_equal((struct isl_map *)set1,
9299 (struct isl_map *)set2);
9302 /* Return an interval that ranges from min to max (inclusive)
9304 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9305 isl_int min, isl_int max)
9307 int k;
9308 struct isl_basic_set *bset = NULL;
9310 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9311 if (!bset)
9312 goto error;
9314 k = isl_basic_set_alloc_inequality(bset);
9315 if (k < 0)
9316 goto error;
9317 isl_int_set_si(bset->ineq[k][1], 1);
9318 isl_int_neg(bset->ineq[k][0], min);
9320 k = isl_basic_set_alloc_inequality(bset);
9321 if (k < 0)
9322 goto error;
9323 isl_int_set_si(bset->ineq[k][1], -1);
9324 isl_int_set(bset->ineq[k][0], max);
9326 return bset;
9327 error:
9328 isl_basic_set_free(bset);
9329 return NULL;
9332 /* Return the basic maps in "map" as a list.
9334 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9335 __isl_keep isl_map *map)
9337 int i;
9338 isl_ctx *ctx;
9339 isl_basic_map_list *list;
9341 if (!map)
9342 return NULL;
9343 ctx = isl_map_get_ctx(map);
9344 list = isl_basic_map_list_alloc(ctx, map->n);
9346 for (i = 0; i < map->n; ++i) {
9347 isl_basic_map *bmap;
9349 bmap = isl_basic_map_copy(map->p[i]);
9350 list = isl_basic_map_list_add(list, bmap);
9353 return list;
9356 /* Return the intersection of the elements in the non-empty list "list".
9357 * All elements are assumed to live in the same space.
9359 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9360 __isl_take isl_basic_map_list *list)
9362 int i, n;
9363 isl_basic_map *bmap;
9365 if (!list)
9366 return NULL;
9367 n = isl_basic_map_list_n_basic_map(list);
9368 if (n < 1)
9369 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9370 "expecting non-empty list", goto error);
9372 bmap = isl_basic_map_list_get_basic_map(list, 0);
9373 for (i = 1; i < n; ++i) {
9374 isl_basic_map *bmap_i;
9376 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9377 bmap = isl_basic_map_intersect(bmap, bmap_i);
9380 isl_basic_map_list_free(list);
9381 return bmap;
9382 error:
9383 isl_basic_map_list_free(list);
9384 return NULL;
9387 /* Return the intersection of the elements in the non-empty list "list".
9388 * All elements are assumed to live in the same space.
9390 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9391 __isl_take isl_basic_set_list *list)
9393 return isl_basic_map_list_intersect(list);
9396 /* Return the union of the elements in the non-empty list "list".
9397 * All elements are assumed to live in the same space.
9399 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9401 int i, n;
9402 isl_set *set;
9404 if (!list)
9405 return NULL;
9406 n = isl_set_list_n_set(list);
9407 if (n < 1)
9408 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9409 "expecting non-empty list", goto error);
9411 set = isl_set_list_get_set(list, 0);
9412 for (i = 1; i < n; ++i) {
9413 isl_set *set_i;
9415 set_i = isl_set_list_get_set(list, i);
9416 set = isl_set_union(set, set_i);
9419 isl_set_list_free(list);
9420 return set;
9421 error:
9422 isl_set_list_free(list);
9423 return NULL;
9426 /* Return the Cartesian product of the basic sets in list (in the given order).
9428 __isl_give isl_basic_set *isl_basic_set_list_product(
9429 __isl_take struct isl_basic_set_list *list)
9431 int i;
9432 unsigned dim;
9433 unsigned nparam;
9434 unsigned extra;
9435 unsigned n_eq;
9436 unsigned n_ineq;
9437 struct isl_basic_set *product = NULL;
9439 if (!list)
9440 goto error;
9441 isl_assert(list->ctx, list->n > 0, goto error);
9442 isl_assert(list->ctx, list->p[0], goto error);
9443 nparam = isl_basic_set_n_param(list->p[0]);
9444 dim = isl_basic_set_n_dim(list->p[0]);
9445 extra = list->p[0]->n_div;
9446 n_eq = list->p[0]->n_eq;
9447 n_ineq = list->p[0]->n_ineq;
9448 for (i = 1; i < list->n; ++i) {
9449 isl_assert(list->ctx, list->p[i], goto error);
9450 isl_assert(list->ctx,
9451 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9452 dim += isl_basic_set_n_dim(list->p[i]);
9453 extra += list->p[i]->n_div;
9454 n_eq += list->p[i]->n_eq;
9455 n_ineq += list->p[i]->n_ineq;
9457 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9458 n_eq, n_ineq);
9459 if (!product)
9460 goto error;
9461 dim = 0;
9462 for (i = 0; i < list->n; ++i) {
9463 isl_basic_set_add_constraints(product,
9464 isl_basic_set_copy(list->p[i]), dim);
9465 dim += isl_basic_set_n_dim(list->p[i]);
9467 isl_basic_set_list_free(list);
9468 return product;
9469 error:
9470 isl_basic_set_free(product);
9471 isl_basic_set_list_free(list);
9472 return NULL;
9475 struct isl_basic_map *isl_basic_map_product(
9476 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9478 isl_space *dim_result = NULL;
9479 struct isl_basic_map *bmap;
9480 unsigned in1, in2, out1, out2, nparam, total, pos;
9481 struct isl_dim_map *dim_map1, *dim_map2;
9483 if (!bmap1 || !bmap2)
9484 goto error;
9486 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9487 bmap2->dim, isl_dim_param), goto error);
9488 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9489 isl_space_copy(bmap2->dim));
9491 in1 = isl_basic_map_n_in(bmap1);
9492 in2 = isl_basic_map_n_in(bmap2);
9493 out1 = isl_basic_map_n_out(bmap1);
9494 out2 = isl_basic_map_n_out(bmap2);
9495 nparam = isl_basic_map_n_param(bmap1);
9497 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9498 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9499 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9500 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9501 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9502 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9503 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9504 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9505 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9506 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9507 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9509 bmap = isl_basic_map_alloc_space(dim_result,
9510 bmap1->n_div + bmap2->n_div,
9511 bmap1->n_eq + bmap2->n_eq,
9512 bmap1->n_ineq + bmap2->n_ineq);
9513 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9514 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9515 bmap = isl_basic_map_simplify(bmap);
9516 return isl_basic_map_finalize(bmap);
9517 error:
9518 isl_basic_map_free(bmap1);
9519 isl_basic_map_free(bmap2);
9520 return NULL;
9523 __isl_give isl_basic_map *isl_basic_map_flat_product(
9524 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9526 isl_basic_map *prod;
9528 prod = isl_basic_map_product(bmap1, bmap2);
9529 prod = isl_basic_map_flatten(prod);
9530 return prod;
9533 __isl_give isl_basic_set *isl_basic_set_flat_product(
9534 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9536 return isl_basic_map_flat_range_product(bset1, bset2);
9539 __isl_give isl_basic_map *isl_basic_map_domain_product(
9540 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9542 isl_space *space_result = NULL;
9543 isl_basic_map *bmap;
9544 unsigned in1, in2, out, nparam, total, pos;
9545 struct isl_dim_map *dim_map1, *dim_map2;
9547 if (!bmap1 || !bmap2)
9548 goto error;
9550 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9551 isl_space_copy(bmap2->dim));
9553 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9554 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9555 out = isl_basic_map_dim(bmap1, isl_dim_out);
9556 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9558 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9559 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9560 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9561 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9562 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9563 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9564 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9565 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9566 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9567 isl_dim_map_div(dim_map1, bmap1, pos += out);
9568 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9570 bmap = isl_basic_map_alloc_space(space_result,
9571 bmap1->n_div + bmap2->n_div,
9572 bmap1->n_eq + bmap2->n_eq,
9573 bmap1->n_ineq + bmap2->n_ineq);
9574 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9575 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9576 bmap = isl_basic_map_simplify(bmap);
9577 return isl_basic_map_finalize(bmap);
9578 error:
9579 isl_basic_map_free(bmap1);
9580 isl_basic_map_free(bmap2);
9581 return NULL;
9584 __isl_give isl_basic_map *isl_basic_map_range_product(
9585 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9587 isl_space *dim_result = NULL;
9588 isl_basic_map *bmap;
9589 unsigned in, out1, out2, nparam, total, pos;
9590 struct isl_dim_map *dim_map1, *dim_map2;
9592 if (!bmap1 || !bmap2)
9593 goto error;
9595 if (!isl_space_match(bmap1->dim, isl_dim_param,
9596 bmap2->dim, isl_dim_param))
9597 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9598 "parameters don't match", goto error);
9600 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9601 isl_space_copy(bmap2->dim));
9603 in = isl_basic_map_dim(bmap1, isl_dim_in);
9604 out1 = isl_basic_map_n_out(bmap1);
9605 out2 = isl_basic_map_n_out(bmap2);
9606 nparam = isl_basic_map_n_param(bmap1);
9608 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9609 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9610 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9611 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9612 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9613 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9614 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9615 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9616 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9617 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9618 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9620 bmap = isl_basic_map_alloc_space(dim_result,
9621 bmap1->n_div + bmap2->n_div,
9622 bmap1->n_eq + bmap2->n_eq,
9623 bmap1->n_ineq + bmap2->n_ineq);
9624 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9625 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9626 bmap = isl_basic_map_simplify(bmap);
9627 return isl_basic_map_finalize(bmap);
9628 error:
9629 isl_basic_map_free(bmap1);
9630 isl_basic_map_free(bmap2);
9631 return NULL;
9634 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9635 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9637 isl_basic_map *prod;
9639 prod = isl_basic_map_range_product(bmap1, bmap2);
9640 prod = isl_basic_map_flatten_range(prod);
9641 return prod;
9644 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9645 * and collect the results.
9646 * The result live in the space obtained by calling "space_product"
9647 * on the spaces of "map1" and "map2".
9648 * If "remove_duplicates" is set then the result may contain duplicates
9649 * (even if the inputs do not) and so we try and remove the obvious
9650 * duplicates.
9652 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9653 __isl_take isl_map *map2,
9654 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9655 __isl_take isl_space *right),
9656 __isl_give isl_basic_map *(*basic_map_product)(
9657 __isl_take isl_basic_map *left,
9658 __isl_take isl_basic_map *right),
9659 int remove_duplicates)
9661 unsigned flags = 0;
9662 struct isl_map *result;
9663 int i, j;
9665 if (!map1 || !map2)
9666 goto error;
9668 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9669 map2->dim, isl_dim_param), goto error);
9671 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9672 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9673 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9675 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9676 isl_space_copy(map2->dim)),
9677 map1->n * map2->n, flags);
9678 if (!result)
9679 goto error;
9680 for (i = 0; i < map1->n; ++i)
9681 for (j = 0; j < map2->n; ++j) {
9682 struct isl_basic_map *part;
9683 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9684 isl_basic_map_copy(map2->p[j]));
9685 if (isl_basic_map_is_empty(part))
9686 isl_basic_map_free(part);
9687 else
9688 result = isl_map_add_basic_map(result, part);
9689 if (!result)
9690 goto error;
9692 if (remove_duplicates)
9693 result = isl_map_remove_obvious_duplicates(result);
9694 isl_map_free(map1);
9695 isl_map_free(map2);
9696 return result;
9697 error:
9698 isl_map_free(map1);
9699 isl_map_free(map2);
9700 return NULL;
9703 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9705 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9706 __isl_take isl_map *map2)
9708 return map_product(map1, map2, &isl_space_product,
9709 &isl_basic_map_product, 0);
9712 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9713 __isl_take isl_map *map2)
9715 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9718 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9720 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9721 __isl_take isl_map *map2)
9723 isl_map *prod;
9725 prod = isl_map_product(map1, map2);
9726 prod = isl_map_flatten(prod);
9727 return prod;
9730 /* Given two set A and B, construct its Cartesian product A x B.
9732 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9734 return isl_map_range_product(set1, set2);
9737 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9738 __isl_take isl_set *set2)
9740 return isl_map_flat_range_product(set1, set2);
9743 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9745 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9746 __isl_take isl_map *map2)
9748 return map_product(map1, map2, &isl_space_domain_product,
9749 &isl_basic_map_domain_product, 1);
9752 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9754 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9755 __isl_take isl_map *map2)
9757 return map_product(map1, map2, &isl_space_range_product,
9758 &isl_basic_map_range_product, 1);
9761 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9762 __isl_take isl_map *map2)
9764 return isl_map_align_params_map_map_and(map1, map2,
9765 &map_domain_product_aligned);
9768 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9769 __isl_take isl_map *map2)
9771 return isl_map_align_params_map_map_and(map1, map2,
9772 &map_range_product_aligned);
9775 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9777 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9779 isl_space *space;
9780 int total1, keep1, total2, keep2;
9782 if (!map)
9783 return NULL;
9784 if (!isl_space_domain_is_wrapping(map->dim) ||
9785 !isl_space_range_is_wrapping(map->dim))
9786 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9787 "not a product", return isl_map_free(map));
9789 space = isl_map_get_space(map);
9790 total1 = isl_space_dim(space, isl_dim_in);
9791 total2 = isl_space_dim(space, isl_dim_out);
9792 space = isl_space_factor_domain(space);
9793 keep1 = isl_space_dim(space, isl_dim_in);
9794 keep2 = isl_space_dim(space, isl_dim_out);
9795 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9796 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9797 map = isl_map_reset_space(map, space);
9799 return map;
9802 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9804 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9806 isl_space *space;
9807 int total1, keep1, total2, keep2;
9809 if (!map)
9810 return NULL;
9811 if (!isl_space_domain_is_wrapping(map->dim) ||
9812 !isl_space_range_is_wrapping(map->dim))
9813 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9814 "not a product", return isl_map_free(map));
9816 space = isl_map_get_space(map);
9817 total1 = isl_space_dim(space, isl_dim_in);
9818 total2 = isl_space_dim(space, isl_dim_out);
9819 space = isl_space_factor_range(space);
9820 keep1 = isl_space_dim(space, isl_dim_in);
9821 keep2 = isl_space_dim(space, isl_dim_out);
9822 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9823 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9824 map = isl_map_reset_space(map, space);
9826 return map;
9829 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9831 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9833 isl_space *space;
9834 int total, keep;
9836 if (!map)
9837 return NULL;
9838 if (!isl_space_domain_is_wrapping(map->dim))
9839 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9840 "domain is not a product", return isl_map_free(map));
9842 space = isl_map_get_space(map);
9843 total = isl_space_dim(space, isl_dim_in);
9844 space = isl_space_domain_factor_domain(space);
9845 keep = isl_space_dim(space, isl_dim_in);
9846 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9847 map = isl_map_reset_space(map, space);
9849 return map;
9852 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9854 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9856 isl_space *space;
9857 int total, keep;
9859 if (!map)
9860 return NULL;
9861 if (!isl_space_domain_is_wrapping(map->dim))
9862 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9863 "domain is not a product", return isl_map_free(map));
9865 space = isl_map_get_space(map);
9866 total = isl_space_dim(space, isl_dim_in);
9867 space = isl_space_domain_factor_range(space);
9868 keep = isl_space_dim(space, isl_dim_in);
9869 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9870 map = isl_map_reset_space(map, space);
9872 return map;
9875 /* Given a map A -> [B -> C], extract the map A -> B.
9877 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9879 isl_space *space;
9880 int total, keep;
9882 if (!map)
9883 return NULL;
9884 if (!isl_space_range_is_wrapping(map->dim))
9885 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9886 "range is not a product", return isl_map_free(map));
9888 space = isl_map_get_space(map);
9889 total = isl_space_dim(space, isl_dim_out);
9890 space = isl_space_range_factor_domain(space);
9891 keep = isl_space_dim(space, isl_dim_out);
9892 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9893 map = isl_map_reset_space(map, space);
9895 return map;
9898 /* Given a map A -> [B -> C], extract the map A -> C.
9900 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9902 isl_space *space;
9903 int total, keep;
9905 if (!map)
9906 return NULL;
9907 if (!isl_space_range_is_wrapping(map->dim))
9908 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9909 "range is not a product", return isl_map_free(map));
9911 space = isl_map_get_space(map);
9912 total = isl_space_dim(space, isl_dim_out);
9913 space = isl_space_range_factor_range(space);
9914 keep = isl_space_dim(space, isl_dim_out);
9915 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9916 map = isl_map_reset_space(map, space);
9918 return map;
9921 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9923 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9924 __isl_take isl_map *map2)
9926 isl_map *prod;
9928 prod = isl_map_domain_product(map1, map2);
9929 prod = isl_map_flatten_domain(prod);
9930 return prod;
9933 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9935 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9936 __isl_take isl_map *map2)
9938 isl_map *prod;
9940 prod = isl_map_range_product(map1, map2);
9941 prod = isl_map_flatten_range(prod);
9942 return prod;
9945 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9947 int i;
9948 uint32_t hash = isl_hash_init();
9949 unsigned total;
9951 if (!bmap)
9952 return 0;
9953 bmap = isl_basic_map_copy(bmap);
9954 bmap = isl_basic_map_normalize(bmap);
9955 if (!bmap)
9956 return 0;
9957 total = isl_basic_map_total_dim(bmap);
9958 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9959 for (i = 0; i < bmap->n_eq; ++i) {
9960 uint32_t c_hash;
9961 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9962 isl_hash_hash(hash, c_hash);
9964 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9965 for (i = 0; i < bmap->n_ineq; ++i) {
9966 uint32_t c_hash;
9967 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9968 isl_hash_hash(hash, c_hash);
9970 isl_hash_byte(hash, bmap->n_div & 0xFF);
9971 for (i = 0; i < bmap->n_div; ++i) {
9972 uint32_t c_hash;
9973 if (isl_int_is_zero(bmap->div[i][0]))
9974 continue;
9975 isl_hash_byte(hash, i & 0xFF);
9976 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9977 isl_hash_hash(hash, c_hash);
9979 isl_basic_map_free(bmap);
9980 return hash;
9983 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9985 return isl_basic_map_get_hash((isl_basic_map *)bset);
9988 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9990 int i;
9991 uint32_t hash;
9993 if (!map)
9994 return 0;
9995 map = isl_map_copy(map);
9996 map = isl_map_normalize(map);
9997 if (!map)
9998 return 0;
10000 hash = isl_hash_init();
10001 for (i = 0; i < map->n; ++i) {
10002 uint32_t bmap_hash;
10003 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10004 isl_hash_hash(hash, bmap_hash);
10007 isl_map_free(map);
10009 return hash;
10012 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10014 return isl_map_get_hash((isl_map *)set);
10017 /* Check if the value for dimension dim is completely determined
10018 * by the values of the other parameters and variables.
10019 * That is, check if dimension dim is involved in an equality.
10021 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10023 int i;
10024 unsigned nparam;
10026 if (!bset)
10027 return -1;
10028 nparam = isl_basic_set_n_param(bset);
10029 for (i = 0; i < bset->n_eq; ++i)
10030 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10031 return 1;
10032 return 0;
10035 /* Check if the value for dimension dim is completely determined
10036 * by the values of the other parameters and variables.
10037 * That is, check if dimension dim is involved in an equality
10038 * for each of the subsets.
10040 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10042 int i;
10044 if (!set)
10045 return -1;
10046 for (i = 0; i < set->n; ++i) {
10047 int unique;
10048 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10049 if (unique != 1)
10050 return unique;
10052 return 1;
10055 /* Return the number of basic maps in the (current) representation of "map".
10057 int isl_map_n_basic_map(__isl_keep isl_map *map)
10059 return map ? map->n : 0;
10062 int isl_set_n_basic_set(__isl_keep isl_set *set)
10064 return set ? set->n : 0;
10067 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10068 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10070 int i;
10072 if (!map)
10073 return isl_stat_error;
10075 for (i = 0; i < map->n; ++i)
10076 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10077 return isl_stat_error;
10079 return isl_stat_ok;
10082 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10083 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10085 int i;
10087 if (!set)
10088 return isl_stat_error;
10090 for (i = 0; i < set->n; ++i)
10091 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10092 return isl_stat_error;
10094 return isl_stat_ok;
10097 /* Return a list of basic sets, the union of which is equal to "set".
10099 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10100 __isl_keep isl_set *set)
10102 int i;
10103 isl_basic_set_list *list;
10105 if (!set)
10106 return NULL;
10108 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10109 for (i = 0; i < set->n; ++i) {
10110 isl_basic_set *bset;
10112 bset = isl_basic_set_copy(set->p[i]);
10113 list = isl_basic_set_list_add(list, bset);
10116 return list;
10119 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10121 isl_space *dim;
10123 if (!bset)
10124 return NULL;
10126 bset = isl_basic_set_cow(bset);
10127 if (!bset)
10128 return NULL;
10130 dim = isl_basic_set_get_space(bset);
10131 dim = isl_space_lift(dim, bset->n_div);
10132 if (!dim)
10133 goto error;
10134 isl_space_free(bset->dim);
10135 bset->dim = dim;
10136 bset->extra -= bset->n_div;
10137 bset->n_div = 0;
10139 bset = isl_basic_set_finalize(bset);
10141 return bset;
10142 error:
10143 isl_basic_set_free(bset);
10144 return NULL;
10147 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10149 int i;
10150 isl_space *dim;
10151 unsigned n_div;
10153 set = isl_set_align_divs(set);
10155 if (!set)
10156 return NULL;
10158 set = isl_set_cow(set);
10159 if (!set)
10160 return NULL;
10162 n_div = set->p[0]->n_div;
10163 dim = isl_set_get_space(set);
10164 dim = isl_space_lift(dim, n_div);
10165 if (!dim)
10166 goto error;
10167 isl_space_free(set->dim);
10168 set->dim = dim;
10170 for (i = 0; i < set->n; ++i) {
10171 set->p[i] = isl_basic_set_lift(set->p[i]);
10172 if (!set->p[i])
10173 goto error;
10176 return set;
10177 error:
10178 isl_set_free(set);
10179 return NULL;
10182 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10184 isl_space *dim;
10185 struct isl_basic_map *bmap;
10186 unsigned n_set;
10187 unsigned n_div;
10188 unsigned n_param;
10189 unsigned total;
10190 int i, k, l;
10192 set = isl_set_align_divs(set);
10194 if (!set)
10195 return NULL;
10197 dim = isl_set_get_space(set);
10198 if (set->n == 0 || set->p[0]->n_div == 0) {
10199 isl_set_free(set);
10200 return isl_map_identity(isl_space_map_from_set(dim));
10203 n_div = set->p[0]->n_div;
10204 dim = isl_space_map_from_set(dim);
10205 n_param = isl_space_dim(dim, isl_dim_param);
10206 n_set = isl_space_dim(dim, isl_dim_in);
10207 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10208 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10209 for (i = 0; i < n_set; ++i)
10210 bmap = var_equal(bmap, i);
10212 total = n_param + n_set + n_set + n_div;
10213 for (i = 0; i < n_div; ++i) {
10214 k = isl_basic_map_alloc_inequality(bmap);
10215 if (k < 0)
10216 goto error;
10217 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10218 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10219 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10220 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10221 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10222 set->p[0]->div[i][0]);
10224 l = isl_basic_map_alloc_inequality(bmap);
10225 if (l < 0)
10226 goto error;
10227 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10228 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10229 set->p[0]->div[i][0]);
10230 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10233 isl_set_free(set);
10234 bmap = isl_basic_map_simplify(bmap);
10235 bmap = isl_basic_map_finalize(bmap);
10236 return isl_map_from_basic_map(bmap);
10237 error:
10238 isl_set_free(set);
10239 isl_basic_map_free(bmap);
10240 return NULL;
10243 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10245 unsigned dim;
10246 int size = 0;
10248 if (!bset)
10249 return -1;
10251 dim = isl_basic_set_total_dim(bset);
10252 size += bset->n_eq * (1 + dim);
10253 size += bset->n_ineq * (1 + dim);
10254 size += bset->n_div * (2 + dim);
10256 return size;
10259 int isl_set_size(__isl_keep isl_set *set)
10261 int i;
10262 int size = 0;
10264 if (!set)
10265 return -1;
10267 for (i = 0; i < set->n; ++i)
10268 size += isl_basic_set_size(set->p[i]);
10270 return size;
10273 /* Check if there is any lower bound (if lower == 0) and/or upper
10274 * bound (if upper == 0) on the specified dim.
10276 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10277 enum isl_dim_type type, unsigned pos, int lower, int upper)
10279 int i;
10281 if (!bmap)
10282 return isl_bool_error;
10284 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10285 return isl_bool_error);
10287 pos += isl_basic_map_offset(bmap, type);
10289 for (i = 0; i < bmap->n_div; ++i) {
10290 if (isl_int_is_zero(bmap->div[i][0]))
10291 continue;
10292 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10293 return isl_bool_true;
10296 for (i = 0; i < bmap->n_eq; ++i)
10297 if (!isl_int_is_zero(bmap->eq[i][pos]))
10298 return isl_bool_true;
10300 for (i = 0; i < bmap->n_ineq; ++i) {
10301 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10302 if (sgn > 0)
10303 lower = 1;
10304 if (sgn < 0)
10305 upper = 1;
10308 return lower && upper;
10311 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10312 enum isl_dim_type type, unsigned pos)
10314 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10317 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10318 enum isl_dim_type type, unsigned pos)
10320 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10323 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10324 enum isl_dim_type type, unsigned pos)
10326 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10329 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10330 enum isl_dim_type type, unsigned pos)
10332 int i;
10334 if (!map)
10335 return -1;
10337 for (i = 0; i < map->n; ++i) {
10338 int bounded;
10339 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10340 if (bounded < 0 || !bounded)
10341 return bounded;
10344 return 1;
10347 /* Return 1 if the specified dim is involved in both an upper bound
10348 * and a lower bound.
10350 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10351 enum isl_dim_type type, unsigned pos)
10353 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10356 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10358 static isl_bool has_any_bound(__isl_keep isl_map *map,
10359 enum isl_dim_type type, unsigned pos,
10360 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10361 enum isl_dim_type type, unsigned pos))
10363 int i;
10365 if (!map)
10366 return isl_bool_error;
10368 for (i = 0; i < map->n; ++i) {
10369 isl_bool bounded;
10370 bounded = fn(map->p[i], type, pos);
10371 if (bounded < 0 || bounded)
10372 return bounded;
10375 return isl_bool_false;
10378 /* Return 1 if the specified dim is involved in any lower bound.
10380 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10381 enum isl_dim_type type, unsigned pos)
10383 return has_any_bound(set, type, pos,
10384 &isl_basic_map_dim_has_lower_bound);
10387 /* Return 1 if the specified dim is involved in any upper bound.
10389 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10390 enum isl_dim_type type, unsigned pos)
10392 return has_any_bound(set, type, pos,
10393 &isl_basic_map_dim_has_upper_bound);
10396 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10398 static isl_bool has_bound(__isl_keep isl_map *map,
10399 enum isl_dim_type type, unsigned pos,
10400 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10401 enum isl_dim_type type, unsigned pos))
10403 int i;
10405 if (!map)
10406 return isl_bool_error;
10408 for (i = 0; i < map->n; ++i) {
10409 isl_bool bounded;
10410 bounded = fn(map->p[i], type, pos);
10411 if (bounded < 0 || !bounded)
10412 return bounded;
10415 return isl_bool_true;
10418 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10420 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10421 enum isl_dim_type type, unsigned pos)
10423 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10426 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10428 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10429 enum isl_dim_type type, unsigned pos)
10431 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10434 /* For each of the "n" variables starting at "first", determine
10435 * the sign of the variable and put the results in the first "n"
10436 * elements of the array "signs".
10437 * Sign
10438 * 1 means that the variable is non-negative
10439 * -1 means that the variable is non-positive
10440 * 0 means the variable attains both positive and negative values.
10442 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10443 unsigned first, unsigned n, int *signs)
10445 isl_vec *bound = NULL;
10446 struct isl_tab *tab = NULL;
10447 struct isl_tab_undo *snap;
10448 int i;
10450 if (!bset || !signs)
10451 return -1;
10453 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10454 tab = isl_tab_from_basic_set(bset, 0);
10455 if (!bound || !tab)
10456 goto error;
10458 isl_seq_clr(bound->el, bound->size);
10459 isl_int_set_si(bound->el[0], -1);
10461 snap = isl_tab_snap(tab);
10462 for (i = 0; i < n; ++i) {
10463 int empty;
10465 isl_int_set_si(bound->el[1 + first + i], -1);
10466 if (isl_tab_add_ineq(tab, bound->el) < 0)
10467 goto error;
10468 empty = tab->empty;
10469 isl_int_set_si(bound->el[1 + first + i], 0);
10470 if (isl_tab_rollback(tab, snap) < 0)
10471 goto error;
10473 if (empty) {
10474 signs[i] = 1;
10475 continue;
10478 isl_int_set_si(bound->el[1 + first + i], 1);
10479 if (isl_tab_add_ineq(tab, bound->el) < 0)
10480 goto error;
10481 empty = tab->empty;
10482 isl_int_set_si(bound->el[1 + first + i], 0);
10483 if (isl_tab_rollback(tab, snap) < 0)
10484 goto error;
10486 signs[i] = empty ? -1 : 0;
10489 isl_tab_free(tab);
10490 isl_vec_free(bound);
10491 return 0;
10492 error:
10493 isl_tab_free(tab);
10494 isl_vec_free(bound);
10495 return -1;
10498 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10499 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10501 if (!bset || !signs)
10502 return -1;
10503 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10504 return -1);
10506 first += pos(bset->dim, type) - 1;
10507 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10510 /* Is it possible for the integer division "div" to depend (possibly
10511 * indirectly) on any output dimensions?
10513 * If the div is undefined, then we conservatively assume that it
10514 * may depend on them.
10515 * Otherwise, we check if it actually depends on them or on any integer
10516 * divisions that may depend on them.
10518 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10520 int i;
10521 unsigned n_out, o_out;
10522 unsigned n_div, o_div;
10524 if (isl_int_is_zero(bmap->div[div][0]))
10525 return 1;
10527 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10528 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10530 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10531 return 1;
10533 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10534 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10536 for (i = 0; i < n_div; ++i) {
10537 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10538 continue;
10539 if (div_may_involve_output(bmap, i))
10540 return 1;
10543 return 0;
10546 /* Return the first integer division of "bmap" in the range
10547 * [first, first + n[ that may depend on any output dimensions and
10548 * that has a non-zero coefficient in "c" (where the first coefficient
10549 * in "c" corresponds to integer division "first").
10551 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10552 isl_int *c, int first, int n)
10554 int k;
10556 if (!bmap)
10557 return -1;
10559 for (k = first; k < first + n; ++k) {
10560 if (isl_int_is_zero(c[k]))
10561 continue;
10562 if (div_may_involve_output(bmap, k))
10563 return k;
10566 return first + n;
10569 /* Look for a pair of inequality constraints in "bmap" of the form
10571 * -l + i >= 0 or i >= l
10572 * and
10573 * n + l - i >= 0 or i <= l + n
10575 * with n < "m" and i the output dimension at position "pos".
10576 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10577 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10578 * and earlier output dimensions, as well as integer divisions that do
10579 * not involve any of the output dimensions.
10581 * Return the index of the first inequality constraint or bmap->n_ineq
10582 * if no such pair can be found.
10584 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10585 int pos, isl_int m)
10587 int i, j;
10588 isl_ctx *ctx;
10589 unsigned total;
10590 unsigned n_div, o_div;
10591 unsigned n_out, o_out;
10592 int less;
10594 if (!bmap)
10595 return -1;
10597 ctx = isl_basic_map_get_ctx(bmap);
10598 total = isl_basic_map_total_dim(bmap);
10599 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10600 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10601 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10602 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10603 for (i = 0; i < bmap->n_ineq; ++i) {
10604 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10605 continue;
10606 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10607 n_out - (pos + 1)) != -1)
10608 continue;
10609 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10610 0, n_div) < n_div)
10611 continue;
10612 for (j = i + 1; j < bmap->n_ineq; ++j) {
10613 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10614 ctx->one))
10615 continue;
10616 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10617 bmap->ineq[j] + 1, total))
10618 continue;
10619 break;
10621 if (j >= bmap->n_ineq)
10622 continue;
10623 isl_int_add(bmap->ineq[i][0],
10624 bmap->ineq[i][0], bmap->ineq[j][0]);
10625 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10626 isl_int_sub(bmap->ineq[i][0],
10627 bmap->ineq[i][0], bmap->ineq[j][0]);
10628 if (!less)
10629 continue;
10630 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10631 return i;
10632 else
10633 return j;
10636 return bmap->n_ineq;
10639 /* Return the index of the equality of "bmap" that defines
10640 * the output dimension "pos" in terms of earlier dimensions.
10641 * The equality may also involve integer divisions, as long
10642 * as those integer divisions are defined in terms of
10643 * parameters or input dimensions.
10644 * In this case, *div is set to the number of integer divisions and
10645 * *ineq is set to the number of inequality constraints (provided
10646 * div and ineq are not NULL).
10648 * The equality may also involve a single integer division involving
10649 * the output dimensions (typically only output dimension "pos") as
10650 * long as the coefficient of output dimension "pos" is 1 or -1 and
10651 * there is a pair of constraints i >= l and i <= l + n, with i referring
10652 * to output dimension "pos", l an expression involving only earlier
10653 * dimensions and n smaller than the coefficient of the integer division
10654 * in the equality. In this case, the output dimension can be defined
10655 * in terms of a modulo expression that does not involve the integer division.
10656 * *div is then set to this single integer division and
10657 * *ineq is set to the index of constraint i >= l.
10659 * Return bmap->n_eq if there is no such equality.
10660 * Return -1 on error.
10662 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10663 int pos, int *div, int *ineq)
10665 int j, k, l;
10666 unsigned n_out, o_out;
10667 unsigned n_div, o_div;
10669 if (!bmap)
10670 return -1;
10672 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10673 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10674 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10675 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10677 if (ineq)
10678 *ineq = bmap->n_ineq;
10679 if (div)
10680 *div = n_div;
10681 for (j = 0; j < bmap->n_eq; ++j) {
10682 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10683 continue;
10684 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10685 n_out - (pos + 1)) != -1)
10686 continue;
10687 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10688 0, n_div);
10689 if (k >= n_div)
10690 return j;
10691 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10692 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10693 continue;
10694 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10695 k + 1, n_div - (k+1)) < n_div)
10696 continue;
10697 l = find_modulo_constraint_pair(bmap, pos,
10698 bmap->eq[j][o_div + k]);
10699 if (l < 0)
10700 return -1;
10701 if (l >= bmap->n_ineq)
10702 continue;
10703 if (div)
10704 *div = k;
10705 if (ineq)
10706 *ineq = l;
10707 return j;
10710 return bmap->n_eq;
10713 /* Check if the given basic map is obviously single-valued.
10714 * In particular, for each output dimension, check that there is
10715 * an equality that defines the output dimension in terms of
10716 * earlier dimensions.
10718 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10720 int i;
10721 unsigned n_out;
10723 if (!bmap)
10724 return isl_bool_error;
10726 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10728 for (i = 0; i < n_out; ++i) {
10729 int eq;
10731 eq = isl_basic_map_output_defining_equality(bmap, i,
10732 NULL, NULL);
10733 if (eq < 0)
10734 return isl_bool_error;
10735 if (eq >= bmap->n_eq)
10736 return isl_bool_false;
10739 return isl_bool_true;
10742 /* Check if the given basic map is single-valued.
10743 * We simply compute
10745 * M \circ M^-1
10747 * and check if the result is a subset of the identity mapping.
10749 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10751 isl_space *space;
10752 isl_basic_map *test;
10753 isl_basic_map *id;
10754 isl_bool sv;
10756 sv = isl_basic_map_plain_is_single_valued(bmap);
10757 if (sv < 0 || sv)
10758 return sv;
10760 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10761 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10763 space = isl_basic_map_get_space(bmap);
10764 space = isl_space_map_from_set(isl_space_range(space));
10765 id = isl_basic_map_identity(space);
10767 sv = isl_basic_map_is_subset(test, id);
10769 isl_basic_map_free(test);
10770 isl_basic_map_free(id);
10772 return sv;
10775 /* Check if the given map is obviously single-valued.
10777 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10779 if (!map)
10780 return isl_bool_error;
10781 if (map->n == 0)
10782 return isl_bool_true;
10783 if (map->n >= 2)
10784 return isl_bool_false;
10786 return isl_basic_map_plain_is_single_valued(map->p[0]);
10789 /* Check if the given map is single-valued.
10790 * We simply compute
10792 * M \circ M^-1
10794 * and check if the result is a subset of the identity mapping.
10796 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10798 isl_space *dim;
10799 isl_map *test;
10800 isl_map *id;
10801 isl_bool sv;
10803 sv = isl_map_plain_is_single_valued(map);
10804 if (sv < 0 || sv)
10805 return sv;
10807 test = isl_map_reverse(isl_map_copy(map));
10808 test = isl_map_apply_range(test, isl_map_copy(map));
10810 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10811 id = isl_map_identity(dim);
10813 sv = isl_map_is_subset(test, id);
10815 isl_map_free(test);
10816 isl_map_free(id);
10818 return sv;
10821 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10823 isl_bool in;
10825 map = isl_map_copy(map);
10826 map = isl_map_reverse(map);
10827 in = isl_map_is_single_valued(map);
10828 isl_map_free(map);
10830 return in;
10833 /* Check if the given map is obviously injective.
10835 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10837 isl_bool in;
10839 map = isl_map_copy(map);
10840 map = isl_map_reverse(map);
10841 in = isl_map_plain_is_single_valued(map);
10842 isl_map_free(map);
10844 return in;
10847 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10849 isl_bool sv;
10851 sv = isl_map_is_single_valued(map);
10852 if (sv < 0 || !sv)
10853 return sv;
10855 return isl_map_is_injective(map);
10858 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10860 return isl_map_is_single_valued((isl_map *)set);
10863 /* Does "map" only map elements to themselves?
10865 * If the domain and range spaces are different, then "map"
10866 * is considered not to be an identity relation, even if it is empty.
10867 * Otherwise, construct the maximal identity relation and
10868 * check whether "map" is a subset of this relation.
10870 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10872 isl_space *space;
10873 isl_map *id;
10874 isl_bool equal, is_identity;
10876 space = isl_map_get_space(map);
10877 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10878 isl_space_free(space);
10879 if (equal < 0 || !equal)
10880 return equal;
10882 id = isl_map_identity(isl_map_get_space(map));
10883 is_identity = isl_map_is_subset(map, id);
10884 isl_map_free(id);
10886 return is_identity;
10889 int isl_map_is_translation(__isl_keep isl_map *map)
10891 int ok;
10892 isl_set *delta;
10894 delta = isl_map_deltas(isl_map_copy(map));
10895 ok = isl_set_is_singleton(delta);
10896 isl_set_free(delta);
10898 return ok;
10901 static int unique(isl_int *p, unsigned pos, unsigned len)
10903 if (isl_seq_first_non_zero(p, pos) != -1)
10904 return 0;
10905 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10906 return 0;
10907 return 1;
10910 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10912 int i, j;
10913 unsigned nvar;
10914 unsigned ovar;
10916 if (!bset)
10917 return -1;
10919 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10920 return 0;
10922 nvar = isl_basic_set_dim(bset, isl_dim_set);
10923 ovar = isl_space_offset(bset->dim, isl_dim_set);
10924 for (j = 0; j < nvar; ++j) {
10925 int lower = 0, upper = 0;
10926 for (i = 0; i < bset->n_eq; ++i) {
10927 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10928 continue;
10929 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10930 return 0;
10931 break;
10933 if (i < bset->n_eq)
10934 continue;
10935 for (i = 0; i < bset->n_ineq; ++i) {
10936 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10937 continue;
10938 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10939 return 0;
10940 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10941 lower = 1;
10942 else
10943 upper = 1;
10945 if (!lower || !upper)
10946 return 0;
10949 return 1;
10952 int isl_set_is_box(__isl_keep isl_set *set)
10954 if (!set)
10955 return -1;
10956 if (set->n != 1)
10957 return 0;
10959 return isl_basic_set_is_box(set->p[0]);
10962 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10964 if (!bset)
10965 return isl_bool_error;
10967 return isl_space_is_wrapping(bset->dim);
10970 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10972 if (!set)
10973 return isl_bool_error;
10975 return isl_space_is_wrapping(set->dim);
10978 /* Modify the space of "map" through a call to "change".
10979 * If "can_change" is set (not NULL), then first call it to check
10980 * if the modification is allowed, printing the error message "cannot_change"
10981 * if it is not.
10983 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
10984 isl_bool (*can_change)(__isl_keep isl_map *map),
10985 const char *cannot_change,
10986 __isl_give isl_space *(*change)(__isl_take isl_space *space))
10988 isl_bool ok;
10989 isl_space *space;
10991 if (!map)
10992 return NULL;
10994 ok = can_change ? can_change(map) : isl_bool_true;
10995 if (ok < 0)
10996 return isl_map_free(map);
10997 if (!ok)
10998 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
10999 return isl_map_free(map));
11001 space = change(isl_map_get_space(map));
11002 map = isl_map_reset_space(map, space);
11004 return map;
11007 /* Is the domain of "map" a wrapped relation?
11009 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11011 if (!map)
11012 return isl_bool_error;
11014 return isl_space_domain_is_wrapping(map->dim);
11017 /* Is the range of "map" a wrapped relation?
11019 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11021 if (!map)
11022 return isl_bool_error;
11024 return isl_space_range_is_wrapping(map->dim);
11027 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11029 bmap = isl_basic_map_cow(bmap);
11030 if (!bmap)
11031 return NULL;
11033 bmap->dim = isl_space_wrap(bmap->dim);
11034 if (!bmap->dim)
11035 goto error;
11037 bmap = isl_basic_map_finalize(bmap);
11039 return (isl_basic_set *)bmap;
11040 error:
11041 isl_basic_map_free(bmap);
11042 return NULL;
11045 /* Given a map A -> B, return the set (A -> B).
11047 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11049 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11052 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11054 bset = isl_basic_set_cow(bset);
11055 if (!bset)
11056 return NULL;
11058 bset->dim = isl_space_unwrap(bset->dim);
11059 if (!bset->dim)
11060 goto error;
11062 bset = isl_basic_set_finalize(bset);
11064 return (isl_basic_map *)bset;
11065 error:
11066 isl_basic_set_free(bset);
11067 return NULL;
11070 /* Given a set (A -> B), return the map A -> B.
11071 * Error out if "set" is not of the form (A -> B).
11073 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11075 return isl_map_change_space(set, &isl_set_is_wrapping,
11076 "not a wrapping set", &isl_space_unwrap);
11079 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11080 enum isl_dim_type type)
11082 if (!bmap)
11083 return NULL;
11085 if (!isl_space_is_named_or_nested(bmap->dim, type))
11086 return bmap;
11088 bmap = isl_basic_map_cow(bmap);
11089 if (!bmap)
11090 return NULL;
11092 bmap->dim = isl_space_reset(bmap->dim, type);
11093 if (!bmap->dim)
11094 goto error;
11096 bmap = isl_basic_map_finalize(bmap);
11098 return bmap;
11099 error:
11100 isl_basic_map_free(bmap);
11101 return NULL;
11104 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11105 enum isl_dim_type type)
11107 int i;
11109 if (!map)
11110 return NULL;
11112 if (!isl_space_is_named_or_nested(map->dim, type))
11113 return map;
11115 map = isl_map_cow(map);
11116 if (!map)
11117 return NULL;
11119 for (i = 0; i < map->n; ++i) {
11120 map->p[i] = isl_basic_map_reset(map->p[i], type);
11121 if (!map->p[i])
11122 goto error;
11124 map->dim = isl_space_reset(map->dim, type);
11125 if (!map->dim)
11126 goto error;
11128 return map;
11129 error:
11130 isl_map_free(map);
11131 return NULL;
11134 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11136 if (!bmap)
11137 return NULL;
11139 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11140 return bmap;
11142 bmap = isl_basic_map_cow(bmap);
11143 if (!bmap)
11144 return NULL;
11146 bmap->dim = isl_space_flatten(bmap->dim);
11147 if (!bmap->dim)
11148 goto error;
11150 bmap = isl_basic_map_finalize(bmap);
11152 return bmap;
11153 error:
11154 isl_basic_map_free(bmap);
11155 return NULL;
11158 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11160 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
11163 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11164 __isl_take isl_basic_map *bmap)
11166 if (!bmap)
11167 return NULL;
11169 if (!bmap->dim->nested[0])
11170 return bmap;
11172 bmap = isl_basic_map_cow(bmap);
11173 if (!bmap)
11174 return NULL;
11176 bmap->dim = isl_space_flatten_domain(bmap->dim);
11177 if (!bmap->dim)
11178 goto error;
11180 bmap = isl_basic_map_finalize(bmap);
11182 return bmap;
11183 error:
11184 isl_basic_map_free(bmap);
11185 return NULL;
11188 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11189 __isl_take isl_basic_map *bmap)
11191 if (!bmap)
11192 return NULL;
11194 if (!bmap->dim->nested[1])
11195 return bmap;
11197 bmap = isl_basic_map_cow(bmap);
11198 if (!bmap)
11199 return NULL;
11201 bmap->dim = isl_space_flatten_range(bmap->dim);
11202 if (!bmap->dim)
11203 goto error;
11205 bmap = isl_basic_map_finalize(bmap);
11207 return bmap;
11208 error:
11209 isl_basic_map_free(bmap);
11210 return NULL;
11213 /* Remove any internal structure from the spaces of domain and range of "map".
11215 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11217 if (!map)
11218 return NULL;
11220 if (!map->dim->nested[0] && !map->dim->nested[1])
11221 return map;
11223 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11226 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11228 return (isl_set *)isl_map_flatten((isl_map *)set);
11231 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11233 isl_space *dim, *flat_dim;
11234 isl_map *map;
11236 dim = isl_set_get_space(set);
11237 flat_dim = isl_space_flatten(isl_space_copy(dim));
11238 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11239 map = isl_map_intersect_domain(map, set);
11241 return map;
11244 /* Remove any internal structure from the space of the domain of "map".
11246 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11248 if (!map)
11249 return NULL;
11251 if (!map->dim->nested[0])
11252 return map;
11254 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11257 /* Remove any internal structure from the space of the range of "map".
11259 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11261 if (!map)
11262 return NULL;
11264 if (!map->dim->nested[1])
11265 return map;
11267 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11270 /* Reorder the dimensions of "bmap" according to the given dim_map
11271 * and set the dimension specification to "dim" and
11272 * perform Gaussian elimination on the result.
11274 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11275 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11277 isl_basic_map *res;
11278 unsigned flags;
11280 bmap = isl_basic_map_cow(bmap);
11281 if (!bmap || !dim || !dim_map)
11282 goto error;
11284 flags = bmap->flags;
11285 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11286 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11287 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11288 res = isl_basic_map_alloc_space(dim,
11289 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11290 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11291 if (res)
11292 res->flags = flags;
11293 res = isl_basic_map_gauss(res, NULL);
11294 res = isl_basic_map_finalize(res);
11295 return res;
11296 error:
11297 free(dim_map);
11298 isl_basic_map_free(bmap);
11299 isl_space_free(dim);
11300 return NULL;
11303 /* Reorder the dimensions of "map" according to given reordering.
11305 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11306 __isl_take isl_reordering *r)
11308 int i;
11309 struct isl_dim_map *dim_map;
11311 map = isl_map_cow(map);
11312 dim_map = isl_dim_map_from_reordering(r);
11313 if (!map || !r || !dim_map)
11314 goto error;
11316 for (i = 0; i < map->n; ++i) {
11317 struct isl_dim_map *dim_map_i;
11319 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11321 map->p[i] = isl_basic_map_realign(map->p[i],
11322 isl_space_copy(r->dim), dim_map_i);
11324 if (!map->p[i])
11325 goto error;
11328 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11330 isl_reordering_free(r);
11331 free(dim_map);
11332 return map;
11333 error:
11334 free(dim_map);
11335 isl_map_free(map);
11336 isl_reordering_free(r);
11337 return NULL;
11340 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11341 __isl_take isl_reordering *r)
11343 return (isl_set *)isl_map_realign((isl_map *)set, r);
11346 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11347 __isl_take isl_space *model)
11349 isl_ctx *ctx;
11351 if (!map || !model)
11352 goto error;
11354 ctx = isl_space_get_ctx(model);
11355 if (!isl_space_has_named_params(model))
11356 isl_die(ctx, isl_error_invalid,
11357 "model has unnamed parameters", goto error);
11358 if (!isl_space_has_named_params(map->dim))
11359 isl_die(ctx, isl_error_invalid,
11360 "relation has unnamed parameters", goto error);
11361 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11362 isl_reordering *exp;
11364 model = isl_space_drop_dims(model, isl_dim_in,
11365 0, isl_space_dim(model, isl_dim_in));
11366 model = isl_space_drop_dims(model, isl_dim_out,
11367 0, isl_space_dim(model, isl_dim_out));
11368 exp = isl_parameter_alignment_reordering(map->dim, model);
11369 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11370 map = isl_map_realign(map, exp);
11373 isl_space_free(model);
11374 return map;
11375 error:
11376 isl_space_free(model);
11377 isl_map_free(map);
11378 return NULL;
11381 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11382 __isl_take isl_space *model)
11384 return isl_map_align_params(set, model);
11387 /* Align the parameters of "bmap" to those of "model", introducing
11388 * additional parameters if needed.
11390 __isl_give isl_basic_map *isl_basic_map_align_params(
11391 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11393 isl_ctx *ctx;
11395 if (!bmap || !model)
11396 goto error;
11398 ctx = isl_space_get_ctx(model);
11399 if (!isl_space_has_named_params(model))
11400 isl_die(ctx, isl_error_invalid,
11401 "model has unnamed parameters", goto error);
11402 if (!isl_space_has_named_params(bmap->dim))
11403 isl_die(ctx, isl_error_invalid,
11404 "relation has unnamed parameters", goto error);
11405 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11406 isl_reordering *exp;
11407 struct isl_dim_map *dim_map;
11409 model = isl_space_drop_dims(model, isl_dim_in,
11410 0, isl_space_dim(model, isl_dim_in));
11411 model = isl_space_drop_dims(model, isl_dim_out,
11412 0, isl_space_dim(model, isl_dim_out));
11413 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11414 exp = isl_reordering_extend_space(exp,
11415 isl_basic_map_get_space(bmap));
11416 dim_map = isl_dim_map_from_reordering(exp);
11417 bmap = isl_basic_map_realign(bmap,
11418 exp ? isl_space_copy(exp->dim) : NULL,
11419 isl_dim_map_extend(dim_map, bmap));
11420 isl_reordering_free(exp);
11421 free(dim_map);
11424 isl_space_free(model);
11425 return bmap;
11426 error:
11427 isl_space_free(model);
11428 isl_basic_map_free(bmap);
11429 return NULL;
11432 /* Align the parameters of "bset" to those of "model", introducing
11433 * additional parameters if needed.
11435 __isl_give isl_basic_set *isl_basic_set_align_params(
11436 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11438 return isl_basic_map_align_params(bset, model);
11441 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11442 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11443 enum isl_dim_type c2, enum isl_dim_type c3,
11444 enum isl_dim_type c4, enum isl_dim_type c5)
11446 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11447 struct isl_mat *mat;
11448 int i, j, k;
11449 int pos;
11451 if (!bmap)
11452 return NULL;
11453 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11454 isl_basic_map_total_dim(bmap) + 1);
11455 if (!mat)
11456 return NULL;
11457 for (i = 0; i < bmap->n_eq; ++i)
11458 for (j = 0, pos = 0; j < 5; ++j) {
11459 int off = isl_basic_map_offset(bmap, c[j]);
11460 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11461 isl_int_set(mat->row[i][pos],
11462 bmap->eq[i][off + k]);
11463 ++pos;
11467 return mat;
11470 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11471 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11472 enum isl_dim_type c2, enum isl_dim_type c3,
11473 enum isl_dim_type c4, enum isl_dim_type c5)
11475 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11476 struct isl_mat *mat;
11477 int i, j, k;
11478 int pos;
11480 if (!bmap)
11481 return NULL;
11482 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11483 isl_basic_map_total_dim(bmap) + 1);
11484 if (!mat)
11485 return NULL;
11486 for (i = 0; i < bmap->n_ineq; ++i)
11487 for (j = 0, pos = 0; j < 5; ++j) {
11488 int off = isl_basic_map_offset(bmap, c[j]);
11489 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11490 isl_int_set(mat->row[i][pos],
11491 bmap->ineq[i][off + k]);
11492 ++pos;
11496 return mat;
11499 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11500 __isl_take isl_space *dim,
11501 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11502 enum isl_dim_type c2, enum isl_dim_type c3,
11503 enum isl_dim_type c4, enum isl_dim_type c5)
11505 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11506 isl_basic_map *bmap;
11507 unsigned total;
11508 unsigned extra;
11509 int i, j, k, l;
11510 int pos;
11512 if (!dim || !eq || !ineq)
11513 goto error;
11515 if (eq->n_col != ineq->n_col)
11516 isl_die(dim->ctx, isl_error_invalid,
11517 "equalities and inequalities matrices should have "
11518 "same number of columns", goto error);
11520 total = 1 + isl_space_dim(dim, isl_dim_all);
11522 if (eq->n_col < total)
11523 isl_die(dim->ctx, isl_error_invalid,
11524 "number of columns too small", goto error);
11526 extra = eq->n_col - total;
11528 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11529 eq->n_row, ineq->n_row);
11530 if (!bmap)
11531 goto error;
11532 for (i = 0; i < extra; ++i) {
11533 k = isl_basic_map_alloc_div(bmap);
11534 if (k < 0)
11535 goto error;
11536 isl_int_set_si(bmap->div[k][0], 0);
11538 for (i = 0; i < eq->n_row; ++i) {
11539 l = isl_basic_map_alloc_equality(bmap);
11540 if (l < 0)
11541 goto error;
11542 for (j = 0, pos = 0; j < 5; ++j) {
11543 int off = isl_basic_map_offset(bmap, c[j]);
11544 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11545 isl_int_set(bmap->eq[l][off + k],
11546 eq->row[i][pos]);
11547 ++pos;
11551 for (i = 0; i < ineq->n_row; ++i) {
11552 l = isl_basic_map_alloc_inequality(bmap);
11553 if (l < 0)
11554 goto error;
11555 for (j = 0, pos = 0; j < 5; ++j) {
11556 int off = isl_basic_map_offset(bmap, c[j]);
11557 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11558 isl_int_set(bmap->ineq[l][off + k],
11559 ineq->row[i][pos]);
11560 ++pos;
11565 isl_space_free(dim);
11566 isl_mat_free(eq);
11567 isl_mat_free(ineq);
11569 bmap = isl_basic_map_simplify(bmap);
11570 return isl_basic_map_finalize(bmap);
11571 error:
11572 isl_space_free(dim);
11573 isl_mat_free(eq);
11574 isl_mat_free(ineq);
11575 return NULL;
11578 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11579 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11580 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11582 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11583 c1, c2, c3, c4, isl_dim_in);
11586 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11587 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11588 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11590 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11591 c1, c2, c3, c4, isl_dim_in);
11594 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11595 __isl_take isl_space *dim,
11596 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11597 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11599 return (isl_basic_set*)
11600 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11601 c1, c2, c3, c4, isl_dim_in);
11604 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11606 if (!bmap)
11607 return isl_bool_error;
11609 return isl_space_can_zip(bmap->dim);
11612 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11614 if (!map)
11615 return isl_bool_error;
11617 return isl_space_can_zip(map->dim);
11620 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11621 * (A -> C) -> (B -> D).
11623 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11625 unsigned pos;
11626 unsigned n1;
11627 unsigned n2;
11629 if (!bmap)
11630 return NULL;
11632 if (!isl_basic_map_can_zip(bmap))
11633 isl_die(bmap->ctx, isl_error_invalid,
11634 "basic map cannot be zipped", goto error);
11635 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11636 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11637 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11638 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11639 bmap = isl_basic_map_cow(bmap);
11640 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11641 if (!bmap)
11642 return NULL;
11643 bmap->dim = isl_space_zip(bmap->dim);
11644 if (!bmap->dim)
11645 goto error;
11646 bmap = isl_basic_map_mark_final(bmap);
11647 return bmap;
11648 error:
11649 isl_basic_map_free(bmap);
11650 return NULL;
11653 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11654 * (A -> C) -> (B -> D).
11656 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11658 int i;
11660 if (!map)
11661 return NULL;
11663 if (!isl_map_can_zip(map))
11664 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11665 goto error);
11667 map = isl_map_cow(map);
11668 if (!map)
11669 return NULL;
11671 for (i = 0; i < map->n; ++i) {
11672 map->p[i] = isl_basic_map_zip(map->p[i]);
11673 if (!map->p[i])
11674 goto error;
11677 map->dim = isl_space_zip(map->dim);
11678 if (!map->dim)
11679 goto error;
11681 return map;
11682 error:
11683 isl_map_free(map);
11684 return NULL;
11687 /* Can we apply isl_basic_map_curry to "bmap"?
11688 * That is, does it have a nested relation in its domain?
11690 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11692 if (!bmap)
11693 return isl_bool_error;
11695 return isl_space_can_curry(bmap->dim);
11698 /* Can we apply isl_map_curry to "map"?
11699 * That is, does it have a nested relation in its domain?
11701 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11703 if (!map)
11704 return isl_bool_error;
11706 return isl_space_can_curry(map->dim);
11709 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11710 * A -> (B -> C).
11712 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11715 if (!bmap)
11716 return NULL;
11718 if (!isl_basic_map_can_curry(bmap))
11719 isl_die(bmap->ctx, isl_error_invalid,
11720 "basic map cannot be curried", goto error);
11721 bmap = isl_basic_map_cow(bmap);
11722 if (!bmap)
11723 return NULL;
11724 bmap->dim = isl_space_curry(bmap->dim);
11725 if (!bmap->dim)
11726 goto error;
11727 bmap = isl_basic_map_mark_final(bmap);
11728 return bmap;
11729 error:
11730 isl_basic_map_free(bmap);
11731 return NULL;
11734 /* Given a map (A -> B) -> C, return the corresponding map
11735 * A -> (B -> C).
11737 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11739 return isl_map_change_space(map, &isl_map_can_curry,
11740 "map cannot be curried", &isl_space_curry);
11743 /* Can isl_map_range_curry be applied to "map"?
11744 * That is, does it have a nested relation in its range,
11745 * the domain of which is itself a nested relation?
11747 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11749 if (!map)
11750 return isl_bool_error;
11752 return isl_space_can_range_curry(map->dim);
11755 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11756 * A -> (B -> (C -> D)).
11758 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11760 return isl_map_change_space(map, &isl_map_can_range_curry,
11761 "map range cannot be curried",
11762 &isl_space_range_curry);
11765 /* Can we apply isl_basic_map_uncurry to "bmap"?
11766 * That is, does it have a nested relation in its domain?
11768 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11770 if (!bmap)
11771 return isl_bool_error;
11773 return isl_space_can_uncurry(bmap->dim);
11776 /* Can we apply isl_map_uncurry to "map"?
11777 * That is, does it have a nested relation in its domain?
11779 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11781 if (!map)
11782 return isl_bool_error;
11784 return isl_space_can_uncurry(map->dim);
11787 /* Given a basic map A -> (B -> C), return the corresponding basic map
11788 * (A -> B) -> C.
11790 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11793 if (!bmap)
11794 return NULL;
11796 if (!isl_basic_map_can_uncurry(bmap))
11797 isl_die(bmap->ctx, isl_error_invalid,
11798 "basic map cannot be uncurried",
11799 return isl_basic_map_free(bmap));
11800 bmap = isl_basic_map_cow(bmap);
11801 if (!bmap)
11802 return NULL;
11803 bmap->dim = isl_space_uncurry(bmap->dim);
11804 if (!bmap->dim)
11805 return isl_basic_map_free(bmap);
11806 bmap = isl_basic_map_mark_final(bmap);
11807 return bmap;
11810 /* Given a map A -> (B -> C), return the corresponding map
11811 * (A -> B) -> C.
11813 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11815 return isl_map_change_space(map, &isl_map_can_uncurry,
11816 "map cannot be uncurried", &isl_space_uncurry);
11819 /* Construct a basic map mapping the domain of the affine expression
11820 * to a one-dimensional range prescribed by the affine expression.
11822 * A NaN affine expression cannot be converted to a basic map.
11824 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11826 int k;
11827 int pos;
11828 isl_bool is_nan;
11829 isl_local_space *ls;
11830 isl_basic_map *bmap = NULL;
11832 if (!aff)
11833 return NULL;
11834 is_nan = isl_aff_is_nan(aff);
11835 if (is_nan < 0)
11836 goto error;
11837 if (is_nan)
11838 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11839 "cannot convert NaN", goto error);
11841 ls = isl_aff_get_local_space(aff);
11842 bmap = isl_basic_map_from_local_space(ls);
11843 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11844 k = isl_basic_map_alloc_equality(bmap);
11845 if (k < 0)
11846 goto error;
11848 pos = isl_basic_map_offset(bmap, isl_dim_out);
11849 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11850 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11851 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11852 aff->v->size - (pos + 1));
11854 isl_aff_free(aff);
11855 bmap = isl_basic_map_finalize(bmap);
11856 return bmap;
11857 error:
11858 isl_aff_free(aff);
11859 isl_basic_map_free(bmap);
11860 return NULL;
11863 /* Construct a map mapping the domain of the affine expression
11864 * to a one-dimensional range prescribed by the affine expression.
11866 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11868 isl_basic_map *bmap;
11870 bmap = isl_basic_map_from_aff(aff);
11871 return isl_map_from_basic_map(bmap);
11874 /* Construct a basic map mapping the domain the multi-affine expression
11875 * to its range, with each dimension in the range equated to the
11876 * corresponding affine expression.
11878 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11879 __isl_take isl_multi_aff *maff)
11881 int i;
11882 isl_space *space;
11883 isl_basic_map *bmap;
11885 if (!maff)
11886 return NULL;
11888 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11889 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11890 "invalid space", goto error);
11892 space = isl_space_domain(isl_multi_aff_get_space(maff));
11893 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11895 for (i = 0; i < maff->n; ++i) {
11896 isl_aff *aff;
11897 isl_basic_map *bmap_i;
11899 aff = isl_aff_copy(maff->p[i]);
11900 bmap_i = isl_basic_map_from_aff(aff);
11902 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11905 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11907 isl_multi_aff_free(maff);
11908 return bmap;
11909 error:
11910 isl_multi_aff_free(maff);
11911 return NULL;
11914 /* Construct a map mapping the domain the multi-affine expression
11915 * to its range, with each dimension in the range equated to the
11916 * corresponding affine expression.
11918 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11920 isl_basic_map *bmap;
11922 bmap = isl_basic_map_from_multi_aff(maff);
11923 return isl_map_from_basic_map(bmap);
11926 /* Construct a basic map mapping a domain in the given space to
11927 * to an n-dimensional range, with n the number of elements in the list,
11928 * where each coordinate in the range is prescribed by the
11929 * corresponding affine expression.
11930 * The domains of all affine expressions in the list are assumed to match
11931 * domain_dim.
11933 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11934 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11936 int i;
11937 isl_space *dim;
11938 isl_basic_map *bmap;
11940 if (!list)
11941 return NULL;
11943 dim = isl_space_from_domain(domain_dim);
11944 bmap = isl_basic_map_universe(dim);
11946 for (i = 0; i < list->n; ++i) {
11947 isl_aff *aff;
11948 isl_basic_map *bmap_i;
11950 aff = isl_aff_copy(list->p[i]);
11951 bmap_i = isl_basic_map_from_aff(aff);
11953 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11956 isl_aff_list_free(list);
11957 return bmap;
11960 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11961 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11963 return isl_map_equate(set, type1, pos1, type2, pos2);
11966 /* Construct a basic map where the given dimensions are equal to each other.
11968 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11969 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11971 isl_basic_map *bmap = NULL;
11972 int i;
11974 if (!space)
11975 return NULL;
11977 if (pos1 >= isl_space_dim(space, type1))
11978 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11979 "index out of bounds", goto error);
11980 if (pos2 >= isl_space_dim(space, type2))
11981 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11982 "index out of bounds", goto error);
11984 if (type1 == type2 && pos1 == pos2)
11985 return isl_basic_map_universe(space);
11987 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11988 i = isl_basic_map_alloc_equality(bmap);
11989 if (i < 0)
11990 goto error;
11991 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11992 pos1 += isl_basic_map_offset(bmap, type1);
11993 pos2 += isl_basic_map_offset(bmap, type2);
11994 isl_int_set_si(bmap->eq[i][pos1], -1);
11995 isl_int_set_si(bmap->eq[i][pos2], 1);
11996 bmap = isl_basic_map_finalize(bmap);
11997 isl_space_free(space);
11998 return bmap;
11999 error:
12000 isl_space_free(space);
12001 isl_basic_map_free(bmap);
12002 return NULL;
12005 /* Add a constraint imposing that the given two dimensions are equal.
12007 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12008 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12010 isl_basic_map *eq;
12012 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12014 bmap = isl_basic_map_intersect(bmap, eq);
12016 return bmap;
12019 /* Add a constraint imposing that the given two dimensions are equal.
12021 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12022 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12024 isl_basic_map *bmap;
12026 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12028 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12030 return map;
12033 /* Add a constraint imposing that the given two dimensions have opposite values.
12035 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12036 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12038 isl_basic_map *bmap = NULL;
12039 int i;
12041 if (!map)
12042 return NULL;
12044 if (pos1 >= isl_map_dim(map, type1))
12045 isl_die(map->ctx, isl_error_invalid,
12046 "index out of bounds", goto error);
12047 if (pos2 >= isl_map_dim(map, type2))
12048 isl_die(map->ctx, isl_error_invalid,
12049 "index out of bounds", goto error);
12051 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12052 i = isl_basic_map_alloc_equality(bmap);
12053 if (i < 0)
12054 goto error;
12055 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12056 pos1 += isl_basic_map_offset(bmap, type1);
12057 pos2 += isl_basic_map_offset(bmap, type2);
12058 isl_int_set_si(bmap->eq[i][pos1], 1);
12059 isl_int_set_si(bmap->eq[i][pos2], 1);
12060 bmap = isl_basic_map_finalize(bmap);
12062 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12064 return map;
12065 error:
12066 isl_basic_map_free(bmap);
12067 isl_map_free(map);
12068 return NULL;
12071 /* Construct a constraint imposing that the value of the first dimension is
12072 * greater than or equal to that of the second.
12074 static __isl_give isl_constraint *constraint_order_ge(
12075 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12076 enum isl_dim_type type2, int pos2)
12078 isl_constraint *c;
12080 if (!space)
12081 return NULL;
12083 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12085 if (pos1 >= isl_constraint_dim(c, type1))
12086 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12087 "index out of bounds", return isl_constraint_free(c));
12088 if (pos2 >= isl_constraint_dim(c, type2))
12089 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12090 "index out of bounds", return isl_constraint_free(c));
12092 if (type1 == type2 && pos1 == pos2)
12093 return c;
12095 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12096 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12098 return c;
12101 /* Add a constraint imposing that the value of the first dimension is
12102 * greater than or equal to that of the second.
12104 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12105 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12107 isl_constraint *c;
12108 isl_space *space;
12110 if (type1 == type2 && pos1 == pos2)
12111 return bmap;
12112 space = isl_basic_map_get_space(bmap);
12113 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12114 bmap = isl_basic_map_add_constraint(bmap, c);
12116 return bmap;
12119 /* Add a constraint imposing that the value of the first dimension is
12120 * greater than or equal to that of the second.
12122 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12123 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12125 isl_constraint *c;
12126 isl_space *space;
12128 if (type1 == type2 && pos1 == pos2)
12129 return map;
12130 space = isl_map_get_space(map);
12131 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12132 map = isl_map_add_constraint(map, c);
12134 return map;
12137 /* Add a constraint imposing that the value of the first dimension is
12138 * less than or equal to that of the second.
12140 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12141 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12143 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12146 /* Construct a basic map where the value of the first dimension is
12147 * greater than that of the second.
12149 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12150 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12152 isl_basic_map *bmap = NULL;
12153 int i;
12155 if (!space)
12156 return NULL;
12158 if (pos1 >= isl_space_dim(space, type1))
12159 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12160 "index out of bounds", goto error);
12161 if (pos2 >= isl_space_dim(space, type2))
12162 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12163 "index out of bounds", goto error);
12165 if (type1 == type2 && pos1 == pos2)
12166 return isl_basic_map_empty(space);
12168 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12169 i = isl_basic_map_alloc_inequality(bmap);
12170 if (i < 0)
12171 return isl_basic_map_free(bmap);
12172 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12173 pos1 += isl_basic_map_offset(bmap, type1);
12174 pos2 += isl_basic_map_offset(bmap, type2);
12175 isl_int_set_si(bmap->ineq[i][pos1], 1);
12176 isl_int_set_si(bmap->ineq[i][pos2], -1);
12177 isl_int_set_si(bmap->ineq[i][0], -1);
12178 bmap = isl_basic_map_finalize(bmap);
12180 return bmap;
12181 error:
12182 isl_space_free(space);
12183 isl_basic_map_free(bmap);
12184 return NULL;
12187 /* Add a constraint imposing that the value of the first dimension is
12188 * greater than that of the second.
12190 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12191 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12193 isl_basic_map *gt;
12195 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12197 bmap = isl_basic_map_intersect(bmap, gt);
12199 return bmap;
12202 /* Add a constraint imposing that the value of the first dimension is
12203 * greater than that of the second.
12205 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12206 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12208 isl_basic_map *bmap;
12210 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12212 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12214 return map;
12217 /* Add a constraint imposing that the value of the first dimension is
12218 * smaller than that of the second.
12220 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12221 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12223 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12226 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12227 int pos)
12229 isl_aff *div;
12230 isl_local_space *ls;
12232 if (!bmap)
12233 return NULL;
12235 if (!isl_basic_map_divs_known(bmap))
12236 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12237 "some divs are unknown", return NULL);
12239 ls = isl_basic_map_get_local_space(bmap);
12240 div = isl_local_space_get_div(ls, pos);
12241 isl_local_space_free(ls);
12243 return div;
12246 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12247 int pos)
12249 return isl_basic_map_get_div(bset, pos);
12252 /* Plug in "subs" for dimension "type", "pos" of "bset".
12254 * Let i be the dimension to replace and let "subs" be of the form
12256 * f/d
12258 * Any integer division with a non-zero coefficient for i,
12260 * floor((a i + g)/m)
12262 * is replaced by
12264 * floor((a f + d g)/(m d))
12266 * Constraints of the form
12268 * a i + g
12270 * are replaced by
12272 * a f + d g
12274 * We currently require that "subs" is an integral expression.
12275 * Handling rational expressions may require us to add stride constraints
12276 * as we do in isl_basic_set_preimage_multi_aff.
12278 __isl_give isl_basic_set *isl_basic_set_substitute(
12279 __isl_take isl_basic_set *bset,
12280 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12282 int i;
12283 isl_int v;
12284 isl_ctx *ctx;
12286 if (bset && isl_basic_set_plain_is_empty(bset))
12287 return bset;
12289 bset = isl_basic_set_cow(bset);
12290 if (!bset || !subs)
12291 goto error;
12293 ctx = isl_basic_set_get_ctx(bset);
12294 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12295 isl_die(ctx, isl_error_invalid,
12296 "spaces don't match", goto error);
12297 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12298 isl_die(ctx, isl_error_unsupported,
12299 "cannot handle divs yet", goto error);
12300 if (!isl_int_is_one(subs->v->el[0]))
12301 isl_die(ctx, isl_error_invalid,
12302 "can only substitute integer expressions", goto error);
12304 pos += isl_basic_set_offset(bset, type);
12306 isl_int_init(v);
12308 for (i = 0; i < bset->n_eq; ++i) {
12309 if (isl_int_is_zero(bset->eq[i][pos]))
12310 continue;
12311 isl_int_set(v, bset->eq[i][pos]);
12312 isl_int_set_si(bset->eq[i][pos], 0);
12313 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12314 v, subs->v->el + 1, subs->v->size - 1);
12317 for (i = 0; i < bset->n_ineq; ++i) {
12318 if (isl_int_is_zero(bset->ineq[i][pos]))
12319 continue;
12320 isl_int_set(v, bset->ineq[i][pos]);
12321 isl_int_set_si(bset->ineq[i][pos], 0);
12322 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12323 v, subs->v->el + 1, subs->v->size - 1);
12326 for (i = 0; i < bset->n_div; ++i) {
12327 if (isl_int_is_zero(bset->div[i][1 + pos]))
12328 continue;
12329 isl_int_set(v, bset->div[i][1 + pos]);
12330 isl_int_set_si(bset->div[i][1 + pos], 0);
12331 isl_seq_combine(bset->div[i] + 1,
12332 subs->v->el[0], bset->div[i] + 1,
12333 v, subs->v->el + 1, subs->v->size - 1);
12334 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12337 isl_int_clear(v);
12339 bset = isl_basic_set_simplify(bset);
12340 return isl_basic_set_finalize(bset);
12341 error:
12342 isl_basic_set_free(bset);
12343 return NULL;
12346 /* Plug in "subs" for dimension "type", "pos" of "set".
12348 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12349 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12351 int i;
12353 if (set && isl_set_plain_is_empty(set))
12354 return set;
12356 set = isl_set_cow(set);
12357 if (!set || !subs)
12358 goto error;
12360 for (i = set->n - 1; i >= 0; --i) {
12361 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12362 if (remove_if_empty(set, i) < 0)
12363 goto error;
12366 return set;
12367 error:
12368 isl_set_free(set);
12369 return NULL;
12372 /* Check if the range of "ma" is compatible with the domain or range
12373 * (depending on "type") of "bmap".
12374 * Return -1 if anything is wrong.
12376 static int check_basic_map_compatible_range_multi_aff(
12377 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12378 __isl_keep isl_multi_aff *ma)
12380 int m;
12381 isl_space *ma_space;
12383 ma_space = isl_multi_aff_get_space(ma);
12385 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12386 if (m < 0)
12387 goto error;
12388 if (!m)
12389 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12390 "parameters don't match", goto error);
12391 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12392 if (m < 0)
12393 goto error;
12394 if (!m)
12395 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12396 "spaces don't match", goto error);
12398 isl_space_free(ma_space);
12399 return m;
12400 error:
12401 isl_space_free(ma_space);
12402 return -1;
12405 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12406 * coefficients before the transformed range of dimensions,
12407 * the "n_after" coefficients after the transformed range of dimensions
12408 * and the coefficients of the other divs in "bmap".
12410 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12411 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12413 int i;
12414 int n_param;
12415 int n_set;
12416 isl_local_space *ls;
12418 if (n_div == 0)
12419 return 0;
12421 ls = isl_aff_get_domain_local_space(ma->p[0]);
12422 if (!ls)
12423 return -1;
12425 n_param = isl_local_space_dim(ls, isl_dim_param);
12426 n_set = isl_local_space_dim(ls, isl_dim_set);
12427 for (i = 0; i < n_div; ++i) {
12428 int o_bmap = 0, o_ls = 0;
12430 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12431 o_bmap += 1 + 1 + n_param;
12432 o_ls += 1 + 1 + n_param;
12433 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12434 o_bmap += n_before;
12435 isl_seq_cpy(bmap->div[i] + o_bmap,
12436 ls->div->row[i] + o_ls, n_set);
12437 o_bmap += n_set;
12438 o_ls += n_set;
12439 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12440 o_bmap += n_after;
12441 isl_seq_cpy(bmap->div[i] + o_bmap,
12442 ls->div->row[i] + o_ls, n_div);
12443 o_bmap += n_div;
12444 o_ls += n_div;
12445 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12446 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12447 goto error;
12450 isl_local_space_free(ls);
12451 return 0;
12452 error:
12453 isl_local_space_free(ls);
12454 return -1;
12457 /* How many stride constraints does "ma" enforce?
12458 * That is, how many of the affine expressions have a denominator
12459 * different from one?
12461 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12463 int i;
12464 int strides = 0;
12466 for (i = 0; i < ma->n; ++i)
12467 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12468 strides++;
12470 return strides;
12473 /* For each affine expression in ma of the form
12475 * x_i = (f_i y + h_i)/m_i
12477 * with m_i different from one, add a constraint to "bmap"
12478 * of the form
12480 * f_i y + h_i = m_i alpha_i
12482 * with alpha_i an additional existentially quantified variable.
12484 * The input variables of "ma" correspond to a subset of the variables
12485 * of "bmap". There are "n_before" variables in "bmap" before this
12486 * subset and "n_after" variables after this subset.
12487 * The integer divisions of the affine expressions in "ma" are assumed
12488 * to have been aligned. There are "n_div_ma" of them and
12489 * they appear first in "bmap", straight after the "n_after" variables.
12491 static __isl_give isl_basic_map *add_ma_strides(
12492 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12493 int n_before, int n_after, int n_div_ma)
12495 int i, k;
12496 int div;
12497 int total;
12498 int n_param;
12499 int n_in;
12501 total = isl_basic_map_total_dim(bmap);
12502 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12503 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12504 for (i = 0; i < ma->n; ++i) {
12505 int o_bmap = 0, o_ma = 1;
12507 if (isl_int_is_one(ma->p[i]->v->el[0]))
12508 continue;
12509 div = isl_basic_map_alloc_div(bmap);
12510 k = isl_basic_map_alloc_equality(bmap);
12511 if (div < 0 || k < 0)
12512 goto error;
12513 isl_int_set_si(bmap->div[div][0], 0);
12514 isl_seq_cpy(bmap->eq[k] + o_bmap,
12515 ma->p[i]->v->el + o_ma, 1 + n_param);
12516 o_bmap += 1 + n_param;
12517 o_ma += 1 + n_param;
12518 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12519 o_bmap += n_before;
12520 isl_seq_cpy(bmap->eq[k] + o_bmap,
12521 ma->p[i]->v->el + o_ma, n_in);
12522 o_bmap += n_in;
12523 o_ma += n_in;
12524 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12525 o_bmap += n_after;
12526 isl_seq_cpy(bmap->eq[k] + o_bmap,
12527 ma->p[i]->v->el + o_ma, n_div_ma);
12528 o_bmap += n_div_ma;
12529 o_ma += n_div_ma;
12530 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12531 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12532 total++;
12535 return bmap;
12536 error:
12537 isl_basic_map_free(bmap);
12538 return NULL;
12541 /* Replace the domain or range space (depending on "type) of "space" by "set".
12543 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12544 enum isl_dim_type type, __isl_take isl_space *set)
12546 if (type == isl_dim_in) {
12547 space = isl_space_range(space);
12548 space = isl_space_map_from_domain_and_range(set, space);
12549 } else {
12550 space = isl_space_domain(space);
12551 space = isl_space_map_from_domain_and_range(space, set);
12554 return space;
12557 /* Compute the preimage of the domain or range (depending on "type")
12558 * of "bmap" under the function represented by "ma".
12559 * In other words, plug in "ma" in the domain or range of "bmap".
12560 * The result is a basic map that lives in the same space as "bmap"
12561 * except that the domain or range has been replaced by
12562 * the domain space of "ma".
12564 * If bmap is represented by
12566 * A(p) + S u + B x + T v + C(divs) >= 0,
12568 * where u and x are input and output dimensions if type == isl_dim_out
12569 * while x and v are input and output dimensions if type == isl_dim_in,
12570 * and ma is represented by
12572 * x = D(p) + F(y) + G(divs')
12574 * then the result is
12576 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12578 * The divs in the input set are similarly adjusted.
12579 * In particular
12581 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12583 * becomes
12585 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12586 * B_i G(divs') + c_i(divs))/n_i)
12588 * If bmap is not a rational map and if F(y) involves any denominators
12590 * x_i = (f_i y + h_i)/m_i
12592 * then additional constraints are added to ensure that we only
12593 * map back integer points. That is we enforce
12595 * f_i y + h_i = m_i alpha_i
12597 * with alpha_i an additional existentially quantified variable.
12599 * We first copy over the divs from "ma".
12600 * Then we add the modified constraints and divs from "bmap".
12601 * Finally, we add the stride constraints, if needed.
12603 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12604 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12605 __isl_take isl_multi_aff *ma)
12607 int i, k;
12608 isl_space *space;
12609 isl_basic_map *res = NULL;
12610 int n_before, n_after, n_div_bmap, n_div_ma;
12611 isl_int f, c1, c2, g;
12612 int rational, strides;
12614 isl_int_init(f);
12615 isl_int_init(c1);
12616 isl_int_init(c2);
12617 isl_int_init(g);
12619 ma = isl_multi_aff_align_divs(ma);
12620 if (!bmap || !ma)
12621 goto error;
12622 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12623 goto error;
12625 if (type == isl_dim_in) {
12626 n_before = 0;
12627 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12628 } else {
12629 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12630 n_after = 0;
12632 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12633 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12635 space = isl_multi_aff_get_domain_space(ma);
12636 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12637 rational = isl_basic_map_is_rational(bmap);
12638 strides = rational ? 0 : multi_aff_strides(ma);
12639 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12640 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12641 if (rational)
12642 res = isl_basic_map_set_rational(res);
12644 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12645 if (isl_basic_map_alloc_div(res) < 0)
12646 goto error;
12648 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12649 goto error;
12651 for (i = 0; i < bmap->n_eq; ++i) {
12652 k = isl_basic_map_alloc_equality(res);
12653 if (k < 0)
12654 goto error;
12655 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12656 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12659 for (i = 0; i < bmap->n_ineq; ++i) {
12660 k = isl_basic_map_alloc_inequality(res);
12661 if (k < 0)
12662 goto error;
12663 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12664 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12667 for (i = 0; i < bmap->n_div; ++i) {
12668 if (isl_int_is_zero(bmap->div[i][0])) {
12669 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12670 continue;
12672 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12673 n_before, n_after, n_div_ma, n_div_bmap,
12674 f, c1, c2, g, 1);
12677 if (strides)
12678 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12680 isl_int_clear(f);
12681 isl_int_clear(c1);
12682 isl_int_clear(c2);
12683 isl_int_clear(g);
12684 isl_basic_map_free(bmap);
12685 isl_multi_aff_free(ma);
12686 res = isl_basic_set_simplify(res);
12687 return isl_basic_map_finalize(res);
12688 error:
12689 isl_int_clear(f);
12690 isl_int_clear(c1);
12691 isl_int_clear(c2);
12692 isl_int_clear(g);
12693 isl_basic_map_free(bmap);
12694 isl_multi_aff_free(ma);
12695 isl_basic_map_free(res);
12696 return NULL;
12699 /* Compute the preimage of "bset" under the function represented by "ma".
12700 * In other words, plug in "ma" in "bset". The result is a basic set
12701 * that lives in the domain space of "ma".
12703 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12704 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12706 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12709 /* Compute the preimage of the domain of "bmap" under the function
12710 * represented by "ma".
12711 * In other words, plug in "ma" in the domain of "bmap".
12712 * The result is a basic map that lives in the same space as "bmap"
12713 * except that the domain has been replaced by the domain space of "ma".
12715 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12716 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12718 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12721 /* Compute the preimage of the range of "bmap" under the function
12722 * represented by "ma".
12723 * In other words, plug in "ma" in the range of "bmap".
12724 * The result is a basic map that lives in the same space as "bmap"
12725 * except that the range has been replaced by the domain space of "ma".
12727 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12728 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12730 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12733 /* Check if the range of "ma" is compatible with the domain or range
12734 * (depending on "type") of "map".
12735 * Return -1 if anything is wrong.
12737 static int check_map_compatible_range_multi_aff(
12738 __isl_keep isl_map *map, enum isl_dim_type type,
12739 __isl_keep isl_multi_aff *ma)
12741 int m;
12742 isl_space *ma_space;
12744 ma_space = isl_multi_aff_get_space(ma);
12745 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12746 isl_space_free(ma_space);
12747 if (m >= 0 && !m)
12748 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12749 "spaces don't match", return -1);
12750 return m;
12753 /* Compute the preimage of the domain or range (depending on "type")
12754 * of "map" under the function represented by "ma".
12755 * In other words, plug in "ma" in the domain or range of "map".
12756 * The result is a map that lives in the same space as "map"
12757 * except that the domain or range has been replaced by
12758 * the domain space of "ma".
12760 * The parameters are assumed to have been aligned.
12762 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12763 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12765 int i;
12766 isl_space *space;
12768 map = isl_map_cow(map);
12769 ma = isl_multi_aff_align_divs(ma);
12770 if (!map || !ma)
12771 goto error;
12772 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12773 goto error;
12775 for (i = 0; i < map->n; ++i) {
12776 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12777 isl_multi_aff_copy(ma));
12778 if (!map->p[i])
12779 goto error;
12782 space = isl_multi_aff_get_domain_space(ma);
12783 space = isl_space_set(isl_map_get_space(map), type, space);
12785 isl_space_free(map->dim);
12786 map->dim = space;
12787 if (!map->dim)
12788 goto error;
12790 isl_multi_aff_free(ma);
12791 if (map->n > 1)
12792 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12793 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12794 return map;
12795 error:
12796 isl_multi_aff_free(ma);
12797 isl_map_free(map);
12798 return NULL;
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 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12809 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12811 if (!map || !ma)
12812 goto error;
12814 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12815 return map_preimage_multi_aff(map, type, ma);
12817 if (!isl_space_has_named_params(map->dim) ||
12818 !isl_space_has_named_params(ma->space))
12819 isl_die(map->ctx, isl_error_invalid,
12820 "unaligned unnamed parameters", goto error);
12821 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12822 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12824 return map_preimage_multi_aff(map, type, ma);
12825 error:
12826 isl_multi_aff_free(ma);
12827 return isl_map_free(map);
12830 /* Compute the preimage of "set" under the function represented by "ma".
12831 * In other words, plug in "ma" in "set". The result is a set
12832 * that lives in the domain space of "ma".
12834 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12835 __isl_take isl_multi_aff *ma)
12837 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12840 /* Compute the preimage of the domain of "map" under the function
12841 * represented by "ma".
12842 * In other words, plug in "ma" in the domain of "map".
12843 * The result is a map that lives in the same space as "map"
12844 * except that the domain has been replaced by the domain space of "ma".
12846 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12847 __isl_take isl_multi_aff *ma)
12849 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12852 /* Compute the preimage of the range of "map" under the function
12853 * represented by "ma".
12854 * In other words, plug in "ma" in the range of "map".
12855 * The result is a map that lives in the same space as "map"
12856 * except that the range has been replaced by the domain space of "ma".
12858 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12859 __isl_take isl_multi_aff *ma)
12861 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12864 /* Compute the preimage of "map" under the function represented by "pma".
12865 * In other words, plug in "pma" in the domain or range of "map".
12866 * The result is a map that lives in the same space as "map",
12867 * except that the space of type "type" has been replaced by
12868 * the domain space of "pma".
12870 * The parameters of "map" and "pma" are assumed to have been aligned.
12872 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12873 __isl_take isl_map *map, enum isl_dim_type type,
12874 __isl_take isl_pw_multi_aff *pma)
12876 int i;
12877 isl_map *res;
12879 if (!pma)
12880 goto error;
12882 if (pma->n == 0) {
12883 isl_pw_multi_aff_free(pma);
12884 res = isl_map_empty(isl_map_get_space(map));
12885 isl_map_free(map);
12886 return res;
12889 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12890 isl_multi_aff_copy(pma->p[0].maff));
12891 if (type == isl_dim_in)
12892 res = isl_map_intersect_domain(res,
12893 isl_map_copy(pma->p[0].set));
12894 else
12895 res = isl_map_intersect_range(res,
12896 isl_map_copy(pma->p[0].set));
12898 for (i = 1; i < pma->n; ++i) {
12899 isl_map *res_i;
12901 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12902 isl_multi_aff_copy(pma->p[i].maff));
12903 if (type == isl_dim_in)
12904 res_i = isl_map_intersect_domain(res_i,
12905 isl_map_copy(pma->p[i].set));
12906 else
12907 res_i = isl_map_intersect_range(res_i,
12908 isl_map_copy(pma->p[i].set));
12909 res = isl_map_union(res, res_i);
12912 isl_pw_multi_aff_free(pma);
12913 isl_map_free(map);
12914 return res;
12915 error:
12916 isl_pw_multi_aff_free(pma);
12917 isl_map_free(map);
12918 return NULL;
12921 /* Compute the preimage of "map" under the function represented by "pma".
12922 * In other words, plug in "pma" in the domain or range of "map".
12923 * The result is a map that lives in the same space as "map",
12924 * except that the space of type "type" has been replaced by
12925 * the domain space of "pma".
12927 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12928 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12930 if (!map || !pma)
12931 goto error;
12933 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12934 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12936 if (!isl_space_has_named_params(map->dim) ||
12937 !isl_space_has_named_params(pma->dim))
12938 isl_die(map->ctx, isl_error_invalid,
12939 "unaligned unnamed parameters", goto error);
12940 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12941 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12943 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12944 error:
12945 isl_pw_multi_aff_free(pma);
12946 return isl_map_free(map);
12949 /* Compute the preimage of "set" under the function represented by "pma".
12950 * In other words, plug in "pma" in "set". The result is a set
12951 * that lives in the domain space of "pma".
12953 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12954 __isl_take isl_pw_multi_aff *pma)
12956 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12959 /* Compute the preimage of the domain of "map" under the function
12960 * represented by "pma".
12961 * In other words, plug in "pma" in the domain of "map".
12962 * The result is a map that lives in the same space as "map",
12963 * except that domain space has been replaced by the domain space of "pma".
12965 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12966 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12968 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12971 /* Compute the preimage of the range of "map" under the function
12972 * represented by "pma".
12973 * In other words, plug in "pma" in the range of "map".
12974 * The result is a map that lives in the same space as "map",
12975 * except that range space has been replaced by the domain space of "pma".
12977 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12978 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12980 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12983 /* Compute the preimage of "map" under the function represented by "mpa".
12984 * In other words, plug in "mpa" in the domain or range of "map".
12985 * The result is a map that lives in the same space as "map",
12986 * except that the space of type "type" has been replaced by
12987 * the domain space of "mpa".
12989 * If the map does not involve any constraints that refer to the
12990 * dimensions of the substituted space, then the only possible
12991 * effect of "mpa" on the map is to map the space to a different space.
12992 * We create a separate isl_multi_aff to effectuate this change
12993 * in order to avoid spurious splitting of the map along the pieces
12994 * of "mpa".
12996 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12997 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12999 int n;
13000 isl_pw_multi_aff *pma;
13002 if (!map || !mpa)
13003 goto error;
13005 n = isl_map_dim(map, type);
13006 if (!isl_map_involves_dims(map, type, 0, n)) {
13007 isl_space *space;
13008 isl_multi_aff *ma;
13010 space = isl_multi_pw_aff_get_space(mpa);
13011 isl_multi_pw_aff_free(mpa);
13012 ma = isl_multi_aff_zero(space);
13013 return isl_map_preimage_multi_aff(map, type, ma);
13016 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13017 return isl_map_preimage_pw_multi_aff(map, type, pma);
13018 error:
13019 isl_map_free(map);
13020 isl_multi_pw_aff_free(mpa);
13021 return NULL;
13024 /* Compute the preimage of "map" under the function represented by "mpa".
13025 * In other words, plug in "mpa" in the domain "map".
13026 * The result is a map that lives in the same space as "map",
13027 * except that domain space has been replaced by the domain space of "mpa".
13029 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13030 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13032 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13035 /* Compute the preimage of "set" by the function represented by "mpa".
13036 * In other words, plug in "mpa" in "set".
13038 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13039 __isl_take isl_multi_pw_aff *mpa)
13041 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);