Merge branch 'maint'
[isl.git] / isl_map.c
blob67a962fc7b5887ad3baf1a7ee76a98dd76cb18d6
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 *space;
98 if (!bmap)
99 return 0;
101 space = bmap->dim;
102 switch (type) {
103 case isl_dim_cst: return 0;
104 case isl_dim_param: return 1;
105 case isl_dim_in: return 1 + space->nparam;
106 case isl_dim_out: return 1 + space->nparam + space->n_in;
107 case isl_dim_div: return 1 + space->nparam + space->n_in +
108 space->n_out;
109 default: return 0;
113 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
114 enum isl_dim_type type)
116 return isl_basic_map_offset(bset, type);
119 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
121 return pos(map->dim, type);
124 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
125 enum isl_dim_type type)
127 return isl_basic_map_dim(bset, type);
130 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
132 return isl_basic_set_dim(bset, isl_dim_set);
135 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
137 return isl_basic_set_dim(bset, isl_dim_param);
140 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
142 if (!bset)
143 return 0;
144 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
147 unsigned isl_set_n_dim(__isl_keep isl_set *set)
149 return isl_set_dim(set, isl_dim_set);
152 unsigned isl_set_n_param(__isl_keep isl_set *set)
154 return isl_set_dim(set, isl_dim_param);
157 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
159 return bmap ? bmap->dim->n_in : 0;
162 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
164 return bmap ? bmap->dim->n_out : 0;
167 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
169 return bmap ? bmap->dim->nparam : 0;
172 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
174 return bmap ? bmap->n_div : 0;
177 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
179 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
182 unsigned isl_map_n_in(const struct isl_map *map)
184 return map ? map->dim->n_in : 0;
187 unsigned isl_map_n_out(const struct isl_map *map)
189 return map ? map->dim->n_out : 0;
192 unsigned isl_map_n_param(const struct isl_map *map)
194 return map ? map->dim->nparam : 0;
197 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
199 int m;
200 if (!map || !set)
201 return -1;
202 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
203 if (m < 0 || !m)
204 return m;
205 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
206 set->dim, isl_dim_set);
209 isl_bool isl_basic_map_compatible_domain(__isl_keep isl_basic_map *bmap,
210 __isl_keep isl_basic_set *bset)
212 isl_bool m;
213 if (!bmap || !bset)
214 return isl_bool_error;
215 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
216 if (m < 0 || !m)
217 return m;
218 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
219 bset->dim, isl_dim_set);
222 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
224 int m;
225 if (!map || !set)
226 return -1;
227 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
228 if (m < 0 || !m)
229 return m;
230 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
231 set->dim, isl_dim_set);
234 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
235 struct isl_basic_set *bset)
237 int m;
238 if (!bmap || !bset)
239 return -1;
240 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
241 if (m < 0 || !m)
242 return m;
243 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
244 bset->dim, isl_dim_set);
247 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
249 return bmap ? bmap->ctx : NULL;
252 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
254 return bset ? bset->ctx : NULL;
257 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
259 return map ? map->ctx : NULL;
262 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
264 return set ? set->ctx : NULL;
267 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
269 if (!bmap)
270 return NULL;
271 return isl_space_copy(bmap->dim);
274 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
276 if (!bset)
277 return NULL;
278 return isl_space_copy(bset->dim);
281 /* Extract the divs in "bmap" as a matrix.
283 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
285 int i;
286 isl_ctx *ctx;
287 isl_mat *div;
288 unsigned total;
289 unsigned cols;
291 if (!bmap)
292 return NULL;
294 ctx = isl_basic_map_get_ctx(bmap);
295 total = isl_space_dim(bmap->dim, isl_dim_all);
296 cols = 1 + 1 + total + bmap->n_div;
297 div = isl_mat_alloc(ctx, bmap->n_div, cols);
298 if (!div)
299 return NULL;
301 for (i = 0; i < bmap->n_div; ++i)
302 isl_seq_cpy(div->row[i], bmap->div[i], cols);
304 return div;
307 /* Extract the divs in "bset" as a matrix.
309 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
311 return isl_basic_map_get_divs(bset);
314 __isl_give isl_local_space *isl_basic_map_get_local_space(
315 __isl_keep isl_basic_map *bmap)
317 isl_mat *div;
319 if (!bmap)
320 return NULL;
322 div = isl_basic_map_get_divs(bmap);
323 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
326 __isl_give isl_local_space *isl_basic_set_get_local_space(
327 __isl_keep isl_basic_set *bset)
329 return isl_basic_map_get_local_space(bset);
332 /* For each known div d = floor(f/m), add the constraints
334 * f - m d >= 0
335 * -(f-(m-1)) + m d >= 0
337 * Do not finalize the result.
339 static __isl_give isl_basic_map *add_known_div_constraints(
340 __isl_take isl_basic_map *bmap)
342 int i;
343 unsigned n_div;
345 if (!bmap)
346 return NULL;
347 n_div = isl_basic_map_dim(bmap, isl_dim_div);
348 if (n_div == 0)
349 return bmap;
350 bmap = isl_basic_map_cow(bmap);
351 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
352 if (!bmap)
353 return NULL;
354 for (i = 0; i < n_div; ++i) {
355 if (isl_int_is_zero(bmap->div[i][0]))
356 continue;
357 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
358 return isl_basic_map_free(bmap);
361 return bmap;
364 __isl_give isl_basic_map *isl_basic_map_from_local_space(
365 __isl_take isl_local_space *ls)
367 int i;
368 int n_div;
369 isl_basic_map *bmap;
371 if (!ls)
372 return NULL;
374 n_div = isl_local_space_dim(ls, isl_dim_div);
375 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
376 n_div, 0, 2 * n_div);
378 for (i = 0; i < n_div; ++i)
379 if (isl_basic_map_alloc_div(bmap) < 0)
380 goto error;
382 for (i = 0; i < n_div; ++i)
383 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
384 bmap = add_known_div_constraints(bmap);
386 isl_local_space_free(ls);
387 return bmap;
388 error:
389 isl_local_space_free(ls);
390 isl_basic_map_free(bmap);
391 return NULL;
394 __isl_give isl_basic_set *isl_basic_set_from_local_space(
395 __isl_take isl_local_space *ls)
397 return isl_basic_map_from_local_space(ls);
400 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
402 if (!map)
403 return NULL;
404 return isl_space_copy(map->dim);
407 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
409 if (!set)
410 return NULL;
411 return isl_space_copy(set->dim);
414 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
415 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
417 bmap = isl_basic_map_cow(bmap);
418 if (!bmap)
419 return NULL;
420 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
421 if (!bmap->dim)
422 goto error;
423 bmap = isl_basic_map_finalize(bmap);
424 return bmap;
425 error:
426 isl_basic_map_free(bmap);
427 return NULL;
430 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
431 __isl_take isl_basic_set *bset, const char *s)
433 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
436 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
437 enum isl_dim_type type)
439 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
442 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
443 enum isl_dim_type type, const char *s)
445 int i;
447 map = isl_map_cow(map);
448 if (!map)
449 return NULL;
451 map->dim = isl_space_set_tuple_name(map->dim, type, s);
452 if (!map->dim)
453 goto error;
455 for (i = 0; i < map->n; ++i) {
456 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
457 if (!map->p[i])
458 goto error;
461 return map;
462 error:
463 isl_map_free(map);
464 return NULL;
467 /* Replace the identifier of the tuple of type "type" by "id".
469 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
470 __isl_take isl_basic_map *bmap,
471 enum isl_dim_type type, __isl_take isl_id *id)
473 bmap = isl_basic_map_cow(bmap);
474 if (!bmap)
475 goto error;
476 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
477 if (!bmap->dim)
478 return isl_basic_map_free(bmap);
479 bmap = isl_basic_map_finalize(bmap);
480 return bmap;
481 error:
482 isl_id_free(id);
483 return NULL;
486 /* Replace the identifier of the tuple by "id".
488 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
489 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
491 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
494 /* Does the input or output tuple have a name?
496 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
498 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
501 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
502 enum isl_dim_type type)
504 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
507 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
508 const char *s)
510 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
513 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
514 enum isl_dim_type type, __isl_take isl_id *id)
516 map = isl_map_cow(map);
517 if (!map)
518 goto error;
520 map->dim = isl_space_set_tuple_id(map->dim, type, id);
522 return isl_map_reset_space(map, isl_space_copy(map->dim));
523 error:
524 isl_id_free(id);
525 return NULL;
528 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
529 __isl_take isl_id *id)
531 return isl_map_set_tuple_id(set, isl_dim_set, id);
534 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
535 enum isl_dim_type type)
537 map = isl_map_cow(map);
538 if (!map)
539 return NULL;
541 map->dim = isl_space_reset_tuple_id(map->dim, type);
543 return isl_map_reset_space(map, isl_space_copy(map->dim));
546 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
548 return isl_map_reset_tuple_id(set, isl_dim_set);
551 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
553 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
556 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
557 enum isl_dim_type type)
559 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
562 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
564 return isl_map_has_tuple_id(set, isl_dim_set);
567 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
569 return isl_map_get_tuple_id(set, isl_dim_set);
572 /* Does the set tuple have a name?
574 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
576 if (!set)
577 return isl_bool_error;
578 return isl_space_has_tuple_name(set->dim, isl_dim_set);
582 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
584 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
587 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
589 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
592 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
593 enum isl_dim_type type, unsigned pos)
595 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
598 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
599 enum isl_dim_type type, unsigned pos)
601 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
604 /* Does the given dimension have a name?
606 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
607 enum isl_dim_type type, unsigned pos)
609 if (!map)
610 return isl_bool_error;
611 return isl_space_has_dim_name(map->dim, type, pos);
614 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
615 enum isl_dim_type type, unsigned pos)
617 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
620 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
621 enum isl_dim_type type, unsigned pos)
623 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
626 /* Does the given dimension have a name?
628 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
629 enum isl_dim_type type, unsigned pos)
631 if (!set)
632 return isl_bool_error;
633 return isl_space_has_dim_name(set->dim, type, pos);
636 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
637 __isl_take isl_basic_map *bmap,
638 enum isl_dim_type type, unsigned pos, const char *s)
640 bmap = isl_basic_map_cow(bmap);
641 if (!bmap)
642 return NULL;
643 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
644 if (!bmap->dim)
645 goto error;
646 return isl_basic_map_finalize(bmap);
647 error:
648 isl_basic_map_free(bmap);
649 return NULL;
652 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
653 enum isl_dim_type type, unsigned pos, const char *s)
655 int i;
657 map = isl_map_cow(map);
658 if (!map)
659 return NULL;
661 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
662 if (!map->dim)
663 goto error;
665 for (i = 0; i < map->n; ++i) {
666 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
667 if (!map->p[i])
668 goto error;
671 return map;
672 error:
673 isl_map_free(map);
674 return NULL;
677 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
678 __isl_take isl_basic_set *bset,
679 enum isl_dim_type type, unsigned pos, const char *s)
681 return (isl_basic_set *)isl_basic_map_set_dim_name(
682 (isl_basic_map *)bset, type, pos, s);
685 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
686 enum isl_dim_type type, unsigned pos, const char *s)
688 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
691 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
692 enum isl_dim_type type, unsigned pos)
694 if (!bmap)
695 return isl_bool_error;
696 return isl_space_has_dim_id(bmap->dim, type, pos);
699 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
700 enum isl_dim_type type, unsigned pos)
702 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
705 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
706 enum isl_dim_type type, unsigned pos)
708 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
711 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
712 enum isl_dim_type type, unsigned pos)
714 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
717 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
718 enum isl_dim_type type, unsigned pos)
720 return isl_map_has_dim_id(set, type, pos);
723 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
724 enum isl_dim_type type, unsigned pos)
726 return isl_map_get_dim_id(set, type, pos);
729 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
730 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
732 map = isl_map_cow(map);
733 if (!map)
734 goto error;
736 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
738 return isl_map_reset_space(map, isl_space_copy(map->dim));
739 error:
740 isl_id_free(id);
741 return NULL;
744 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
745 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
747 return isl_map_set_dim_id(set, type, pos, id);
750 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
751 __isl_keep isl_id *id)
753 if (!map)
754 return -1;
755 return isl_space_find_dim_by_id(map->dim, type, id);
758 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
759 __isl_keep isl_id *id)
761 return isl_map_find_dim_by_id(set, type, id);
764 /* Return the position of the dimension of the given type and name
765 * in "bmap".
766 * Return -1 if no such dimension can be found.
768 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
769 enum isl_dim_type type, const char *name)
771 if (!bmap)
772 return -1;
773 return isl_space_find_dim_by_name(bmap->dim, type, name);
776 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
777 const char *name)
779 if (!map)
780 return -1;
781 return isl_space_find_dim_by_name(map->dim, type, name);
784 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
785 const char *name)
787 return isl_map_find_dim_by_name(set, type, name);
790 /* Reset the user pointer on all identifiers of parameters and tuples
791 * of the space of "map".
793 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
795 isl_space *space;
797 space = isl_map_get_space(map);
798 space = isl_space_reset_user(space);
799 map = isl_map_reset_space(map, space);
801 return map;
804 /* Reset the user pointer on all identifiers of parameters and tuples
805 * of the space of "set".
807 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
809 return isl_map_reset_user(set);
812 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
814 if (!bmap)
815 return -1;
816 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
819 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
821 return isl_basic_map_is_rational(bset);
824 /* Does "bmap" contain any rational points?
826 * If "bmap" has an equality for each dimension, equating the dimension
827 * to an integer constant, then it has no rational points, even if it
828 * is marked as rational.
830 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
832 int has_rational = 1;
833 unsigned total;
835 if (!bmap)
836 return -1;
837 if (isl_basic_map_plain_is_empty(bmap))
838 return 0;
839 if (!isl_basic_map_is_rational(bmap))
840 return 0;
841 bmap = isl_basic_map_copy(bmap);
842 bmap = isl_basic_map_implicit_equalities(bmap);
843 if (!bmap)
844 return -1;
845 total = isl_basic_map_total_dim(bmap);
846 if (bmap->n_eq == total) {
847 int i, j;
848 for (i = 0; i < bmap->n_eq; ++i) {
849 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
850 if (j < 0)
851 break;
852 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
853 !isl_int_is_negone(bmap->eq[i][1 + j]))
854 break;
855 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
856 total - j - 1);
857 if (j >= 0)
858 break;
860 if (i == bmap->n_eq)
861 has_rational = 0;
863 isl_basic_map_free(bmap);
865 return has_rational;
868 /* Does "map" contain any rational points?
870 int isl_map_has_rational(__isl_keep isl_map *map)
872 int i;
873 int has_rational;
875 if (!map)
876 return -1;
877 for (i = 0; i < map->n; ++i) {
878 has_rational = isl_basic_map_has_rational(map->p[i]);
879 if (has_rational < 0)
880 return -1;
881 if (has_rational)
882 return 1;
884 return 0;
887 /* Does "set" contain any rational points?
889 int isl_set_has_rational(__isl_keep isl_set *set)
891 return isl_map_has_rational(set);
894 /* Is this basic set a parameter domain?
896 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
898 if (!bset)
899 return -1;
900 return isl_space_is_params(bset->dim);
903 /* Is this set a parameter domain?
905 isl_bool isl_set_is_params(__isl_keep isl_set *set)
907 if (!set)
908 return isl_bool_error;
909 return isl_space_is_params(set->dim);
912 /* Is this map actually a parameter domain?
913 * Users should never call this function. Outside of isl,
914 * a map can never be a parameter domain.
916 int isl_map_is_params(__isl_keep isl_map *map)
918 if (!map)
919 return -1;
920 return isl_space_is_params(map->dim);
923 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
924 struct isl_basic_map *bmap, unsigned extra,
925 unsigned n_eq, unsigned n_ineq)
927 int i;
928 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
930 bmap->ctx = ctx;
931 isl_ctx_ref(ctx);
933 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
934 if (isl_blk_is_error(bmap->block))
935 goto error;
937 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
938 if ((n_ineq + n_eq) && !bmap->ineq)
939 goto error;
941 if (extra == 0) {
942 bmap->block2 = isl_blk_empty();
943 bmap->div = NULL;
944 } else {
945 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
946 if (isl_blk_is_error(bmap->block2))
947 goto error;
949 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
950 if (!bmap->div)
951 goto error;
954 for (i = 0; i < n_ineq + n_eq; ++i)
955 bmap->ineq[i] = bmap->block.data + i * row_size;
957 for (i = 0; i < extra; ++i)
958 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
960 bmap->ref = 1;
961 bmap->flags = 0;
962 bmap->c_size = n_eq + n_ineq;
963 bmap->eq = bmap->ineq + n_ineq;
964 bmap->extra = extra;
965 bmap->n_eq = 0;
966 bmap->n_ineq = 0;
967 bmap->n_div = 0;
968 bmap->sample = NULL;
970 return bmap;
971 error:
972 isl_basic_map_free(bmap);
973 return NULL;
976 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
977 unsigned nparam, unsigned dim, unsigned extra,
978 unsigned n_eq, unsigned n_ineq)
980 struct isl_basic_map *bmap;
981 isl_space *space;
983 space = isl_space_set_alloc(ctx, nparam, dim);
984 if (!space)
985 return NULL;
987 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
988 return (struct isl_basic_set *)bmap;
991 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
992 unsigned extra, unsigned n_eq, unsigned n_ineq)
994 struct isl_basic_map *bmap;
995 if (!dim)
996 return NULL;
997 isl_assert(dim->ctx, dim->n_in == 0, goto error);
998 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
999 return (struct isl_basic_set *)bmap;
1000 error:
1001 isl_space_free(dim);
1002 return NULL;
1005 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
1006 unsigned extra, unsigned n_eq, unsigned n_ineq)
1008 struct isl_basic_map *bmap;
1010 if (!dim)
1011 return NULL;
1012 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1013 if (!bmap)
1014 goto error;
1015 bmap->dim = dim;
1017 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1018 error:
1019 isl_space_free(dim);
1020 return NULL;
1023 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1024 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1025 unsigned n_eq, unsigned n_ineq)
1027 struct isl_basic_map *bmap;
1028 isl_space *dim;
1030 dim = isl_space_alloc(ctx, nparam, in, out);
1031 if (!dim)
1032 return NULL;
1034 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1035 return bmap;
1038 static void dup_constraints(
1039 struct isl_basic_map *dst, struct isl_basic_map *src)
1041 int i;
1042 unsigned total = isl_basic_map_total_dim(src);
1044 for (i = 0; i < src->n_eq; ++i) {
1045 int j = isl_basic_map_alloc_equality(dst);
1046 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1049 for (i = 0; i < src->n_ineq; ++i) {
1050 int j = isl_basic_map_alloc_inequality(dst);
1051 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1054 for (i = 0; i < src->n_div; ++i) {
1055 int j = isl_basic_map_alloc_div(dst);
1056 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1058 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1061 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1063 struct isl_basic_map *dup;
1065 if (!bmap)
1066 return NULL;
1067 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1068 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1069 if (!dup)
1070 return NULL;
1071 dup_constraints(dup, bmap);
1072 dup->flags = bmap->flags;
1073 dup->sample = isl_vec_copy(bmap->sample);
1074 return dup;
1077 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1079 struct isl_basic_map *dup;
1081 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1082 return (struct isl_basic_set *)dup;
1085 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1087 if (!bset)
1088 return NULL;
1090 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1091 bset->ref++;
1092 return bset;
1094 return isl_basic_set_dup(bset);
1097 struct isl_set *isl_set_copy(struct isl_set *set)
1099 if (!set)
1100 return NULL;
1102 set->ref++;
1103 return set;
1106 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1108 if (!bmap)
1109 return NULL;
1111 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1112 bmap->ref++;
1113 return bmap;
1115 bmap = isl_basic_map_dup(bmap);
1116 if (bmap)
1117 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1118 return bmap;
1121 struct isl_map *isl_map_copy(struct isl_map *map)
1123 if (!map)
1124 return NULL;
1126 map->ref++;
1127 return map;
1130 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1132 if (!bmap)
1133 return NULL;
1135 if (--bmap->ref > 0)
1136 return NULL;
1138 isl_ctx_deref(bmap->ctx);
1139 free(bmap->div);
1140 isl_blk_free(bmap->ctx, bmap->block2);
1141 free(bmap->ineq);
1142 isl_blk_free(bmap->ctx, bmap->block);
1143 isl_vec_free(bmap->sample);
1144 isl_space_free(bmap->dim);
1145 free(bmap);
1147 return NULL;
1150 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1152 return isl_basic_map_free((struct isl_basic_map *)bset);
1155 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1157 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1160 __isl_give isl_map *isl_map_align_params_map_map_and(
1161 __isl_take isl_map *map1, __isl_take isl_map *map2,
1162 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1163 __isl_take isl_map *map2))
1165 if (!map1 || !map2)
1166 goto error;
1167 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1168 return fn(map1, map2);
1169 if (!isl_space_has_named_params(map1->dim) ||
1170 !isl_space_has_named_params(map2->dim))
1171 isl_die(map1->ctx, isl_error_invalid,
1172 "unaligned unnamed parameters", goto error);
1173 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1174 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1175 return fn(map1, map2);
1176 error:
1177 isl_map_free(map1);
1178 isl_map_free(map2);
1179 return NULL;
1182 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1183 __isl_keep isl_map *map2,
1184 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1186 isl_bool r;
1188 if (!map1 || !map2)
1189 return isl_bool_error;
1190 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1191 return fn(map1, map2);
1192 if (!isl_space_has_named_params(map1->dim) ||
1193 !isl_space_has_named_params(map2->dim))
1194 isl_die(map1->ctx, isl_error_invalid,
1195 "unaligned unnamed parameters", return isl_bool_error);
1196 map1 = isl_map_copy(map1);
1197 map2 = isl_map_copy(map2);
1198 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1199 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1200 r = fn(map1, map2);
1201 isl_map_free(map1);
1202 isl_map_free(map2);
1203 return r;
1206 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1208 struct isl_ctx *ctx;
1209 if (!bmap)
1210 return -1;
1211 ctx = bmap->ctx;
1212 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1213 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1214 return -1);
1215 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1216 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1217 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1218 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1219 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1220 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1221 isl_int *t;
1222 int j = isl_basic_map_alloc_inequality(bmap);
1223 if (j < 0)
1224 return -1;
1225 t = bmap->ineq[j];
1226 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1227 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1228 bmap->eq[-1] = t;
1229 bmap->n_eq++;
1230 bmap->n_ineq--;
1231 bmap->eq--;
1232 return 0;
1234 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1235 bmap->extra - bmap->n_div);
1236 return bmap->n_eq++;
1239 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1241 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1244 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1246 if (!bmap)
1247 return -1;
1248 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1249 bmap->n_eq -= n;
1250 return 0;
1253 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1255 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1258 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1260 isl_int *t;
1261 if (!bmap)
1262 return -1;
1263 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1265 if (pos != bmap->n_eq - 1) {
1266 t = bmap->eq[pos];
1267 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1268 bmap->eq[bmap->n_eq - 1] = t;
1270 bmap->n_eq--;
1271 return 0;
1274 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1276 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1279 /* Turn inequality "pos" of "bmap" into an equality.
1281 * In particular, we move the inequality in front of the equalities
1282 * and move the last inequality in the position of the moved inequality.
1283 * Note that isl_tab_make_equalities_explicit depends on this particular
1284 * change in the ordering of the constraints.
1286 void isl_basic_map_inequality_to_equality(
1287 struct isl_basic_map *bmap, unsigned pos)
1289 isl_int *t;
1291 t = bmap->ineq[pos];
1292 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1293 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1294 bmap->eq[-1] = t;
1295 bmap->n_eq++;
1296 bmap->n_ineq--;
1297 bmap->eq--;
1298 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1299 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1300 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1301 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1304 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1306 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1309 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1311 struct isl_ctx *ctx;
1312 if (!bmap)
1313 return -1;
1314 ctx = bmap->ctx;
1315 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1316 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1317 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1318 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1319 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1320 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1321 1 + isl_basic_map_total_dim(bmap),
1322 bmap->extra - bmap->n_div);
1323 return bmap->n_ineq++;
1326 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1328 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1331 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1333 if (!bmap)
1334 return -1;
1335 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1336 bmap->n_ineq -= n;
1337 return 0;
1340 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1342 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1345 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1347 isl_int *t;
1348 if (!bmap)
1349 return -1;
1350 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1352 if (pos != bmap->n_ineq - 1) {
1353 t = bmap->ineq[pos];
1354 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1355 bmap->ineq[bmap->n_ineq - 1] = t;
1356 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1358 bmap->n_ineq--;
1359 return 0;
1362 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1364 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1367 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1368 isl_int *eq)
1370 int k;
1372 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1373 if (!bmap)
1374 return NULL;
1375 k = isl_basic_map_alloc_equality(bmap);
1376 if (k < 0)
1377 goto error;
1378 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1379 return bmap;
1380 error:
1381 isl_basic_map_free(bmap);
1382 return NULL;
1385 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1386 isl_int *eq)
1388 return (isl_basic_set *)
1389 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1392 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1393 isl_int *ineq)
1395 int k;
1397 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1398 if (!bmap)
1399 return NULL;
1400 k = isl_basic_map_alloc_inequality(bmap);
1401 if (k < 0)
1402 goto error;
1403 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1404 return bmap;
1405 error:
1406 isl_basic_map_free(bmap);
1407 return NULL;
1410 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1411 isl_int *ineq)
1413 return (isl_basic_set *)
1414 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1417 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1419 if (!bmap)
1420 return -1;
1421 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1422 isl_seq_clr(bmap->div[bmap->n_div] +
1423 1 + 1 + isl_basic_map_total_dim(bmap),
1424 bmap->extra - bmap->n_div);
1425 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1426 return bmap->n_div++;
1429 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1431 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1434 /* Insert an extra integer division, prescribed by "div", to "bmap"
1435 * at (integer division) position "pos".
1437 * The integer division is first added at the end and then moved
1438 * into the right position.
1440 __isl_give isl_basic_map *isl_basic_map_insert_div(
1441 __isl_take isl_basic_map *bmap, int pos, __isl_keep isl_vec *div)
1443 int i, k, n_div;
1445 bmap = isl_basic_map_cow(bmap);
1446 if (!bmap || !div)
1447 return isl_basic_map_free(bmap);
1449 if (div->size != 1 + 1 + isl_basic_map_dim(bmap, isl_dim_all))
1450 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1451 "unexpected size", return isl_basic_map_free(bmap));
1452 n_div = isl_basic_map_dim(bmap, isl_dim_div);
1453 if (pos < 0 || pos > n_div)
1454 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
1455 "invalid position", return isl_basic_map_free(bmap));
1457 bmap = isl_basic_map_extend_space(bmap,
1458 isl_basic_map_get_space(bmap), 1, 0, 2);
1459 k = isl_basic_map_alloc_div(bmap);
1460 if (k < 0)
1461 return isl_basic_map_free(bmap);
1462 isl_seq_cpy(bmap->div[k], div->el, div->size);
1463 isl_int_set_si(bmap->div[k][div->size], 0);
1465 for (i = k; i > pos; --i)
1466 isl_basic_map_swap_div(bmap, i, i - 1);
1468 return bmap;
1471 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1473 if (!bmap)
1474 return -1;
1475 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1476 bmap->n_div -= n;
1477 return 0;
1480 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1482 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1485 /* Copy constraint from src to dst, putting the vars of src at offset
1486 * dim_off in dst and the divs of src at offset div_off in dst.
1487 * If both sets are actually map, then dim_off applies to the input
1488 * variables.
1490 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1491 struct isl_basic_map *src_map, isl_int *src,
1492 unsigned in_off, unsigned out_off, unsigned div_off)
1494 unsigned src_nparam = isl_basic_map_n_param(src_map);
1495 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1496 unsigned src_in = isl_basic_map_n_in(src_map);
1497 unsigned dst_in = isl_basic_map_n_in(dst_map);
1498 unsigned src_out = isl_basic_map_n_out(src_map);
1499 unsigned dst_out = isl_basic_map_n_out(dst_map);
1500 isl_int_set(dst[0], src[0]);
1501 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1502 if (dst_nparam > src_nparam)
1503 isl_seq_clr(dst+1+src_nparam,
1504 dst_nparam - src_nparam);
1505 isl_seq_clr(dst+1+dst_nparam, in_off);
1506 isl_seq_cpy(dst+1+dst_nparam+in_off,
1507 src+1+src_nparam,
1508 isl_min(dst_in-in_off, src_in));
1509 if (dst_in-in_off > src_in)
1510 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1511 dst_in - in_off - src_in);
1512 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1513 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1514 src+1+src_nparam+src_in,
1515 isl_min(dst_out-out_off, src_out));
1516 if (dst_out-out_off > src_out)
1517 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1518 dst_out - out_off - src_out);
1519 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1520 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1521 src+1+src_nparam+src_in+src_out,
1522 isl_min(dst_map->extra-div_off, src_map->n_div));
1523 if (dst_map->n_div-div_off > src_map->n_div)
1524 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1525 div_off+src_map->n_div,
1526 dst_map->n_div - div_off - src_map->n_div);
1529 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1530 struct isl_basic_map *src_map, isl_int *src,
1531 unsigned in_off, unsigned out_off, unsigned div_off)
1533 isl_int_set(dst[0], src[0]);
1534 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1537 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1538 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1540 int i;
1541 unsigned div_off;
1543 if (!bmap1 || !bmap2)
1544 goto error;
1546 div_off = bmap1->n_div;
1548 for (i = 0; i < bmap2->n_eq; ++i) {
1549 int i1 = isl_basic_map_alloc_equality(bmap1);
1550 if (i1 < 0)
1551 goto error;
1552 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1553 i_pos, o_pos, div_off);
1556 for (i = 0; i < bmap2->n_ineq; ++i) {
1557 int i1 = isl_basic_map_alloc_inequality(bmap1);
1558 if (i1 < 0)
1559 goto error;
1560 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1561 i_pos, o_pos, div_off);
1564 for (i = 0; i < bmap2->n_div; ++i) {
1565 int i1 = isl_basic_map_alloc_div(bmap1);
1566 if (i1 < 0)
1567 goto error;
1568 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1569 i_pos, o_pos, div_off);
1572 isl_basic_map_free(bmap2);
1574 return bmap1;
1576 error:
1577 isl_basic_map_free(bmap1);
1578 isl_basic_map_free(bmap2);
1579 return NULL;
1582 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1583 struct isl_basic_set *bset2, unsigned pos)
1585 return (struct isl_basic_set *)
1586 add_constraints((struct isl_basic_map *)bset1,
1587 (struct isl_basic_map *)bset2, 0, pos);
1590 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1591 __isl_take isl_space *dim, unsigned extra,
1592 unsigned n_eq, unsigned n_ineq)
1594 struct isl_basic_map *ext;
1595 unsigned flags;
1596 int dims_ok;
1598 if (!dim)
1599 goto error;
1601 if (!base)
1602 goto error;
1604 dims_ok = isl_space_is_equal(base->dim, dim) &&
1605 base->extra >= base->n_div + extra;
1607 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1608 room_for_ineq(base, n_ineq)) {
1609 isl_space_free(dim);
1610 return base;
1613 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1614 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1615 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1616 extra += base->extra;
1617 n_eq += base->n_eq;
1618 n_ineq += base->n_ineq;
1620 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1621 dim = NULL;
1622 if (!ext)
1623 goto error;
1625 if (dims_ok)
1626 ext->sample = isl_vec_copy(base->sample);
1627 flags = base->flags;
1628 ext = add_constraints(ext, base, 0, 0);
1629 if (ext) {
1630 ext->flags = flags;
1631 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1634 return ext;
1636 error:
1637 isl_space_free(dim);
1638 isl_basic_map_free(base);
1639 return NULL;
1642 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1643 __isl_take isl_space *dim, unsigned extra,
1644 unsigned n_eq, unsigned n_ineq)
1646 return (struct isl_basic_set *)
1647 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1648 extra, n_eq, n_ineq);
1651 struct isl_basic_map *isl_basic_map_extend_constraints(
1652 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1654 if (!base)
1655 return NULL;
1656 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1657 0, n_eq, n_ineq);
1660 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1661 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1662 unsigned n_eq, unsigned n_ineq)
1664 struct isl_basic_map *bmap;
1665 isl_space *dim;
1667 if (!base)
1668 return NULL;
1669 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1670 if (!dim)
1671 goto error;
1673 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1674 return bmap;
1675 error:
1676 isl_basic_map_free(base);
1677 return NULL;
1680 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1681 unsigned nparam, unsigned dim, unsigned extra,
1682 unsigned n_eq, unsigned n_ineq)
1684 return (struct isl_basic_set *)
1685 isl_basic_map_extend((struct isl_basic_map *)base,
1686 nparam, 0, dim, extra, n_eq, n_ineq);
1689 struct isl_basic_set *isl_basic_set_extend_constraints(
1690 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1692 return (struct isl_basic_set *)
1693 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1694 n_eq, n_ineq);
1697 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1699 return (struct isl_basic_set *)
1700 isl_basic_map_cow((struct isl_basic_map *)bset);
1703 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1705 if (!bmap)
1706 return NULL;
1708 if (bmap->ref > 1) {
1709 bmap->ref--;
1710 bmap = isl_basic_map_dup(bmap);
1712 if (bmap) {
1713 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1714 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1716 return bmap;
1719 /* Clear all cached information in "map", either because it is about
1720 * to be modified or because it is being freed.
1721 * Always return the same pointer that is passed in.
1722 * This is needed for the use in isl_map_free.
1724 static __isl_give isl_map *clear_caches(__isl_take isl_map *map)
1726 isl_basic_map_free(map->cached_simple_hull[0]);
1727 isl_basic_map_free(map->cached_simple_hull[1]);
1728 map->cached_simple_hull[0] = NULL;
1729 map->cached_simple_hull[1] = NULL;
1730 return map;
1733 struct isl_set *isl_set_cow(struct isl_set *set)
1735 return isl_map_cow(set);
1738 /* Return an isl_map that is equal to "map" and that has only
1739 * a single reference.
1741 * If the original input already has only one reference, then
1742 * simply return it, but clear all cached information, since
1743 * it may be rendered invalid by the operations that will be
1744 * performed on the result.
1746 * Otherwise, create a duplicate (without any cached information).
1748 struct isl_map *isl_map_cow(struct isl_map *map)
1750 if (!map)
1751 return NULL;
1753 if (map->ref == 1)
1754 return clear_caches(map);
1755 map->ref--;
1756 return isl_map_dup(map);
1759 static void swap_vars(struct isl_blk blk, isl_int *a,
1760 unsigned a_len, unsigned b_len)
1762 isl_seq_cpy(blk.data, a+a_len, b_len);
1763 isl_seq_cpy(blk.data+b_len, a, a_len);
1764 isl_seq_cpy(a, blk.data, b_len+a_len);
1767 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1768 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1770 int i;
1771 struct isl_blk blk;
1773 if (!bmap)
1774 goto error;
1776 isl_assert(bmap->ctx,
1777 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1779 if (n1 == 0 || n2 == 0)
1780 return bmap;
1782 bmap = isl_basic_map_cow(bmap);
1783 if (!bmap)
1784 return NULL;
1786 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1787 if (isl_blk_is_error(blk))
1788 goto error;
1790 for (i = 0; i < bmap->n_eq; ++i)
1791 swap_vars(blk,
1792 bmap->eq[i] + pos, n1, n2);
1794 for (i = 0; i < bmap->n_ineq; ++i)
1795 swap_vars(blk,
1796 bmap->ineq[i] + pos, n1, n2);
1798 for (i = 0; i < bmap->n_div; ++i)
1799 swap_vars(blk,
1800 bmap->div[i]+1 + pos, n1, n2);
1802 isl_blk_free(bmap->ctx, blk);
1804 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1805 bmap = isl_basic_map_gauss(bmap, NULL);
1806 return isl_basic_map_finalize(bmap);
1807 error:
1808 isl_basic_map_free(bmap);
1809 return NULL;
1812 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1814 int i = 0;
1815 unsigned total;
1816 if (!bmap)
1817 goto error;
1818 total = isl_basic_map_total_dim(bmap);
1819 isl_basic_map_free_div(bmap, bmap->n_div);
1820 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1821 if (bmap->n_eq > 0)
1822 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1823 else {
1824 i = isl_basic_map_alloc_equality(bmap);
1825 if (i < 0)
1826 goto error;
1828 isl_int_set_si(bmap->eq[i][0], 1);
1829 isl_seq_clr(bmap->eq[i]+1, total);
1830 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1831 isl_vec_free(bmap->sample);
1832 bmap->sample = NULL;
1833 return isl_basic_map_finalize(bmap);
1834 error:
1835 isl_basic_map_free(bmap);
1836 return NULL;
1839 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1841 return (struct isl_basic_set *)
1842 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1845 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1846 * of "bmap").
1848 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1850 isl_int *t = bmap->div[a];
1851 bmap->div[a] = bmap->div[b];
1852 bmap->div[b] = t;
1855 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1856 * div definitions accordingly.
1858 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1860 int i;
1861 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1863 swap_div(bmap, a, b);
1865 for (i = 0; i < bmap->n_eq; ++i)
1866 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1868 for (i = 0; i < bmap->n_ineq; ++i)
1869 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1871 for (i = 0; i < bmap->n_div; ++i)
1872 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1873 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1876 /* Swap divs "a" and "b" in "bset" and adjust the constraints and
1877 * div definitions accordingly.
1879 void isl_basic_set_swap_div(__isl_keep isl_basic_set *bset, int a, int b)
1881 isl_basic_map_swap_div(bset, a, b);
1884 /* Eliminate the specified n dimensions starting at first from the
1885 * constraints, without removing the dimensions from the space.
1886 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1888 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1889 enum isl_dim_type type, unsigned first, unsigned n)
1891 int i;
1893 if (!map)
1894 return NULL;
1895 if (n == 0)
1896 return map;
1898 if (first + n > isl_map_dim(map, type) || first + n < first)
1899 isl_die(map->ctx, isl_error_invalid,
1900 "index out of bounds", goto error);
1902 map = isl_map_cow(map);
1903 if (!map)
1904 return NULL;
1906 for (i = 0; i < map->n; ++i) {
1907 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1908 if (!map->p[i])
1909 goto error;
1911 return map;
1912 error:
1913 isl_map_free(map);
1914 return NULL;
1917 /* Eliminate the specified n dimensions starting at first from the
1918 * constraints, without removing the dimensions from the space.
1919 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1921 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1922 enum isl_dim_type type, unsigned first, unsigned n)
1924 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1927 /* Eliminate the specified n dimensions starting at first from the
1928 * constraints, without removing the dimensions from the space.
1929 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1931 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1932 unsigned first, unsigned n)
1934 return isl_set_eliminate(set, isl_dim_set, first, n);
1937 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1938 __isl_take isl_basic_map *bmap)
1940 if (!bmap)
1941 return NULL;
1942 bmap = isl_basic_map_eliminate_vars(bmap,
1943 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1944 if (!bmap)
1945 return NULL;
1946 bmap->n_div = 0;
1947 return isl_basic_map_finalize(bmap);
1950 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1951 __isl_take isl_basic_set *bset)
1953 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1954 (struct isl_basic_map *)bset);
1957 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1959 int i;
1961 if (!map)
1962 return NULL;
1963 if (map->n == 0)
1964 return map;
1966 map = isl_map_cow(map);
1967 if (!map)
1968 return NULL;
1970 for (i = 0; i < map->n; ++i) {
1971 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1972 if (!map->p[i])
1973 goto error;
1975 return map;
1976 error:
1977 isl_map_free(map);
1978 return NULL;
1981 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1983 return isl_map_remove_divs(set);
1986 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1987 enum isl_dim_type type, unsigned first, unsigned n)
1989 if (!bmap)
1990 return NULL;
1991 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1992 goto error);
1993 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1994 return bmap;
1995 bmap = isl_basic_map_eliminate_vars(bmap,
1996 isl_basic_map_offset(bmap, type) - 1 + first, n);
1997 if (!bmap)
1998 return bmap;
1999 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
2000 return bmap;
2001 bmap = isl_basic_map_drop(bmap, type, first, n);
2002 return bmap;
2003 error:
2004 isl_basic_map_free(bmap);
2005 return NULL;
2008 /* Return true if the definition of the given div (recursively) involves
2009 * any of the given variables.
2011 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
2012 unsigned first, unsigned n)
2014 int i;
2015 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2017 if (isl_int_is_zero(bmap->div[div][0]))
2018 return 0;
2019 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
2020 return 1;
2022 for (i = bmap->n_div - 1; i >= 0; --i) {
2023 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2024 continue;
2025 if (div_involves_vars(bmap, i, first, n))
2026 return 1;
2029 return 0;
2032 /* Try and add a lower and/or upper bound on "div" to "bmap"
2033 * based on inequality "i".
2034 * "total" is the total number of variables (excluding the divs).
2035 * "v" is a temporary object that can be used during the calculations.
2036 * If "lb" is set, then a lower bound should be constructed.
2037 * If "ub" is set, then an upper bound should be constructed.
2039 * The calling function has already checked that the inequality does not
2040 * reference "div", but we still need to check that the inequality is
2041 * of the right form. We'll consider the case where we want to construct
2042 * a lower bound. The construction of upper bounds is similar.
2044 * Let "div" be of the form
2046 * q = floor((a + f(x))/d)
2048 * We essentially check if constraint "i" is of the form
2050 * b + f(x) >= 0
2052 * so that we can use it to derive a lower bound on "div".
2053 * However, we allow a slightly more general form
2055 * b + g(x) >= 0
2057 * with the condition that the coefficients of g(x) - f(x) are all
2058 * divisible by d.
2059 * Rewriting this constraint as
2061 * 0 >= -b - g(x)
2063 * adding a + f(x) to both sides and dividing by d, we obtain
2065 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
2067 * Taking the floor on both sides, we obtain
2069 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2071 * or
2073 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2075 * In the case of an upper bound, we construct the constraint
2077 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2080 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2081 __isl_take isl_basic_map *bmap, int div, int i,
2082 unsigned total, isl_int v, int lb, int ub)
2084 int j;
2086 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2087 if (lb) {
2088 isl_int_sub(v, bmap->ineq[i][1 + j],
2089 bmap->div[div][1 + 1 + j]);
2090 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2092 if (ub) {
2093 isl_int_add(v, bmap->ineq[i][1 + j],
2094 bmap->div[div][1 + 1 + j]);
2095 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2098 if (!lb && !ub)
2099 return bmap;
2101 bmap = isl_basic_map_cow(bmap);
2102 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2103 if (lb) {
2104 int k = isl_basic_map_alloc_inequality(bmap);
2105 if (k < 0)
2106 goto error;
2107 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2108 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2109 bmap->div[div][1 + j]);
2110 isl_int_cdiv_q(bmap->ineq[k][j],
2111 bmap->ineq[k][j], bmap->div[div][0]);
2113 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2115 if (ub) {
2116 int k = isl_basic_map_alloc_inequality(bmap);
2117 if (k < 0)
2118 goto error;
2119 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2120 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2121 bmap->div[div][1 + j]);
2122 isl_int_fdiv_q(bmap->ineq[k][j],
2123 bmap->ineq[k][j], bmap->div[div][0]);
2125 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2128 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2129 return bmap;
2130 error:
2131 isl_basic_map_free(bmap);
2132 return NULL;
2135 /* This function is called right before "div" is eliminated from "bmap"
2136 * using Fourier-Motzkin.
2137 * Look through the constraints of "bmap" for constraints on the argument
2138 * of the integer division and use them to construct constraints on the
2139 * integer division itself. These constraints can then be combined
2140 * during the Fourier-Motzkin elimination.
2141 * Note that it is only useful to introduce lower bounds on "div"
2142 * if "bmap" already contains upper bounds on "div" as the newly
2143 * introduce lower bounds can then be combined with the pre-existing
2144 * upper bounds. Similarly for upper bounds.
2145 * We therefore first check if "bmap" contains any lower and/or upper bounds
2146 * on "div".
2148 * It is interesting to note that the introduction of these constraints
2149 * can indeed lead to more accurate results, even when compared to
2150 * deriving constraints on the argument of "div" from constraints on "div".
2151 * Consider, for example, the set
2153 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2155 * The second constraint can be rewritten as
2157 * 2 * [(-i-2j+3)/4] + k >= 0
2159 * from which we can derive
2161 * -i - 2j + 3 >= -2k
2163 * or
2165 * i + 2j <= 3 + 2k
2167 * Combined with the first constraint, we obtain
2169 * -3 <= 3 + 2k or k >= -3
2171 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2172 * the first constraint, we obtain
2174 * [(i + 2j)/4] >= [-3/4] = -1
2176 * Combining this constraint with the second constraint, we obtain
2178 * k >= -2
2180 static __isl_give isl_basic_map *insert_bounds_on_div(
2181 __isl_take isl_basic_map *bmap, int div)
2183 int i;
2184 int check_lb, check_ub;
2185 isl_int v;
2186 unsigned total;
2188 if (!bmap)
2189 return NULL;
2191 if (isl_int_is_zero(bmap->div[div][0]))
2192 return bmap;
2194 total = isl_space_dim(bmap->dim, isl_dim_all);
2196 check_lb = 0;
2197 check_ub = 0;
2198 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2199 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2200 if (s > 0)
2201 check_ub = 1;
2202 if (s < 0)
2203 check_lb = 1;
2206 if (!check_lb && !check_ub)
2207 return bmap;
2209 isl_int_init(v);
2211 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2212 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2213 continue;
2215 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2216 check_lb, check_ub);
2219 isl_int_clear(v);
2221 return bmap;
2224 /* Remove all divs (recursively) involving any of the given dimensions
2225 * in their definitions.
2227 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2228 __isl_take isl_basic_map *bmap,
2229 enum isl_dim_type type, unsigned first, unsigned n)
2231 int i;
2233 if (!bmap)
2234 return NULL;
2235 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2236 goto error);
2237 first += isl_basic_map_offset(bmap, type);
2239 for (i = bmap->n_div - 1; i >= 0; --i) {
2240 if (!div_involves_vars(bmap, i, first, n))
2241 continue;
2242 bmap = insert_bounds_on_div(bmap, i);
2243 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2244 if (!bmap)
2245 return NULL;
2246 i = bmap->n_div;
2249 return bmap;
2250 error:
2251 isl_basic_map_free(bmap);
2252 return NULL;
2255 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2256 __isl_take isl_basic_set *bset,
2257 enum isl_dim_type type, unsigned first, unsigned n)
2259 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2262 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2263 enum isl_dim_type type, unsigned first, unsigned n)
2265 int i;
2267 if (!map)
2268 return NULL;
2269 if (map->n == 0)
2270 return map;
2272 map = isl_map_cow(map);
2273 if (!map)
2274 return NULL;
2276 for (i = 0; i < map->n; ++i) {
2277 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2278 type, first, n);
2279 if (!map->p[i])
2280 goto error;
2282 return map;
2283 error:
2284 isl_map_free(map);
2285 return NULL;
2288 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2289 enum isl_dim_type type, unsigned first, unsigned n)
2291 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2292 type, first, n);
2295 /* Does the desciption of "bmap" depend on the specified dimensions?
2296 * We also check whether the dimensions appear in any of the div definitions.
2297 * In principle there is no need for this check. If the dimensions appear
2298 * in a div definition, they also appear in the defining constraints of that
2299 * div.
2301 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2302 enum isl_dim_type type, unsigned first, unsigned n)
2304 int i;
2306 if (!bmap)
2307 return isl_bool_error;
2309 if (first + n > isl_basic_map_dim(bmap, type))
2310 isl_die(bmap->ctx, isl_error_invalid,
2311 "index out of bounds", return isl_bool_error);
2313 first += isl_basic_map_offset(bmap, type);
2314 for (i = 0; i < bmap->n_eq; ++i)
2315 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2316 return isl_bool_true;
2317 for (i = 0; i < bmap->n_ineq; ++i)
2318 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2319 return isl_bool_true;
2320 for (i = 0; i < bmap->n_div; ++i) {
2321 if (isl_int_is_zero(bmap->div[i][0]))
2322 continue;
2323 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2324 return isl_bool_true;
2327 return isl_bool_false;
2330 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2331 enum isl_dim_type type, unsigned first, unsigned n)
2333 int i;
2335 if (!map)
2336 return isl_bool_error;
2338 if (first + n > isl_map_dim(map, type))
2339 isl_die(map->ctx, isl_error_invalid,
2340 "index out of bounds", return isl_bool_error);
2342 for (i = 0; i < map->n; ++i) {
2343 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2344 type, first, n);
2345 if (involves < 0 || involves)
2346 return involves;
2349 return isl_bool_false;
2352 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2353 enum isl_dim_type type, unsigned first, unsigned n)
2355 return isl_basic_map_involves_dims(bset, type, first, n);
2358 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2359 enum isl_dim_type type, unsigned first, unsigned n)
2361 return isl_map_involves_dims(set, type, first, n);
2364 /* Does local variable "div" of "bmap" have a complete explicit representation?
2365 * Having a complete explicit representation requires not only
2366 * an explicit representation, but also that all local variables
2367 * that appear in this explicit representation in turn have
2368 * a complete explicit representation.
2370 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
2372 int i;
2373 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2374 isl_bool marked;
2376 marked = isl_basic_map_div_is_marked_unknown(bmap, div);
2377 if (marked < 0 || marked)
2378 return isl_bool_not(marked);
2380 for (i = bmap->n_div - 1; i >= 0; --i) {
2381 isl_bool known;
2383 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2384 continue;
2385 known = isl_basic_map_div_is_known(bmap, i);
2386 if (known < 0 || !known)
2387 return known;
2390 return isl_bool_true;
2393 /* Does local variable "div" of "bset" have a complete explicit representation?
2395 isl_bool isl_basic_set_div_is_known(__isl_keep isl_basic_set *bset, int div)
2397 return isl_basic_map_div_is_known(bset, div);
2400 /* Remove all divs that are unknown or defined in terms of unknown divs.
2402 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2403 __isl_take isl_basic_map *bmap)
2405 int i;
2407 if (!bmap)
2408 return NULL;
2410 for (i = bmap->n_div - 1; i >= 0; --i) {
2411 if (isl_basic_map_div_is_known(bmap, i))
2412 continue;
2413 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2414 if (!bmap)
2415 return NULL;
2416 i = bmap->n_div;
2419 return bmap;
2422 /* Remove all divs that are unknown or defined in terms of unknown divs.
2424 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2425 __isl_take isl_basic_set *bset)
2427 return isl_basic_map_remove_unknown_divs(bset);
2430 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2432 int i;
2434 if (!map)
2435 return NULL;
2436 if (map->n == 0)
2437 return map;
2439 map = isl_map_cow(map);
2440 if (!map)
2441 return NULL;
2443 for (i = 0; i < map->n; ++i) {
2444 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2445 if (!map->p[i])
2446 goto error;
2448 return map;
2449 error:
2450 isl_map_free(map);
2451 return NULL;
2454 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2456 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2459 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2460 __isl_take isl_basic_set *bset,
2461 enum isl_dim_type type, unsigned first, unsigned n)
2463 return (isl_basic_set *)
2464 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2467 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2468 enum isl_dim_type type, unsigned first, unsigned n)
2470 int i;
2472 if (n == 0)
2473 return map;
2475 map = isl_map_cow(map);
2476 if (!map)
2477 return NULL;
2478 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2480 for (i = 0; i < map->n; ++i) {
2481 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2482 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2483 if (!map->p[i])
2484 goto error;
2486 map = isl_map_drop(map, type, first, n);
2487 return map;
2488 error:
2489 isl_map_free(map);
2490 return NULL;
2493 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2494 enum isl_dim_type type, unsigned first, unsigned n)
2496 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2499 /* Project out n inputs starting at first using Fourier-Motzkin */
2500 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2501 unsigned first, unsigned n)
2503 return isl_map_remove_dims(map, isl_dim_in, first, n);
2506 static void dump_term(struct isl_basic_map *bmap,
2507 isl_int c, int pos, FILE *out)
2509 const char *name;
2510 unsigned in = isl_basic_map_n_in(bmap);
2511 unsigned dim = in + isl_basic_map_n_out(bmap);
2512 unsigned nparam = isl_basic_map_n_param(bmap);
2513 if (!pos)
2514 isl_int_print(out, c, 0);
2515 else {
2516 if (!isl_int_is_one(c))
2517 isl_int_print(out, c, 0);
2518 if (pos < 1 + nparam) {
2519 name = isl_space_get_dim_name(bmap->dim,
2520 isl_dim_param, pos - 1);
2521 if (name)
2522 fprintf(out, "%s", name);
2523 else
2524 fprintf(out, "p%d", pos - 1);
2525 } else if (pos < 1 + nparam + in)
2526 fprintf(out, "i%d", pos - 1 - nparam);
2527 else if (pos < 1 + nparam + dim)
2528 fprintf(out, "o%d", pos - 1 - nparam - in);
2529 else
2530 fprintf(out, "e%d", pos - 1 - nparam - dim);
2534 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2535 int sign, FILE *out)
2537 int i;
2538 int first;
2539 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2540 isl_int v;
2542 isl_int_init(v);
2543 for (i = 0, first = 1; i < len; ++i) {
2544 if (isl_int_sgn(c[i]) * sign <= 0)
2545 continue;
2546 if (!first)
2547 fprintf(out, " + ");
2548 first = 0;
2549 isl_int_abs(v, c[i]);
2550 dump_term(bmap, v, i, out);
2552 isl_int_clear(v);
2553 if (first)
2554 fprintf(out, "0");
2557 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2558 const char *op, FILE *out, int indent)
2560 int i;
2562 fprintf(out, "%*s", indent, "");
2564 dump_constraint_sign(bmap, c, 1, out);
2565 fprintf(out, " %s ", op);
2566 dump_constraint_sign(bmap, c, -1, out);
2568 fprintf(out, "\n");
2570 for (i = bmap->n_div; i < bmap->extra; ++i) {
2571 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2572 continue;
2573 fprintf(out, "%*s", indent, "");
2574 fprintf(out, "ERROR: unused div coefficient not zero\n");
2575 abort();
2579 static void dump_constraints(struct isl_basic_map *bmap,
2580 isl_int **c, unsigned n,
2581 const char *op, FILE *out, int indent)
2583 int i;
2585 for (i = 0; i < n; ++i)
2586 dump_constraint(bmap, c[i], op, out, indent);
2589 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2591 int j;
2592 int first = 1;
2593 unsigned total = isl_basic_map_total_dim(bmap);
2595 for (j = 0; j < 1 + total; ++j) {
2596 if (isl_int_is_zero(exp[j]))
2597 continue;
2598 if (!first && isl_int_is_pos(exp[j]))
2599 fprintf(out, "+");
2600 dump_term(bmap, exp[j], j, out);
2601 first = 0;
2605 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2607 int i;
2609 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2610 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2612 for (i = 0; i < bmap->n_div; ++i) {
2613 fprintf(out, "%*s", indent, "");
2614 fprintf(out, "e%d = [(", i);
2615 dump_affine(bmap, bmap->div[i]+1, out);
2616 fprintf(out, ")/");
2617 isl_int_print(out, bmap->div[i][0], 0);
2618 fprintf(out, "]\n");
2622 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2623 FILE *out, int indent)
2625 if (!bset) {
2626 fprintf(out, "null basic set\n");
2627 return;
2630 fprintf(out, "%*s", indent, "");
2631 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2632 bset->ref, bset->dim->nparam, bset->dim->n_out,
2633 bset->extra, bset->flags);
2634 dump((struct isl_basic_map *)bset, out, indent);
2637 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2638 FILE *out, int indent)
2640 if (!bmap) {
2641 fprintf(out, "null basic map\n");
2642 return;
2645 fprintf(out, "%*s", indent, "");
2646 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2647 "flags: %x, n_name: %d\n",
2648 bmap->ref,
2649 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2650 bmap->extra, bmap->flags, bmap->dim->n_id);
2651 dump(bmap, out, indent);
2654 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2656 unsigned total;
2657 if (!bmap)
2658 return -1;
2659 total = isl_basic_map_total_dim(bmap);
2660 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2661 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2662 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2663 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2664 return 0;
2667 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *space, int n,
2668 unsigned flags)
2670 if (!space)
2671 return NULL;
2672 if (isl_space_dim(space, isl_dim_in) != 0)
2673 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2674 "set cannot have input dimensions", goto error);
2675 return isl_map_alloc_space(space, n, flags);
2676 error:
2677 isl_space_free(space);
2678 return NULL;
2681 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2682 unsigned nparam, unsigned dim, int n, unsigned flags)
2684 struct isl_set *set;
2685 isl_space *dims;
2687 dims = isl_space_alloc(ctx, nparam, 0, dim);
2688 if (!dims)
2689 return NULL;
2691 set = isl_set_alloc_space(dims, n, flags);
2692 return set;
2695 /* Make sure "map" has room for at least "n" more basic maps.
2697 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2699 int i;
2700 struct isl_map *grown = NULL;
2702 if (!map)
2703 return NULL;
2704 isl_assert(map->ctx, n >= 0, goto error);
2705 if (map->n + n <= map->size)
2706 return map;
2707 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2708 if (!grown)
2709 goto error;
2710 for (i = 0; i < map->n; ++i) {
2711 grown->p[i] = isl_basic_map_copy(map->p[i]);
2712 if (!grown->p[i])
2713 goto error;
2714 grown->n++;
2716 isl_map_free(map);
2717 return grown;
2718 error:
2719 isl_map_free(grown);
2720 isl_map_free(map);
2721 return NULL;
2724 /* Make sure "set" has room for at least "n" more basic sets.
2726 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2728 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2731 struct isl_set *isl_set_dup(struct isl_set *set)
2733 int i;
2734 struct isl_set *dup;
2736 if (!set)
2737 return NULL;
2739 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2740 if (!dup)
2741 return NULL;
2742 for (i = 0; i < set->n; ++i)
2743 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2744 return dup;
2747 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2749 return isl_map_from_basic_map(bset);
2752 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2754 struct isl_map *map;
2756 if (!bmap)
2757 return NULL;
2759 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2760 return isl_map_add_basic_map(map, bmap);
2763 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2764 __isl_take isl_basic_set *bset)
2766 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2767 (struct isl_basic_map *)bset);
2770 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2772 return isl_map_free(set);
2775 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2777 int i;
2779 if (!set) {
2780 fprintf(out, "null set\n");
2781 return;
2784 fprintf(out, "%*s", indent, "");
2785 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2786 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2787 set->flags);
2788 for (i = 0; i < set->n; ++i) {
2789 fprintf(out, "%*s", indent, "");
2790 fprintf(out, "basic set %d:\n", i);
2791 isl_basic_set_print_internal(set->p[i], out, indent+4);
2795 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2797 int i;
2799 if (!map) {
2800 fprintf(out, "null map\n");
2801 return;
2804 fprintf(out, "%*s", indent, "");
2805 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2806 "flags: %x, n_name: %d\n",
2807 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2808 map->dim->n_out, map->flags, map->dim->n_id);
2809 for (i = 0; i < map->n; ++i) {
2810 fprintf(out, "%*s", indent, "");
2811 fprintf(out, "basic map %d:\n", i);
2812 isl_basic_map_print_internal(map->p[i], out, indent+4);
2816 struct isl_basic_map *isl_basic_map_intersect_domain(
2817 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2819 struct isl_basic_map *bmap_domain;
2821 if (!bmap || !bset)
2822 goto error;
2824 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2825 bset->dim, isl_dim_param), goto error);
2827 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2828 isl_assert(bset->ctx,
2829 isl_basic_map_compatible_domain(bmap, bset), goto error);
2831 bmap = isl_basic_map_cow(bmap);
2832 if (!bmap)
2833 goto error;
2834 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2835 bset->n_div, bset->n_eq, bset->n_ineq);
2836 bmap_domain = isl_basic_map_from_domain(bset);
2837 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2839 bmap = isl_basic_map_simplify(bmap);
2840 return isl_basic_map_finalize(bmap);
2841 error:
2842 isl_basic_map_free(bmap);
2843 isl_basic_set_free(bset);
2844 return NULL;
2847 struct isl_basic_map *isl_basic_map_intersect_range(
2848 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2850 struct isl_basic_map *bmap_range;
2852 if (!bmap || !bset)
2853 goto error;
2855 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2856 bset->dim, isl_dim_param), goto error);
2858 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2859 isl_assert(bset->ctx,
2860 isl_basic_map_compatible_range(bmap, bset), goto error);
2862 if (isl_basic_set_plain_is_universe(bset)) {
2863 isl_basic_set_free(bset);
2864 return bmap;
2867 bmap = isl_basic_map_cow(bmap);
2868 if (!bmap)
2869 goto error;
2870 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2871 bset->n_div, bset->n_eq, bset->n_ineq);
2872 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2873 bmap = add_constraints(bmap, bmap_range, 0, 0);
2875 bmap = isl_basic_map_simplify(bmap);
2876 return isl_basic_map_finalize(bmap);
2877 error:
2878 isl_basic_map_free(bmap);
2879 isl_basic_set_free(bset);
2880 return NULL;
2883 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2884 __isl_keep isl_vec *vec)
2886 int i;
2887 unsigned total;
2888 isl_int s;
2890 if (!bmap || !vec)
2891 return isl_bool_error;
2893 total = 1 + isl_basic_map_total_dim(bmap);
2894 if (total != vec->size)
2895 return isl_bool_error;
2897 isl_int_init(s);
2899 for (i = 0; i < bmap->n_eq; ++i) {
2900 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2901 if (!isl_int_is_zero(s)) {
2902 isl_int_clear(s);
2903 return isl_bool_false;
2907 for (i = 0; i < bmap->n_ineq; ++i) {
2908 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2909 if (isl_int_is_neg(s)) {
2910 isl_int_clear(s);
2911 return isl_bool_false;
2915 isl_int_clear(s);
2917 return isl_bool_true;
2920 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2921 __isl_keep isl_vec *vec)
2923 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2926 struct isl_basic_map *isl_basic_map_intersect(
2927 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2929 struct isl_vec *sample = NULL;
2931 if (!bmap1 || !bmap2)
2932 goto error;
2934 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2935 bmap2->dim, isl_dim_param), goto error);
2936 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2937 isl_space_dim(bmap1->dim, isl_dim_param) &&
2938 isl_space_dim(bmap2->dim, isl_dim_all) !=
2939 isl_space_dim(bmap2->dim, isl_dim_param))
2940 return isl_basic_map_intersect(bmap2, bmap1);
2942 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2943 isl_space_dim(bmap2->dim, isl_dim_param))
2944 isl_assert(bmap1->ctx,
2945 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2947 if (isl_basic_map_plain_is_empty(bmap1)) {
2948 isl_basic_map_free(bmap2);
2949 return bmap1;
2951 if (isl_basic_map_plain_is_empty(bmap2)) {
2952 isl_basic_map_free(bmap1);
2953 return bmap2;
2956 if (bmap1->sample &&
2957 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2958 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2959 sample = isl_vec_copy(bmap1->sample);
2960 else if (bmap2->sample &&
2961 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2962 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2963 sample = isl_vec_copy(bmap2->sample);
2965 bmap1 = isl_basic_map_cow(bmap1);
2966 if (!bmap1)
2967 goto error;
2968 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2969 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2970 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2972 if (!bmap1)
2973 isl_vec_free(sample);
2974 else if (sample) {
2975 isl_vec_free(bmap1->sample);
2976 bmap1->sample = sample;
2979 bmap1 = isl_basic_map_simplify(bmap1);
2980 return isl_basic_map_finalize(bmap1);
2981 error:
2982 if (sample)
2983 isl_vec_free(sample);
2984 isl_basic_map_free(bmap1);
2985 isl_basic_map_free(bmap2);
2986 return NULL;
2989 struct isl_basic_set *isl_basic_set_intersect(
2990 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2992 return (struct isl_basic_set *)
2993 isl_basic_map_intersect(
2994 (struct isl_basic_map *)bset1,
2995 (struct isl_basic_map *)bset2);
2998 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2999 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
3001 return isl_basic_set_intersect(bset1, bset2);
3004 /* Special case of isl_map_intersect, where both map1 and map2
3005 * are convex, without any divs and such that either map1 or map2
3006 * contains a single constraint. This constraint is then simply
3007 * added to the other map.
3009 static __isl_give isl_map *map_intersect_add_constraint(
3010 __isl_take isl_map *map1, __isl_take isl_map *map2)
3012 isl_assert(map1->ctx, map1->n == 1, goto error);
3013 isl_assert(map2->ctx, map1->n == 1, goto error);
3014 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
3015 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
3017 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
3018 return isl_map_intersect(map2, map1);
3020 isl_assert(map2->ctx,
3021 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
3023 map1 = isl_map_cow(map1);
3024 if (!map1)
3025 goto error;
3026 if (isl_map_plain_is_empty(map1)) {
3027 isl_map_free(map2);
3028 return map1;
3030 map1->p[0] = isl_basic_map_cow(map1->p[0]);
3031 if (map2->p[0]->n_eq == 1)
3032 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
3033 else
3034 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
3035 map2->p[0]->ineq[0]);
3037 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
3038 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
3039 if (!map1->p[0])
3040 goto error;
3042 if (isl_basic_map_plain_is_empty(map1->p[0])) {
3043 isl_basic_map_free(map1->p[0]);
3044 map1->n = 0;
3047 isl_map_free(map2);
3049 return map1;
3050 error:
3051 isl_map_free(map1);
3052 isl_map_free(map2);
3053 return NULL;
3056 /* map2 may be either a parameter domain or a map living in the same
3057 * space as map1.
3059 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3060 __isl_take isl_map *map2)
3062 unsigned flags = 0;
3063 isl_map *result;
3064 int i, j;
3066 if (!map1 || !map2)
3067 goto error;
3069 if ((isl_map_plain_is_empty(map1) ||
3070 isl_map_plain_is_universe(map2)) &&
3071 isl_space_is_equal(map1->dim, map2->dim)) {
3072 isl_map_free(map2);
3073 return map1;
3075 if ((isl_map_plain_is_empty(map2) ||
3076 isl_map_plain_is_universe(map1)) &&
3077 isl_space_is_equal(map1->dim, map2->dim)) {
3078 isl_map_free(map1);
3079 return map2;
3082 if (map1->n == 1 && map2->n == 1 &&
3083 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3084 isl_space_is_equal(map1->dim, map2->dim) &&
3085 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3086 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3087 return map_intersect_add_constraint(map1, map2);
3089 if (isl_space_dim(map2->dim, isl_dim_all) !=
3090 isl_space_dim(map2->dim, isl_dim_param))
3091 isl_assert(map1->ctx,
3092 isl_space_is_equal(map1->dim, map2->dim), goto error);
3094 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3095 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3096 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3098 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3099 map1->n * map2->n, flags);
3100 if (!result)
3101 goto error;
3102 for (i = 0; i < map1->n; ++i)
3103 for (j = 0; j < map2->n; ++j) {
3104 struct isl_basic_map *part;
3105 part = isl_basic_map_intersect(
3106 isl_basic_map_copy(map1->p[i]),
3107 isl_basic_map_copy(map2->p[j]));
3108 if (isl_basic_map_is_empty(part) < 0)
3109 part = isl_basic_map_free(part);
3110 result = isl_map_add_basic_map(result, part);
3111 if (!result)
3112 goto error;
3114 isl_map_free(map1);
3115 isl_map_free(map2);
3116 return result;
3117 error:
3118 isl_map_free(map1);
3119 isl_map_free(map2);
3120 return NULL;
3123 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3124 __isl_take isl_map *map2)
3126 if (!map1 || !map2)
3127 goto error;
3128 if (!isl_space_is_equal(map1->dim, map2->dim))
3129 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3130 "spaces don't match", goto error);
3131 return map_intersect_internal(map1, map2);
3132 error:
3133 isl_map_free(map1);
3134 isl_map_free(map2);
3135 return NULL;
3138 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3139 __isl_take isl_map *map2)
3141 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3144 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3146 return (struct isl_set *)
3147 isl_map_intersect((struct isl_map *)set1,
3148 (struct isl_map *)set2);
3151 /* map_intersect_internal accepts intersections
3152 * with parameter domains, so we can just call that function.
3154 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3155 __isl_take isl_set *params)
3157 return map_intersect_internal(map, params);
3160 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3161 __isl_take isl_map *map2)
3163 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3166 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3167 __isl_take isl_set *params)
3169 return isl_map_intersect_params(set, params);
3172 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3174 isl_space *space;
3175 unsigned pos, n1, n2;
3177 if (!bmap)
3178 return NULL;
3179 bmap = isl_basic_map_cow(bmap);
3180 if (!bmap)
3181 return NULL;
3182 space = isl_space_reverse(isl_space_copy(bmap->dim));
3183 pos = isl_basic_map_offset(bmap, isl_dim_in);
3184 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3185 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3186 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3187 return isl_basic_map_reset_space(bmap, space);
3190 static __isl_give isl_basic_map *basic_map_space_reset(
3191 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3193 isl_space *space;
3195 if (!bmap)
3196 return NULL;
3197 if (!isl_space_is_named_or_nested(bmap->dim, type))
3198 return bmap;
3200 space = isl_basic_map_get_space(bmap);
3201 space = isl_space_reset(space, type);
3202 bmap = isl_basic_map_reset_space(bmap, space);
3203 return bmap;
3206 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3207 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3208 unsigned pos, unsigned n)
3210 isl_space *res_dim;
3211 struct isl_basic_map *res;
3212 struct isl_dim_map *dim_map;
3213 unsigned total, off;
3214 enum isl_dim_type t;
3216 if (n == 0)
3217 return basic_map_space_reset(bmap, type);
3219 if (!bmap)
3220 return NULL;
3222 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3224 total = isl_basic_map_total_dim(bmap) + n;
3225 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3226 off = 0;
3227 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3228 if (t != type) {
3229 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3230 } else {
3231 unsigned size = isl_basic_map_dim(bmap, t);
3232 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3233 0, pos, off);
3234 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3235 pos, size - pos, off + pos + n);
3237 off += isl_space_dim(res_dim, t);
3239 isl_dim_map_div(dim_map, bmap, off);
3241 res = isl_basic_map_alloc_space(res_dim,
3242 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3243 if (isl_basic_map_is_rational(bmap))
3244 res = isl_basic_map_set_rational(res);
3245 if (isl_basic_map_plain_is_empty(bmap)) {
3246 isl_basic_map_free(bmap);
3247 free(dim_map);
3248 return isl_basic_map_set_to_empty(res);
3250 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3251 return isl_basic_map_finalize(res);
3254 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3255 __isl_take isl_basic_set *bset,
3256 enum isl_dim_type type, unsigned pos, unsigned n)
3258 return isl_basic_map_insert_dims(bset, type, pos, n);
3261 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3262 enum isl_dim_type type, unsigned n)
3264 if (!bmap)
3265 return NULL;
3266 return isl_basic_map_insert_dims(bmap, type,
3267 isl_basic_map_dim(bmap, type), n);
3270 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3271 enum isl_dim_type type, unsigned n)
3273 if (!bset)
3274 return NULL;
3275 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3276 return isl_basic_map_add_dims(bset, type, n);
3277 error:
3278 isl_basic_set_free(bset);
3279 return NULL;
3282 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3283 enum isl_dim_type type)
3285 isl_space *space;
3287 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3288 return map;
3290 space = isl_map_get_space(map);
3291 space = isl_space_reset(space, type);
3292 map = isl_map_reset_space(map, space);
3293 return map;
3296 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3297 enum isl_dim_type type, unsigned pos, unsigned n)
3299 int i;
3301 if (n == 0)
3302 return map_space_reset(map, type);
3304 map = isl_map_cow(map);
3305 if (!map)
3306 return NULL;
3308 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3309 if (!map->dim)
3310 goto error;
3312 for (i = 0; i < map->n; ++i) {
3313 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3314 if (!map->p[i])
3315 goto error;
3318 return map;
3319 error:
3320 isl_map_free(map);
3321 return NULL;
3324 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3325 enum isl_dim_type type, unsigned pos, unsigned n)
3327 return isl_map_insert_dims(set, type, pos, n);
3330 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3331 enum isl_dim_type type, unsigned n)
3333 if (!map)
3334 return NULL;
3335 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3338 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3339 enum isl_dim_type type, unsigned n)
3341 if (!set)
3342 return NULL;
3343 isl_assert(set->ctx, type != isl_dim_in, goto error);
3344 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3345 error:
3346 isl_set_free(set);
3347 return NULL;
3350 __isl_give isl_basic_map *isl_basic_map_move_dims(
3351 __isl_take isl_basic_map *bmap,
3352 enum isl_dim_type dst_type, unsigned dst_pos,
3353 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3355 struct isl_dim_map *dim_map;
3356 struct isl_basic_map *res;
3357 enum isl_dim_type t;
3358 unsigned total, off;
3360 if (!bmap)
3361 return NULL;
3362 if (n == 0)
3363 return bmap;
3365 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3366 goto error);
3368 if (dst_type == src_type && dst_pos == src_pos)
3369 return bmap;
3371 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3373 if (pos(bmap->dim, dst_type) + dst_pos ==
3374 pos(bmap->dim, src_type) + src_pos +
3375 ((src_type < dst_type) ? n : 0)) {
3376 bmap = isl_basic_map_cow(bmap);
3377 if (!bmap)
3378 return NULL;
3380 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3381 src_type, src_pos, n);
3382 if (!bmap->dim)
3383 goto error;
3385 bmap = isl_basic_map_finalize(bmap);
3387 return bmap;
3390 total = isl_basic_map_total_dim(bmap);
3391 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3393 off = 0;
3394 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3395 unsigned size = isl_space_dim(bmap->dim, t);
3396 if (t == dst_type) {
3397 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3398 0, dst_pos, off);
3399 off += dst_pos;
3400 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3401 src_pos, n, off);
3402 off += n;
3403 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3404 dst_pos, size - dst_pos, off);
3405 off += size - dst_pos;
3406 } else if (t == src_type) {
3407 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3408 0, src_pos, off);
3409 off += src_pos;
3410 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3411 src_pos + n, size - src_pos - n, off);
3412 off += size - src_pos - n;
3413 } else {
3414 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3415 off += size;
3418 isl_dim_map_div(dim_map, bmap, off);
3420 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3421 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3422 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3423 if (!bmap)
3424 goto error;
3426 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3427 src_type, src_pos, n);
3428 if (!bmap->dim)
3429 goto error;
3431 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3432 bmap = isl_basic_map_gauss(bmap, NULL);
3433 bmap = isl_basic_map_finalize(bmap);
3435 return bmap;
3436 error:
3437 isl_basic_map_free(bmap);
3438 return NULL;
3441 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3442 enum isl_dim_type dst_type, unsigned dst_pos,
3443 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3445 return (isl_basic_set *)isl_basic_map_move_dims(
3446 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3449 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3450 enum isl_dim_type dst_type, unsigned dst_pos,
3451 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3453 if (!set)
3454 return NULL;
3455 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3456 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3457 src_type, src_pos, n);
3458 error:
3459 isl_set_free(set);
3460 return NULL;
3463 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3464 enum isl_dim_type dst_type, unsigned dst_pos,
3465 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3467 int i;
3469 if (!map)
3470 return NULL;
3471 if (n == 0)
3472 return map;
3474 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3475 goto error);
3477 if (dst_type == src_type && dst_pos == src_pos)
3478 return map;
3480 isl_assert(map->ctx, dst_type != src_type, goto error);
3482 map = isl_map_cow(map);
3483 if (!map)
3484 return NULL;
3486 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3487 if (!map->dim)
3488 goto error;
3490 for (i = 0; i < map->n; ++i) {
3491 map->p[i] = isl_basic_map_move_dims(map->p[i],
3492 dst_type, dst_pos,
3493 src_type, src_pos, n);
3494 if (!map->p[i])
3495 goto error;
3498 return map;
3499 error:
3500 isl_map_free(map);
3501 return NULL;
3504 /* Move the specified dimensions to the last columns right before
3505 * the divs. Don't change the dimension specification of bmap.
3506 * That's the responsibility of the caller.
3508 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3509 enum isl_dim_type type, unsigned first, unsigned n)
3511 struct isl_dim_map *dim_map;
3512 struct isl_basic_map *res;
3513 enum isl_dim_type t;
3514 unsigned total, off;
3516 if (!bmap)
3517 return NULL;
3518 if (pos(bmap->dim, type) + first + n ==
3519 1 + isl_space_dim(bmap->dim, isl_dim_all))
3520 return bmap;
3522 total = isl_basic_map_total_dim(bmap);
3523 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3525 off = 0;
3526 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3527 unsigned size = isl_space_dim(bmap->dim, t);
3528 if (t == type) {
3529 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3530 0, first, off);
3531 off += first;
3532 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3533 first, n, total - bmap->n_div - n);
3534 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3535 first + n, size - (first + n), off);
3536 off += size - (first + n);
3537 } else {
3538 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3539 off += size;
3542 isl_dim_map_div(dim_map, bmap, off + n);
3544 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3545 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3546 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3547 return res;
3550 /* Insert "n" rows in the divs of "bmap".
3552 * The number of columns is not changed, which means that the last
3553 * dimensions of "bmap" are being reintepreted as the new divs.
3554 * The space of "bmap" is not adjusted, however, which means
3555 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3556 * from the space of "bmap" is the responsibility of the caller.
3558 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3559 int n)
3561 int i;
3562 size_t row_size;
3563 isl_int **new_div;
3564 isl_int *old;
3566 bmap = isl_basic_map_cow(bmap);
3567 if (!bmap)
3568 return NULL;
3570 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3571 old = bmap->block2.data;
3572 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3573 (bmap->extra + n) * (1 + row_size));
3574 if (!bmap->block2.data)
3575 return isl_basic_map_free(bmap);
3576 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3577 if (!new_div)
3578 return isl_basic_map_free(bmap);
3579 for (i = 0; i < n; ++i) {
3580 new_div[i] = bmap->block2.data +
3581 (bmap->extra + i) * (1 + row_size);
3582 isl_seq_clr(new_div[i], 1 + row_size);
3584 for (i = 0; i < bmap->extra; ++i)
3585 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3586 free(bmap->div);
3587 bmap->div = new_div;
3588 bmap->n_div += n;
3589 bmap->extra += n;
3591 return bmap;
3594 /* Drop constraints from "bmap" that only involve the variables
3595 * of "type" in the range [first, first + n] that are not related
3596 * to any of the variables outside that interval.
3597 * These constraints cannot influence the values for the variables
3598 * outside the interval, except in case they cause "bmap" to be empty.
3599 * Only drop the constraints if "bmap" is known to be non-empty.
3601 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3602 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3603 unsigned first, unsigned n)
3605 int i;
3606 int *groups;
3607 unsigned dim, n_div;
3608 isl_bool non_empty;
3610 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3611 if (non_empty < 0)
3612 return isl_basic_map_free(bmap);
3613 if (!non_empty)
3614 return bmap;
3616 dim = isl_basic_map_dim(bmap, isl_dim_all);
3617 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3618 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3619 if (!groups)
3620 return isl_basic_map_free(bmap);
3621 first += isl_basic_map_offset(bmap, type) - 1;
3622 for (i = 0; i < first; ++i)
3623 groups[i] = -1;
3624 for (i = first + n; i < dim - n_div; ++i)
3625 groups[i] = -1;
3627 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3629 return bmap;
3632 /* Turn the n dimensions of type type, starting at first
3633 * into existentially quantified variables.
3635 * If a subset of the projected out variables are unrelated
3636 * to any of the variables that remain, then the constraints
3637 * involving this subset are simply dropped first.
3639 __isl_give isl_basic_map *isl_basic_map_project_out(
3640 __isl_take isl_basic_map *bmap,
3641 enum isl_dim_type type, unsigned first, unsigned n)
3643 if (n == 0)
3644 return basic_map_space_reset(bmap, type);
3645 if (type == isl_dim_div)
3646 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3647 "cannot project out existentially quantified variables",
3648 return isl_basic_map_free(bmap));
3650 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3651 if (!bmap)
3652 return NULL;
3654 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3655 return isl_basic_map_remove_dims(bmap, type, first, n);
3657 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3658 goto error);
3660 bmap = move_last(bmap, type, first, n);
3661 bmap = isl_basic_map_cow(bmap);
3662 bmap = insert_div_rows(bmap, n);
3663 if (!bmap)
3664 return NULL;
3666 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3667 if (!bmap->dim)
3668 goto error;
3669 bmap = isl_basic_map_simplify(bmap);
3670 bmap = isl_basic_map_drop_redundant_divs(bmap);
3671 return isl_basic_map_finalize(bmap);
3672 error:
3673 isl_basic_map_free(bmap);
3674 return NULL;
3677 /* Turn the n dimensions of type type, starting at first
3678 * into existentially quantified variables.
3680 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3681 enum isl_dim_type type, unsigned first, unsigned n)
3683 return (isl_basic_set *)isl_basic_map_project_out(
3684 (isl_basic_map *)bset, type, first, n);
3687 /* Turn the n dimensions of type type, starting at first
3688 * into existentially quantified variables.
3690 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3691 enum isl_dim_type type, unsigned first, unsigned n)
3693 int i;
3695 if (!map)
3696 return NULL;
3698 if (n == 0)
3699 return map_space_reset(map, type);
3701 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3703 map = isl_map_cow(map);
3704 if (!map)
3705 return NULL;
3707 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3708 if (!map->dim)
3709 goto error;
3711 for (i = 0; i < map->n; ++i) {
3712 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3713 if (!map->p[i])
3714 goto error;
3717 return map;
3718 error:
3719 isl_map_free(map);
3720 return NULL;
3723 /* Turn the n dimensions of type type, starting at first
3724 * into existentially quantified variables.
3726 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3727 enum isl_dim_type type, unsigned first, unsigned n)
3729 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3732 /* Return a map that projects the elements in "set" onto their
3733 * "n" set dimensions starting at "first".
3734 * "type" should be equal to isl_dim_set.
3736 __isl_give isl_map *isl_set_project_onto_map(__isl_take isl_set *set,
3737 enum isl_dim_type type, unsigned first, unsigned n)
3739 int i;
3740 int dim;
3741 isl_map *map;
3743 if (!set)
3744 return NULL;
3745 if (type != isl_dim_set)
3746 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3747 "only set dimensions can be projected out", goto error);
3748 dim = isl_set_dim(set, isl_dim_set);
3749 if (first + n > dim || first + n < first)
3750 isl_die(isl_set_get_ctx(set), isl_error_invalid,
3751 "index out of bounds", goto error);
3753 map = isl_map_from_domain(set);
3754 map = isl_map_add_dims(map, isl_dim_out, n);
3755 for (i = 0; i < n; ++i)
3756 map = isl_map_equate(map, isl_dim_in, first + i,
3757 isl_dim_out, i);
3758 return map;
3759 error:
3760 isl_set_free(set);
3761 return NULL;
3764 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3766 int i, j;
3768 for (i = 0; i < n; ++i) {
3769 j = isl_basic_map_alloc_div(bmap);
3770 if (j < 0)
3771 goto error;
3772 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3774 return bmap;
3775 error:
3776 isl_basic_map_free(bmap);
3777 return NULL;
3780 struct isl_basic_map *isl_basic_map_apply_range(
3781 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3783 isl_space *dim_result = NULL;
3784 struct isl_basic_map *bmap;
3785 unsigned n_in, n_out, n, nparam, total, pos;
3786 struct isl_dim_map *dim_map1, *dim_map2;
3788 if (!bmap1 || !bmap2)
3789 goto error;
3790 if (!isl_space_match(bmap1->dim, isl_dim_param,
3791 bmap2->dim, isl_dim_param))
3792 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3793 "parameters don't match", goto error);
3794 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3795 bmap2->dim, isl_dim_in))
3796 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3797 "spaces don't match", goto error);
3799 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3800 isl_space_copy(bmap2->dim));
3802 n_in = isl_basic_map_n_in(bmap1);
3803 n_out = isl_basic_map_n_out(bmap2);
3804 n = isl_basic_map_n_out(bmap1);
3805 nparam = isl_basic_map_n_param(bmap1);
3807 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3808 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3809 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3810 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3811 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3812 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3813 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3814 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3815 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3816 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3817 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3819 bmap = isl_basic_map_alloc_space(dim_result,
3820 bmap1->n_div + bmap2->n_div + n,
3821 bmap1->n_eq + bmap2->n_eq,
3822 bmap1->n_ineq + bmap2->n_ineq);
3823 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3824 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3825 bmap = add_divs(bmap, n);
3826 bmap = isl_basic_map_simplify(bmap);
3827 bmap = isl_basic_map_drop_redundant_divs(bmap);
3828 return isl_basic_map_finalize(bmap);
3829 error:
3830 isl_basic_map_free(bmap1);
3831 isl_basic_map_free(bmap2);
3832 return NULL;
3835 struct isl_basic_set *isl_basic_set_apply(
3836 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3838 if (!bset || !bmap)
3839 goto error;
3841 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3842 goto error);
3844 return (struct isl_basic_set *)
3845 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3846 error:
3847 isl_basic_set_free(bset);
3848 isl_basic_map_free(bmap);
3849 return NULL;
3852 struct isl_basic_map *isl_basic_map_apply_domain(
3853 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3855 if (!bmap1 || !bmap2)
3856 goto error;
3858 isl_assert(bmap1->ctx,
3859 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3860 isl_assert(bmap1->ctx,
3861 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3862 goto error);
3864 bmap1 = isl_basic_map_reverse(bmap1);
3865 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3866 return isl_basic_map_reverse(bmap1);
3867 error:
3868 isl_basic_map_free(bmap1);
3869 isl_basic_map_free(bmap2);
3870 return NULL;
3873 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3874 * A \cap B -> f(A) + f(B)
3876 struct isl_basic_map *isl_basic_map_sum(
3877 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3879 unsigned n_in, n_out, nparam, total, pos;
3880 struct isl_basic_map *bmap = NULL;
3881 struct isl_dim_map *dim_map1, *dim_map2;
3882 int i;
3884 if (!bmap1 || !bmap2)
3885 goto error;
3887 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3888 goto error);
3890 nparam = isl_basic_map_n_param(bmap1);
3891 n_in = isl_basic_map_n_in(bmap1);
3892 n_out = isl_basic_map_n_out(bmap1);
3894 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3895 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3896 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3897 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3898 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3899 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3900 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3901 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3902 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3903 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3904 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3906 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3907 bmap1->n_div + bmap2->n_div + 2 * n_out,
3908 bmap1->n_eq + bmap2->n_eq + n_out,
3909 bmap1->n_ineq + bmap2->n_ineq);
3910 for (i = 0; i < n_out; ++i) {
3911 int j = isl_basic_map_alloc_equality(bmap);
3912 if (j < 0)
3913 goto error;
3914 isl_seq_clr(bmap->eq[j], 1+total);
3915 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3916 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3917 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3919 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3920 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3921 bmap = add_divs(bmap, 2 * n_out);
3923 bmap = isl_basic_map_simplify(bmap);
3924 return isl_basic_map_finalize(bmap);
3925 error:
3926 isl_basic_map_free(bmap);
3927 isl_basic_map_free(bmap1);
3928 isl_basic_map_free(bmap2);
3929 return NULL;
3932 /* Given two maps A -> f(A) and B -> g(B), construct a map
3933 * A \cap B -> f(A) + f(B)
3935 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3937 struct isl_map *result;
3938 int i, j;
3940 if (!map1 || !map2)
3941 goto error;
3943 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3945 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3946 map1->n * map2->n, 0);
3947 if (!result)
3948 goto error;
3949 for (i = 0; i < map1->n; ++i)
3950 for (j = 0; j < map2->n; ++j) {
3951 struct isl_basic_map *part;
3952 part = isl_basic_map_sum(
3953 isl_basic_map_copy(map1->p[i]),
3954 isl_basic_map_copy(map2->p[j]));
3955 if (isl_basic_map_is_empty(part))
3956 isl_basic_map_free(part);
3957 else
3958 result = isl_map_add_basic_map(result, part);
3959 if (!result)
3960 goto error;
3962 isl_map_free(map1);
3963 isl_map_free(map2);
3964 return result;
3965 error:
3966 isl_map_free(map1);
3967 isl_map_free(map2);
3968 return NULL;
3971 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3972 __isl_take isl_set *set2)
3974 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3977 /* Given a basic map A -> f(A), construct A -> -f(A).
3979 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3981 int i, j;
3982 unsigned off, n;
3984 bmap = isl_basic_map_cow(bmap);
3985 if (!bmap)
3986 return NULL;
3988 n = isl_basic_map_dim(bmap, isl_dim_out);
3989 off = isl_basic_map_offset(bmap, isl_dim_out);
3990 for (i = 0; i < bmap->n_eq; ++i)
3991 for (j = 0; j < n; ++j)
3992 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3993 for (i = 0; i < bmap->n_ineq; ++i)
3994 for (j = 0; j < n; ++j)
3995 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3996 for (i = 0; i < bmap->n_div; ++i)
3997 for (j = 0; j < n; ++j)
3998 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3999 bmap = isl_basic_map_gauss(bmap, NULL);
4000 return isl_basic_map_finalize(bmap);
4003 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
4005 return isl_basic_map_neg(bset);
4008 /* Given a map A -> f(A), construct A -> -f(A).
4010 struct isl_map *isl_map_neg(struct isl_map *map)
4012 int i;
4014 map = isl_map_cow(map);
4015 if (!map)
4016 return NULL;
4018 for (i = 0; i < map->n; ++i) {
4019 map->p[i] = isl_basic_map_neg(map->p[i]);
4020 if (!map->p[i])
4021 goto error;
4024 return map;
4025 error:
4026 isl_map_free(map);
4027 return NULL;
4030 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
4032 return (isl_set *)isl_map_neg((isl_map *)set);
4035 /* Given a basic map A -> f(A) and an integer d, construct a basic map
4036 * A -> floor(f(A)/d).
4038 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
4039 isl_int d)
4041 unsigned n_in, n_out, nparam, total, pos;
4042 struct isl_basic_map *result = NULL;
4043 struct isl_dim_map *dim_map;
4044 int i;
4046 if (!bmap)
4047 return NULL;
4049 nparam = isl_basic_map_n_param(bmap);
4050 n_in = isl_basic_map_n_in(bmap);
4051 n_out = isl_basic_map_n_out(bmap);
4053 total = nparam + n_in + n_out + bmap->n_div + n_out;
4054 dim_map = isl_dim_map_alloc(bmap->ctx, total);
4055 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
4056 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
4057 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
4058 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
4060 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
4061 bmap->n_div + n_out,
4062 bmap->n_eq, bmap->n_ineq + 2 * n_out);
4063 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
4064 result = add_divs(result, n_out);
4065 for (i = 0; i < n_out; ++i) {
4066 int j;
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_neg(result->ineq[j][1+nparam+n_in+i], d);
4072 isl_int_set_si(result->ineq[j][1+pos+i], 1);
4073 j = isl_basic_map_alloc_inequality(result);
4074 if (j < 0)
4075 goto error;
4076 isl_seq_clr(result->ineq[j], 1+total);
4077 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
4078 isl_int_set_si(result->ineq[j][1+pos+i], -1);
4079 isl_int_sub_ui(result->ineq[j][0], d, 1);
4082 result = isl_basic_map_simplify(result);
4083 return isl_basic_map_finalize(result);
4084 error:
4085 isl_basic_map_free(result);
4086 return NULL;
4089 /* Given a map A -> f(A) and an integer d, construct a map
4090 * A -> floor(f(A)/d).
4092 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4094 int i;
4096 map = isl_map_cow(map);
4097 if (!map)
4098 return NULL;
4100 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4101 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4102 for (i = 0; i < map->n; ++i) {
4103 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4104 if (!map->p[i])
4105 goto error;
4108 return map;
4109 error:
4110 isl_map_free(map);
4111 return NULL;
4114 /* Given a map A -> f(A) and an integer d, construct a map
4115 * A -> floor(f(A)/d).
4117 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4118 __isl_take isl_val *d)
4120 if (!map || !d)
4121 goto error;
4122 if (!isl_val_is_int(d))
4123 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4124 "expecting integer denominator", goto error);
4125 map = isl_map_floordiv(map, d->n);
4126 isl_val_free(d);
4127 return map;
4128 error:
4129 isl_map_free(map);
4130 isl_val_free(d);
4131 return NULL;
4134 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4136 int i;
4137 unsigned nparam;
4138 unsigned n_in;
4140 i = isl_basic_map_alloc_equality(bmap);
4141 if (i < 0)
4142 goto error;
4143 nparam = isl_basic_map_n_param(bmap);
4144 n_in = isl_basic_map_n_in(bmap);
4145 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4146 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4147 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4148 return isl_basic_map_finalize(bmap);
4149 error:
4150 isl_basic_map_free(bmap);
4151 return NULL;
4154 /* Add a constraints to "bmap" expressing i_pos < o_pos
4156 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4158 int i;
4159 unsigned nparam;
4160 unsigned n_in;
4162 i = isl_basic_map_alloc_inequality(bmap);
4163 if (i < 0)
4164 goto error;
4165 nparam = isl_basic_map_n_param(bmap);
4166 n_in = isl_basic_map_n_in(bmap);
4167 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4168 isl_int_set_si(bmap->ineq[i][0], -1);
4169 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4170 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4171 return isl_basic_map_finalize(bmap);
4172 error:
4173 isl_basic_map_free(bmap);
4174 return NULL;
4177 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4179 static __isl_give isl_basic_map *var_less_or_equal(
4180 __isl_take isl_basic_map *bmap, unsigned pos)
4182 int i;
4183 unsigned nparam;
4184 unsigned n_in;
4186 i = isl_basic_map_alloc_inequality(bmap);
4187 if (i < 0)
4188 goto error;
4189 nparam = isl_basic_map_n_param(bmap);
4190 n_in = isl_basic_map_n_in(bmap);
4191 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4192 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4193 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4194 return isl_basic_map_finalize(bmap);
4195 error:
4196 isl_basic_map_free(bmap);
4197 return NULL;
4200 /* Add a constraints to "bmap" expressing i_pos > o_pos
4202 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4204 int i;
4205 unsigned nparam;
4206 unsigned n_in;
4208 i = isl_basic_map_alloc_inequality(bmap);
4209 if (i < 0)
4210 goto error;
4211 nparam = isl_basic_map_n_param(bmap);
4212 n_in = isl_basic_map_n_in(bmap);
4213 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4214 isl_int_set_si(bmap->ineq[i][0], -1);
4215 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4216 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4217 return isl_basic_map_finalize(bmap);
4218 error:
4219 isl_basic_map_free(bmap);
4220 return NULL;
4223 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4225 static __isl_give isl_basic_map *var_more_or_equal(
4226 __isl_take isl_basic_map *bmap, unsigned pos)
4228 int i;
4229 unsigned nparam;
4230 unsigned n_in;
4232 i = isl_basic_map_alloc_inequality(bmap);
4233 if (i < 0)
4234 goto error;
4235 nparam = isl_basic_map_n_param(bmap);
4236 n_in = isl_basic_map_n_in(bmap);
4237 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4238 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4239 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4240 return isl_basic_map_finalize(bmap);
4241 error:
4242 isl_basic_map_free(bmap);
4243 return NULL;
4246 __isl_give isl_basic_map *isl_basic_map_equal(
4247 __isl_take isl_space *dim, unsigned n_equal)
4249 int i;
4250 struct isl_basic_map *bmap;
4251 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4252 if (!bmap)
4253 return NULL;
4254 for (i = 0; i < n_equal && bmap; ++i)
4255 bmap = var_equal(bmap, i);
4256 return isl_basic_map_finalize(bmap);
4259 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4261 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4262 unsigned pos)
4264 int i;
4265 struct isl_basic_map *bmap;
4266 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4267 if (!bmap)
4268 return NULL;
4269 for (i = 0; i < pos && bmap; ++i)
4270 bmap = var_equal(bmap, i);
4271 if (bmap)
4272 bmap = var_less(bmap, pos);
4273 return isl_basic_map_finalize(bmap);
4276 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4278 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4279 __isl_take isl_space *dim, unsigned pos)
4281 int i;
4282 isl_basic_map *bmap;
4284 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4285 for (i = 0; i < pos; ++i)
4286 bmap = var_equal(bmap, i);
4287 bmap = var_less_or_equal(bmap, pos);
4288 return isl_basic_map_finalize(bmap);
4291 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4293 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4294 unsigned pos)
4296 int i;
4297 struct isl_basic_map *bmap;
4298 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4299 if (!bmap)
4300 return NULL;
4301 for (i = 0; i < pos && bmap; ++i)
4302 bmap = var_equal(bmap, i);
4303 if (bmap)
4304 bmap = var_more(bmap, pos);
4305 return isl_basic_map_finalize(bmap);
4308 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4310 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4311 __isl_take isl_space *dim, unsigned pos)
4313 int i;
4314 isl_basic_map *bmap;
4316 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4317 for (i = 0; i < pos; ++i)
4318 bmap = var_equal(bmap, i);
4319 bmap = var_more_or_equal(bmap, pos);
4320 return isl_basic_map_finalize(bmap);
4323 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4324 unsigned n, int equal)
4326 struct isl_map *map;
4327 int i;
4329 if (n == 0 && equal)
4330 return isl_map_universe(dims);
4332 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4334 for (i = 0; i + 1 < n; ++i)
4335 map = isl_map_add_basic_map(map,
4336 isl_basic_map_less_at(isl_space_copy(dims), i));
4337 if (n > 0) {
4338 if (equal)
4339 map = isl_map_add_basic_map(map,
4340 isl_basic_map_less_or_equal_at(dims, n - 1));
4341 else
4342 map = isl_map_add_basic_map(map,
4343 isl_basic_map_less_at(dims, n - 1));
4344 } else
4345 isl_space_free(dims);
4347 return map;
4350 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4352 if (!dims)
4353 return NULL;
4354 return map_lex_lte_first(dims, dims->n_out, equal);
4357 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4359 return map_lex_lte_first(dim, n, 0);
4362 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4364 return map_lex_lte_first(dim, n, 1);
4367 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4369 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4372 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4374 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4377 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4378 unsigned n, int equal)
4380 struct isl_map *map;
4381 int i;
4383 if (n == 0 && equal)
4384 return isl_map_universe(dims);
4386 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4388 for (i = 0; i + 1 < n; ++i)
4389 map = isl_map_add_basic_map(map,
4390 isl_basic_map_more_at(isl_space_copy(dims), i));
4391 if (n > 0) {
4392 if (equal)
4393 map = isl_map_add_basic_map(map,
4394 isl_basic_map_more_or_equal_at(dims, n - 1));
4395 else
4396 map = isl_map_add_basic_map(map,
4397 isl_basic_map_more_at(dims, n - 1));
4398 } else
4399 isl_space_free(dims);
4401 return map;
4404 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4406 if (!dims)
4407 return NULL;
4408 return map_lex_gte_first(dims, dims->n_out, equal);
4411 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4413 return map_lex_gte_first(dim, n, 0);
4416 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4418 return map_lex_gte_first(dim, n, 1);
4421 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4423 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4426 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4428 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4431 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4432 __isl_take isl_set *set2)
4434 isl_map *map;
4435 map = isl_map_lex_le(isl_set_get_space(set1));
4436 map = isl_map_intersect_domain(map, set1);
4437 map = isl_map_intersect_range(map, set2);
4438 return map;
4441 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4442 __isl_take isl_set *set2)
4444 isl_map *map;
4445 map = isl_map_lex_lt(isl_set_get_space(set1));
4446 map = isl_map_intersect_domain(map, set1);
4447 map = isl_map_intersect_range(map, set2);
4448 return map;
4451 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4452 __isl_take isl_set *set2)
4454 isl_map *map;
4455 map = isl_map_lex_ge(isl_set_get_space(set1));
4456 map = isl_map_intersect_domain(map, set1);
4457 map = isl_map_intersect_range(map, set2);
4458 return map;
4461 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4462 __isl_take isl_set *set2)
4464 isl_map *map;
4465 map = isl_map_lex_gt(isl_set_get_space(set1));
4466 map = isl_map_intersect_domain(map, set1);
4467 map = isl_map_intersect_range(map, set2);
4468 return map;
4471 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4472 __isl_take isl_map *map2)
4474 isl_map *map;
4475 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4476 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4477 map = isl_map_apply_range(map, isl_map_reverse(map2));
4478 return map;
4481 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4482 __isl_take isl_map *map2)
4484 isl_map *map;
4485 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4486 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4487 map = isl_map_apply_range(map, isl_map_reverse(map2));
4488 return map;
4491 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4492 __isl_take isl_map *map2)
4494 isl_map *map;
4495 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4496 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4497 map = isl_map_apply_range(map, isl_map_reverse(map2));
4498 return map;
4501 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4502 __isl_take isl_map *map2)
4504 isl_map *map;
4505 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4506 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4507 map = isl_map_apply_range(map, isl_map_reverse(map2));
4508 return map;
4511 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4512 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4514 struct isl_basic_map *bmap;
4516 bset = isl_basic_set_cow(bset);
4517 if (!bset || !dim)
4518 goto error;
4520 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4521 isl_space_free(bset->dim);
4522 bmap = (struct isl_basic_map *) bset;
4523 bmap->dim = dim;
4524 return isl_basic_map_finalize(bmap);
4525 error:
4526 isl_basic_set_free(bset);
4527 isl_space_free(dim);
4528 return NULL;
4531 /* For a div d = floor(f/m), add the constraint
4533 * f - m d >= 0
4535 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4536 unsigned pos, isl_int *div)
4538 int i;
4539 unsigned total = isl_basic_map_total_dim(bmap);
4541 i = isl_basic_map_alloc_inequality(bmap);
4542 if (i < 0)
4543 return -1;
4544 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4545 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4547 return 0;
4550 /* For a div d = floor(f/m), add the constraint
4552 * -(f-(m-1)) + m d >= 0
4554 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4555 unsigned pos, isl_int *div)
4557 int i;
4558 unsigned total = isl_basic_map_total_dim(bmap);
4560 i = isl_basic_map_alloc_inequality(bmap);
4561 if (i < 0)
4562 return -1;
4563 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4564 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4565 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4566 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4568 return 0;
4571 /* For a div d = floor(f/m), add the constraints
4573 * f - m d >= 0
4574 * -(f-(m-1)) + m d >= 0
4576 * Note that the second constraint is the negation of
4578 * f - m d >= m
4580 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4581 unsigned pos, isl_int *div)
4583 if (add_upper_div_constraint(bmap, pos, div) < 0)
4584 return -1;
4585 if (add_lower_div_constraint(bmap, pos, div) < 0)
4586 return -1;
4587 return 0;
4590 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4591 unsigned pos, isl_int *div)
4593 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4594 pos, div);
4597 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4599 unsigned total = isl_basic_map_total_dim(bmap);
4600 unsigned div_pos = total - bmap->n_div + div;
4602 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4603 bmap->div[div]);
4606 /* For each known div d = floor(f/m), add the constraints
4608 * f - m d >= 0
4609 * -(f-(m-1)) + m d >= 0
4611 * Remove duplicate constraints in case of some these div constraints
4612 * already appear in "bmap".
4614 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4615 __isl_take isl_basic_map *bmap)
4617 unsigned n_div;
4619 if (!bmap)
4620 return NULL;
4621 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4622 if (n_div == 0)
4623 return bmap;
4625 bmap = add_known_div_constraints(bmap);
4626 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4627 bmap = isl_basic_map_finalize(bmap);
4628 return bmap;
4631 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4633 * In particular, if this div is of the form d = floor(f/m),
4634 * then add the constraint
4636 * f - m d >= 0
4638 * if sign < 0 or the constraint
4640 * -(f-(m-1)) + m d >= 0
4642 * if sign > 0.
4644 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4645 unsigned div, int sign)
4647 unsigned total;
4648 unsigned div_pos;
4650 if (!bmap)
4651 return -1;
4653 total = isl_basic_map_total_dim(bmap);
4654 div_pos = total - bmap->n_div + div;
4656 if (sign < 0)
4657 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4658 else
4659 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4662 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4664 return isl_basic_map_add_div_constraints(bset, div);
4667 struct isl_basic_set *isl_basic_map_underlying_set(
4668 struct isl_basic_map *bmap)
4670 if (!bmap)
4671 goto error;
4672 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4673 bmap->n_div == 0 &&
4674 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4675 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4676 return (struct isl_basic_set *)bmap;
4677 bmap = isl_basic_map_cow(bmap);
4678 if (!bmap)
4679 goto error;
4680 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4681 if (!bmap->dim)
4682 goto error;
4683 bmap->extra -= bmap->n_div;
4684 bmap->n_div = 0;
4685 bmap = isl_basic_map_finalize(bmap);
4686 return (struct isl_basic_set *)bmap;
4687 error:
4688 isl_basic_map_free(bmap);
4689 return NULL;
4692 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4693 __isl_take isl_basic_set *bset)
4695 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4698 /* Replace each element in "list" by the result of applying
4699 * isl_basic_map_underlying_set to the element.
4701 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4702 __isl_take isl_basic_map_list *list)
4704 int i, n;
4706 if (!list)
4707 return NULL;
4709 n = isl_basic_map_list_n_basic_map(list);
4710 for (i = 0; i < n; ++i) {
4711 isl_basic_map *bmap;
4712 isl_basic_set *bset;
4714 bmap = isl_basic_map_list_get_basic_map(list, i);
4715 bset = isl_basic_set_underlying_set(bmap);
4716 list = isl_basic_set_list_set_basic_set(list, i, bset);
4719 return list;
4722 struct isl_basic_map *isl_basic_map_overlying_set(
4723 struct isl_basic_set *bset, struct isl_basic_map *like)
4725 struct isl_basic_map *bmap;
4726 struct isl_ctx *ctx;
4727 unsigned total;
4728 int i;
4730 if (!bset || !like)
4731 goto error;
4732 ctx = bset->ctx;
4733 isl_assert(ctx, bset->n_div == 0, goto error);
4734 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4735 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4736 goto error);
4737 if (like->n_div == 0) {
4738 isl_space *space = isl_basic_map_get_space(like);
4739 isl_basic_map_free(like);
4740 return isl_basic_map_reset_space(bset, space);
4742 bset = isl_basic_set_cow(bset);
4743 if (!bset)
4744 goto error;
4745 total = bset->dim->n_out + bset->extra;
4746 bmap = (struct isl_basic_map *)bset;
4747 isl_space_free(bmap->dim);
4748 bmap->dim = isl_space_copy(like->dim);
4749 if (!bmap->dim)
4750 goto error;
4751 bmap->n_div = like->n_div;
4752 bmap->extra += like->n_div;
4753 if (bmap->extra) {
4754 unsigned ltotal;
4755 isl_int **div;
4756 ltotal = total - bmap->extra + like->extra;
4757 if (ltotal > total)
4758 ltotal = total;
4759 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4760 bmap->extra * (1 + 1 + total));
4761 if (isl_blk_is_error(bmap->block2))
4762 goto error;
4763 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4764 if (!div)
4765 goto error;
4766 bmap->div = div;
4767 for (i = 0; i < bmap->extra; ++i)
4768 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4769 for (i = 0; i < like->n_div; ++i) {
4770 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4771 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4773 bmap = isl_basic_map_add_known_div_constraints(bmap);
4775 isl_basic_map_free(like);
4776 bmap = isl_basic_map_simplify(bmap);
4777 bmap = isl_basic_map_finalize(bmap);
4778 return bmap;
4779 error:
4780 isl_basic_map_free(like);
4781 isl_basic_set_free(bset);
4782 return NULL;
4785 struct isl_basic_set *isl_basic_set_from_underlying_set(
4786 struct isl_basic_set *bset, struct isl_basic_set *like)
4788 return (struct isl_basic_set *)
4789 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4792 struct isl_set *isl_set_from_underlying_set(
4793 struct isl_set *set, struct isl_basic_set *like)
4795 int i;
4797 if (!set || !like)
4798 goto error;
4799 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4800 goto error);
4801 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4802 isl_basic_set_free(like);
4803 return set;
4805 set = isl_set_cow(set);
4806 if (!set)
4807 goto error;
4808 for (i = 0; i < set->n; ++i) {
4809 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4810 isl_basic_set_copy(like));
4811 if (!set->p[i])
4812 goto error;
4814 isl_space_free(set->dim);
4815 set->dim = isl_space_copy(like->dim);
4816 if (!set->dim)
4817 goto error;
4818 isl_basic_set_free(like);
4819 return set;
4820 error:
4821 isl_basic_set_free(like);
4822 isl_set_free(set);
4823 return NULL;
4826 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4828 int i;
4830 map = isl_map_cow(map);
4831 if (!map)
4832 return NULL;
4833 map->dim = isl_space_cow(map->dim);
4834 if (!map->dim)
4835 goto error;
4837 for (i = 1; i < map->n; ++i)
4838 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4839 goto error);
4840 for (i = 0; i < map->n; ++i) {
4841 map->p[i] = (struct isl_basic_map *)
4842 isl_basic_map_underlying_set(map->p[i]);
4843 if (!map->p[i])
4844 goto error;
4846 if (map->n == 0)
4847 map->dim = isl_space_underlying(map->dim, 0);
4848 else {
4849 isl_space_free(map->dim);
4850 map->dim = isl_space_copy(map->p[0]->dim);
4852 if (!map->dim)
4853 goto error;
4854 return (struct isl_set *)map;
4855 error:
4856 isl_map_free(map);
4857 return NULL;
4860 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4862 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4865 /* Replace the space of "bmap" by "space".
4867 * If the space of "bmap" is identical to "space" (including the identifiers
4868 * of the input and output dimensions), then simply return the original input.
4870 __isl_give isl_basic_map *isl_basic_map_reset_space(
4871 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4873 isl_bool equal;
4875 if (!bmap)
4876 goto error;
4877 equal = isl_space_is_equal(bmap->dim, space);
4878 if (equal >= 0 && equal)
4879 equal = isl_space_match(bmap->dim, isl_dim_in,
4880 space, isl_dim_in);
4881 if (equal >= 0 && equal)
4882 equal = isl_space_match(bmap->dim, isl_dim_out,
4883 space, isl_dim_out);
4884 if (equal < 0)
4885 goto error;
4886 if (equal) {
4887 isl_space_free(space);
4888 return bmap;
4890 bmap = isl_basic_map_cow(bmap);
4891 if (!bmap || !space)
4892 goto error;
4894 isl_space_free(bmap->dim);
4895 bmap->dim = space;
4897 bmap = isl_basic_map_finalize(bmap);
4899 return bmap;
4900 error:
4901 isl_basic_map_free(bmap);
4902 isl_space_free(space);
4903 return NULL;
4906 __isl_give isl_basic_set *isl_basic_set_reset_space(
4907 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4909 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4910 dim);
4913 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4914 __isl_take isl_space *dim)
4916 int i;
4918 map = isl_map_cow(map);
4919 if (!map || !dim)
4920 goto error;
4922 for (i = 0; i < map->n; ++i) {
4923 map->p[i] = isl_basic_map_reset_space(map->p[i],
4924 isl_space_copy(dim));
4925 if (!map->p[i])
4926 goto error;
4928 isl_space_free(map->dim);
4929 map->dim = dim;
4931 return map;
4932 error:
4933 isl_map_free(map);
4934 isl_space_free(dim);
4935 return NULL;
4938 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4939 __isl_take isl_space *dim)
4941 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4944 /* Compute the parameter domain of the given basic set.
4946 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4948 isl_space *space;
4949 unsigned n;
4951 if (isl_basic_set_is_params(bset))
4952 return bset;
4954 n = isl_basic_set_dim(bset, isl_dim_set);
4955 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4956 space = isl_basic_set_get_space(bset);
4957 space = isl_space_params(space);
4958 bset = isl_basic_set_reset_space(bset, space);
4959 return bset;
4962 /* Construct a zero-dimensional basic set with the given parameter domain.
4964 __isl_give isl_basic_set *isl_basic_set_from_params(
4965 __isl_take isl_basic_set *bset)
4967 isl_space *space;
4968 space = isl_basic_set_get_space(bset);
4969 space = isl_space_set_from_params(space);
4970 bset = isl_basic_set_reset_space(bset, space);
4971 return bset;
4974 /* Compute the parameter domain of the given set.
4976 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4978 isl_space *space;
4979 unsigned n;
4981 if (isl_set_is_params(set))
4982 return set;
4984 n = isl_set_dim(set, isl_dim_set);
4985 set = isl_set_project_out(set, isl_dim_set, 0, n);
4986 space = isl_set_get_space(set);
4987 space = isl_space_params(space);
4988 set = isl_set_reset_space(set, space);
4989 return set;
4992 /* Construct a zero-dimensional set with the given parameter domain.
4994 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4996 isl_space *space;
4997 space = isl_set_get_space(set);
4998 space = isl_space_set_from_params(space);
4999 set = isl_set_reset_space(set, space);
5000 return set;
5003 /* Compute the parameter domain of the given map.
5005 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
5007 isl_space *space;
5008 unsigned n;
5010 n = isl_map_dim(map, isl_dim_in);
5011 map = isl_map_project_out(map, isl_dim_in, 0, n);
5012 n = isl_map_dim(map, isl_dim_out);
5013 map = isl_map_project_out(map, isl_dim_out, 0, n);
5014 space = isl_map_get_space(map);
5015 space = isl_space_params(space);
5016 map = isl_map_reset_space(map, space);
5017 return map;
5020 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
5022 isl_space *space;
5023 unsigned n_out;
5025 if (!bmap)
5026 return NULL;
5027 space = isl_space_domain(isl_basic_map_get_space(bmap));
5029 n_out = isl_basic_map_n_out(bmap);
5030 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
5032 return isl_basic_map_reset_space(bmap, space);
5035 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
5037 if (!bmap)
5038 return -1;
5039 return isl_space_may_be_set(bmap->dim);
5042 /* Is this basic map actually a set?
5043 * Users should never call this function. Outside of isl,
5044 * the type should indicate whether something is a set or a map.
5046 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
5048 if (!bmap)
5049 return -1;
5050 return isl_space_is_set(bmap->dim);
5053 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
5055 if (!bmap)
5056 return NULL;
5057 if (isl_basic_map_is_set(bmap))
5058 return bmap;
5059 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
5062 __isl_give isl_basic_map *isl_basic_map_domain_map(
5063 __isl_take isl_basic_map *bmap)
5065 int i, k;
5066 isl_space *dim;
5067 isl_basic_map *domain;
5068 int nparam, n_in, n_out;
5069 unsigned total;
5071 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5072 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5073 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5075 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
5076 domain = isl_basic_map_universe(dim);
5078 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5079 bmap = isl_basic_map_apply_range(bmap, domain);
5080 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
5082 total = isl_basic_map_total_dim(bmap);
5084 for (i = 0; i < n_in; ++i) {
5085 k = isl_basic_map_alloc_equality(bmap);
5086 if (k < 0)
5087 goto error;
5088 isl_seq_clr(bmap->eq[k], 1 + total);
5089 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5090 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5093 bmap = isl_basic_map_gauss(bmap, NULL);
5094 return isl_basic_map_finalize(bmap);
5095 error:
5096 isl_basic_map_free(bmap);
5097 return NULL;
5100 __isl_give isl_basic_map *isl_basic_map_range_map(
5101 __isl_take isl_basic_map *bmap)
5103 int i, k;
5104 isl_space *dim;
5105 isl_basic_map *range;
5106 int nparam, n_in, n_out;
5107 unsigned total;
5109 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5110 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5111 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5113 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5114 range = isl_basic_map_universe(dim);
5116 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5117 bmap = isl_basic_map_apply_range(bmap, range);
5118 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5120 total = isl_basic_map_total_dim(bmap);
5122 for (i = 0; i < n_out; ++i) {
5123 k = isl_basic_map_alloc_equality(bmap);
5124 if (k < 0)
5125 goto error;
5126 isl_seq_clr(bmap->eq[k], 1 + total);
5127 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5128 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5131 bmap = isl_basic_map_gauss(bmap, NULL);
5132 return isl_basic_map_finalize(bmap);
5133 error:
5134 isl_basic_map_free(bmap);
5135 return NULL;
5138 int isl_map_may_be_set(__isl_keep isl_map *map)
5140 if (!map)
5141 return -1;
5142 return isl_space_may_be_set(map->dim);
5145 /* Is this map actually a set?
5146 * Users should never call this function. Outside of isl,
5147 * the type should indicate whether something is a set or a map.
5149 int isl_map_is_set(__isl_keep isl_map *map)
5151 if (!map)
5152 return -1;
5153 return isl_space_is_set(map->dim);
5156 struct isl_set *isl_map_range(struct isl_map *map)
5158 int i;
5159 struct isl_set *set;
5161 if (!map)
5162 goto error;
5163 if (isl_map_is_set(map))
5164 return (isl_set *)map;
5166 map = isl_map_cow(map);
5167 if (!map)
5168 goto error;
5170 set = (struct isl_set *) map;
5171 set->dim = isl_space_range(set->dim);
5172 if (!set->dim)
5173 goto error;
5174 for (i = 0; i < map->n; ++i) {
5175 set->p[i] = isl_basic_map_range(map->p[i]);
5176 if (!set->p[i])
5177 goto error;
5179 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5180 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5181 return set;
5182 error:
5183 isl_map_free(map);
5184 return NULL;
5187 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5189 int i;
5191 map = isl_map_cow(map);
5192 if (!map)
5193 return NULL;
5195 map->dim = isl_space_domain_map(map->dim);
5196 if (!map->dim)
5197 goto error;
5198 for (i = 0; i < map->n; ++i) {
5199 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5200 if (!map->p[i])
5201 goto error;
5203 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5204 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5205 return map;
5206 error:
5207 isl_map_free(map);
5208 return NULL;
5211 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5213 int i;
5214 isl_space *range_dim;
5216 map = isl_map_cow(map);
5217 if (!map)
5218 return NULL;
5220 range_dim = isl_space_range(isl_map_get_space(map));
5221 range_dim = isl_space_from_range(range_dim);
5222 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5223 map->dim = isl_space_join(map->dim, range_dim);
5224 if (!map->dim)
5225 goto error;
5226 for (i = 0; i < map->n; ++i) {
5227 map->p[i] = isl_basic_map_range_map(map->p[i]);
5228 if (!map->p[i])
5229 goto error;
5231 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5232 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5233 return map;
5234 error:
5235 isl_map_free(map);
5236 return NULL;
5239 /* Given a wrapped map of the form A[B -> C],
5240 * return the map A[B -> C] -> B.
5242 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5244 isl_id *id;
5245 isl_map *map;
5247 if (!set)
5248 return NULL;
5249 if (!isl_set_has_tuple_id(set))
5250 return isl_map_domain_map(isl_set_unwrap(set));
5252 id = isl_set_get_tuple_id(set);
5253 map = isl_map_domain_map(isl_set_unwrap(set));
5254 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5256 return map;
5259 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5260 __isl_take isl_space *dim)
5262 int i;
5263 struct isl_map *map = NULL;
5265 set = isl_set_cow(set);
5266 if (!set || !dim)
5267 goto error;
5268 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5269 map = (struct isl_map *)set;
5270 for (i = 0; i < set->n; ++i) {
5271 map->p[i] = isl_basic_map_from_basic_set(
5272 set->p[i], isl_space_copy(dim));
5273 if (!map->p[i])
5274 goto error;
5276 isl_space_free(map->dim);
5277 map->dim = dim;
5278 return map;
5279 error:
5280 isl_space_free(dim);
5281 isl_set_free(set);
5282 return NULL;
5285 __isl_give isl_basic_map *isl_basic_map_from_domain(
5286 __isl_take isl_basic_set *bset)
5288 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5291 __isl_give isl_basic_map *isl_basic_map_from_range(
5292 __isl_take isl_basic_set *bset)
5294 isl_space *space;
5295 space = isl_basic_set_get_space(bset);
5296 space = isl_space_from_range(space);
5297 bset = isl_basic_set_reset_space(bset, space);
5298 return (isl_basic_map *)bset;
5301 /* Create a relation with the given set as range.
5302 * The domain of the created relation is a zero-dimensional
5303 * flat anonymous space.
5305 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5307 isl_space *space;
5308 space = isl_set_get_space(set);
5309 space = isl_space_from_range(space);
5310 set = isl_set_reset_space(set, space);
5311 return (struct isl_map *)set;
5314 /* Create a relation with the given set as domain.
5315 * The range of the created relation is a zero-dimensional
5316 * flat anonymous space.
5318 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5320 return isl_map_reverse(isl_map_from_range(set));
5323 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5324 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5326 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5329 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5330 __isl_take isl_set *range)
5332 return isl_map_apply_range(isl_map_reverse(domain), range);
5335 /* Return a newly allocated isl_map with given space and flags and
5336 * room for "n" basic maps.
5337 * Make sure that all cached information is cleared.
5339 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *space, int n,
5340 unsigned flags)
5342 struct isl_map *map;
5344 if (!space)
5345 return NULL;
5346 if (n < 0)
5347 isl_die(space->ctx, isl_error_internal,
5348 "negative number of basic maps", goto error);
5349 map = isl_calloc(space->ctx, struct isl_map,
5350 sizeof(struct isl_map) +
5351 (n - 1) * sizeof(struct isl_basic_map *));
5352 if (!map)
5353 goto error;
5355 map->ctx = space->ctx;
5356 isl_ctx_ref(map->ctx);
5357 map->ref = 1;
5358 map->size = n;
5359 map->n = 0;
5360 map->dim = space;
5361 map->flags = flags;
5362 return map;
5363 error:
5364 isl_space_free(space);
5365 return NULL;
5368 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5369 unsigned nparam, unsigned in, unsigned out, int n,
5370 unsigned flags)
5372 struct isl_map *map;
5373 isl_space *dims;
5375 dims = isl_space_alloc(ctx, nparam, in, out);
5376 if (!dims)
5377 return NULL;
5379 map = isl_map_alloc_space(dims, n, flags);
5380 return map;
5383 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5385 struct isl_basic_map *bmap;
5386 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5387 bmap = isl_basic_map_set_to_empty(bmap);
5388 return bmap;
5391 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5393 struct isl_basic_set *bset;
5394 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5395 bset = isl_basic_set_set_to_empty(bset);
5396 return bset;
5399 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5401 struct isl_basic_map *bmap;
5402 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5403 bmap = isl_basic_map_finalize(bmap);
5404 return bmap;
5407 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5409 struct isl_basic_set *bset;
5410 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5411 bset = isl_basic_set_finalize(bset);
5412 return bset;
5415 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5417 int i;
5418 unsigned total = isl_space_dim(dim, isl_dim_all);
5419 isl_basic_map *bmap;
5421 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5422 for (i = 0; i < total; ++i) {
5423 int k = isl_basic_map_alloc_inequality(bmap);
5424 if (k < 0)
5425 goto error;
5426 isl_seq_clr(bmap->ineq[k], 1 + total);
5427 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5429 return bmap;
5430 error:
5431 isl_basic_map_free(bmap);
5432 return NULL;
5435 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5437 return isl_basic_map_nat_universe(dim);
5440 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5442 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5445 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5447 return isl_map_nat_universe(dim);
5450 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5452 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5455 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5457 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5460 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5462 struct isl_map *map;
5463 if (!dim)
5464 return NULL;
5465 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5466 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5467 return map;
5470 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5472 struct isl_set *set;
5473 if (!dim)
5474 return NULL;
5475 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5476 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5477 return set;
5480 struct isl_map *isl_map_dup(struct isl_map *map)
5482 int i;
5483 struct isl_map *dup;
5485 if (!map)
5486 return NULL;
5487 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5488 for (i = 0; i < map->n; ++i)
5489 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5490 return dup;
5493 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5494 __isl_take isl_basic_map *bmap)
5496 if (!bmap || !map)
5497 goto error;
5498 if (isl_basic_map_plain_is_empty(bmap)) {
5499 isl_basic_map_free(bmap);
5500 return map;
5502 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5503 isl_assert(map->ctx, map->n < map->size, goto error);
5504 map->p[map->n] = bmap;
5505 map->n++;
5506 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5507 return map;
5508 error:
5509 if (map)
5510 isl_map_free(map);
5511 if (bmap)
5512 isl_basic_map_free(bmap);
5513 return NULL;
5516 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5518 int i;
5520 if (!map)
5521 return NULL;
5523 if (--map->ref > 0)
5524 return NULL;
5526 clear_caches(map);
5527 isl_ctx_deref(map->ctx);
5528 for (i = 0; i < map->n; ++i)
5529 isl_basic_map_free(map->p[i]);
5530 isl_space_free(map->dim);
5531 free(map);
5533 return NULL;
5536 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5537 struct isl_basic_map *bmap, unsigned pos, int value)
5539 int j;
5541 bmap = isl_basic_map_cow(bmap);
5542 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5543 j = isl_basic_map_alloc_equality(bmap);
5544 if (j < 0)
5545 goto error;
5546 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5547 isl_int_set_si(bmap->eq[j][pos], -1);
5548 isl_int_set_si(bmap->eq[j][0], value);
5549 bmap = isl_basic_map_simplify(bmap);
5550 return isl_basic_map_finalize(bmap);
5551 error:
5552 isl_basic_map_free(bmap);
5553 return NULL;
5556 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5557 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5559 int j;
5561 bmap = isl_basic_map_cow(bmap);
5562 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5563 j = isl_basic_map_alloc_equality(bmap);
5564 if (j < 0)
5565 goto error;
5566 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5567 isl_int_set_si(bmap->eq[j][pos], -1);
5568 isl_int_set(bmap->eq[j][0], value);
5569 bmap = isl_basic_map_simplify(bmap);
5570 return isl_basic_map_finalize(bmap);
5571 error:
5572 isl_basic_map_free(bmap);
5573 return NULL;
5576 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5577 enum isl_dim_type type, unsigned pos, int value)
5579 if (!bmap)
5580 return NULL;
5581 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5582 return isl_basic_map_fix_pos_si(bmap,
5583 isl_basic_map_offset(bmap, type) + pos, value);
5584 error:
5585 isl_basic_map_free(bmap);
5586 return NULL;
5589 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5590 enum isl_dim_type type, unsigned pos, isl_int value)
5592 if (!bmap)
5593 return NULL;
5594 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5595 return isl_basic_map_fix_pos(bmap,
5596 isl_basic_map_offset(bmap, type) + pos, value);
5597 error:
5598 isl_basic_map_free(bmap);
5599 return NULL;
5602 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5603 * to be equal to "v".
5605 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5606 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5608 if (!bmap || !v)
5609 goto error;
5610 if (!isl_val_is_int(v))
5611 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5612 "expecting integer value", goto error);
5613 if (pos >= isl_basic_map_dim(bmap, type))
5614 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5615 "index out of bounds", goto error);
5616 pos += isl_basic_map_offset(bmap, type);
5617 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5618 isl_val_free(v);
5619 return bmap;
5620 error:
5621 isl_basic_map_free(bmap);
5622 isl_val_free(v);
5623 return NULL;
5626 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5627 * to be equal to "v".
5629 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5630 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5632 return isl_basic_map_fix_val(bset, type, pos, v);
5635 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5636 enum isl_dim_type type, unsigned pos, int value)
5638 return (struct isl_basic_set *)
5639 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5640 type, pos, value);
5643 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5644 enum isl_dim_type type, unsigned pos, isl_int value)
5646 return (struct isl_basic_set *)
5647 isl_basic_map_fix((struct isl_basic_map *)bset,
5648 type, pos, value);
5651 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5652 unsigned input, int value)
5654 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5657 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5658 unsigned dim, int value)
5660 return (struct isl_basic_set *)
5661 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5662 isl_dim_set, dim, value);
5665 static int remove_if_empty(__isl_keep isl_map *map, int i)
5667 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5669 if (empty < 0)
5670 return -1;
5671 if (!empty)
5672 return 0;
5674 isl_basic_map_free(map->p[i]);
5675 if (i != map->n - 1) {
5676 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5677 map->p[i] = map->p[map->n - 1];
5679 map->n--;
5681 return 0;
5684 /* Perform "fn" on each basic map of "map", where we may not be holding
5685 * the only reference to "map".
5686 * In particular, "fn" should be a semantics preserving operation
5687 * that we want to apply to all copies of "map". We therefore need
5688 * to be careful not to modify "map" in a way that breaks "map"
5689 * in case anything goes wrong.
5691 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5692 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5694 struct isl_basic_map *bmap;
5695 int i;
5697 if (!map)
5698 return NULL;
5700 for (i = map->n - 1; i >= 0; --i) {
5701 bmap = isl_basic_map_copy(map->p[i]);
5702 bmap = fn(bmap);
5703 if (!bmap)
5704 goto error;
5705 isl_basic_map_free(map->p[i]);
5706 map->p[i] = bmap;
5707 if (remove_if_empty(map, i) < 0)
5708 goto error;
5711 return map;
5712 error:
5713 isl_map_free(map);
5714 return NULL;
5717 struct isl_map *isl_map_fix_si(struct isl_map *map,
5718 enum isl_dim_type type, unsigned pos, int value)
5720 int i;
5722 map = isl_map_cow(map);
5723 if (!map)
5724 return NULL;
5726 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5727 for (i = map->n - 1; i >= 0; --i) {
5728 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5729 if (remove_if_empty(map, i) < 0)
5730 goto error;
5732 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5733 return map;
5734 error:
5735 isl_map_free(map);
5736 return NULL;
5739 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5740 enum isl_dim_type type, unsigned pos, int value)
5742 return (struct isl_set *)
5743 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5746 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5747 enum isl_dim_type type, unsigned pos, isl_int value)
5749 int i;
5751 map = isl_map_cow(map);
5752 if (!map)
5753 return NULL;
5755 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5756 for (i = 0; i < map->n; ++i) {
5757 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5758 if (!map->p[i])
5759 goto error;
5761 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5762 return map;
5763 error:
5764 isl_map_free(map);
5765 return NULL;
5768 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5769 enum isl_dim_type type, unsigned pos, isl_int value)
5771 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5774 /* Fix the value of the variable at position "pos" of type "type" of "map"
5775 * to be equal to "v".
5777 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5778 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5780 int i;
5782 map = isl_map_cow(map);
5783 if (!map || !v)
5784 goto error;
5786 if (!isl_val_is_int(v))
5787 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5788 "expecting integer value", goto error);
5789 if (pos >= isl_map_dim(map, type))
5790 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5791 "index out of bounds", goto error);
5792 for (i = map->n - 1; i >= 0; --i) {
5793 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5794 isl_val_copy(v));
5795 if (remove_if_empty(map, i) < 0)
5796 goto error;
5798 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5799 isl_val_free(v);
5800 return map;
5801 error:
5802 isl_map_free(map);
5803 isl_val_free(v);
5804 return NULL;
5807 /* Fix the value of the variable at position "pos" of type "type" of "set"
5808 * to be equal to "v".
5810 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5811 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5813 return isl_map_fix_val(set, type, pos, v);
5816 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5817 unsigned input, int value)
5819 return isl_map_fix_si(map, isl_dim_in, input, value);
5822 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5824 return (struct isl_set *)
5825 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5828 static __isl_give isl_basic_map *basic_map_bound_si(
5829 __isl_take isl_basic_map *bmap,
5830 enum isl_dim_type type, unsigned pos, int value, int upper)
5832 int j;
5834 if (!bmap)
5835 return NULL;
5836 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5837 pos += isl_basic_map_offset(bmap, type);
5838 bmap = isl_basic_map_cow(bmap);
5839 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5840 j = isl_basic_map_alloc_inequality(bmap);
5841 if (j < 0)
5842 goto error;
5843 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5844 if (upper) {
5845 isl_int_set_si(bmap->ineq[j][pos], -1);
5846 isl_int_set_si(bmap->ineq[j][0], value);
5847 } else {
5848 isl_int_set_si(bmap->ineq[j][pos], 1);
5849 isl_int_set_si(bmap->ineq[j][0], -value);
5851 bmap = isl_basic_map_simplify(bmap);
5852 return isl_basic_map_finalize(bmap);
5853 error:
5854 isl_basic_map_free(bmap);
5855 return NULL;
5858 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5859 __isl_take isl_basic_map *bmap,
5860 enum isl_dim_type type, unsigned pos, int value)
5862 return basic_map_bound_si(bmap, type, pos, value, 0);
5865 /* Constrain the values of the given dimension to be no greater than "value".
5867 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5868 __isl_take isl_basic_map *bmap,
5869 enum isl_dim_type type, unsigned pos, int value)
5871 return basic_map_bound_si(bmap, type, pos, value, 1);
5874 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5875 unsigned dim, isl_int value)
5877 int j;
5879 bset = isl_basic_set_cow(bset);
5880 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5881 j = isl_basic_set_alloc_inequality(bset);
5882 if (j < 0)
5883 goto error;
5884 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5885 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5886 isl_int_neg(bset->ineq[j][0], value);
5887 bset = isl_basic_set_simplify(bset);
5888 return isl_basic_set_finalize(bset);
5889 error:
5890 isl_basic_set_free(bset);
5891 return NULL;
5894 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5895 enum isl_dim_type type, unsigned pos, int value, int upper)
5897 int i;
5899 map = isl_map_cow(map);
5900 if (!map)
5901 return NULL;
5903 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5904 for (i = 0; i < map->n; ++i) {
5905 map->p[i] = basic_map_bound_si(map->p[i],
5906 type, pos, value, upper);
5907 if (!map->p[i])
5908 goto error;
5910 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5911 return map;
5912 error:
5913 isl_map_free(map);
5914 return NULL;
5917 __isl_give isl_map *isl_map_lower_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, 0);
5923 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5924 enum isl_dim_type type, unsigned pos, int value)
5926 return map_bound_si(map, type, pos, value, 1);
5929 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5930 enum isl_dim_type type, unsigned pos, int value)
5932 return (struct isl_set *)
5933 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5936 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5937 enum isl_dim_type type, unsigned pos, int value)
5939 return isl_map_upper_bound_si(set, type, pos, value);
5942 /* Bound the given variable of "bmap" from below (or above is "upper"
5943 * is set) to "value".
5945 static __isl_give isl_basic_map *basic_map_bound(
5946 __isl_take isl_basic_map *bmap,
5947 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5949 int j;
5951 if (!bmap)
5952 return NULL;
5953 if (pos >= isl_basic_map_dim(bmap, type))
5954 isl_die(bmap->ctx, isl_error_invalid,
5955 "index out of bounds", goto error);
5956 pos += isl_basic_map_offset(bmap, type);
5957 bmap = isl_basic_map_cow(bmap);
5958 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5959 j = isl_basic_map_alloc_inequality(bmap);
5960 if (j < 0)
5961 goto error;
5962 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5963 if (upper) {
5964 isl_int_set_si(bmap->ineq[j][pos], -1);
5965 isl_int_set(bmap->ineq[j][0], value);
5966 } else {
5967 isl_int_set_si(bmap->ineq[j][pos], 1);
5968 isl_int_neg(bmap->ineq[j][0], value);
5970 bmap = isl_basic_map_simplify(bmap);
5971 return isl_basic_map_finalize(bmap);
5972 error:
5973 isl_basic_map_free(bmap);
5974 return NULL;
5977 /* Bound the given variable of "map" from below (or above is "upper"
5978 * is set) to "value".
5980 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5981 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5983 int i;
5985 map = isl_map_cow(map);
5986 if (!map)
5987 return NULL;
5989 if (pos >= isl_map_dim(map, type))
5990 isl_die(map->ctx, isl_error_invalid,
5991 "index out of bounds", goto error);
5992 for (i = map->n - 1; i >= 0; --i) {
5993 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5994 if (remove_if_empty(map, i) < 0)
5995 goto error;
5997 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5998 return map;
5999 error:
6000 isl_map_free(map);
6001 return NULL;
6004 __isl_give isl_map *isl_map_lower_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, 0);
6010 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
6011 enum isl_dim_type type, unsigned pos, isl_int value)
6013 return map_bound(map, type, pos, value, 1);
6016 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
6017 enum isl_dim_type type, unsigned pos, isl_int value)
6019 return isl_map_lower_bound(set, type, pos, value);
6022 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
6023 enum isl_dim_type type, unsigned pos, isl_int value)
6025 return isl_map_upper_bound(set, type, pos, value);
6028 /* Force the values of the variable at position "pos" of type "type" of "set"
6029 * to be no smaller than "value".
6031 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
6032 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6034 if (!value)
6035 goto error;
6036 if (!isl_val_is_int(value))
6037 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6038 "expecting integer value", goto error);
6039 set = isl_set_lower_bound(set, type, pos, value->n);
6040 isl_val_free(value);
6041 return set;
6042 error:
6043 isl_val_free(value);
6044 isl_set_free(set);
6045 return NULL;
6048 /* Force the values of the variable at position "pos" of type "type" of "set"
6049 * to be no greater than "value".
6051 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
6052 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
6054 if (!value)
6055 goto error;
6056 if (!isl_val_is_int(value))
6057 isl_die(isl_set_get_ctx(set), isl_error_invalid,
6058 "expecting integer value", goto error);
6059 set = isl_set_upper_bound(set, type, pos, value->n);
6060 isl_val_free(value);
6061 return set;
6062 error:
6063 isl_val_free(value);
6064 isl_set_free(set);
6065 return NULL;
6068 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
6069 isl_int value)
6071 int i;
6073 set = isl_set_cow(set);
6074 if (!set)
6075 return NULL;
6077 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
6078 for (i = 0; i < set->n; ++i) {
6079 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
6080 if (!set->p[i])
6081 goto error;
6083 return set;
6084 error:
6085 isl_set_free(set);
6086 return NULL;
6089 struct isl_map *isl_map_reverse(struct isl_map *map)
6091 int i;
6093 map = isl_map_cow(map);
6094 if (!map)
6095 return NULL;
6097 map->dim = isl_space_reverse(map->dim);
6098 if (!map->dim)
6099 goto error;
6100 for (i = 0; i < map->n; ++i) {
6101 map->p[i] = isl_basic_map_reverse(map->p[i]);
6102 if (!map->p[i])
6103 goto error;
6105 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6106 return map;
6107 error:
6108 isl_map_free(map);
6109 return NULL;
6112 #undef TYPE
6113 #define TYPE isl_pw_multi_aff
6114 #undef SUFFIX
6115 #define SUFFIX _pw_multi_aff
6116 #undef EMPTY
6117 #define EMPTY isl_pw_multi_aff_empty
6118 #undef ADD
6119 #define ADD isl_pw_multi_aff_union_add
6120 #include "isl_map_lexopt_templ.c"
6122 /* Given a map "map", compute the lexicographically minimal
6123 * (or maximal) image element for each domain element in dom,
6124 * in the form of an isl_pw_multi_aff.
6125 * If "empty" is not NULL, then set *empty to those elements in dom that
6126 * do not have an image element.
6127 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6128 * should be computed over the domain of "map". "empty" is also NULL
6129 * in this case.
6131 * We first compute the lexicographically minimal or maximal element
6132 * in the first basic map. This results in a partial solution "res"
6133 * and a subset "todo" of dom that still need to be handled.
6134 * We then consider each of the remaining maps in "map" and successively
6135 * update both "res" and "todo".
6136 * If "empty" is NULL, then the todo sets are not needed and therefore
6137 * also not computed.
6139 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6140 __isl_take isl_map *map, __isl_take isl_set *dom,
6141 __isl_give isl_set **empty, unsigned flags)
6143 int i;
6144 int full;
6145 isl_pw_multi_aff *res;
6146 isl_set *todo;
6148 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6149 if (!map || (!full && !dom))
6150 goto error;
6152 if (isl_map_plain_is_empty(map)) {
6153 if (empty)
6154 *empty = dom;
6155 else
6156 isl_set_free(dom);
6157 return isl_pw_multi_aff_from_map(map);
6160 res = basic_map_partial_lexopt_pw_multi_aff(
6161 isl_basic_map_copy(map->p[0]),
6162 isl_set_copy(dom), empty, flags);
6164 if (empty)
6165 todo = *empty;
6166 for (i = 1; i < map->n; ++i) {
6167 isl_pw_multi_aff *res_i;
6169 res_i = basic_map_partial_lexopt_pw_multi_aff(
6170 isl_basic_map_copy(map->p[i]),
6171 isl_set_copy(dom), empty, flags);
6173 if (ISL_FL_ISSET(flags, ISL_OPT_MAX))
6174 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6175 else
6176 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6178 if (empty)
6179 todo = isl_set_intersect(todo, *empty);
6182 isl_set_free(dom);
6183 isl_map_free(map);
6185 if (empty)
6186 *empty = todo;
6188 return res;
6189 error:
6190 if (empty)
6191 *empty = NULL;
6192 isl_set_free(dom);
6193 isl_map_free(map);
6194 return NULL;
6197 #undef TYPE
6198 #define TYPE isl_map
6199 #undef SUFFIX
6200 #define SUFFIX
6201 #undef EMPTY
6202 #define EMPTY isl_map_empty
6203 #undef ADD
6204 #define ADD isl_map_union_disjoint
6205 #include "isl_map_lexopt_templ.c"
6207 /* Given a map "map", compute the lexicographically minimal
6208 * (or maximal) image element for each domain element in "dom",
6209 * in the form of an isl_map.
6210 * If "empty" is not NULL, then set *empty to those elements in "dom" that
6211 * do not have an image element.
6212 * If "flags" includes ISL_OPT_FULL, then "dom" is NULL and the optimum
6213 * should be computed over the domain of "map". "empty" is also NULL
6214 * in this case.
6216 * If the input consists of more than one disjunct, then first
6217 * compute the desired result in the form of an isl_pw_multi_aff and
6218 * then convert that into an isl_map.
6220 * This function used to have an explicit implementation in terms
6221 * of isl_maps, but it would continually intersect the domains of
6222 * partial results with the complement of the domain of the next
6223 * partial solution, potentially leading to an explosion in the number
6224 * of disjuncts if there are several disjuncts in the input.
6225 * An even earlier implementation of this function would look for
6226 * better results in the domain of the partial result and for extra
6227 * results in the complement of this domain, which would lead to
6228 * even more splintering.
6230 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6231 __isl_take isl_map *map, __isl_take isl_set *dom,
6232 __isl_give isl_set **empty, unsigned flags)
6234 int full;
6235 struct isl_map *res;
6236 isl_pw_multi_aff *pma;
6238 full = ISL_FL_ISSET(flags, ISL_OPT_FULL);
6239 if (!map || (!full && !dom))
6240 goto error;
6242 if (isl_map_plain_is_empty(map)) {
6243 if (empty)
6244 *empty = dom;
6245 else
6246 isl_set_free(dom);
6247 return map;
6250 if (map->n == 1) {
6251 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6252 dom, empty, flags);
6253 isl_map_free(map);
6254 return res;
6257 pma = isl_map_partial_lexopt_aligned_pw_multi_aff(map, dom, empty,
6258 flags);
6259 return isl_map_from_pw_multi_aff(pma);
6260 error:
6261 if (empty)
6262 *empty = NULL;
6263 isl_set_free(dom);
6264 isl_map_free(map);
6265 return NULL;
6268 __isl_give isl_map *isl_map_partial_lexmax(
6269 __isl_take isl_map *map, __isl_take isl_set *dom,
6270 __isl_give isl_set **empty)
6272 return isl_map_partial_lexopt(map, dom, empty, ISL_OPT_MAX);
6275 __isl_give isl_map *isl_map_partial_lexmin(
6276 __isl_take isl_map *map, __isl_take isl_set *dom,
6277 __isl_give isl_set **empty)
6279 return isl_map_partial_lexopt(map, dom, empty, 0);
6282 __isl_give isl_set *isl_set_partial_lexmin(
6283 __isl_take isl_set *set, __isl_take isl_set *dom,
6284 __isl_give isl_set **empty)
6286 return (struct isl_set *)
6287 isl_map_partial_lexmin((struct isl_map *)set,
6288 dom, empty);
6291 __isl_give isl_set *isl_set_partial_lexmax(
6292 __isl_take isl_set *set, __isl_take isl_set *dom,
6293 __isl_give isl_set **empty)
6295 return (struct isl_set *)
6296 isl_map_partial_lexmax((struct isl_map *)set,
6297 dom, empty);
6300 /* Compute the lexicographic minimum (or maximum if "flags" includes
6301 * ISL_OPT_MAX) of "bset" over its parametric domain.
6303 __isl_give isl_set *isl_basic_set_lexopt(__isl_take isl_basic_set *bset,
6304 unsigned flags)
6306 return isl_basic_map_lexopt(bset, flags);
6309 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6311 return isl_basic_map_lexopt(bmap, ISL_OPT_MAX);
6314 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6316 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6319 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6321 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6324 /* Compute the lexicographic minimum of "bset" over its parametric domain
6325 * for the purpose of quantifier elimination.
6326 * That is, find an explicit representation for all the existentially
6327 * quantified variables in "bset" by computing their lexicographic
6328 * minimum.
6330 static __isl_give isl_set *isl_basic_set_lexmin_compute_divs(
6331 __isl_take isl_basic_set *bset)
6333 return isl_basic_set_lexopt(bset, ISL_OPT_QE);
6336 /* Extract the first and only affine expression from list
6337 * and then add it to *pwaff with the given dom.
6338 * This domain is known to be disjoint from other domains
6339 * because of the way isl_basic_map_foreach_lexmax works.
6341 static int update_dim_opt(__isl_take isl_basic_set *dom,
6342 __isl_take isl_aff_list *list, void *user)
6344 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6345 isl_aff *aff;
6346 isl_pw_aff **pwaff = user;
6347 isl_pw_aff *pwaff_i;
6349 if (!list)
6350 goto error;
6351 if (isl_aff_list_n_aff(list) != 1)
6352 isl_die(ctx, isl_error_internal,
6353 "expecting single element list", goto error);
6355 aff = isl_aff_list_get_aff(list, 0);
6356 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6358 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6360 isl_aff_list_free(list);
6362 return 0;
6363 error:
6364 isl_basic_set_free(dom);
6365 isl_aff_list_free(list);
6366 return -1;
6369 /* Given a basic map with one output dimension, compute the minimum or
6370 * maximum of that dimension as an isl_pw_aff.
6372 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6373 * call update_dim_opt on each leaf of the result.
6375 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6376 int max)
6378 isl_space *dim = isl_basic_map_get_space(bmap);
6379 isl_pw_aff *pwaff;
6380 int r;
6382 dim = isl_space_from_domain(isl_space_domain(dim));
6383 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6384 pwaff = isl_pw_aff_empty(dim);
6386 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6387 if (r < 0)
6388 return isl_pw_aff_free(pwaff);
6390 return pwaff;
6393 /* Compute the minimum or maximum of the given output dimension
6394 * as a function of the parameters and the input dimensions,
6395 * but independently of the other output dimensions.
6397 * We first project out the other output dimension and then compute
6398 * the "lexicographic" maximum in each basic map, combining the results
6399 * using isl_pw_aff_union_max.
6401 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6402 int max)
6404 int i;
6405 isl_pw_aff *pwaff;
6406 unsigned n_out;
6408 n_out = isl_map_dim(map, isl_dim_out);
6409 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6410 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6411 if (!map)
6412 return NULL;
6414 if (map->n == 0) {
6415 isl_space *dim = isl_map_get_space(map);
6416 isl_map_free(map);
6417 return isl_pw_aff_empty(dim);
6420 pwaff = basic_map_dim_opt(map->p[0], max);
6421 for (i = 1; i < map->n; ++i) {
6422 isl_pw_aff *pwaff_i;
6424 pwaff_i = basic_map_dim_opt(map->p[i], max);
6425 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6428 isl_map_free(map);
6430 return pwaff;
6433 /* Compute the minimum of the given output dimension as a function of the
6434 * parameters and input dimensions, but independently of
6435 * the other output dimensions.
6437 __isl_give isl_pw_aff *isl_map_dim_min(__isl_take isl_map *map, int pos)
6439 return map_dim_opt(map, pos, 0);
6442 /* Compute the maximum of the given output dimension as a function of the
6443 * parameters and input dimensions, but independently of
6444 * the other output dimensions.
6446 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6448 return map_dim_opt(map, pos, 1);
6451 /* Compute the minimum or maximum of the given set dimension
6452 * as a function of the parameters,
6453 * but independently of the other set dimensions.
6455 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6456 int max)
6458 return map_dim_opt(set, pos, max);
6461 /* Compute the maximum of the given set dimension as a function of the
6462 * parameters, but independently of the other set dimensions.
6464 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6466 return set_dim_opt(set, pos, 1);
6469 /* Compute the minimum of the given set dimension as a function of the
6470 * parameters, but independently of the other set dimensions.
6472 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6474 return set_dim_opt(set, pos, 0);
6477 /* Apply a preimage specified by "mat" on the parameters of "bset".
6478 * bset is assumed to have only parameters and divs.
6480 static struct isl_basic_set *basic_set_parameter_preimage(
6481 struct isl_basic_set *bset, struct isl_mat *mat)
6483 unsigned nparam;
6485 if (!bset || !mat)
6486 goto error;
6488 bset->dim = isl_space_cow(bset->dim);
6489 if (!bset->dim)
6490 goto error;
6492 nparam = isl_basic_set_dim(bset, isl_dim_param);
6494 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6496 bset->dim->nparam = 0;
6497 bset->dim->n_out = nparam;
6498 bset = isl_basic_set_preimage(bset, mat);
6499 if (bset) {
6500 bset->dim->nparam = bset->dim->n_out;
6501 bset->dim->n_out = 0;
6503 return bset;
6504 error:
6505 isl_mat_free(mat);
6506 isl_basic_set_free(bset);
6507 return NULL;
6510 /* Apply a preimage specified by "mat" on the parameters of "set".
6511 * set is assumed to have only parameters and divs.
6513 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6514 __isl_take isl_mat *mat)
6516 isl_space *space;
6517 unsigned nparam;
6519 if (!set || !mat)
6520 goto error;
6522 nparam = isl_set_dim(set, isl_dim_param);
6524 if (mat->n_row != 1 + nparam)
6525 isl_die(isl_set_get_ctx(set), isl_error_internal,
6526 "unexpected number of rows", goto error);
6528 space = isl_set_get_space(set);
6529 space = isl_space_move_dims(space, isl_dim_set, 0,
6530 isl_dim_param, 0, nparam);
6531 set = isl_set_reset_space(set, space);
6532 set = isl_set_preimage(set, mat);
6533 nparam = isl_set_dim(set, isl_dim_out);
6534 space = isl_set_get_space(set);
6535 space = isl_space_move_dims(space, isl_dim_param, 0,
6536 isl_dim_out, 0, nparam);
6537 set = isl_set_reset_space(set, space);
6538 return set;
6539 error:
6540 isl_mat_free(mat);
6541 isl_set_free(set);
6542 return NULL;
6545 /* Intersect the basic set "bset" with the affine space specified by the
6546 * equalities in "eq".
6548 static struct isl_basic_set *basic_set_append_equalities(
6549 struct isl_basic_set *bset, struct isl_mat *eq)
6551 int i, k;
6552 unsigned len;
6554 if (!bset || !eq)
6555 goto error;
6557 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6558 eq->n_row, 0);
6559 if (!bset)
6560 goto error;
6562 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6563 for (i = 0; i < eq->n_row; ++i) {
6564 k = isl_basic_set_alloc_equality(bset);
6565 if (k < 0)
6566 goto error;
6567 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6568 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6570 isl_mat_free(eq);
6572 bset = isl_basic_set_gauss(bset, NULL);
6573 bset = isl_basic_set_finalize(bset);
6575 return bset;
6576 error:
6577 isl_mat_free(eq);
6578 isl_basic_set_free(bset);
6579 return NULL;
6582 /* Intersect the set "set" with the affine space specified by the
6583 * equalities in "eq".
6585 static struct isl_set *set_append_equalities(struct isl_set *set,
6586 struct isl_mat *eq)
6588 int i;
6590 if (!set || !eq)
6591 goto error;
6593 for (i = 0; i < set->n; ++i) {
6594 set->p[i] = basic_set_append_equalities(set->p[i],
6595 isl_mat_copy(eq));
6596 if (!set->p[i])
6597 goto error;
6599 isl_mat_free(eq);
6600 return set;
6601 error:
6602 isl_mat_free(eq);
6603 isl_set_free(set);
6604 return NULL;
6607 /* Given a basic set "bset" that only involves parameters and existentially
6608 * quantified variables, return the index of the first equality
6609 * that only involves parameters. If there is no such equality then
6610 * return bset->n_eq.
6612 * This function assumes that isl_basic_set_gauss has been called on "bset".
6614 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6616 int i, j;
6617 unsigned nparam, n_div;
6619 if (!bset)
6620 return -1;
6622 nparam = isl_basic_set_dim(bset, isl_dim_param);
6623 n_div = isl_basic_set_dim(bset, isl_dim_div);
6625 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6626 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6627 ++i;
6630 return i;
6633 /* Compute an explicit representation for the existentially quantified
6634 * variables in "bset" by computing the "minimal value" of the set
6635 * variables. Since there are no set variables, the computation of
6636 * the minimal value essentially computes an explicit representation
6637 * of the non-empty part(s) of "bset".
6639 * The input only involves parameters and existentially quantified variables.
6640 * All equalities among parameters have been removed.
6642 * Since the existentially quantified variables in the result are in general
6643 * going to be different from those in the input, we first replace
6644 * them by the minimal number of variables based on their equalities.
6645 * This should simplify the parametric integer programming.
6647 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6649 isl_morph *morph1, *morph2;
6650 isl_set *set;
6651 unsigned n;
6653 if (!bset)
6654 return NULL;
6655 if (bset->n_eq == 0)
6656 return isl_basic_set_lexmin_compute_divs(bset);
6658 morph1 = isl_basic_set_parameter_compression(bset);
6659 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6660 bset = isl_basic_set_lift(bset);
6661 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6662 bset = isl_morph_basic_set(morph2, bset);
6663 n = isl_basic_set_dim(bset, isl_dim_set);
6664 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6666 set = isl_basic_set_lexmin_compute_divs(bset);
6668 set = isl_morph_set(isl_morph_inverse(morph1), set);
6670 return set;
6673 /* Project the given basic set onto its parameter domain, possibly introducing
6674 * new, explicit, existential variables in the constraints.
6675 * The input has parameters and (possibly implicit) existential variables.
6676 * The output has the same parameters, but only
6677 * explicit existentially quantified variables.
6679 * The actual projection is performed by pip, but pip doesn't seem
6680 * to like equalities very much, so we first remove the equalities
6681 * among the parameters by performing a variable compression on
6682 * the parameters. Afterward, an inverse transformation is performed
6683 * and the equalities among the parameters are inserted back in.
6685 * The variable compression on the parameters may uncover additional
6686 * equalities that were only implicit before. We therefore check
6687 * if there are any new parameter equalities in the result and
6688 * if so recurse. The removal of parameter equalities is required
6689 * for the parameter compression performed by base_compute_divs.
6691 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6693 int i;
6694 struct isl_mat *eq;
6695 struct isl_mat *T, *T2;
6696 struct isl_set *set;
6697 unsigned nparam;
6699 bset = isl_basic_set_cow(bset);
6700 if (!bset)
6701 return NULL;
6703 if (bset->n_eq == 0)
6704 return base_compute_divs(bset);
6706 bset = isl_basic_set_gauss(bset, NULL);
6707 if (!bset)
6708 return NULL;
6709 if (isl_basic_set_plain_is_empty(bset))
6710 return isl_set_from_basic_set(bset);
6712 i = first_parameter_equality(bset);
6713 if (i == bset->n_eq)
6714 return base_compute_divs(bset);
6716 nparam = isl_basic_set_dim(bset, isl_dim_param);
6717 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6718 0, 1 + nparam);
6719 eq = isl_mat_cow(eq);
6720 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6721 if (T && T->n_col == 0) {
6722 isl_mat_free(T);
6723 isl_mat_free(T2);
6724 isl_mat_free(eq);
6725 bset = isl_basic_set_set_to_empty(bset);
6726 return isl_set_from_basic_set(bset);
6728 bset = basic_set_parameter_preimage(bset, T);
6730 i = first_parameter_equality(bset);
6731 if (!bset)
6732 set = NULL;
6733 else if (i == bset->n_eq)
6734 set = base_compute_divs(bset);
6735 else
6736 set = parameter_compute_divs(bset);
6737 set = set_parameter_preimage(set, T2);
6738 set = set_append_equalities(set, eq);
6739 return set;
6742 /* Insert the divs from "ls" before those of "bmap".
6744 * The number of columns is not changed, which means that the last
6745 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6746 * The caller is responsible for removing the same number of dimensions
6747 * from the space of "bmap".
6749 static __isl_give isl_basic_map *insert_divs_from_local_space(
6750 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6752 int i;
6753 int n_div;
6754 int old_n_div;
6756 n_div = isl_local_space_dim(ls, isl_dim_div);
6757 if (n_div == 0)
6758 return bmap;
6760 old_n_div = bmap->n_div;
6761 bmap = insert_div_rows(bmap, n_div);
6762 if (!bmap)
6763 return NULL;
6765 for (i = 0; i < n_div; ++i) {
6766 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6767 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6770 return bmap;
6773 /* Replace the space of "bmap" by the space and divs of "ls".
6775 * If "ls" has any divs, then we simplify the result since we may
6776 * have discovered some additional equalities that could simplify
6777 * the div expressions.
6779 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6780 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6782 int n_div;
6784 bmap = isl_basic_map_cow(bmap);
6785 if (!bmap || !ls)
6786 goto error;
6788 n_div = isl_local_space_dim(ls, isl_dim_div);
6789 bmap = insert_divs_from_local_space(bmap, ls);
6790 if (!bmap)
6791 goto error;
6793 isl_space_free(bmap->dim);
6794 bmap->dim = isl_local_space_get_space(ls);
6795 if (!bmap->dim)
6796 goto error;
6798 isl_local_space_free(ls);
6799 if (n_div > 0)
6800 bmap = isl_basic_map_simplify(bmap);
6801 bmap = isl_basic_map_finalize(bmap);
6802 return bmap;
6803 error:
6804 isl_basic_map_free(bmap);
6805 isl_local_space_free(ls);
6806 return NULL;
6809 /* Replace the space of "map" by the space and divs of "ls".
6811 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6812 __isl_take isl_local_space *ls)
6814 int i;
6816 map = isl_map_cow(map);
6817 if (!map || !ls)
6818 goto error;
6820 for (i = 0; i < map->n; ++i) {
6821 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6822 isl_local_space_copy(ls));
6823 if (!map->p[i])
6824 goto error;
6826 isl_space_free(map->dim);
6827 map->dim = isl_local_space_get_space(ls);
6828 if (!map->dim)
6829 goto error;
6831 isl_local_space_free(ls);
6832 return map;
6833 error:
6834 isl_local_space_free(ls);
6835 isl_map_free(map);
6836 return NULL;
6839 /* Compute an explicit representation for the existentially
6840 * quantified variables for which do not know any explicit representation yet.
6842 * We first sort the existentially quantified variables so that the
6843 * existentially quantified variables for which we already have an explicit
6844 * representation are placed before those for which we do not.
6845 * The input dimensions, the output dimensions and the existentially
6846 * quantified variables for which we already have an explicit
6847 * representation are then turned into parameters.
6848 * compute_divs returns a map with the same parameters and
6849 * no input or output dimensions and the dimension specification
6850 * is reset to that of the input, including the existentially quantified
6851 * variables for which we already had an explicit representation.
6853 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6855 struct isl_basic_set *bset;
6856 struct isl_set *set;
6857 struct isl_map *map;
6858 isl_space *dim;
6859 isl_local_space *ls;
6860 unsigned nparam;
6861 unsigned n_in;
6862 unsigned n_out;
6863 int n_known;
6864 int i;
6866 bmap = isl_basic_map_sort_divs(bmap);
6867 bmap = isl_basic_map_cow(bmap);
6868 if (!bmap)
6869 return NULL;
6871 n_known = isl_basic_map_first_unknown_div(bmap);
6872 if (n_known < 0)
6873 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6875 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6876 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6877 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6878 dim = isl_space_set_alloc(bmap->ctx,
6879 nparam + n_in + n_out + n_known, 0);
6880 if (!dim)
6881 goto error;
6883 ls = isl_basic_map_get_local_space(bmap);
6884 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6885 n_known, bmap->n_div - n_known);
6886 if (n_known > 0) {
6887 for (i = n_known; i < bmap->n_div; ++i)
6888 swap_div(bmap, i - n_known, i);
6889 bmap->n_div -= n_known;
6890 bmap->extra -= n_known;
6892 bmap = isl_basic_map_reset_space(bmap, dim);
6893 bset = (struct isl_basic_set *)bmap;
6895 set = parameter_compute_divs(bset);
6896 map = (struct isl_map *)set;
6897 map = replace_space_by_local_space(map, ls);
6899 return map;
6900 error:
6901 isl_basic_map_free(bmap);
6902 return NULL;
6905 /* Remove the explicit representation of local variable "div",
6906 * if there is any.
6908 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6909 __isl_take isl_basic_map *bmap, int div)
6911 isl_bool unknown;
6913 unknown = isl_basic_map_div_is_marked_unknown(bmap, div);
6914 if (unknown < 0)
6915 return isl_basic_map_free(bmap);
6916 if (unknown)
6917 return bmap;
6919 bmap = isl_basic_map_cow(bmap);
6920 if (!bmap)
6921 return NULL;
6922 isl_int_set_si(bmap->div[div][0], 0);
6923 return bmap;
6926 /* Is local variable "div" of "bmap" marked as not having an explicit
6927 * representation?
6928 * Note that even if "div" is not marked in this way and therefore
6929 * has an explicit representation, this representation may still
6930 * depend (indirectly) on other local variables that do not
6931 * have an explicit representation.
6933 isl_bool isl_basic_map_div_is_marked_unknown(__isl_keep isl_basic_map *bmap,
6934 int div)
6936 if (!bmap)
6937 return isl_bool_error;
6938 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6939 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6940 "position out of bounds", return isl_bool_error);
6941 return isl_int_is_zero(bmap->div[div][0]);
6944 /* Return the position of the first local variable that does not
6945 * have an explicit representation.
6946 * Return the total number of local variables if they all have
6947 * an explicit representation.
6948 * Return -1 on error.
6950 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
6952 int i;
6954 if (!bmap)
6955 return -1;
6957 for (i = 0; i < bmap->n_div; ++i) {
6958 if (!isl_basic_map_div_is_known(bmap, i))
6959 return i;
6961 return bmap->n_div;
6964 /* Return the position of the first local variable that does not
6965 * have an explicit representation.
6966 * Return the total number of local variables if they all have
6967 * an explicit representation.
6968 * Return -1 on error.
6970 int isl_basic_set_first_unknown_div(__isl_keep isl_basic_set *bset)
6972 return isl_basic_map_first_unknown_div(bset);
6975 /* Does "bmap" have an explicit representation for all local variables?
6977 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6979 int first, n;
6981 n = isl_basic_map_dim(bmap, isl_dim_div);
6982 first = isl_basic_map_first_unknown_div(bmap);
6983 if (first < 0)
6984 return isl_bool_error;
6985 return first == n;
6988 /* Do all basic maps in "map" have an explicit representation
6989 * for all local variables?
6991 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
6993 int i;
6995 if (!map)
6996 return isl_bool_error;
6998 for (i = 0; i < map->n; ++i) {
6999 int known = isl_basic_map_divs_known(map->p[i]);
7000 if (known <= 0)
7001 return known;
7004 return isl_bool_true;
7007 /* If bmap contains any unknown divs, then compute explicit
7008 * expressions for them. However, this computation may be
7009 * quite expensive, so first try to remove divs that aren't
7010 * strictly needed.
7012 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7014 int known;
7015 struct isl_map *map;
7017 known = isl_basic_map_divs_known(bmap);
7018 if (known < 0)
7019 goto error;
7020 if (known)
7021 return isl_map_from_basic_map(bmap);
7023 bmap = isl_basic_map_drop_redundant_divs(bmap);
7025 known = isl_basic_map_divs_known(bmap);
7026 if (known < 0)
7027 goto error;
7028 if (known)
7029 return isl_map_from_basic_map(bmap);
7031 map = compute_divs(bmap);
7032 return map;
7033 error:
7034 isl_basic_map_free(bmap);
7035 return NULL;
7038 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7040 int i;
7041 int known;
7042 struct isl_map *res;
7044 if (!map)
7045 return NULL;
7046 if (map->n == 0)
7047 return map;
7049 known = isl_map_divs_known(map);
7050 if (known < 0) {
7051 isl_map_free(map);
7052 return NULL;
7054 if (known)
7055 return map;
7057 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7058 for (i = 1 ; i < map->n; ++i) {
7059 struct isl_map *r2;
7060 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7061 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7062 res = isl_map_union_disjoint(res, r2);
7063 else
7064 res = isl_map_union(res, r2);
7066 isl_map_free(map);
7068 return res;
7071 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7073 return (struct isl_set *)
7074 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7077 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7079 return (struct isl_set *)
7080 isl_map_compute_divs((struct isl_map *)set);
7083 struct isl_set *isl_map_domain(struct isl_map *map)
7085 int i;
7086 struct isl_set *set;
7088 if (!map)
7089 goto error;
7091 map = isl_map_cow(map);
7092 if (!map)
7093 return NULL;
7095 set = (struct isl_set *)map;
7096 set->dim = isl_space_domain(set->dim);
7097 if (!set->dim)
7098 goto error;
7099 for (i = 0; i < map->n; ++i) {
7100 set->p[i] = isl_basic_map_domain(map->p[i]);
7101 if (!set->p[i])
7102 goto error;
7104 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7105 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7106 return set;
7107 error:
7108 isl_map_free(map);
7109 return NULL;
7112 /* Return the union of "map1" and "map2", where we assume for now that
7113 * "map1" and "map2" are disjoint. Note that the basic maps inside
7114 * "map1" or "map2" may not be disjoint from each other.
7115 * Also note that this function is also called from isl_map_union,
7116 * which takes care of handling the situation where "map1" and "map2"
7117 * may not be disjoint.
7119 * If one of the inputs is empty, we can simply return the other input.
7120 * Similarly, if one of the inputs is universal, then it is equal to the union.
7122 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7123 __isl_take isl_map *map2)
7125 int i;
7126 unsigned flags = 0;
7127 struct isl_map *map = NULL;
7128 int is_universe;
7130 if (!map1 || !map2)
7131 goto error;
7133 if (!isl_space_is_equal(map1->dim, map2->dim))
7134 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7135 "spaces don't match", goto error);
7137 if (map1->n == 0) {
7138 isl_map_free(map1);
7139 return map2;
7141 if (map2->n == 0) {
7142 isl_map_free(map2);
7143 return map1;
7146 is_universe = isl_map_plain_is_universe(map1);
7147 if (is_universe < 0)
7148 goto error;
7149 if (is_universe) {
7150 isl_map_free(map2);
7151 return map1;
7154 is_universe = isl_map_plain_is_universe(map2);
7155 if (is_universe < 0)
7156 goto error;
7157 if (is_universe) {
7158 isl_map_free(map1);
7159 return map2;
7162 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7163 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7164 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7166 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7167 map1->n + map2->n, flags);
7168 if (!map)
7169 goto error;
7170 for (i = 0; i < map1->n; ++i) {
7171 map = isl_map_add_basic_map(map,
7172 isl_basic_map_copy(map1->p[i]));
7173 if (!map)
7174 goto error;
7176 for (i = 0; i < map2->n; ++i) {
7177 map = isl_map_add_basic_map(map,
7178 isl_basic_map_copy(map2->p[i]));
7179 if (!map)
7180 goto error;
7182 isl_map_free(map1);
7183 isl_map_free(map2);
7184 return map;
7185 error:
7186 isl_map_free(map);
7187 isl_map_free(map1);
7188 isl_map_free(map2);
7189 return NULL;
7192 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7193 * guaranteed to be disjoint by the caller.
7195 * Note that this functions is called from within isl_map_make_disjoint,
7196 * so we have to be careful not to touch the constraints of the inputs
7197 * in any way.
7199 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7200 __isl_take isl_map *map2)
7202 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7205 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7206 * not be disjoint. The parameters are assumed to have been aligned.
7208 * We currently simply call map_union_disjoint, the internal operation
7209 * of which does not really depend on the inputs being disjoint.
7210 * If the result contains more than one basic map, then we clear
7211 * the disjoint flag since the result may contain basic maps from
7212 * both inputs and these are not guaranteed to be disjoint.
7214 * As a special case, if "map1" and "map2" are obviously equal,
7215 * then we simply return "map1".
7217 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7218 __isl_take isl_map *map2)
7220 int equal;
7222 if (!map1 || !map2)
7223 goto error;
7225 equal = isl_map_plain_is_equal(map1, map2);
7226 if (equal < 0)
7227 goto error;
7228 if (equal) {
7229 isl_map_free(map2);
7230 return map1;
7233 map1 = map_union_disjoint(map1, map2);
7234 if (!map1)
7235 return NULL;
7236 if (map1->n > 1)
7237 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7238 return map1;
7239 error:
7240 isl_map_free(map1);
7241 isl_map_free(map2);
7242 return NULL;
7245 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7246 * not be disjoint.
7248 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7249 __isl_take isl_map *map2)
7251 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7254 struct isl_set *isl_set_union_disjoint(
7255 struct isl_set *set1, struct isl_set *set2)
7257 return (struct isl_set *)
7258 isl_map_union_disjoint(
7259 (struct isl_map *)set1, (struct isl_map *)set2);
7262 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7264 return (struct isl_set *)
7265 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7268 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7269 * the results.
7271 * "map" and "set" are assumed to be compatible and non-NULL.
7273 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7274 __isl_take isl_set *set,
7275 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7276 __isl_take isl_basic_set *bset))
7278 unsigned flags = 0;
7279 struct isl_map *result;
7280 int i, j;
7282 if (isl_set_plain_is_universe(set)) {
7283 isl_set_free(set);
7284 return map;
7287 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7288 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7289 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7291 result = isl_map_alloc_space(isl_space_copy(map->dim),
7292 map->n * set->n, flags);
7293 for (i = 0; result && i < map->n; ++i)
7294 for (j = 0; j < set->n; ++j) {
7295 result = isl_map_add_basic_map(result,
7296 fn(isl_basic_map_copy(map->p[i]),
7297 isl_basic_set_copy(set->p[j])));
7298 if (!result)
7299 break;
7302 isl_map_free(map);
7303 isl_set_free(set);
7304 return result;
7307 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7308 __isl_take isl_set *set)
7310 if (!map || !set)
7311 goto error;
7313 if (!isl_map_compatible_range(map, set))
7314 isl_die(set->ctx, isl_error_invalid,
7315 "incompatible spaces", goto error);
7317 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7318 error:
7319 isl_map_free(map);
7320 isl_set_free(set);
7321 return NULL;
7324 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7325 __isl_take isl_set *set)
7327 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7330 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7331 __isl_take isl_set *set)
7333 if (!map || !set)
7334 goto error;
7336 if (!isl_map_compatible_domain(map, set))
7337 isl_die(set->ctx, isl_error_invalid,
7338 "incompatible spaces", goto error);
7340 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7341 error:
7342 isl_map_free(map);
7343 isl_set_free(set);
7344 return NULL;
7347 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7348 __isl_take isl_set *set)
7350 return isl_map_align_params_map_map_and(map, set,
7351 &map_intersect_domain);
7354 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7355 __isl_take isl_map *map2)
7357 if (!map1 || !map2)
7358 goto error;
7359 map1 = isl_map_reverse(map1);
7360 map1 = isl_map_apply_range(map1, map2);
7361 return isl_map_reverse(map1);
7362 error:
7363 isl_map_free(map1);
7364 isl_map_free(map2);
7365 return NULL;
7368 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7369 __isl_take isl_map *map2)
7371 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7374 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7375 __isl_take isl_map *map2)
7377 isl_space *dim_result;
7378 struct isl_map *result;
7379 int i, j;
7381 if (!map1 || !map2)
7382 goto error;
7384 dim_result = isl_space_join(isl_space_copy(map1->dim),
7385 isl_space_copy(map2->dim));
7387 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7388 if (!result)
7389 goto error;
7390 for (i = 0; i < map1->n; ++i)
7391 for (j = 0; j < map2->n; ++j) {
7392 result = isl_map_add_basic_map(result,
7393 isl_basic_map_apply_range(
7394 isl_basic_map_copy(map1->p[i]),
7395 isl_basic_map_copy(map2->p[j])));
7396 if (!result)
7397 goto error;
7399 isl_map_free(map1);
7400 isl_map_free(map2);
7401 if (result && result->n <= 1)
7402 ISL_F_SET(result, ISL_MAP_DISJOINT);
7403 return result;
7404 error:
7405 isl_map_free(map1);
7406 isl_map_free(map2);
7407 return NULL;
7410 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7411 __isl_take isl_map *map2)
7413 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7417 * returns range - domain
7419 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7421 isl_space *target_space;
7422 struct isl_basic_set *bset;
7423 unsigned dim;
7424 unsigned nparam;
7425 int i;
7427 if (!bmap)
7428 goto error;
7429 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7430 bmap->dim, isl_dim_out),
7431 goto error);
7432 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7433 dim = isl_basic_map_n_in(bmap);
7434 nparam = isl_basic_map_n_param(bmap);
7435 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7436 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7437 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7438 for (i = 0; i < dim; ++i) {
7439 int j = isl_basic_map_alloc_equality(bmap);
7440 if (j < 0) {
7441 bmap = isl_basic_map_free(bmap);
7442 break;
7444 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7445 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7446 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7447 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7449 bset = isl_basic_map_domain(bmap);
7450 bset = isl_basic_set_reset_space(bset, target_space);
7451 return bset;
7452 error:
7453 isl_basic_map_free(bmap);
7454 return NULL;
7458 * returns range - domain
7460 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7462 int i;
7463 isl_space *dim;
7464 struct isl_set *result;
7466 if (!map)
7467 return NULL;
7469 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7470 map->dim, isl_dim_out),
7471 goto error);
7472 dim = isl_map_get_space(map);
7473 dim = isl_space_domain(dim);
7474 result = isl_set_alloc_space(dim, map->n, 0);
7475 if (!result)
7476 goto error;
7477 for (i = 0; i < map->n; ++i)
7478 result = isl_set_add_basic_set(result,
7479 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7480 isl_map_free(map);
7481 return result;
7482 error:
7483 isl_map_free(map);
7484 return NULL;
7488 * returns [domain -> range] -> range - domain
7490 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7491 __isl_take isl_basic_map *bmap)
7493 int i, k;
7494 isl_space *dim;
7495 isl_basic_map *domain;
7496 int nparam, n;
7497 unsigned total;
7499 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7500 bmap->dim, isl_dim_out))
7501 isl_die(bmap->ctx, isl_error_invalid,
7502 "domain and range don't match", goto error);
7504 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7505 n = isl_basic_map_dim(bmap, isl_dim_in);
7507 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7508 domain = isl_basic_map_universe(dim);
7510 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7511 bmap = isl_basic_map_apply_range(bmap, domain);
7512 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7514 total = isl_basic_map_total_dim(bmap);
7516 for (i = 0; i < n; ++i) {
7517 k = isl_basic_map_alloc_equality(bmap);
7518 if (k < 0)
7519 goto error;
7520 isl_seq_clr(bmap->eq[k], 1 + total);
7521 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7522 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7523 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7526 bmap = isl_basic_map_gauss(bmap, NULL);
7527 return isl_basic_map_finalize(bmap);
7528 error:
7529 isl_basic_map_free(bmap);
7530 return NULL;
7534 * returns [domain -> range] -> range - domain
7536 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7538 int i;
7539 isl_space *domain_dim;
7541 if (!map)
7542 return NULL;
7544 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7545 map->dim, isl_dim_out))
7546 isl_die(map->ctx, isl_error_invalid,
7547 "domain and range don't match", goto error);
7549 map = isl_map_cow(map);
7550 if (!map)
7551 return NULL;
7553 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7554 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7555 map->dim = isl_space_join(map->dim, domain_dim);
7556 if (!map->dim)
7557 goto error;
7558 for (i = 0; i < map->n; ++i) {
7559 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7560 if (!map->p[i])
7561 goto error;
7563 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7564 return map;
7565 error:
7566 isl_map_free(map);
7567 return NULL;
7570 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7572 struct isl_basic_map *bmap;
7573 unsigned nparam;
7574 unsigned dim;
7575 int i;
7577 if (!dims)
7578 return NULL;
7580 nparam = dims->nparam;
7581 dim = dims->n_out;
7582 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7583 if (!bmap)
7584 goto error;
7586 for (i = 0; i < dim; ++i) {
7587 int j = isl_basic_map_alloc_equality(bmap);
7588 if (j < 0)
7589 goto error;
7590 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7591 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7592 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7594 return isl_basic_map_finalize(bmap);
7595 error:
7596 isl_basic_map_free(bmap);
7597 return NULL;
7600 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7602 if (!dim)
7603 return NULL;
7604 if (dim->n_in != dim->n_out)
7605 isl_die(dim->ctx, isl_error_invalid,
7606 "number of input and output dimensions needs to be "
7607 "the same", goto error);
7608 return basic_map_identity(dim);
7609 error:
7610 isl_space_free(dim);
7611 return NULL;
7614 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7616 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7619 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7621 isl_space *dim = isl_set_get_space(set);
7622 isl_map *id;
7623 id = isl_map_identity(isl_space_map_from_set(dim));
7624 return isl_map_intersect_range(id, set);
7627 /* Construct a basic set with all set dimensions having only non-negative
7628 * values.
7630 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7631 __isl_take isl_space *space)
7633 int i;
7634 unsigned nparam;
7635 unsigned dim;
7636 struct isl_basic_set *bset;
7638 if (!space)
7639 return NULL;
7640 nparam = space->nparam;
7641 dim = space->n_out;
7642 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7643 if (!bset)
7644 return NULL;
7645 for (i = 0; i < dim; ++i) {
7646 int k = isl_basic_set_alloc_inequality(bset);
7647 if (k < 0)
7648 goto error;
7649 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7650 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7652 return bset;
7653 error:
7654 isl_basic_set_free(bset);
7655 return NULL;
7658 /* Construct the half-space x_pos >= 0.
7660 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7661 int pos)
7663 int k;
7664 isl_basic_set *nonneg;
7666 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7667 k = isl_basic_set_alloc_inequality(nonneg);
7668 if (k < 0)
7669 goto error;
7670 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7671 isl_int_set_si(nonneg->ineq[k][pos], 1);
7673 return isl_basic_set_finalize(nonneg);
7674 error:
7675 isl_basic_set_free(nonneg);
7676 return NULL;
7679 /* Construct the half-space x_pos <= -1.
7681 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7683 int k;
7684 isl_basic_set *neg;
7686 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7687 k = isl_basic_set_alloc_inequality(neg);
7688 if (k < 0)
7689 goto error;
7690 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7691 isl_int_set_si(neg->ineq[k][0], -1);
7692 isl_int_set_si(neg->ineq[k][pos], -1);
7694 return isl_basic_set_finalize(neg);
7695 error:
7696 isl_basic_set_free(neg);
7697 return NULL;
7700 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7701 enum isl_dim_type type, unsigned first, unsigned n)
7703 int i;
7704 unsigned offset;
7705 isl_basic_set *nonneg;
7706 isl_basic_set *neg;
7708 if (!set)
7709 return NULL;
7710 if (n == 0)
7711 return set;
7713 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7715 offset = pos(set->dim, type);
7716 for (i = 0; i < n; ++i) {
7717 nonneg = nonneg_halfspace(isl_set_get_space(set),
7718 offset + first + i);
7719 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7721 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7724 return set;
7725 error:
7726 isl_set_free(set);
7727 return NULL;
7730 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7731 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7732 void *user)
7734 isl_set *half;
7736 if (!set)
7737 return -1;
7738 if (isl_set_plain_is_empty(set)) {
7739 isl_set_free(set);
7740 return 0;
7742 if (first == len)
7743 return fn(set, signs, user);
7745 signs[first] = 1;
7746 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7747 1 + first));
7748 half = isl_set_intersect(half, isl_set_copy(set));
7749 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7750 goto error;
7752 signs[first] = -1;
7753 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7754 1 + first));
7755 half = isl_set_intersect(half, set);
7756 return foreach_orthant(half, signs, first + 1, len, fn, user);
7757 error:
7758 isl_set_free(set);
7759 return -1;
7762 /* Call "fn" on the intersections of "set" with each of the orthants
7763 * (except for obviously empty intersections). The orthant is identified
7764 * by the signs array, with each entry having value 1 or -1 according
7765 * to the sign of the corresponding variable.
7767 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7768 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7769 void *user)
7771 unsigned nparam;
7772 unsigned nvar;
7773 int *signs;
7774 int r;
7776 if (!set)
7777 return -1;
7778 if (isl_set_plain_is_empty(set))
7779 return 0;
7781 nparam = isl_set_dim(set, isl_dim_param);
7782 nvar = isl_set_dim(set, isl_dim_set);
7784 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7786 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7787 fn, user);
7789 free(signs);
7791 return r;
7794 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7796 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7799 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7800 __isl_keep isl_basic_map *bmap2)
7802 int is_subset;
7803 struct isl_map *map1;
7804 struct isl_map *map2;
7806 if (!bmap1 || !bmap2)
7807 return isl_bool_error;
7809 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7810 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7812 is_subset = isl_map_is_subset(map1, map2);
7814 isl_map_free(map1);
7815 isl_map_free(map2);
7817 return is_subset;
7820 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7821 __isl_keep isl_basic_set *bset2)
7823 return isl_basic_map_is_subset(bset1, bset2);
7826 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7827 __isl_keep isl_basic_map *bmap2)
7829 isl_bool is_subset;
7831 if (!bmap1 || !bmap2)
7832 return isl_bool_error;
7833 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7834 if (is_subset != isl_bool_true)
7835 return is_subset;
7836 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7837 return is_subset;
7840 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7841 __isl_keep isl_basic_set *bset2)
7843 return isl_basic_map_is_equal(
7844 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7847 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7849 int i;
7850 int is_empty;
7852 if (!map)
7853 return isl_bool_error;
7854 for (i = 0; i < map->n; ++i) {
7855 is_empty = isl_basic_map_is_empty(map->p[i]);
7856 if (is_empty < 0)
7857 return isl_bool_error;
7858 if (!is_empty)
7859 return isl_bool_false;
7861 return isl_bool_true;
7864 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7866 return map ? map->n == 0 : isl_bool_error;
7869 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7871 return set ? set->n == 0 : isl_bool_error;
7874 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7876 return isl_map_is_empty((struct isl_map *)set);
7879 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7881 if (!map1 || !map2)
7882 return -1;
7884 return isl_space_is_equal(map1->dim, map2->dim);
7887 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7889 if (!set1 || !set2)
7890 return -1;
7892 return isl_space_is_equal(set1->dim, set2->dim);
7895 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7897 isl_bool is_subset;
7899 if (!map1 || !map2)
7900 return isl_bool_error;
7901 is_subset = isl_map_is_subset(map1, map2);
7902 if (is_subset != isl_bool_true)
7903 return is_subset;
7904 is_subset = isl_map_is_subset(map2, map1);
7905 return is_subset;
7908 /* Is "map1" equal to "map2"?
7910 * First check if they are obviously equal.
7911 * If not, then perform a more detailed analysis.
7913 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7915 isl_bool equal;
7917 equal = isl_map_plain_is_equal(map1, map2);
7918 if (equal < 0 || equal)
7919 return equal;
7920 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7923 isl_bool isl_basic_map_is_strict_subset(
7924 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7926 isl_bool is_subset;
7928 if (!bmap1 || !bmap2)
7929 return isl_bool_error;
7930 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7931 if (is_subset != isl_bool_true)
7932 return is_subset;
7933 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7934 if (is_subset == isl_bool_error)
7935 return is_subset;
7936 return !is_subset;
7939 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7940 __isl_keep isl_map *map2)
7942 isl_bool is_subset;
7944 if (!map1 || !map2)
7945 return isl_bool_error;
7946 is_subset = isl_map_is_subset(map1, map2);
7947 if (is_subset != isl_bool_true)
7948 return is_subset;
7949 is_subset = isl_map_is_subset(map2, map1);
7950 if (is_subset == isl_bool_error)
7951 return is_subset;
7952 return !is_subset;
7955 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7956 __isl_keep isl_set *set2)
7958 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7961 /* Is "bmap" obviously equal to the universe with the same space?
7963 * That is, does it not have any constraints?
7965 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
7967 if (!bmap)
7968 return isl_bool_error;
7969 return bmap->n_eq == 0 && bmap->n_ineq == 0;
7972 /* Is "bset" obviously equal to the universe with the same space?
7974 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
7976 return isl_basic_map_plain_is_universe(bset);
7979 /* If "c" does not involve any existentially quantified variables,
7980 * then set *univ to false and abort
7982 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
7984 isl_bool *univ = user;
7985 unsigned n;
7987 n = isl_constraint_dim(c, isl_dim_div);
7988 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
7989 isl_constraint_free(c);
7990 if (*univ < 0 || !*univ)
7991 return isl_stat_error;
7992 return isl_stat_ok;
7995 /* Is "bmap" equal to the universe with the same space?
7997 * First check if it is obviously equal to the universe.
7998 * If not and if there are any constraints not involving
7999 * existentially quantified variables, then it is certainly
8000 * not equal to the universe.
8001 * Otherwise, check if the universe is a subset of "bmap".
8003 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8005 isl_bool univ;
8006 isl_basic_map *test;
8008 univ = isl_basic_map_plain_is_universe(bmap);
8009 if (univ < 0 || univ)
8010 return univ;
8011 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8012 return isl_bool_false;
8013 univ = isl_bool_true;
8014 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8015 univ)
8016 return isl_bool_error;
8017 if (univ < 0 || !univ)
8018 return univ;
8019 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8020 univ = isl_basic_map_is_subset(test, bmap);
8021 isl_basic_map_free(test);
8022 return univ;
8025 /* Is "bset" equal to the universe with the same space?
8027 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8029 return isl_basic_map_is_universe(bset);
8032 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8034 int i;
8036 if (!map)
8037 return isl_bool_error;
8039 for (i = 0; i < map->n; ++i) {
8040 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8041 if (r < 0 || r)
8042 return r;
8045 return isl_bool_false;
8048 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8050 return isl_map_plain_is_universe((isl_map *) set);
8053 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8055 struct isl_basic_set *bset = NULL;
8056 struct isl_vec *sample = NULL;
8057 isl_bool empty, non_empty;
8059 if (!bmap)
8060 return isl_bool_error;
8062 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8063 return isl_bool_true;
8065 if (isl_basic_map_plain_is_universe(bmap))
8066 return isl_bool_false;
8068 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8069 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8070 copy = isl_basic_map_remove_redundancies(copy);
8071 empty = isl_basic_map_plain_is_empty(copy);
8072 isl_basic_map_free(copy);
8073 return empty;
8076 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8077 if (non_empty < 0)
8078 return isl_bool_error;
8079 if (non_empty)
8080 return isl_bool_false;
8081 isl_vec_free(bmap->sample);
8082 bmap->sample = NULL;
8083 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8084 if (!bset)
8085 return isl_bool_error;
8086 sample = isl_basic_set_sample_vec(bset);
8087 if (!sample)
8088 return isl_bool_error;
8089 empty = sample->size == 0;
8090 isl_vec_free(bmap->sample);
8091 bmap->sample = sample;
8092 if (empty)
8093 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8095 return empty;
8098 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8100 if (!bmap)
8101 return isl_bool_error;
8102 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8105 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8107 if (!bset)
8108 return isl_bool_error;
8109 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8112 /* Is "bmap" known to be non-empty?
8114 * That is, is the cached sample still valid?
8116 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8118 unsigned total;
8120 if (!bmap)
8121 return isl_bool_error;
8122 if (!bmap->sample)
8123 return isl_bool_false;
8124 total = 1 + isl_basic_map_total_dim(bmap);
8125 if (bmap->sample->size != total)
8126 return isl_bool_false;
8127 return isl_basic_map_contains(bmap, bmap->sample);
8130 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8132 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8135 struct isl_map *isl_basic_map_union(
8136 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8138 struct isl_map *map;
8139 if (!bmap1 || !bmap2)
8140 goto error;
8142 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8144 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8145 if (!map)
8146 goto error;
8147 map = isl_map_add_basic_map(map, bmap1);
8148 map = isl_map_add_basic_map(map, bmap2);
8149 return map;
8150 error:
8151 isl_basic_map_free(bmap1);
8152 isl_basic_map_free(bmap2);
8153 return NULL;
8156 struct isl_set *isl_basic_set_union(
8157 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8159 return (struct isl_set *)isl_basic_map_union(
8160 (struct isl_basic_map *)bset1,
8161 (struct isl_basic_map *)bset2);
8164 /* Order divs such that any div only depends on previous divs */
8165 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8167 int i;
8168 unsigned off;
8170 if (!bmap)
8171 return NULL;
8173 off = isl_space_dim(bmap->dim, isl_dim_all);
8175 for (i = 0; i < bmap->n_div; ++i) {
8176 int pos;
8177 if (isl_int_is_zero(bmap->div[i][0]))
8178 continue;
8179 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8180 bmap->n_div-i);
8181 if (pos == -1)
8182 continue;
8183 if (pos == 0)
8184 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8185 "integer division depends on itself",
8186 return isl_basic_map_free(bmap));
8187 isl_basic_map_swap_div(bmap, i, i + pos);
8188 --i;
8190 return bmap;
8193 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8195 return (struct isl_basic_set *)
8196 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8199 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8201 int i;
8203 if (!map)
8204 return 0;
8206 for (i = 0; i < map->n; ++i) {
8207 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8208 if (!map->p[i])
8209 goto error;
8212 return map;
8213 error:
8214 isl_map_free(map);
8215 return NULL;
8218 /* Apply the expansion computed by isl_merge_divs.
8219 * The expansion itself is given by "exp" while the resulting
8220 * list of divs is given by "div".
8222 * Move the integer divisions of "bmap" into the right position
8223 * according to "exp" and then introduce the additional integer
8224 * divisions, adding div constraints.
8225 * The moving should be done first to avoid moving coefficients
8226 * in the definitions of the extra integer divisions.
8228 __isl_give isl_basic_map *isl_basic_map_expand_divs(
8229 __isl_take isl_basic_set *bmap, __isl_take isl_mat *div, int *exp)
8231 int i, j;
8232 int n_div;
8234 bmap = isl_basic_map_cow(bmap);
8235 if (!bmap || !div)
8236 goto error;
8238 if (div->n_row < bmap->n_div)
8239 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8240 "not an expansion", goto error);
8242 n_div = bmap->n_div;
8243 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
8244 div->n_row - n_div, 0,
8245 2 * (div->n_row - n_div));
8247 for (i = n_div; i < div->n_row; ++i)
8248 if (isl_basic_map_alloc_div(bmap) < 0)
8249 goto error;
8251 for (j = n_div - 1; j >= 0; --j) {
8252 if (exp[j] == j)
8253 break;
8254 isl_basic_map_swap_div(bmap, j, exp[j]);
8256 j = 0;
8257 for (i = 0; i < div->n_row; ++i) {
8258 if (j < n_div && exp[j] == i) {
8259 j++;
8260 } else {
8261 isl_seq_cpy(bmap->div[i], div->row[i], div->n_col);
8262 if (isl_basic_map_div_is_marked_unknown(bmap, i))
8263 continue;
8264 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
8265 goto error;
8269 isl_mat_free(div);
8270 return bmap;
8271 error:
8272 isl_basic_map_free(bmap);
8273 isl_mat_free(div);
8274 return NULL;
8277 /* Apply the expansion computed by isl_merge_divs.
8278 * The expansion itself is given by "exp" while the resulting
8279 * list of divs is given by "div".
8281 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8282 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8284 return isl_basic_map_expand_divs(bset, div, exp);
8287 /* Look for a div in dst that corresponds to the div "div" in src.
8288 * The divs before "div" in src and dst are assumed to be the same.
8290 * Returns -1 if no corresponding div was found and the position
8291 * of the corresponding div in dst otherwise.
8293 static int find_div(struct isl_basic_map *dst,
8294 struct isl_basic_map *src, unsigned div)
8296 int i;
8298 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8300 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8301 for (i = div; i < dst->n_div; ++i)
8302 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8303 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8304 dst->n_div - div) == -1)
8305 return i;
8306 return -1;
8309 /* Align the divs of "dst" to those of "src", adding divs from "src"
8310 * if needed. That is, make sure that the first src->n_div divs
8311 * of the result are equal to those of src.
8313 * The result is not finalized as by design it will have redundant
8314 * divs if any divs from "src" were copied.
8316 __isl_give isl_basic_map *isl_basic_map_align_divs(
8317 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8319 int i;
8320 int known, extended;
8321 unsigned total;
8323 if (!dst || !src)
8324 return isl_basic_map_free(dst);
8326 if (src->n_div == 0)
8327 return dst;
8329 known = isl_basic_map_divs_known(src);
8330 if (known < 0)
8331 return isl_basic_map_free(dst);
8332 if (!known)
8333 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8334 "some src divs are unknown",
8335 return isl_basic_map_free(dst));
8337 src = isl_basic_map_order_divs(src);
8339 extended = 0;
8340 total = isl_space_dim(src->dim, isl_dim_all);
8341 for (i = 0; i < src->n_div; ++i) {
8342 int j = find_div(dst, src, i);
8343 if (j < 0) {
8344 if (!extended) {
8345 int extra = src->n_div - i;
8346 dst = isl_basic_map_cow(dst);
8347 if (!dst)
8348 return NULL;
8349 dst = isl_basic_map_extend_space(dst,
8350 isl_space_copy(dst->dim),
8351 extra, 0, 2 * extra);
8352 extended = 1;
8354 j = isl_basic_map_alloc_div(dst);
8355 if (j < 0)
8356 return isl_basic_map_free(dst);
8357 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8358 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8359 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8360 return isl_basic_map_free(dst);
8362 if (j != i)
8363 isl_basic_map_swap_div(dst, i, j);
8365 return dst;
8368 struct isl_basic_set *isl_basic_set_align_divs(
8369 struct isl_basic_set *dst, struct isl_basic_set *src)
8371 return (struct isl_basic_set *)isl_basic_map_align_divs(
8372 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8375 struct isl_map *isl_map_align_divs(struct isl_map *map)
8377 int i;
8379 if (!map)
8380 return NULL;
8381 if (map->n == 0)
8382 return map;
8383 map = isl_map_compute_divs(map);
8384 map = isl_map_cow(map);
8385 if (!map)
8386 return NULL;
8388 for (i = 1; i < map->n; ++i)
8389 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8390 for (i = 1; i < map->n; ++i) {
8391 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8392 if (!map->p[i])
8393 return isl_map_free(map);
8396 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8397 return map;
8400 struct isl_set *isl_set_align_divs(struct isl_set *set)
8402 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8405 /* Align the divs of the basic maps in "map" to those
8406 * of the basic maps in "list", as well as to the other basic maps in "map".
8407 * The elements in "list" are assumed to have known divs.
8409 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8410 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8412 int i, n;
8414 map = isl_map_compute_divs(map);
8415 map = isl_map_cow(map);
8416 if (!map || !list)
8417 return isl_map_free(map);
8418 if (map->n == 0)
8419 return map;
8421 n = isl_basic_map_list_n_basic_map(list);
8422 for (i = 0; i < n; ++i) {
8423 isl_basic_map *bmap;
8425 bmap = isl_basic_map_list_get_basic_map(list, i);
8426 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8427 isl_basic_map_free(bmap);
8429 if (!map->p[0])
8430 return isl_map_free(map);
8432 return isl_map_align_divs(map);
8435 /* Align the divs of each element of "list" to those of "bmap".
8436 * Both "bmap" and the elements of "list" are assumed to have known divs.
8438 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8439 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8441 int i, n;
8443 if (!list || !bmap)
8444 return isl_basic_map_list_free(list);
8446 n = isl_basic_map_list_n_basic_map(list);
8447 for (i = 0; i < n; ++i) {
8448 isl_basic_map *bmap_i;
8450 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8451 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8452 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8455 return list;
8458 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8459 __isl_take isl_map *map)
8461 if (!set || !map)
8462 goto error;
8463 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8464 map = isl_map_intersect_domain(map, set);
8465 set = isl_map_range(map);
8466 return set;
8467 error:
8468 isl_set_free(set);
8469 isl_map_free(map);
8470 return NULL;
8473 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8474 __isl_take isl_map *map)
8476 return isl_map_align_params_map_map_and(set, map, &set_apply);
8479 /* There is no need to cow as removing empty parts doesn't change
8480 * the meaning of the set.
8482 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8484 int i;
8486 if (!map)
8487 return NULL;
8489 for (i = map->n - 1; i >= 0; --i)
8490 remove_if_empty(map, i);
8492 return map;
8495 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8497 return (struct isl_set *)
8498 isl_map_remove_empty_parts((struct isl_map *)set);
8501 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8503 struct isl_basic_map *bmap;
8504 if (!map || map->n == 0)
8505 return NULL;
8506 bmap = map->p[map->n-1];
8507 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8508 return isl_basic_map_copy(bmap);
8511 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8513 return (struct isl_basic_set *)
8514 isl_map_copy_basic_map((struct isl_map *)set);
8517 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8518 __isl_keep isl_basic_map *bmap)
8520 int i;
8522 if (!map || !bmap)
8523 goto error;
8524 for (i = map->n-1; i >= 0; --i) {
8525 if (map->p[i] != bmap)
8526 continue;
8527 map = isl_map_cow(map);
8528 if (!map)
8529 goto error;
8530 isl_basic_map_free(map->p[i]);
8531 if (i != map->n-1) {
8532 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8533 map->p[i] = map->p[map->n-1];
8535 map->n--;
8536 return map;
8538 return map;
8539 error:
8540 isl_map_free(map);
8541 return NULL;
8544 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8545 struct isl_basic_set *bset)
8547 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8548 (struct isl_basic_map *)bset);
8551 /* Given two basic sets bset1 and bset2, compute the maximal difference
8552 * between the values of dimension pos in bset1 and those in bset2
8553 * for any common value of the parameters and dimensions preceding pos.
8555 static enum isl_lp_result basic_set_maximal_difference_at(
8556 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8557 int pos, isl_int *opt)
8559 isl_space *dims;
8560 struct isl_basic_map *bmap1 = NULL;
8561 struct isl_basic_map *bmap2 = NULL;
8562 struct isl_ctx *ctx;
8563 struct isl_vec *obj;
8564 unsigned total;
8565 unsigned nparam;
8566 unsigned dim1, dim2;
8567 enum isl_lp_result res;
8569 if (!bset1 || !bset2)
8570 return isl_lp_error;
8572 nparam = isl_basic_set_n_param(bset1);
8573 dim1 = isl_basic_set_n_dim(bset1);
8574 dim2 = isl_basic_set_n_dim(bset2);
8575 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8576 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8577 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8578 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8579 if (!bmap1 || !bmap2)
8580 goto error;
8581 bmap1 = isl_basic_map_cow(bmap1);
8582 bmap1 = isl_basic_map_extend(bmap1, nparam,
8583 pos, (dim1 - pos) + (dim2 - pos),
8584 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8585 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8586 if (!bmap1)
8587 goto error2;
8588 total = isl_basic_map_total_dim(bmap1);
8589 ctx = bmap1->ctx;
8590 obj = isl_vec_alloc(ctx, 1 + total);
8591 if (!obj)
8592 goto error2;
8593 isl_seq_clr(obj->block.data, 1 + total);
8594 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8595 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8596 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8597 opt, NULL, NULL);
8598 isl_basic_map_free(bmap1);
8599 isl_vec_free(obj);
8600 return res;
8601 error:
8602 isl_basic_map_free(bmap2);
8603 error2:
8604 isl_basic_map_free(bmap1);
8605 return isl_lp_error;
8608 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8609 * for any common value of the parameters and dimensions preceding pos
8610 * in both basic sets, the values of dimension pos in bset1 are
8611 * smaller or larger than those in bset2.
8613 * Returns
8614 * 1 if bset1 follows bset2
8615 * -1 if bset1 precedes bset2
8616 * 0 if bset1 and bset2 are incomparable
8617 * -2 if some error occurred.
8619 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8620 struct isl_basic_set *bset2, int pos)
8622 isl_int opt;
8623 enum isl_lp_result res;
8624 int cmp;
8626 isl_int_init(opt);
8628 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8630 if (res == isl_lp_empty)
8631 cmp = 0;
8632 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8633 res == isl_lp_unbounded)
8634 cmp = 1;
8635 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8636 cmp = -1;
8637 else
8638 cmp = -2;
8640 isl_int_clear(opt);
8641 return cmp;
8644 /* Given two basic sets bset1 and bset2, check whether
8645 * for any common value of the parameters and dimensions preceding pos
8646 * there is a value of dimension pos in bset1 that is larger
8647 * than a value of the same dimension in bset2.
8649 * Return
8650 * 1 if there exists such a pair
8651 * 0 if there is no such pair, but there is a pair of equal values
8652 * -1 otherwise
8653 * -2 if some error occurred.
8655 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8656 __isl_keep isl_basic_set *bset2, int pos)
8658 isl_int opt;
8659 enum isl_lp_result res;
8660 int cmp;
8662 isl_int_init(opt);
8664 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8666 if (res == isl_lp_empty)
8667 cmp = -1;
8668 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8669 res == isl_lp_unbounded)
8670 cmp = 1;
8671 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8672 cmp = -1;
8673 else if (res == isl_lp_ok)
8674 cmp = 0;
8675 else
8676 cmp = -2;
8678 isl_int_clear(opt);
8679 return cmp;
8682 /* Given two sets set1 and set2, check whether
8683 * for any common value of the parameters and dimensions preceding pos
8684 * there is a value of dimension pos in set1 that is larger
8685 * than a value of the same dimension in set2.
8687 * Return
8688 * 1 if there exists such a pair
8689 * 0 if there is no such pair, but there is a pair of equal values
8690 * -1 otherwise
8691 * -2 if some error occurred.
8693 int isl_set_follows_at(__isl_keep isl_set *set1,
8694 __isl_keep isl_set *set2, int pos)
8696 int i, j;
8697 int follows = -1;
8699 if (!set1 || !set2)
8700 return -2;
8702 for (i = 0; i < set1->n; ++i)
8703 for (j = 0; j < set2->n; ++j) {
8704 int f;
8705 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8706 if (f == 1 || f == -2)
8707 return f;
8708 if (f > follows)
8709 follows = f;
8712 return follows;
8715 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8716 unsigned pos, isl_int *val)
8718 int i;
8719 int d;
8720 unsigned total;
8722 if (!bmap)
8723 return -1;
8724 total = isl_basic_map_total_dim(bmap);
8725 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8726 for (; d+1 > pos; --d)
8727 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8728 break;
8729 if (d != pos)
8730 continue;
8731 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8732 return 0;
8733 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8734 return 0;
8735 if (!isl_int_is_one(bmap->eq[i][1+d]))
8736 return 0;
8737 if (val)
8738 isl_int_neg(*val, bmap->eq[i][0]);
8739 return 1;
8741 return 0;
8744 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8745 unsigned pos, isl_int *val)
8747 int i;
8748 isl_int v;
8749 isl_int tmp;
8750 int fixed;
8752 if (!map)
8753 return -1;
8754 if (map->n == 0)
8755 return 0;
8756 if (map->n == 1)
8757 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8758 isl_int_init(v);
8759 isl_int_init(tmp);
8760 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8761 for (i = 1; fixed == 1 && i < map->n; ++i) {
8762 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8763 if (fixed == 1 && isl_int_ne(tmp, v))
8764 fixed = 0;
8766 if (val)
8767 isl_int_set(*val, v);
8768 isl_int_clear(tmp);
8769 isl_int_clear(v);
8770 return fixed;
8773 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8774 unsigned pos, isl_int *val)
8776 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8777 pos, val);
8780 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8781 isl_int *val)
8783 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8786 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8787 enum isl_dim_type type, unsigned pos, isl_int *val)
8789 if (pos >= isl_basic_map_dim(bmap, type))
8790 return -1;
8791 return isl_basic_map_plain_has_fixed_var(bmap,
8792 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8795 /* If "bmap" obviously lies on a hyperplane where the given dimension
8796 * has a fixed value, then return that value.
8797 * Otherwise return NaN.
8799 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8800 __isl_keep isl_basic_map *bmap,
8801 enum isl_dim_type type, unsigned pos)
8803 isl_ctx *ctx;
8804 isl_val *v;
8805 int fixed;
8807 if (!bmap)
8808 return NULL;
8809 ctx = isl_basic_map_get_ctx(bmap);
8810 v = isl_val_alloc(ctx);
8811 if (!v)
8812 return NULL;
8813 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8814 if (fixed < 0)
8815 return isl_val_free(v);
8816 if (fixed) {
8817 isl_int_set_si(v->d, 1);
8818 return v;
8820 isl_val_free(v);
8821 return isl_val_nan(ctx);
8824 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8825 enum isl_dim_type type, unsigned pos, isl_int *val)
8827 if (pos >= isl_map_dim(map, type))
8828 return -1;
8829 return isl_map_plain_has_fixed_var(map,
8830 map_offset(map, type) - 1 + pos, val);
8833 /* If "map" obviously lies on a hyperplane where the given dimension
8834 * has a fixed value, then return that value.
8835 * Otherwise return NaN.
8837 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8838 enum isl_dim_type type, unsigned pos)
8840 isl_ctx *ctx;
8841 isl_val *v;
8842 int fixed;
8844 if (!map)
8845 return NULL;
8846 ctx = isl_map_get_ctx(map);
8847 v = isl_val_alloc(ctx);
8848 if (!v)
8849 return NULL;
8850 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8851 if (fixed < 0)
8852 return isl_val_free(v);
8853 if (fixed) {
8854 isl_int_set_si(v->d, 1);
8855 return v;
8857 isl_val_free(v);
8858 return isl_val_nan(ctx);
8861 /* If "set" obviously lies on a hyperplane where the given dimension
8862 * has a fixed value, then return that value.
8863 * Otherwise return NaN.
8865 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8866 enum isl_dim_type type, unsigned pos)
8868 return isl_map_plain_get_val_if_fixed(set, type, pos);
8871 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8872 enum isl_dim_type type, unsigned pos, isl_int *val)
8874 return isl_map_plain_is_fixed(set, type, pos, val);
8877 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8878 * then return this fixed value in *val.
8880 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8881 unsigned dim, isl_int *val)
8883 return isl_basic_set_plain_has_fixed_var(bset,
8884 isl_basic_set_n_param(bset) + dim, val);
8887 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8888 * then return this fixed value in *val.
8890 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8891 unsigned dim, isl_int *val)
8893 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8896 /* Check if input variable in has fixed value and if so and if val is not NULL,
8897 * then return this fixed value in *val.
8899 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8900 unsigned in, isl_int *val)
8902 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8905 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8906 * and if val is not NULL, then return this lower bound in *val.
8908 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8909 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8911 int i, i_eq = -1, i_ineq = -1;
8912 isl_int *c;
8913 unsigned total;
8914 unsigned nparam;
8916 if (!bset)
8917 return -1;
8918 total = isl_basic_set_total_dim(bset);
8919 nparam = isl_basic_set_n_param(bset);
8920 for (i = 0; i < bset->n_eq; ++i) {
8921 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8922 continue;
8923 if (i_eq != -1)
8924 return 0;
8925 i_eq = i;
8927 for (i = 0; i < bset->n_ineq; ++i) {
8928 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8929 continue;
8930 if (i_eq != -1 || i_ineq != -1)
8931 return 0;
8932 i_ineq = i;
8934 if (i_eq == -1 && i_ineq == -1)
8935 return 0;
8936 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8937 /* The coefficient should always be one due to normalization. */
8938 if (!isl_int_is_one(c[1+nparam+dim]))
8939 return 0;
8940 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8941 return 0;
8942 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8943 total - nparam - dim - 1) != -1)
8944 return 0;
8945 if (val)
8946 isl_int_neg(*val, c[0]);
8947 return 1;
8950 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8951 unsigned dim, isl_int *val)
8953 int i;
8954 isl_int v;
8955 isl_int tmp;
8956 int fixed;
8958 if (!set)
8959 return -1;
8960 if (set->n == 0)
8961 return 0;
8962 if (set->n == 1)
8963 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8964 dim, val);
8965 isl_int_init(v);
8966 isl_int_init(tmp);
8967 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8968 dim, &v);
8969 for (i = 1; fixed == 1 && i < set->n; ++i) {
8970 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8971 dim, &tmp);
8972 if (fixed == 1 && isl_int_ne(tmp, v))
8973 fixed = 0;
8975 if (val)
8976 isl_int_set(*val, v);
8977 isl_int_clear(tmp);
8978 isl_int_clear(v);
8979 return fixed;
8982 /* Return -1 if the constraint "c1" should be sorted before "c2"
8983 * and 1 if it should be sorted after "c2".
8984 * Return 0 if the two constraints are the same (up to the constant term).
8986 * In particular, if a constraint involves later variables than another
8987 * then it is sorted after this other constraint.
8988 * uset_gist depends on constraints without existentially quantified
8989 * variables sorting first.
8991 * For constraints that have the same latest variable, those
8992 * with the same coefficient for this latest variable (first in absolute value
8993 * and then in actual value) are grouped together.
8994 * This is useful for detecting pairs of constraints that can
8995 * be chained in their printed representation.
8997 * Finally, within a group, constraints are sorted according to
8998 * their coefficients (excluding the constant term).
9000 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9002 isl_int **c1 = (isl_int **) p1;
9003 isl_int **c2 = (isl_int **) p2;
9004 int l1, l2;
9005 unsigned size = *(unsigned *) arg;
9006 int cmp;
9008 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9009 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9011 if (l1 != l2)
9012 return l1 - l2;
9014 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9015 if (cmp != 0)
9016 return cmp;
9017 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9018 if (cmp != 0)
9019 return -cmp;
9021 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9024 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9025 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9026 * and 0 if the two constraints are the same (up to the constant term).
9028 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9029 isl_int *c1, isl_int *c2)
9031 unsigned total;
9033 if (!bmap)
9034 return -2;
9035 total = isl_basic_map_total_dim(bmap);
9036 return sort_constraint_cmp(&c1, &c2, &total);
9039 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9040 __isl_take isl_basic_map *bmap)
9042 unsigned total;
9044 if (!bmap)
9045 return NULL;
9046 if (bmap->n_ineq == 0)
9047 return bmap;
9048 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9049 return bmap;
9050 total = isl_basic_map_total_dim(bmap);
9051 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9052 &sort_constraint_cmp, &total) < 0)
9053 return isl_basic_map_free(bmap);
9054 return bmap;
9057 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9058 __isl_take isl_basic_set *bset)
9060 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9061 (struct isl_basic_map *)bset);
9064 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9066 if (!bmap)
9067 return NULL;
9068 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9069 return bmap;
9070 bmap = isl_basic_map_remove_redundancies(bmap);
9071 bmap = isl_basic_map_sort_constraints(bmap);
9072 if (bmap)
9073 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9074 return bmap;
9077 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9079 return (struct isl_basic_set *)isl_basic_map_normalize(
9080 (struct isl_basic_map *)bset);
9083 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9084 const __isl_keep isl_basic_map *bmap2)
9086 int i, cmp;
9087 unsigned total;
9089 if (!bmap1 || !bmap2)
9090 return -1;
9092 if (bmap1 == bmap2)
9093 return 0;
9094 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9095 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9096 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9097 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9098 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9099 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9100 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9101 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9102 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9103 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9104 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9105 return 0;
9106 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9107 return 1;
9108 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9109 return -1;
9110 if (bmap1->n_eq != bmap2->n_eq)
9111 return bmap1->n_eq - bmap2->n_eq;
9112 if (bmap1->n_ineq != bmap2->n_ineq)
9113 return bmap1->n_ineq - bmap2->n_ineq;
9114 if (bmap1->n_div != bmap2->n_div)
9115 return bmap1->n_div - bmap2->n_div;
9116 total = isl_basic_map_total_dim(bmap1);
9117 for (i = 0; i < bmap1->n_eq; ++i) {
9118 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9119 if (cmp)
9120 return cmp;
9122 for (i = 0; i < bmap1->n_ineq; ++i) {
9123 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9124 if (cmp)
9125 return cmp;
9127 for (i = 0; i < bmap1->n_div; ++i) {
9128 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9129 if (cmp)
9130 return cmp;
9132 return 0;
9135 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9136 const __isl_keep isl_basic_set *bset2)
9138 return isl_basic_map_plain_cmp(bset1, bset2);
9141 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9143 int i, cmp;
9145 if (set1 == set2)
9146 return 0;
9147 if (set1->n != set2->n)
9148 return set1->n - set2->n;
9150 for (i = 0; i < set1->n; ++i) {
9151 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9152 if (cmp)
9153 return cmp;
9156 return 0;
9159 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9160 __isl_keep isl_basic_map *bmap2)
9162 if (!bmap1 || !bmap2)
9163 return isl_bool_error;
9164 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9167 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9168 __isl_keep isl_basic_set *bset2)
9170 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9171 (isl_basic_map *)bset2);
9174 static int qsort_bmap_cmp(const void *p1, const void *p2)
9176 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9177 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9179 return isl_basic_map_plain_cmp(bmap1, bmap2);
9182 /* Sort the basic maps of "map" and remove duplicate basic maps.
9184 * While removing basic maps, we make sure that the basic maps remain
9185 * sorted because isl_map_normalize expects the basic maps of the result
9186 * to be sorted.
9188 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9190 int i, j;
9192 map = isl_map_remove_empty_parts(map);
9193 if (!map)
9194 return NULL;
9195 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9196 for (i = map->n - 1; i >= 1; --i) {
9197 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9198 continue;
9199 isl_basic_map_free(map->p[i-1]);
9200 for (j = i; j < map->n; ++j)
9201 map->p[j - 1] = map->p[j];
9202 map->n--;
9205 return map;
9208 /* Remove obvious duplicates among the basic maps of "map".
9210 * Unlike isl_map_normalize, this function does not remove redundant
9211 * constraints and only removes duplicates that have exactly the same
9212 * constraints in the input. It does sort the constraints and
9213 * the basic maps to ease the detection of duplicates.
9215 * If "map" has already been normalized or if the basic maps are
9216 * disjoint, then there can be no duplicates.
9218 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9220 int i;
9221 isl_basic_map *bmap;
9223 if (!map)
9224 return NULL;
9225 if (map->n <= 1)
9226 return map;
9227 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9228 return map;
9229 for (i = 0; i < map->n; ++i) {
9230 bmap = isl_basic_map_copy(map->p[i]);
9231 bmap = isl_basic_map_sort_constraints(bmap);
9232 if (!bmap)
9233 return isl_map_free(map);
9234 isl_basic_map_free(map->p[i]);
9235 map->p[i] = bmap;
9238 map = sort_and_remove_duplicates(map);
9239 return map;
9242 /* We normalize in place, but if anything goes wrong we need
9243 * to return NULL, so we need to make sure we don't change the
9244 * meaning of any possible other copies of map.
9246 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9248 int i;
9249 struct isl_basic_map *bmap;
9251 if (!map)
9252 return NULL;
9253 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9254 return map;
9255 for (i = 0; i < map->n; ++i) {
9256 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9257 if (!bmap)
9258 goto error;
9259 isl_basic_map_free(map->p[i]);
9260 map->p[i] = bmap;
9263 map = sort_and_remove_duplicates(map);
9264 if (map)
9265 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9266 return map;
9267 error:
9268 isl_map_free(map);
9269 return NULL;
9272 struct isl_set *isl_set_normalize(struct isl_set *set)
9274 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9277 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9278 __isl_keep isl_map *map2)
9280 int i;
9281 isl_bool equal;
9283 if (!map1 || !map2)
9284 return isl_bool_error;
9286 if (map1 == map2)
9287 return isl_bool_true;
9288 if (!isl_space_is_equal(map1->dim, map2->dim))
9289 return isl_bool_false;
9291 map1 = isl_map_copy(map1);
9292 map2 = isl_map_copy(map2);
9293 map1 = isl_map_normalize(map1);
9294 map2 = isl_map_normalize(map2);
9295 if (!map1 || !map2)
9296 goto error;
9297 equal = map1->n == map2->n;
9298 for (i = 0; equal && i < map1->n; ++i) {
9299 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9300 if (equal < 0)
9301 goto error;
9303 isl_map_free(map1);
9304 isl_map_free(map2);
9305 return equal;
9306 error:
9307 isl_map_free(map1);
9308 isl_map_free(map2);
9309 return isl_bool_error;
9312 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9313 __isl_keep isl_set *set2)
9315 return isl_map_plain_is_equal((struct isl_map *)set1,
9316 (struct isl_map *)set2);
9319 /* Return an interval that ranges from min to max (inclusive)
9321 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9322 isl_int min, isl_int max)
9324 int k;
9325 struct isl_basic_set *bset = NULL;
9327 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9328 if (!bset)
9329 goto error;
9331 k = isl_basic_set_alloc_inequality(bset);
9332 if (k < 0)
9333 goto error;
9334 isl_int_set_si(bset->ineq[k][1], 1);
9335 isl_int_neg(bset->ineq[k][0], min);
9337 k = isl_basic_set_alloc_inequality(bset);
9338 if (k < 0)
9339 goto error;
9340 isl_int_set_si(bset->ineq[k][1], -1);
9341 isl_int_set(bset->ineq[k][0], max);
9343 return bset;
9344 error:
9345 isl_basic_set_free(bset);
9346 return NULL;
9349 /* Return the basic maps in "map" as a list.
9351 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9352 __isl_keep isl_map *map)
9354 int i;
9355 isl_ctx *ctx;
9356 isl_basic_map_list *list;
9358 if (!map)
9359 return NULL;
9360 ctx = isl_map_get_ctx(map);
9361 list = isl_basic_map_list_alloc(ctx, map->n);
9363 for (i = 0; i < map->n; ++i) {
9364 isl_basic_map *bmap;
9366 bmap = isl_basic_map_copy(map->p[i]);
9367 list = isl_basic_map_list_add(list, bmap);
9370 return list;
9373 /* Return the intersection of the elements in the non-empty list "list".
9374 * All elements are assumed to live in the same space.
9376 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9377 __isl_take isl_basic_map_list *list)
9379 int i, n;
9380 isl_basic_map *bmap;
9382 if (!list)
9383 return NULL;
9384 n = isl_basic_map_list_n_basic_map(list);
9385 if (n < 1)
9386 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9387 "expecting non-empty list", goto error);
9389 bmap = isl_basic_map_list_get_basic_map(list, 0);
9390 for (i = 1; i < n; ++i) {
9391 isl_basic_map *bmap_i;
9393 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9394 bmap = isl_basic_map_intersect(bmap, bmap_i);
9397 isl_basic_map_list_free(list);
9398 return bmap;
9399 error:
9400 isl_basic_map_list_free(list);
9401 return NULL;
9404 /* Return the intersection of the elements in the non-empty list "list".
9405 * All elements are assumed to live in the same space.
9407 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9408 __isl_take isl_basic_set_list *list)
9410 return isl_basic_map_list_intersect(list);
9413 /* Return the union of the elements in the non-empty list "list".
9414 * All elements are assumed to live in the same space.
9416 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9418 int i, n;
9419 isl_set *set;
9421 if (!list)
9422 return NULL;
9423 n = isl_set_list_n_set(list);
9424 if (n < 1)
9425 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9426 "expecting non-empty list", goto error);
9428 set = isl_set_list_get_set(list, 0);
9429 for (i = 1; i < n; ++i) {
9430 isl_set *set_i;
9432 set_i = isl_set_list_get_set(list, i);
9433 set = isl_set_union(set, set_i);
9436 isl_set_list_free(list);
9437 return set;
9438 error:
9439 isl_set_list_free(list);
9440 return NULL;
9443 /* Return the Cartesian product of the basic sets in list (in the given order).
9445 __isl_give isl_basic_set *isl_basic_set_list_product(
9446 __isl_take struct isl_basic_set_list *list)
9448 int i;
9449 unsigned dim;
9450 unsigned nparam;
9451 unsigned extra;
9452 unsigned n_eq;
9453 unsigned n_ineq;
9454 struct isl_basic_set *product = NULL;
9456 if (!list)
9457 goto error;
9458 isl_assert(list->ctx, list->n > 0, goto error);
9459 isl_assert(list->ctx, list->p[0], goto error);
9460 nparam = isl_basic_set_n_param(list->p[0]);
9461 dim = isl_basic_set_n_dim(list->p[0]);
9462 extra = list->p[0]->n_div;
9463 n_eq = list->p[0]->n_eq;
9464 n_ineq = list->p[0]->n_ineq;
9465 for (i = 1; i < list->n; ++i) {
9466 isl_assert(list->ctx, list->p[i], goto error);
9467 isl_assert(list->ctx,
9468 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9469 dim += isl_basic_set_n_dim(list->p[i]);
9470 extra += list->p[i]->n_div;
9471 n_eq += list->p[i]->n_eq;
9472 n_ineq += list->p[i]->n_ineq;
9474 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9475 n_eq, n_ineq);
9476 if (!product)
9477 goto error;
9478 dim = 0;
9479 for (i = 0; i < list->n; ++i) {
9480 isl_basic_set_add_constraints(product,
9481 isl_basic_set_copy(list->p[i]), dim);
9482 dim += isl_basic_set_n_dim(list->p[i]);
9484 isl_basic_set_list_free(list);
9485 return product;
9486 error:
9487 isl_basic_set_free(product);
9488 isl_basic_set_list_free(list);
9489 return NULL;
9492 struct isl_basic_map *isl_basic_map_product(
9493 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9495 isl_space *dim_result = NULL;
9496 struct isl_basic_map *bmap;
9497 unsigned in1, in2, out1, out2, nparam, total, pos;
9498 struct isl_dim_map *dim_map1, *dim_map2;
9500 if (!bmap1 || !bmap2)
9501 goto error;
9503 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9504 bmap2->dim, isl_dim_param), goto error);
9505 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9506 isl_space_copy(bmap2->dim));
9508 in1 = isl_basic_map_n_in(bmap1);
9509 in2 = isl_basic_map_n_in(bmap2);
9510 out1 = isl_basic_map_n_out(bmap1);
9511 out2 = isl_basic_map_n_out(bmap2);
9512 nparam = isl_basic_map_n_param(bmap1);
9514 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9515 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9516 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9517 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9518 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9519 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9520 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9521 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9522 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9523 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9524 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9526 bmap = isl_basic_map_alloc_space(dim_result,
9527 bmap1->n_div + bmap2->n_div,
9528 bmap1->n_eq + bmap2->n_eq,
9529 bmap1->n_ineq + bmap2->n_ineq);
9530 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9531 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9532 bmap = isl_basic_map_simplify(bmap);
9533 return isl_basic_map_finalize(bmap);
9534 error:
9535 isl_basic_map_free(bmap1);
9536 isl_basic_map_free(bmap2);
9537 return NULL;
9540 __isl_give isl_basic_map *isl_basic_map_flat_product(
9541 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9543 isl_basic_map *prod;
9545 prod = isl_basic_map_product(bmap1, bmap2);
9546 prod = isl_basic_map_flatten(prod);
9547 return prod;
9550 __isl_give isl_basic_set *isl_basic_set_flat_product(
9551 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9553 return isl_basic_map_flat_range_product(bset1, bset2);
9556 __isl_give isl_basic_map *isl_basic_map_domain_product(
9557 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9559 isl_space *space_result = NULL;
9560 isl_basic_map *bmap;
9561 unsigned in1, in2, out, nparam, total, pos;
9562 struct isl_dim_map *dim_map1, *dim_map2;
9564 if (!bmap1 || !bmap2)
9565 goto error;
9567 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9568 isl_space_copy(bmap2->dim));
9570 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9571 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9572 out = isl_basic_map_dim(bmap1, isl_dim_out);
9573 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9575 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9576 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9577 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9578 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9579 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9580 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9581 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9582 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9583 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9584 isl_dim_map_div(dim_map1, bmap1, pos += out);
9585 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9587 bmap = isl_basic_map_alloc_space(space_result,
9588 bmap1->n_div + bmap2->n_div,
9589 bmap1->n_eq + bmap2->n_eq,
9590 bmap1->n_ineq + bmap2->n_ineq);
9591 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9592 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9593 bmap = isl_basic_map_simplify(bmap);
9594 return isl_basic_map_finalize(bmap);
9595 error:
9596 isl_basic_map_free(bmap1);
9597 isl_basic_map_free(bmap2);
9598 return NULL;
9601 __isl_give isl_basic_map *isl_basic_map_range_product(
9602 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9604 isl_space *dim_result = NULL;
9605 isl_basic_map *bmap;
9606 unsigned in, out1, out2, nparam, total, pos;
9607 struct isl_dim_map *dim_map1, *dim_map2;
9609 if (!bmap1 || !bmap2)
9610 goto error;
9612 if (!isl_space_match(bmap1->dim, isl_dim_param,
9613 bmap2->dim, isl_dim_param))
9614 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9615 "parameters don't match", goto error);
9617 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9618 isl_space_copy(bmap2->dim));
9620 in = isl_basic_map_dim(bmap1, isl_dim_in);
9621 out1 = isl_basic_map_n_out(bmap1);
9622 out2 = isl_basic_map_n_out(bmap2);
9623 nparam = isl_basic_map_n_param(bmap1);
9625 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9626 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9627 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9628 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9629 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9630 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9631 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9632 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9633 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9634 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9635 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9637 bmap = isl_basic_map_alloc_space(dim_result,
9638 bmap1->n_div + bmap2->n_div,
9639 bmap1->n_eq + bmap2->n_eq,
9640 bmap1->n_ineq + bmap2->n_ineq);
9641 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9642 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9643 bmap = isl_basic_map_simplify(bmap);
9644 return isl_basic_map_finalize(bmap);
9645 error:
9646 isl_basic_map_free(bmap1);
9647 isl_basic_map_free(bmap2);
9648 return NULL;
9651 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9652 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9654 isl_basic_map *prod;
9656 prod = isl_basic_map_range_product(bmap1, bmap2);
9657 prod = isl_basic_map_flatten_range(prod);
9658 return prod;
9661 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9662 * and collect the results.
9663 * The result live in the space obtained by calling "space_product"
9664 * on the spaces of "map1" and "map2".
9665 * If "remove_duplicates" is set then the result may contain duplicates
9666 * (even if the inputs do not) and so we try and remove the obvious
9667 * duplicates.
9669 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9670 __isl_take isl_map *map2,
9671 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9672 __isl_take isl_space *right),
9673 __isl_give isl_basic_map *(*basic_map_product)(
9674 __isl_take isl_basic_map *left,
9675 __isl_take isl_basic_map *right),
9676 int remove_duplicates)
9678 unsigned flags = 0;
9679 struct isl_map *result;
9680 int i, j;
9682 if (!map1 || !map2)
9683 goto error;
9685 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9686 map2->dim, isl_dim_param), goto error);
9688 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9689 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9690 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9692 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9693 isl_space_copy(map2->dim)),
9694 map1->n * map2->n, flags);
9695 if (!result)
9696 goto error;
9697 for (i = 0; i < map1->n; ++i)
9698 for (j = 0; j < map2->n; ++j) {
9699 struct isl_basic_map *part;
9700 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9701 isl_basic_map_copy(map2->p[j]));
9702 if (isl_basic_map_is_empty(part))
9703 isl_basic_map_free(part);
9704 else
9705 result = isl_map_add_basic_map(result, part);
9706 if (!result)
9707 goto error;
9709 if (remove_duplicates)
9710 result = isl_map_remove_obvious_duplicates(result);
9711 isl_map_free(map1);
9712 isl_map_free(map2);
9713 return result;
9714 error:
9715 isl_map_free(map1);
9716 isl_map_free(map2);
9717 return NULL;
9720 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9722 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9723 __isl_take isl_map *map2)
9725 return map_product(map1, map2, &isl_space_product,
9726 &isl_basic_map_product, 0);
9729 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9730 __isl_take isl_map *map2)
9732 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9735 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9737 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9738 __isl_take isl_map *map2)
9740 isl_map *prod;
9742 prod = isl_map_product(map1, map2);
9743 prod = isl_map_flatten(prod);
9744 return prod;
9747 /* Given two set A and B, construct its Cartesian product A x B.
9749 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9751 return isl_map_range_product(set1, set2);
9754 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9755 __isl_take isl_set *set2)
9757 return isl_map_flat_range_product(set1, set2);
9760 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9762 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9763 __isl_take isl_map *map2)
9765 return map_product(map1, map2, &isl_space_domain_product,
9766 &isl_basic_map_domain_product, 1);
9769 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9771 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9772 __isl_take isl_map *map2)
9774 return map_product(map1, map2, &isl_space_range_product,
9775 &isl_basic_map_range_product, 1);
9778 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9779 __isl_take isl_map *map2)
9781 return isl_map_align_params_map_map_and(map1, map2,
9782 &map_domain_product_aligned);
9785 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9786 __isl_take isl_map *map2)
9788 return isl_map_align_params_map_map_and(map1, map2,
9789 &map_range_product_aligned);
9792 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9794 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9796 isl_space *space;
9797 int total1, keep1, total2, keep2;
9799 if (!map)
9800 return NULL;
9801 if (!isl_space_domain_is_wrapping(map->dim) ||
9802 !isl_space_range_is_wrapping(map->dim))
9803 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9804 "not a product", return isl_map_free(map));
9806 space = isl_map_get_space(map);
9807 total1 = isl_space_dim(space, isl_dim_in);
9808 total2 = isl_space_dim(space, isl_dim_out);
9809 space = isl_space_factor_domain(space);
9810 keep1 = isl_space_dim(space, isl_dim_in);
9811 keep2 = isl_space_dim(space, isl_dim_out);
9812 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9813 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9814 map = isl_map_reset_space(map, space);
9816 return map;
9819 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9821 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9823 isl_space *space;
9824 int total1, keep1, total2, keep2;
9826 if (!map)
9827 return NULL;
9828 if (!isl_space_domain_is_wrapping(map->dim) ||
9829 !isl_space_range_is_wrapping(map->dim))
9830 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9831 "not a product", return isl_map_free(map));
9833 space = isl_map_get_space(map);
9834 total1 = isl_space_dim(space, isl_dim_in);
9835 total2 = isl_space_dim(space, isl_dim_out);
9836 space = isl_space_factor_range(space);
9837 keep1 = isl_space_dim(space, isl_dim_in);
9838 keep2 = isl_space_dim(space, isl_dim_out);
9839 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9840 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9841 map = isl_map_reset_space(map, space);
9843 return map;
9846 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9848 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9850 isl_space *space;
9851 int total, keep;
9853 if (!map)
9854 return NULL;
9855 if (!isl_space_domain_is_wrapping(map->dim))
9856 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9857 "domain is not a product", return isl_map_free(map));
9859 space = isl_map_get_space(map);
9860 total = isl_space_dim(space, isl_dim_in);
9861 space = isl_space_domain_factor_domain(space);
9862 keep = isl_space_dim(space, isl_dim_in);
9863 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9864 map = isl_map_reset_space(map, space);
9866 return map;
9869 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9871 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9873 isl_space *space;
9874 int total, keep;
9876 if (!map)
9877 return NULL;
9878 if (!isl_space_domain_is_wrapping(map->dim))
9879 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9880 "domain is not a product", return isl_map_free(map));
9882 space = isl_map_get_space(map);
9883 total = isl_space_dim(space, isl_dim_in);
9884 space = isl_space_domain_factor_range(space);
9885 keep = isl_space_dim(space, isl_dim_in);
9886 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9887 map = isl_map_reset_space(map, space);
9889 return map;
9892 /* Given a map A -> [B -> C], extract the map A -> B.
9894 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9896 isl_space *space;
9897 int total, keep;
9899 if (!map)
9900 return NULL;
9901 if (!isl_space_range_is_wrapping(map->dim))
9902 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9903 "range is not a product", return isl_map_free(map));
9905 space = isl_map_get_space(map);
9906 total = isl_space_dim(space, isl_dim_out);
9907 space = isl_space_range_factor_domain(space);
9908 keep = isl_space_dim(space, isl_dim_out);
9909 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9910 map = isl_map_reset_space(map, space);
9912 return map;
9915 /* Given a map A -> [B -> C], extract the map A -> C.
9917 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9919 isl_space *space;
9920 int total, keep;
9922 if (!map)
9923 return NULL;
9924 if (!isl_space_range_is_wrapping(map->dim))
9925 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9926 "range is not a product", return isl_map_free(map));
9928 space = isl_map_get_space(map);
9929 total = isl_space_dim(space, isl_dim_out);
9930 space = isl_space_range_factor_range(space);
9931 keep = isl_space_dim(space, isl_dim_out);
9932 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9933 map = isl_map_reset_space(map, space);
9935 return map;
9938 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9940 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9941 __isl_take isl_map *map2)
9943 isl_map *prod;
9945 prod = isl_map_domain_product(map1, map2);
9946 prod = isl_map_flatten_domain(prod);
9947 return prod;
9950 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9952 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9953 __isl_take isl_map *map2)
9955 isl_map *prod;
9957 prod = isl_map_range_product(map1, map2);
9958 prod = isl_map_flatten_range(prod);
9959 return prod;
9962 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9964 int i;
9965 uint32_t hash = isl_hash_init();
9966 unsigned total;
9968 if (!bmap)
9969 return 0;
9970 bmap = isl_basic_map_copy(bmap);
9971 bmap = isl_basic_map_normalize(bmap);
9972 if (!bmap)
9973 return 0;
9974 total = isl_basic_map_total_dim(bmap);
9975 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9976 for (i = 0; i < bmap->n_eq; ++i) {
9977 uint32_t c_hash;
9978 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9979 isl_hash_hash(hash, c_hash);
9981 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9982 for (i = 0; i < bmap->n_ineq; ++i) {
9983 uint32_t c_hash;
9984 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9985 isl_hash_hash(hash, c_hash);
9987 isl_hash_byte(hash, bmap->n_div & 0xFF);
9988 for (i = 0; i < bmap->n_div; ++i) {
9989 uint32_t c_hash;
9990 if (isl_int_is_zero(bmap->div[i][0]))
9991 continue;
9992 isl_hash_byte(hash, i & 0xFF);
9993 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9994 isl_hash_hash(hash, c_hash);
9996 isl_basic_map_free(bmap);
9997 return hash;
10000 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10002 return isl_basic_map_get_hash((isl_basic_map *)bset);
10005 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10007 int i;
10008 uint32_t hash;
10010 if (!map)
10011 return 0;
10012 map = isl_map_copy(map);
10013 map = isl_map_normalize(map);
10014 if (!map)
10015 return 0;
10017 hash = isl_hash_init();
10018 for (i = 0; i < map->n; ++i) {
10019 uint32_t bmap_hash;
10020 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10021 isl_hash_hash(hash, bmap_hash);
10024 isl_map_free(map);
10026 return hash;
10029 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10031 return isl_map_get_hash((isl_map *)set);
10034 /* Check if the value for dimension dim is completely determined
10035 * by the values of the other parameters and variables.
10036 * That is, check if dimension dim is involved in an equality.
10038 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10040 int i;
10041 unsigned nparam;
10043 if (!bset)
10044 return -1;
10045 nparam = isl_basic_set_n_param(bset);
10046 for (i = 0; i < bset->n_eq; ++i)
10047 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10048 return 1;
10049 return 0;
10052 /* Check if the value for dimension dim is completely determined
10053 * by the values of the other parameters and variables.
10054 * That is, check if dimension dim is involved in an equality
10055 * for each of the subsets.
10057 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10059 int i;
10061 if (!set)
10062 return -1;
10063 for (i = 0; i < set->n; ++i) {
10064 int unique;
10065 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10066 if (unique != 1)
10067 return unique;
10069 return 1;
10072 /* Return the number of basic maps in the (current) representation of "map".
10074 int isl_map_n_basic_map(__isl_keep isl_map *map)
10076 return map ? map->n : 0;
10079 int isl_set_n_basic_set(__isl_keep isl_set *set)
10081 return set ? set->n : 0;
10084 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10085 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10087 int i;
10089 if (!map)
10090 return isl_stat_error;
10092 for (i = 0; i < map->n; ++i)
10093 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10094 return isl_stat_error;
10096 return isl_stat_ok;
10099 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10100 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10102 int i;
10104 if (!set)
10105 return isl_stat_error;
10107 for (i = 0; i < set->n; ++i)
10108 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10109 return isl_stat_error;
10111 return isl_stat_ok;
10114 /* Return a list of basic sets, the union of which is equal to "set".
10116 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10117 __isl_keep isl_set *set)
10119 int i;
10120 isl_basic_set_list *list;
10122 if (!set)
10123 return NULL;
10125 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10126 for (i = 0; i < set->n; ++i) {
10127 isl_basic_set *bset;
10129 bset = isl_basic_set_copy(set->p[i]);
10130 list = isl_basic_set_list_add(list, bset);
10133 return list;
10136 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10138 isl_space *dim;
10140 if (!bset)
10141 return NULL;
10143 bset = isl_basic_set_cow(bset);
10144 if (!bset)
10145 return NULL;
10147 dim = isl_basic_set_get_space(bset);
10148 dim = isl_space_lift(dim, bset->n_div);
10149 if (!dim)
10150 goto error;
10151 isl_space_free(bset->dim);
10152 bset->dim = dim;
10153 bset->extra -= bset->n_div;
10154 bset->n_div = 0;
10156 bset = isl_basic_set_finalize(bset);
10158 return bset;
10159 error:
10160 isl_basic_set_free(bset);
10161 return NULL;
10164 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10166 int i;
10167 isl_space *dim;
10168 unsigned n_div;
10170 set = isl_set_align_divs(set);
10172 if (!set)
10173 return NULL;
10175 set = isl_set_cow(set);
10176 if (!set)
10177 return NULL;
10179 n_div = set->p[0]->n_div;
10180 dim = isl_set_get_space(set);
10181 dim = isl_space_lift(dim, n_div);
10182 if (!dim)
10183 goto error;
10184 isl_space_free(set->dim);
10185 set->dim = dim;
10187 for (i = 0; i < set->n; ++i) {
10188 set->p[i] = isl_basic_set_lift(set->p[i]);
10189 if (!set->p[i])
10190 goto error;
10193 return set;
10194 error:
10195 isl_set_free(set);
10196 return NULL;
10199 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10201 isl_space *dim;
10202 struct isl_basic_map *bmap;
10203 unsigned n_set;
10204 unsigned n_div;
10205 unsigned n_param;
10206 unsigned total;
10207 int i, k, l;
10209 set = isl_set_align_divs(set);
10211 if (!set)
10212 return NULL;
10214 dim = isl_set_get_space(set);
10215 if (set->n == 0 || set->p[0]->n_div == 0) {
10216 isl_set_free(set);
10217 return isl_map_identity(isl_space_map_from_set(dim));
10220 n_div = set->p[0]->n_div;
10221 dim = isl_space_map_from_set(dim);
10222 n_param = isl_space_dim(dim, isl_dim_param);
10223 n_set = isl_space_dim(dim, isl_dim_in);
10224 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10225 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10226 for (i = 0; i < n_set; ++i)
10227 bmap = var_equal(bmap, i);
10229 total = n_param + n_set + n_set + n_div;
10230 for (i = 0; i < n_div; ++i) {
10231 k = isl_basic_map_alloc_inequality(bmap);
10232 if (k < 0)
10233 goto error;
10234 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10235 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10236 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10237 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10238 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10239 set->p[0]->div[i][0]);
10241 l = isl_basic_map_alloc_inequality(bmap);
10242 if (l < 0)
10243 goto error;
10244 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10245 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10246 set->p[0]->div[i][0]);
10247 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10250 isl_set_free(set);
10251 bmap = isl_basic_map_simplify(bmap);
10252 bmap = isl_basic_map_finalize(bmap);
10253 return isl_map_from_basic_map(bmap);
10254 error:
10255 isl_set_free(set);
10256 isl_basic_map_free(bmap);
10257 return NULL;
10260 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10262 unsigned dim;
10263 int size = 0;
10265 if (!bset)
10266 return -1;
10268 dim = isl_basic_set_total_dim(bset);
10269 size += bset->n_eq * (1 + dim);
10270 size += bset->n_ineq * (1 + dim);
10271 size += bset->n_div * (2 + dim);
10273 return size;
10276 int isl_set_size(__isl_keep isl_set *set)
10278 int i;
10279 int size = 0;
10281 if (!set)
10282 return -1;
10284 for (i = 0; i < set->n; ++i)
10285 size += isl_basic_set_size(set->p[i]);
10287 return size;
10290 /* Check if there is any lower bound (if lower == 0) and/or upper
10291 * bound (if upper == 0) on the specified dim.
10293 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10294 enum isl_dim_type type, unsigned pos, int lower, int upper)
10296 int i;
10298 if (!bmap)
10299 return isl_bool_error;
10301 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10302 return isl_bool_error);
10304 pos += isl_basic_map_offset(bmap, type);
10306 for (i = 0; i < bmap->n_div; ++i) {
10307 if (isl_int_is_zero(bmap->div[i][0]))
10308 continue;
10309 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10310 return isl_bool_true;
10313 for (i = 0; i < bmap->n_eq; ++i)
10314 if (!isl_int_is_zero(bmap->eq[i][pos]))
10315 return isl_bool_true;
10317 for (i = 0; i < bmap->n_ineq; ++i) {
10318 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10319 if (sgn > 0)
10320 lower = 1;
10321 if (sgn < 0)
10322 upper = 1;
10325 return lower && upper;
10328 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10329 enum isl_dim_type type, unsigned pos)
10331 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10334 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10335 enum isl_dim_type type, unsigned pos)
10337 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10340 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10341 enum isl_dim_type type, unsigned pos)
10343 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10346 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10347 enum isl_dim_type type, unsigned pos)
10349 int i;
10351 if (!map)
10352 return -1;
10354 for (i = 0; i < map->n; ++i) {
10355 int bounded;
10356 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10357 if (bounded < 0 || !bounded)
10358 return bounded;
10361 return 1;
10364 /* Return 1 if the specified dim is involved in both an upper bound
10365 * and a lower bound.
10367 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10368 enum isl_dim_type type, unsigned pos)
10370 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10373 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10375 static isl_bool has_any_bound(__isl_keep isl_map *map,
10376 enum isl_dim_type type, unsigned pos,
10377 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10378 enum isl_dim_type type, unsigned pos))
10380 int i;
10382 if (!map)
10383 return isl_bool_error;
10385 for (i = 0; i < map->n; ++i) {
10386 isl_bool bounded;
10387 bounded = fn(map->p[i], type, pos);
10388 if (bounded < 0 || bounded)
10389 return bounded;
10392 return isl_bool_false;
10395 /* Return 1 if the specified dim is involved in any lower bound.
10397 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10398 enum isl_dim_type type, unsigned pos)
10400 return has_any_bound(set, type, pos,
10401 &isl_basic_map_dim_has_lower_bound);
10404 /* Return 1 if the specified dim is involved in any upper bound.
10406 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10407 enum isl_dim_type type, unsigned pos)
10409 return has_any_bound(set, type, pos,
10410 &isl_basic_map_dim_has_upper_bound);
10413 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10415 static isl_bool has_bound(__isl_keep isl_map *map,
10416 enum isl_dim_type type, unsigned pos,
10417 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10418 enum isl_dim_type type, unsigned pos))
10420 int i;
10422 if (!map)
10423 return isl_bool_error;
10425 for (i = 0; i < map->n; ++i) {
10426 isl_bool bounded;
10427 bounded = fn(map->p[i], type, pos);
10428 if (bounded < 0 || !bounded)
10429 return bounded;
10432 return isl_bool_true;
10435 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10437 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10438 enum isl_dim_type type, unsigned pos)
10440 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10443 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10445 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10446 enum isl_dim_type type, unsigned pos)
10448 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10451 /* For each of the "n" variables starting at "first", determine
10452 * the sign of the variable and put the results in the first "n"
10453 * elements of the array "signs".
10454 * Sign
10455 * 1 means that the variable is non-negative
10456 * -1 means that the variable is non-positive
10457 * 0 means the variable attains both positive and negative values.
10459 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10460 unsigned first, unsigned n, int *signs)
10462 isl_vec *bound = NULL;
10463 struct isl_tab *tab = NULL;
10464 struct isl_tab_undo *snap;
10465 int i;
10467 if (!bset || !signs)
10468 return -1;
10470 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10471 tab = isl_tab_from_basic_set(bset, 0);
10472 if (!bound || !tab)
10473 goto error;
10475 isl_seq_clr(bound->el, bound->size);
10476 isl_int_set_si(bound->el[0], -1);
10478 snap = isl_tab_snap(tab);
10479 for (i = 0; i < n; ++i) {
10480 int empty;
10482 isl_int_set_si(bound->el[1 + first + i], -1);
10483 if (isl_tab_add_ineq(tab, bound->el) < 0)
10484 goto error;
10485 empty = tab->empty;
10486 isl_int_set_si(bound->el[1 + first + i], 0);
10487 if (isl_tab_rollback(tab, snap) < 0)
10488 goto error;
10490 if (empty) {
10491 signs[i] = 1;
10492 continue;
10495 isl_int_set_si(bound->el[1 + first + i], 1);
10496 if (isl_tab_add_ineq(tab, bound->el) < 0)
10497 goto error;
10498 empty = tab->empty;
10499 isl_int_set_si(bound->el[1 + first + i], 0);
10500 if (isl_tab_rollback(tab, snap) < 0)
10501 goto error;
10503 signs[i] = empty ? -1 : 0;
10506 isl_tab_free(tab);
10507 isl_vec_free(bound);
10508 return 0;
10509 error:
10510 isl_tab_free(tab);
10511 isl_vec_free(bound);
10512 return -1;
10515 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10516 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10518 if (!bset || !signs)
10519 return -1;
10520 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10521 return -1);
10523 first += pos(bset->dim, type) - 1;
10524 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10527 /* Is it possible for the integer division "div" to depend (possibly
10528 * indirectly) on any output dimensions?
10530 * If the div is undefined, then we conservatively assume that it
10531 * may depend on them.
10532 * Otherwise, we check if it actually depends on them or on any integer
10533 * divisions that may depend on them.
10535 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10537 int i;
10538 unsigned n_out, o_out;
10539 unsigned n_div, o_div;
10541 if (isl_int_is_zero(bmap->div[div][0]))
10542 return 1;
10544 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10545 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10547 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10548 return 1;
10550 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10551 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10553 for (i = 0; i < n_div; ++i) {
10554 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10555 continue;
10556 if (div_may_involve_output(bmap, i))
10557 return 1;
10560 return 0;
10563 /* Return the first integer division of "bmap" in the range
10564 * [first, first + n[ that may depend on any output dimensions and
10565 * that has a non-zero coefficient in "c" (where the first coefficient
10566 * in "c" corresponds to integer division "first").
10568 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10569 isl_int *c, int first, int n)
10571 int k;
10573 if (!bmap)
10574 return -1;
10576 for (k = first; k < first + n; ++k) {
10577 if (isl_int_is_zero(c[k]))
10578 continue;
10579 if (div_may_involve_output(bmap, k))
10580 return k;
10583 return first + n;
10586 /* Look for a pair of inequality constraints in "bmap" of the form
10588 * -l + i >= 0 or i >= l
10589 * and
10590 * n + l - i >= 0 or i <= l + n
10592 * with n < "m" and i the output dimension at position "pos".
10593 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10594 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10595 * and earlier output dimensions, as well as integer divisions that do
10596 * not involve any of the output dimensions.
10598 * Return the index of the first inequality constraint or bmap->n_ineq
10599 * if no such pair can be found.
10601 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10602 int pos, isl_int m)
10604 int i, j;
10605 isl_ctx *ctx;
10606 unsigned total;
10607 unsigned n_div, o_div;
10608 unsigned n_out, o_out;
10609 int less;
10611 if (!bmap)
10612 return -1;
10614 ctx = isl_basic_map_get_ctx(bmap);
10615 total = isl_basic_map_total_dim(bmap);
10616 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10617 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10618 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10619 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10620 for (i = 0; i < bmap->n_ineq; ++i) {
10621 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10622 continue;
10623 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10624 n_out - (pos + 1)) != -1)
10625 continue;
10626 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10627 0, n_div) < n_div)
10628 continue;
10629 for (j = i + 1; j < bmap->n_ineq; ++j) {
10630 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10631 ctx->one))
10632 continue;
10633 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10634 bmap->ineq[j] + 1, total))
10635 continue;
10636 break;
10638 if (j >= bmap->n_ineq)
10639 continue;
10640 isl_int_add(bmap->ineq[i][0],
10641 bmap->ineq[i][0], bmap->ineq[j][0]);
10642 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10643 isl_int_sub(bmap->ineq[i][0],
10644 bmap->ineq[i][0], bmap->ineq[j][0]);
10645 if (!less)
10646 continue;
10647 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10648 return i;
10649 else
10650 return j;
10653 return bmap->n_ineq;
10656 /* Return the index of the equality of "bmap" that defines
10657 * the output dimension "pos" in terms of earlier dimensions.
10658 * The equality may also involve integer divisions, as long
10659 * as those integer divisions are defined in terms of
10660 * parameters or input dimensions.
10661 * In this case, *div is set to the number of integer divisions and
10662 * *ineq is set to the number of inequality constraints (provided
10663 * div and ineq are not NULL).
10665 * The equality may also involve a single integer division involving
10666 * the output dimensions (typically only output dimension "pos") as
10667 * long as the coefficient of output dimension "pos" is 1 or -1 and
10668 * there is a pair of constraints i >= l and i <= l + n, with i referring
10669 * to output dimension "pos", l an expression involving only earlier
10670 * dimensions and n smaller than the coefficient of the integer division
10671 * in the equality. In this case, the output dimension can be defined
10672 * in terms of a modulo expression that does not involve the integer division.
10673 * *div is then set to this single integer division and
10674 * *ineq is set to the index of constraint i >= l.
10676 * Return bmap->n_eq if there is no such equality.
10677 * Return -1 on error.
10679 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10680 int pos, int *div, int *ineq)
10682 int j, k, l;
10683 unsigned n_out, o_out;
10684 unsigned n_div, o_div;
10686 if (!bmap)
10687 return -1;
10689 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10690 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10691 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10692 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10694 if (ineq)
10695 *ineq = bmap->n_ineq;
10696 if (div)
10697 *div = n_div;
10698 for (j = 0; j < bmap->n_eq; ++j) {
10699 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10700 continue;
10701 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10702 n_out - (pos + 1)) != -1)
10703 continue;
10704 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10705 0, n_div);
10706 if (k >= n_div)
10707 return j;
10708 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10709 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10710 continue;
10711 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10712 k + 1, n_div - (k+1)) < n_div)
10713 continue;
10714 l = find_modulo_constraint_pair(bmap, pos,
10715 bmap->eq[j][o_div + k]);
10716 if (l < 0)
10717 return -1;
10718 if (l >= bmap->n_ineq)
10719 continue;
10720 if (div)
10721 *div = k;
10722 if (ineq)
10723 *ineq = l;
10724 return j;
10727 return bmap->n_eq;
10730 /* Check if the given basic map is obviously single-valued.
10731 * In particular, for each output dimension, check that there is
10732 * an equality that defines the output dimension in terms of
10733 * earlier dimensions.
10735 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10737 int i;
10738 unsigned n_out;
10740 if (!bmap)
10741 return isl_bool_error;
10743 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10745 for (i = 0; i < n_out; ++i) {
10746 int eq;
10748 eq = isl_basic_map_output_defining_equality(bmap, i,
10749 NULL, NULL);
10750 if (eq < 0)
10751 return isl_bool_error;
10752 if (eq >= bmap->n_eq)
10753 return isl_bool_false;
10756 return isl_bool_true;
10759 /* Check if the given basic map is single-valued.
10760 * We simply compute
10762 * M \circ M^-1
10764 * and check if the result is a subset of the identity mapping.
10766 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10768 isl_space *space;
10769 isl_basic_map *test;
10770 isl_basic_map *id;
10771 isl_bool sv;
10773 sv = isl_basic_map_plain_is_single_valued(bmap);
10774 if (sv < 0 || sv)
10775 return sv;
10777 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10778 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10780 space = isl_basic_map_get_space(bmap);
10781 space = isl_space_map_from_set(isl_space_range(space));
10782 id = isl_basic_map_identity(space);
10784 sv = isl_basic_map_is_subset(test, id);
10786 isl_basic_map_free(test);
10787 isl_basic_map_free(id);
10789 return sv;
10792 /* Check if the given map is obviously single-valued.
10794 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10796 if (!map)
10797 return isl_bool_error;
10798 if (map->n == 0)
10799 return isl_bool_true;
10800 if (map->n >= 2)
10801 return isl_bool_false;
10803 return isl_basic_map_plain_is_single_valued(map->p[0]);
10806 /* Check if the given map is single-valued.
10807 * We simply compute
10809 * M \circ M^-1
10811 * and check if the result is a subset of the identity mapping.
10813 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10815 isl_space *dim;
10816 isl_map *test;
10817 isl_map *id;
10818 isl_bool sv;
10820 sv = isl_map_plain_is_single_valued(map);
10821 if (sv < 0 || sv)
10822 return sv;
10824 test = isl_map_reverse(isl_map_copy(map));
10825 test = isl_map_apply_range(test, isl_map_copy(map));
10827 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10828 id = isl_map_identity(dim);
10830 sv = isl_map_is_subset(test, id);
10832 isl_map_free(test);
10833 isl_map_free(id);
10835 return sv;
10838 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10840 isl_bool in;
10842 map = isl_map_copy(map);
10843 map = isl_map_reverse(map);
10844 in = isl_map_is_single_valued(map);
10845 isl_map_free(map);
10847 return in;
10850 /* Check if the given map is obviously injective.
10852 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10854 isl_bool in;
10856 map = isl_map_copy(map);
10857 map = isl_map_reverse(map);
10858 in = isl_map_plain_is_single_valued(map);
10859 isl_map_free(map);
10861 return in;
10864 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10866 isl_bool sv;
10868 sv = isl_map_is_single_valued(map);
10869 if (sv < 0 || !sv)
10870 return sv;
10872 return isl_map_is_injective(map);
10875 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10877 return isl_map_is_single_valued((isl_map *)set);
10880 /* Does "map" only map elements to themselves?
10882 * If the domain and range spaces are different, then "map"
10883 * is considered not to be an identity relation, even if it is empty.
10884 * Otherwise, construct the maximal identity relation and
10885 * check whether "map" is a subset of this relation.
10887 isl_bool isl_map_is_identity(__isl_keep isl_map *map)
10889 isl_space *space;
10890 isl_map *id;
10891 isl_bool equal, is_identity;
10893 space = isl_map_get_space(map);
10894 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
10895 isl_space_free(space);
10896 if (equal < 0 || !equal)
10897 return equal;
10899 id = isl_map_identity(isl_map_get_space(map));
10900 is_identity = isl_map_is_subset(map, id);
10901 isl_map_free(id);
10903 return is_identity;
10906 int isl_map_is_translation(__isl_keep isl_map *map)
10908 int ok;
10909 isl_set *delta;
10911 delta = isl_map_deltas(isl_map_copy(map));
10912 ok = isl_set_is_singleton(delta);
10913 isl_set_free(delta);
10915 return ok;
10918 static int unique(isl_int *p, unsigned pos, unsigned len)
10920 if (isl_seq_first_non_zero(p, pos) != -1)
10921 return 0;
10922 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10923 return 0;
10924 return 1;
10927 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10929 int i, j;
10930 unsigned nvar;
10931 unsigned ovar;
10933 if (!bset)
10934 return -1;
10936 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10937 return 0;
10939 nvar = isl_basic_set_dim(bset, isl_dim_set);
10940 ovar = isl_space_offset(bset->dim, isl_dim_set);
10941 for (j = 0; j < nvar; ++j) {
10942 int lower = 0, upper = 0;
10943 for (i = 0; i < bset->n_eq; ++i) {
10944 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10945 continue;
10946 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10947 return 0;
10948 break;
10950 if (i < bset->n_eq)
10951 continue;
10952 for (i = 0; i < bset->n_ineq; ++i) {
10953 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10954 continue;
10955 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10956 return 0;
10957 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10958 lower = 1;
10959 else
10960 upper = 1;
10962 if (!lower || !upper)
10963 return 0;
10966 return 1;
10969 int isl_set_is_box(__isl_keep isl_set *set)
10971 if (!set)
10972 return -1;
10973 if (set->n != 1)
10974 return 0;
10976 return isl_basic_set_is_box(set->p[0]);
10979 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10981 if (!bset)
10982 return isl_bool_error;
10984 return isl_space_is_wrapping(bset->dim);
10987 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10989 if (!set)
10990 return isl_bool_error;
10992 return isl_space_is_wrapping(set->dim);
10995 /* Modify the space of "map" through a call to "change".
10996 * If "can_change" is set (not NULL), then first call it to check
10997 * if the modification is allowed, printing the error message "cannot_change"
10998 * if it is not.
11000 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
11001 isl_bool (*can_change)(__isl_keep isl_map *map),
11002 const char *cannot_change,
11003 __isl_give isl_space *(*change)(__isl_take isl_space *space))
11005 isl_bool ok;
11006 isl_space *space;
11008 if (!map)
11009 return NULL;
11011 ok = can_change ? can_change(map) : isl_bool_true;
11012 if (ok < 0)
11013 return isl_map_free(map);
11014 if (!ok)
11015 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11016 return isl_map_free(map));
11018 space = change(isl_map_get_space(map));
11019 map = isl_map_reset_space(map, space);
11021 return map;
11024 /* Is the domain of "map" a wrapped relation?
11026 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11028 if (!map)
11029 return isl_bool_error;
11031 return isl_space_domain_is_wrapping(map->dim);
11034 /* Is the range of "map" a wrapped relation?
11036 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11038 if (!map)
11039 return isl_bool_error;
11041 return isl_space_range_is_wrapping(map->dim);
11044 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11046 bmap = isl_basic_map_cow(bmap);
11047 if (!bmap)
11048 return NULL;
11050 bmap->dim = isl_space_wrap(bmap->dim);
11051 if (!bmap->dim)
11052 goto error;
11054 bmap = isl_basic_map_finalize(bmap);
11056 return (isl_basic_set *)bmap;
11057 error:
11058 isl_basic_map_free(bmap);
11059 return NULL;
11062 /* Given a map A -> B, return the set (A -> B).
11064 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11066 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11069 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11071 bset = isl_basic_set_cow(bset);
11072 if (!bset)
11073 return NULL;
11075 bset->dim = isl_space_unwrap(bset->dim);
11076 if (!bset->dim)
11077 goto error;
11079 bset = isl_basic_set_finalize(bset);
11081 return (isl_basic_map *)bset;
11082 error:
11083 isl_basic_set_free(bset);
11084 return NULL;
11087 /* Given a set (A -> B), return the map A -> B.
11088 * Error out if "set" is not of the form (A -> B).
11090 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11092 return isl_map_change_space(set, &isl_set_is_wrapping,
11093 "not a wrapping set", &isl_space_unwrap);
11096 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11097 enum isl_dim_type type)
11099 if (!bmap)
11100 return NULL;
11102 if (!isl_space_is_named_or_nested(bmap->dim, type))
11103 return bmap;
11105 bmap = isl_basic_map_cow(bmap);
11106 if (!bmap)
11107 return NULL;
11109 bmap->dim = isl_space_reset(bmap->dim, type);
11110 if (!bmap->dim)
11111 goto error;
11113 bmap = isl_basic_map_finalize(bmap);
11115 return bmap;
11116 error:
11117 isl_basic_map_free(bmap);
11118 return NULL;
11121 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11122 enum isl_dim_type type)
11124 int i;
11126 if (!map)
11127 return NULL;
11129 if (!isl_space_is_named_or_nested(map->dim, type))
11130 return map;
11132 map = isl_map_cow(map);
11133 if (!map)
11134 return NULL;
11136 for (i = 0; i < map->n; ++i) {
11137 map->p[i] = isl_basic_map_reset(map->p[i], type);
11138 if (!map->p[i])
11139 goto error;
11141 map->dim = isl_space_reset(map->dim, type);
11142 if (!map->dim)
11143 goto error;
11145 return map;
11146 error:
11147 isl_map_free(map);
11148 return NULL;
11151 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11153 if (!bmap)
11154 return NULL;
11156 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11157 return bmap;
11159 bmap = isl_basic_map_cow(bmap);
11160 if (!bmap)
11161 return NULL;
11163 bmap->dim = isl_space_flatten(bmap->dim);
11164 if (!bmap->dim)
11165 goto error;
11167 bmap = isl_basic_map_finalize(bmap);
11169 return bmap;
11170 error:
11171 isl_basic_map_free(bmap);
11172 return NULL;
11175 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11177 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
11180 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11181 __isl_take isl_basic_map *bmap)
11183 if (!bmap)
11184 return NULL;
11186 if (!bmap->dim->nested[0])
11187 return bmap;
11189 bmap = isl_basic_map_cow(bmap);
11190 if (!bmap)
11191 return NULL;
11193 bmap->dim = isl_space_flatten_domain(bmap->dim);
11194 if (!bmap->dim)
11195 goto error;
11197 bmap = isl_basic_map_finalize(bmap);
11199 return bmap;
11200 error:
11201 isl_basic_map_free(bmap);
11202 return NULL;
11205 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11206 __isl_take isl_basic_map *bmap)
11208 if (!bmap)
11209 return NULL;
11211 if (!bmap->dim->nested[1])
11212 return bmap;
11214 bmap = isl_basic_map_cow(bmap);
11215 if (!bmap)
11216 return NULL;
11218 bmap->dim = isl_space_flatten_range(bmap->dim);
11219 if (!bmap->dim)
11220 goto error;
11222 bmap = isl_basic_map_finalize(bmap);
11224 return bmap;
11225 error:
11226 isl_basic_map_free(bmap);
11227 return NULL;
11230 /* Remove any internal structure from the spaces of domain and range of "map".
11232 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11234 if (!map)
11235 return NULL;
11237 if (!map->dim->nested[0] && !map->dim->nested[1])
11238 return map;
11240 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11243 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11245 return (isl_set *)isl_map_flatten((isl_map *)set);
11248 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11250 isl_space *dim, *flat_dim;
11251 isl_map *map;
11253 dim = isl_set_get_space(set);
11254 flat_dim = isl_space_flatten(isl_space_copy(dim));
11255 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11256 map = isl_map_intersect_domain(map, set);
11258 return map;
11261 /* Remove any internal structure from the space of the domain of "map".
11263 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11265 if (!map)
11266 return NULL;
11268 if (!map->dim->nested[0])
11269 return map;
11271 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11274 /* Remove any internal structure from the space of the range of "map".
11276 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11278 if (!map)
11279 return NULL;
11281 if (!map->dim->nested[1])
11282 return map;
11284 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11287 /* Reorder the dimensions of "bmap" according to the given dim_map
11288 * and set the dimension specification to "dim" and
11289 * perform Gaussian elimination on the result.
11291 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11292 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11294 isl_basic_map *res;
11295 unsigned flags;
11297 bmap = isl_basic_map_cow(bmap);
11298 if (!bmap || !dim || !dim_map)
11299 goto error;
11301 flags = bmap->flags;
11302 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11303 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11304 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11305 res = isl_basic_map_alloc_space(dim,
11306 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11307 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11308 if (res)
11309 res->flags = flags;
11310 res = isl_basic_map_gauss(res, NULL);
11311 res = isl_basic_map_finalize(res);
11312 return res;
11313 error:
11314 free(dim_map);
11315 isl_basic_map_free(bmap);
11316 isl_space_free(dim);
11317 return NULL;
11320 /* Reorder the dimensions of "map" according to given reordering.
11322 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11323 __isl_take isl_reordering *r)
11325 int i;
11326 struct isl_dim_map *dim_map;
11328 map = isl_map_cow(map);
11329 dim_map = isl_dim_map_from_reordering(r);
11330 if (!map || !r || !dim_map)
11331 goto error;
11333 for (i = 0; i < map->n; ++i) {
11334 struct isl_dim_map *dim_map_i;
11336 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11338 map->p[i] = isl_basic_map_realign(map->p[i],
11339 isl_space_copy(r->dim), dim_map_i);
11341 if (!map->p[i])
11342 goto error;
11345 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11347 isl_reordering_free(r);
11348 free(dim_map);
11349 return map;
11350 error:
11351 free(dim_map);
11352 isl_map_free(map);
11353 isl_reordering_free(r);
11354 return NULL;
11357 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11358 __isl_take isl_reordering *r)
11360 return (isl_set *)isl_map_realign((isl_map *)set, r);
11363 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11364 __isl_take isl_space *model)
11366 isl_ctx *ctx;
11368 if (!map || !model)
11369 goto error;
11371 ctx = isl_space_get_ctx(model);
11372 if (!isl_space_has_named_params(model))
11373 isl_die(ctx, isl_error_invalid,
11374 "model has unnamed parameters", goto error);
11375 if (!isl_space_has_named_params(map->dim))
11376 isl_die(ctx, isl_error_invalid,
11377 "relation has unnamed parameters", goto error);
11378 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11379 isl_reordering *exp;
11381 model = isl_space_drop_dims(model, isl_dim_in,
11382 0, isl_space_dim(model, isl_dim_in));
11383 model = isl_space_drop_dims(model, isl_dim_out,
11384 0, isl_space_dim(model, isl_dim_out));
11385 exp = isl_parameter_alignment_reordering(map->dim, model);
11386 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11387 map = isl_map_realign(map, exp);
11390 isl_space_free(model);
11391 return map;
11392 error:
11393 isl_space_free(model);
11394 isl_map_free(map);
11395 return NULL;
11398 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11399 __isl_take isl_space *model)
11401 return isl_map_align_params(set, model);
11404 /* Align the parameters of "bmap" to those of "model", introducing
11405 * additional parameters if needed.
11407 __isl_give isl_basic_map *isl_basic_map_align_params(
11408 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11410 isl_ctx *ctx;
11412 if (!bmap || !model)
11413 goto error;
11415 ctx = isl_space_get_ctx(model);
11416 if (!isl_space_has_named_params(model))
11417 isl_die(ctx, isl_error_invalid,
11418 "model has unnamed parameters", goto error);
11419 if (!isl_space_has_named_params(bmap->dim))
11420 isl_die(ctx, isl_error_invalid,
11421 "relation has unnamed parameters", goto error);
11422 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11423 isl_reordering *exp;
11424 struct isl_dim_map *dim_map;
11426 model = isl_space_drop_dims(model, isl_dim_in,
11427 0, isl_space_dim(model, isl_dim_in));
11428 model = isl_space_drop_dims(model, isl_dim_out,
11429 0, isl_space_dim(model, isl_dim_out));
11430 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11431 exp = isl_reordering_extend_space(exp,
11432 isl_basic_map_get_space(bmap));
11433 dim_map = isl_dim_map_from_reordering(exp);
11434 bmap = isl_basic_map_realign(bmap,
11435 exp ? isl_space_copy(exp->dim) : NULL,
11436 isl_dim_map_extend(dim_map, bmap));
11437 isl_reordering_free(exp);
11438 free(dim_map);
11441 isl_space_free(model);
11442 return bmap;
11443 error:
11444 isl_space_free(model);
11445 isl_basic_map_free(bmap);
11446 return NULL;
11449 /* Align the parameters of "bset" to those of "model", introducing
11450 * additional parameters if needed.
11452 __isl_give isl_basic_set *isl_basic_set_align_params(
11453 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11455 return isl_basic_map_align_params(bset, model);
11458 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11459 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11460 enum isl_dim_type c2, enum isl_dim_type c3,
11461 enum isl_dim_type c4, enum isl_dim_type c5)
11463 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11464 struct isl_mat *mat;
11465 int i, j, k;
11466 int pos;
11468 if (!bmap)
11469 return NULL;
11470 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11471 isl_basic_map_total_dim(bmap) + 1);
11472 if (!mat)
11473 return NULL;
11474 for (i = 0; i < bmap->n_eq; ++i)
11475 for (j = 0, pos = 0; j < 5; ++j) {
11476 int off = isl_basic_map_offset(bmap, c[j]);
11477 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11478 isl_int_set(mat->row[i][pos],
11479 bmap->eq[i][off + k]);
11480 ++pos;
11484 return mat;
11487 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11488 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11489 enum isl_dim_type c2, enum isl_dim_type c3,
11490 enum isl_dim_type c4, enum isl_dim_type c5)
11492 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11493 struct isl_mat *mat;
11494 int i, j, k;
11495 int pos;
11497 if (!bmap)
11498 return NULL;
11499 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11500 isl_basic_map_total_dim(bmap) + 1);
11501 if (!mat)
11502 return NULL;
11503 for (i = 0; i < bmap->n_ineq; ++i)
11504 for (j = 0, pos = 0; j < 5; ++j) {
11505 int off = isl_basic_map_offset(bmap, c[j]);
11506 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11507 isl_int_set(mat->row[i][pos],
11508 bmap->ineq[i][off + k]);
11509 ++pos;
11513 return mat;
11516 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11517 __isl_take isl_space *dim,
11518 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11519 enum isl_dim_type c2, enum isl_dim_type c3,
11520 enum isl_dim_type c4, enum isl_dim_type c5)
11522 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11523 isl_basic_map *bmap;
11524 unsigned total;
11525 unsigned extra;
11526 int i, j, k, l;
11527 int pos;
11529 if (!dim || !eq || !ineq)
11530 goto error;
11532 if (eq->n_col != ineq->n_col)
11533 isl_die(dim->ctx, isl_error_invalid,
11534 "equalities and inequalities matrices should have "
11535 "same number of columns", goto error);
11537 total = 1 + isl_space_dim(dim, isl_dim_all);
11539 if (eq->n_col < total)
11540 isl_die(dim->ctx, isl_error_invalid,
11541 "number of columns too small", goto error);
11543 extra = eq->n_col - total;
11545 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11546 eq->n_row, ineq->n_row);
11547 if (!bmap)
11548 goto error;
11549 for (i = 0; i < extra; ++i) {
11550 k = isl_basic_map_alloc_div(bmap);
11551 if (k < 0)
11552 goto error;
11553 isl_int_set_si(bmap->div[k][0], 0);
11555 for (i = 0; i < eq->n_row; ++i) {
11556 l = isl_basic_map_alloc_equality(bmap);
11557 if (l < 0)
11558 goto error;
11559 for (j = 0, pos = 0; j < 5; ++j) {
11560 int off = isl_basic_map_offset(bmap, c[j]);
11561 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11562 isl_int_set(bmap->eq[l][off + k],
11563 eq->row[i][pos]);
11564 ++pos;
11568 for (i = 0; i < ineq->n_row; ++i) {
11569 l = isl_basic_map_alloc_inequality(bmap);
11570 if (l < 0)
11571 goto error;
11572 for (j = 0, pos = 0; j < 5; ++j) {
11573 int off = isl_basic_map_offset(bmap, c[j]);
11574 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11575 isl_int_set(bmap->ineq[l][off + k],
11576 ineq->row[i][pos]);
11577 ++pos;
11582 isl_space_free(dim);
11583 isl_mat_free(eq);
11584 isl_mat_free(ineq);
11586 bmap = isl_basic_map_simplify(bmap);
11587 return isl_basic_map_finalize(bmap);
11588 error:
11589 isl_space_free(dim);
11590 isl_mat_free(eq);
11591 isl_mat_free(ineq);
11592 return NULL;
11595 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11596 __isl_keep isl_basic_set *bset, 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_map_equalities_matrix((isl_basic_map *)bset,
11600 c1, c2, c3, c4, isl_dim_in);
11603 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
11604 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11605 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11607 return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
11608 c1, c2, c3, c4, isl_dim_in);
11611 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11612 __isl_take isl_space *dim,
11613 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11614 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11616 return (isl_basic_set*)
11617 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11618 c1, c2, c3, c4, isl_dim_in);
11621 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11623 if (!bmap)
11624 return isl_bool_error;
11626 return isl_space_can_zip(bmap->dim);
11629 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11631 if (!map)
11632 return isl_bool_error;
11634 return isl_space_can_zip(map->dim);
11637 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11638 * (A -> C) -> (B -> D).
11640 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11642 unsigned pos;
11643 unsigned n1;
11644 unsigned n2;
11646 if (!bmap)
11647 return NULL;
11649 if (!isl_basic_map_can_zip(bmap))
11650 isl_die(bmap->ctx, isl_error_invalid,
11651 "basic map cannot be zipped", goto error);
11652 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11653 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11654 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11655 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11656 bmap = isl_basic_map_cow(bmap);
11657 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11658 if (!bmap)
11659 return NULL;
11660 bmap->dim = isl_space_zip(bmap->dim);
11661 if (!bmap->dim)
11662 goto error;
11663 bmap = isl_basic_map_mark_final(bmap);
11664 return bmap;
11665 error:
11666 isl_basic_map_free(bmap);
11667 return NULL;
11670 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11671 * (A -> C) -> (B -> D).
11673 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11675 int i;
11677 if (!map)
11678 return NULL;
11680 if (!isl_map_can_zip(map))
11681 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11682 goto error);
11684 map = isl_map_cow(map);
11685 if (!map)
11686 return NULL;
11688 for (i = 0; i < map->n; ++i) {
11689 map->p[i] = isl_basic_map_zip(map->p[i]);
11690 if (!map->p[i])
11691 goto error;
11694 map->dim = isl_space_zip(map->dim);
11695 if (!map->dim)
11696 goto error;
11698 return map;
11699 error:
11700 isl_map_free(map);
11701 return NULL;
11704 /* Can we apply isl_basic_map_curry to "bmap"?
11705 * That is, does it have a nested relation in its domain?
11707 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11709 if (!bmap)
11710 return isl_bool_error;
11712 return isl_space_can_curry(bmap->dim);
11715 /* Can we apply isl_map_curry to "map"?
11716 * That is, does it have a nested relation in its domain?
11718 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11720 if (!map)
11721 return isl_bool_error;
11723 return isl_space_can_curry(map->dim);
11726 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11727 * A -> (B -> C).
11729 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11732 if (!bmap)
11733 return NULL;
11735 if (!isl_basic_map_can_curry(bmap))
11736 isl_die(bmap->ctx, isl_error_invalid,
11737 "basic map cannot be curried", goto error);
11738 bmap = isl_basic_map_cow(bmap);
11739 if (!bmap)
11740 return NULL;
11741 bmap->dim = isl_space_curry(bmap->dim);
11742 if (!bmap->dim)
11743 goto error;
11744 bmap = isl_basic_map_mark_final(bmap);
11745 return bmap;
11746 error:
11747 isl_basic_map_free(bmap);
11748 return NULL;
11751 /* Given a map (A -> B) -> C, return the corresponding map
11752 * A -> (B -> C).
11754 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11756 return isl_map_change_space(map, &isl_map_can_curry,
11757 "map cannot be curried", &isl_space_curry);
11760 /* Can isl_map_range_curry be applied to "map"?
11761 * That is, does it have a nested relation in its range,
11762 * the domain of which is itself a nested relation?
11764 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11766 if (!map)
11767 return isl_bool_error;
11769 return isl_space_can_range_curry(map->dim);
11772 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11773 * A -> (B -> (C -> D)).
11775 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11777 return isl_map_change_space(map, &isl_map_can_range_curry,
11778 "map range cannot be curried",
11779 &isl_space_range_curry);
11782 /* Can we apply isl_basic_map_uncurry to "bmap"?
11783 * That is, does it have a nested relation in its domain?
11785 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11787 if (!bmap)
11788 return isl_bool_error;
11790 return isl_space_can_uncurry(bmap->dim);
11793 /* Can we apply isl_map_uncurry to "map"?
11794 * That is, does it have a nested relation in its domain?
11796 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11798 if (!map)
11799 return isl_bool_error;
11801 return isl_space_can_uncurry(map->dim);
11804 /* Given a basic map A -> (B -> C), return the corresponding basic map
11805 * (A -> B) -> C.
11807 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11810 if (!bmap)
11811 return NULL;
11813 if (!isl_basic_map_can_uncurry(bmap))
11814 isl_die(bmap->ctx, isl_error_invalid,
11815 "basic map cannot be uncurried",
11816 return isl_basic_map_free(bmap));
11817 bmap = isl_basic_map_cow(bmap);
11818 if (!bmap)
11819 return NULL;
11820 bmap->dim = isl_space_uncurry(bmap->dim);
11821 if (!bmap->dim)
11822 return isl_basic_map_free(bmap);
11823 bmap = isl_basic_map_mark_final(bmap);
11824 return bmap;
11827 /* Given a map A -> (B -> C), return the corresponding map
11828 * (A -> B) -> C.
11830 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11832 return isl_map_change_space(map, &isl_map_can_uncurry,
11833 "map cannot be uncurried", &isl_space_uncurry);
11836 /* Construct a basic map mapping the domain of the affine expression
11837 * to a one-dimensional range prescribed by the affine expression.
11839 * A NaN affine expression cannot be converted to a basic map.
11841 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11843 int k;
11844 int pos;
11845 isl_bool is_nan;
11846 isl_local_space *ls;
11847 isl_basic_map *bmap = NULL;
11849 if (!aff)
11850 return NULL;
11851 is_nan = isl_aff_is_nan(aff);
11852 if (is_nan < 0)
11853 goto error;
11854 if (is_nan)
11855 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
11856 "cannot convert NaN", goto error);
11858 ls = isl_aff_get_local_space(aff);
11859 bmap = isl_basic_map_from_local_space(ls);
11860 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11861 k = isl_basic_map_alloc_equality(bmap);
11862 if (k < 0)
11863 goto error;
11865 pos = isl_basic_map_offset(bmap, isl_dim_out);
11866 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11867 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11868 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11869 aff->v->size - (pos + 1));
11871 isl_aff_free(aff);
11872 bmap = isl_basic_map_finalize(bmap);
11873 return bmap;
11874 error:
11875 isl_aff_free(aff);
11876 isl_basic_map_free(bmap);
11877 return NULL;
11880 /* Construct a map mapping the domain of the affine expression
11881 * to a one-dimensional range prescribed by the affine expression.
11883 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11885 isl_basic_map *bmap;
11887 bmap = isl_basic_map_from_aff(aff);
11888 return isl_map_from_basic_map(bmap);
11891 /* Construct a basic map mapping the domain the multi-affine expression
11892 * to its range, with each dimension in the range equated to the
11893 * corresponding affine expression.
11895 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11896 __isl_take isl_multi_aff *maff)
11898 int i;
11899 isl_space *space;
11900 isl_basic_map *bmap;
11902 if (!maff)
11903 return NULL;
11905 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11906 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11907 "invalid space", goto error);
11909 space = isl_space_domain(isl_multi_aff_get_space(maff));
11910 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11912 for (i = 0; i < maff->n; ++i) {
11913 isl_aff *aff;
11914 isl_basic_map *bmap_i;
11916 aff = isl_aff_copy(maff->p[i]);
11917 bmap_i = isl_basic_map_from_aff(aff);
11919 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11922 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11924 isl_multi_aff_free(maff);
11925 return bmap;
11926 error:
11927 isl_multi_aff_free(maff);
11928 return NULL;
11931 /* Construct a map mapping the domain the multi-affine expression
11932 * to its range, with each dimension in the range equated to the
11933 * corresponding affine expression.
11935 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11937 isl_basic_map *bmap;
11939 bmap = isl_basic_map_from_multi_aff(maff);
11940 return isl_map_from_basic_map(bmap);
11943 /* Construct a basic map mapping a domain in the given space to
11944 * to an n-dimensional range, with n the number of elements in the list,
11945 * where each coordinate in the range is prescribed by the
11946 * corresponding affine expression.
11947 * The domains of all affine expressions in the list are assumed to match
11948 * domain_dim.
11950 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11951 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11953 int i;
11954 isl_space *dim;
11955 isl_basic_map *bmap;
11957 if (!list)
11958 return NULL;
11960 dim = isl_space_from_domain(domain_dim);
11961 bmap = isl_basic_map_universe(dim);
11963 for (i = 0; i < list->n; ++i) {
11964 isl_aff *aff;
11965 isl_basic_map *bmap_i;
11967 aff = isl_aff_copy(list->p[i]);
11968 bmap_i = isl_basic_map_from_aff(aff);
11970 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11973 isl_aff_list_free(list);
11974 return bmap;
11977 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11978 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11980 return isl_map_equate(set, type1, pos1, type2, pos2);
11983 /* Construct a basic map where the given dimensions are equal to each other.
11985 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11986 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11988 isl_basic_map *bmap = NULL;
11989 int i;
11991 if (!space)
11992 return NULL;
11994 if (pos1 >= isl_space_dim(space, type1))
11995 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11996 "index out of bounds", goto error);
11997 if (pos2 >= isl_space_dim(space, type2))
11998 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11999 "index out of bounds", goto error);
12001 if (type1 == type2 && pos1 == pos2)
12002 return isl_basic_map_universe(space);
12004 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
12005 i = isl_basic_map_alloc_equality(bmap);
12006 if (i < 0)
12007 goto error;
12008 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12009 pos1 += isl_basic_map_offset(bmap, type1);
12010 pos2 += isl_basic_map_offset(bmap, type2);
12011 isl_int_set_si(bmap->eq[i][pos1], -1);
12012 isl_int_set_si(bmap->eq[i][pos2], 1);
12013 bmap = isl_basic_map_finalize(bmap);
12014 isl_space_free(space);
12015 return bmap;
12016 error:
12017 isl_space_free(space);
12018 isl_basic_map_free(bmap);
12019 return NULL;
12022 /* Add a constraint imposing that the given two dimensions are equal.
12024 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12025 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12027 isl_basic_map *eq;
12029 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12031 bmap = isl_basic_map_intersect(bmap, eq);
12033 return bmap;
12036 /* Add a constraint imposing that the given two dimensions are equal.
12038 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12039 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12041 isl_basic_map *bmap;
12043 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12045 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12047 return map;
12050 /* Add a constraint imposing that the given two dimensions have opposite values.
12052 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12053 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12055 isl_basic_map *bmap = NULL;
12056 int i;
12058 if (!map)
12059 return NULL;
12061 if (pos1 >= isl_map_dim(map, type1))
12062 isl_die(map->ctx, isl_error_invalid,
12063 "index out of bounds", goto error);
12064 if (pos2 >= isl_map_dim(map, type2))
12065 isl_die(map->ctx, isl_error_invalid,
12066 "index out of bounds", goto error);
12068 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12069 i = isl_basic_map_alloc_equality(bmap);
12070 if (i < 0)
12071 goto error;
12072 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12073 pos1 += isl_basic_map_offset(bmap, type1);
12074 pos2 += isl_basic_map_offset(bmap, type2);
12075 isl_int_set_si(bmap->eq[i][pos1], 1);
12076 isl_int_set_si(bmap->eq[i][pos2], 1);
12077 bmap = isl_basic_map_finalize(bmap);
12079 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12081 return map;
12082 error:
12083 isl_basic_map_free(bmap);
12084 isl_map_free(map);
12085 return NULL;
12088 /* Construct a constraint imposing that the value of the first dimension is
12089 * greater than or equal to that of the second.
12091 static __isl_give isl_constraint *constraint_order_ge(
12092 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12093 enum isl_dim_type type2, int pos2)
12095 isl_constraint *c;
12097 if (!space)
12098 return NULL;
12100 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12102 if (pos1 >= isl_constraint_dim(c, type1))
12103 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12104 "index out of bounds", return isl_constraint_free(c));
12105 if (pos2 >= isl_constraint_dim(c, type2))
12106 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12107 "index out of bounds", return isl_constraint_free(c));
12109 if (type1 == type2 && pos1 == pos2)
12110 return c;
12112 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12113 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12115 return c;
12118 /* Add a constraint imposing that the value of the first dimension is
12119 * greater than or equal to that of the second.
12121 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12122 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12124 isl_constraint *c;
12125 isl_space *space;
12127 if (type1 == type2 && pos1 == pos2)
12128 return bmap;
12129 space = isl_basic_map_get_space(bmap);
12130 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12131 bmap = isl_basic_map_add_constraint(bmap, c);
12133 return bmap;
12136 /* Add a constraint imposing that the value of the first dimension is
12137 * greater than or equal to that of the second.
12139 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12140 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12142 isl_constraint *c;
12143 isl_space *space;
12145 if (type1 == type2 && pos1 == pos2)
12146 return map;
12147 space = isl_map_get_space(map);
12148 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12149 map = isl_map_add_constraint(map, c);
12151 return map;
12154 /* Add a constraint imposing that the value of the first dimension is
12155 * less than or equal to that of the second.
12157 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12158 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12160 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12163 /* Construct a basic map where the value of the first dimension is
12164 * greater than that of the second.
12166 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12167 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12169 isl_basic_map *bmap = NULL;
12170 int i;
12172 if (!space)
12173 return NULL;
12175 if (pos1 >= isl_space_dim(space, type1))
12176 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12177 "index out of bounds", goto error);
12178 if (pos2 >= isl_space_dim(space, type2))
12179 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12180 "index out of bounds", goto error);
12182 if (type1 == type2 && pos1 == pos2)
12183 return isl_basic_map_empty(space);
12185 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12186 i = isl_basic_map_alloc_inequality(bmap);
12187 if (i < 0)
12188 return isl_basic_map_free(bmap);
12189 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12190 pos1 += isl_basic_map_offset(bmap, type1);
12191 pos2 += isl_basic_map_offset(bmap, type2);
12192 isl_int_set_si(bmap->ineq[i][pos1], 1);
12193 isl_int_set_si(bmap->ineq[i][pos2], -1);
12194 isl_int_set_si(bmap->ineq[i][0], -1);
12195 bmap = isl_basic_map_finalize(bmap);
12197 return bmap;
12198 error:
12199 isl_space_free(space);
12200 isl_basic_map_free(bmap);
12201 return NULL;
12204 /* Add a constraint imposing that the value of the first dimension is
12205 * greater than that of the second.
12207 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12208 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12210 isl_basic_map *gt;
12212 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12214 bmap = isl_basic_map_intersect(bmap, gt);
12216 return bmap;
12219 /* Add a constraint imposing that the value of the first dimension is
12220 * greater than that of the second.
12222 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12223 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12225 isl_basic_map *bmap;
12227 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12229 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12231 return map;
12234 /* Add a constraint imposing that the value of the first dimension is
12235 * smaller than that of the second.
12237 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12238 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12240 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12243 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12244 int pos)
12246 isl_aff *div;
12247 isl_local_space *ls;
12249 if (!bmap)
12250 return NULL;
12252 if (!isl_basic_map_divs_known(bmap))
12253 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12254 "some divs are unknown", return NULL);
12256 ls = isl_basic_map_get_local_space(bmap);
12257 div = isl_local_space_get_div(ls, pos);
12258 isl_local_space_free(ls);
12260 return div;
12263 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12264 int pos)
12266 return isl_basic_map_get_div(bset, pos);
12269 /* Plug in "subs" for dimension "type", "pos" of "bset".
12271 * Let i be the dimension to replace and let "subs" be of the form
12273 * f/d
12275 * Any integer division with a non-zero coefficient for i,
12277 * floor((a i + g)/m)
12279 * is replaced by
12281 * floor((a f + d g)/(m d))
12283 * Constraints of the form
12285 * a i + g
12287 * are replaced by
12289 * a f + d g
12291 * We currently require that "subs" is an integral expression.
12292 * Handling rational expressions may require us to add stride constraints
12293 * as we do in isl_basic_set_preimage_multi_aff.
12295 __isl_give isl_basic_set *isl_basic_set_substitute(
12296 __isl_take isl_basic_set *bset,
12297 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12299 int i;
12300 isl_int v;
12301 isl_ctx *ctx;
12303 if (bset && isl_basic_set_plain_is_empty(bset))
12304 return bset;
12306 bset = isl_basic_set_cow(bset);
12307 if (!bset || !subs)
12308 goto error;
12310 ctx = isl_basic_set_get_ctx(bset);
12311 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12312 isl_die(ctx, isl_error_invalid,
12313 "spaces don't match", goto error);
12314 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12315 isl_die(ctx, isl_error_unsupported,
12316 "cannot handle divs yet", goto error);
12317 if (!isl_int_is_one(subs->v->el[0]))
12318 isl_die(ctx, isl_error_invalid,
12319 "can only substitute integer expressions", goto error);
12321 pos += isl_basic_set_offset(bset, type);
12323 isl_int_init(v);
12325 for (i = 0; i < bset->n_eq; ++i) {
12326 if (isl_int_is_zero(bset->eq[i][pos]))
12327 continue;
12328 isl_int_set(v, bset->eq[i][pos]);
12329 isl_int_set_si(bset->eq[i][pos], 0);
12330 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12331 v, subs->v->el + 1, subs->v->size - 1);
12334 for (i = 0; i < bset->n_ineq; ++i) {
12335 if (isl_int_is_zero(bset->ineq[i][pos]))
12336 continue;
12337 isl_int_set(v, bset->ineq[i][pos]);
12338 isl_int_set_si(bset->ineq[i][pos], 0);
12339 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12340 v, subs->v->el + 1, subs->v->size - 1);
12343 for (i = 0; i < bset->n_div; ++i) {
12344 if (isl_int_is_zero(bset->div[i][1 + pos]))
12345 continue;
12346 isl_int_set(v, bset->div[i][1 + pos]);
12347 isl_int_set_si(bset->div[i][1 + pos], 0);
12348 isl_seq_combine(bset->div[i] + 1,
12349 subs->v->el[0], bset->div[i] + 1,
12350 v, subs->v->el + 1, subs->v->size - 1);
12351 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12354 isl_int_clear(v);
12356 bset = isl_basic_set_simplify(bset);
12357 return isl_basic_set_finalize(bset);
12358 error:
12359 isl_basic_set_free(bset);
12360 return NULL;
12363 /* Plug in "subs" for dimension "type", "pos" of "set".
12365 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12366 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12368 int i;
12370 if (set && isl_set_plain_is_empty(set))
12371 return set;
12373 set = isl_set_cow(set);
12374 if (!set || !subs)
12375 goto error;
12377 for (i = set->n - 1; i >= 0; --i) {
12378 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12379 if (remove_if_empty(set, i) < 0)
12380 goto error;
12383 return set;
12384 error:
12385 isl_set_free(set);
12386 return NULL;
12389 /* Check if the range of "ma" is compatible with the domain or range
12390 * (depending on "type") of "bmap".
12391 * Return -1 if anything is wrong.
12393 static int check_basic_map_compatible_range_multi_aff(
12394 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12395 __isl_keep isl_multi_aff *ma)
12397 int m;
12398 isl_space *ma_space;
12400 ma_space = isl_multi_aff_get_space(ma);
12402 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12403 if (m < 0)
12404 goto error;
12405 if (!m)
12406 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12407 "parameters don't match", goto error);
12408 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12409 if (m < 0)
12410 goto error;
12411 if (!m)
12412 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12413 "spaces don't match", goto error);
12415 isl_space_free(ma_space);
12416 return m;
12417 error:
12418 isl_space_free(ma_space);
12419 return -1;
12422 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12423 * coefficients before the transformed range of dimensions,
12424 * the "n_after" coefficients after the transformed range of dimensions
12425 * and the coefficients of the other divs in "bmap".
12427 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12428 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12430 int i;
12431 int n_param;
12432 int n_set;
12433 isl_local_space *ls;
12435 if (n_div == 0)
12436 return 0;
12438 ls = isl_aff_get_domain_local_space(ma->p[0]);
12439 if (!ls)
12440 return -1;
12442 n_param = isl_local_space_dim(ls, isl_dim_param);
12443 n_set = isl_local_space_dim(ls, isl_dim_set);
12444 for (i = 0; i < n_div; ++i) {
12445 int o_bmap = 0, o_ls = 0;
12447 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12448 o_bmap += 1 + 1 + n_param;
12449 o_ls += 1 + 1 + n_param;
12450 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12451 o_bmap += n_before;
12452 isl_seq_cpy(bmap->div[i] + o_bmap,
12453 ls->div->row[i] + o_ls, n_set);
12454 o_bmap += n_set;
12455 o_ls += n_set;
12456 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12457 o_bmap += n_after;
12458 isl_seq_cpy(bmap->div[i] + o_bmap,
12459 ls->div->row[i] + o_ls, n_div);
12460 o_bmap += n_div;
12461 o_ls += n_div;
12462 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12463 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12464 goto error;
12467 isl_local_space_free(ls);
12468 return 0;
12469 error:
12470 isl_local_space_free(ls);
12471 return -1;
12474 /* How many stride constraints does "ma" enforce?
12475 * That is, how many of the affine expressions have a denominator
12476 * different from one?
12478 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12480 int i;
12481 int strides = 0;
12483 for (i = 0; i < ma->n; ++i)
12484 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12485 strides++;
12487 return strides;
12490 /* For each affine expression in ma of the form
12492 * x_i = (f_i y + h_i)/m_i
12494 * with m_i different from one, add a constraint to "bmap"
12495 * of the form
12497 * f_i y + h_i = m_i alpha_i
12499 * with alpha_i an additional existentially quantified variable.
12501 * The input variables of "ma" correspond to a subset of the variables
12502 * of "bmap". There are "n_before" variables in "bmap" before this
12503 * subset and "n_after" variables after this subset.
12504 * The integer divisions of the affine expressions in "ma" are assumed
12505 * to have been aligned. There are "n_div_ma" of them and
12506 * they appear first in "bmap", straight after the "n_after" variables.
12508 static __isl_give isl_basic_map *add_ma_strides(
12509 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12510 int n_before, int n_after, int n_div_ma)
12512 int i, k;
12513 int div;
12514 int total;
12515 int n_param;
12516 int n_in;
12518 total = isl_basic_map_total_dim(bmap);
12519 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12520 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12521 for (i = 0; i < ma->n; ++i) {
12522 int o_bmap = 0, o_ma = 1;
12524 if (isl_int_is_one(ma->p[i]->v->el[0]))
12525 continue;
12526 div = isl_basic_map_alloc_div(bmap);
12527 k = isl_basic_map_alloc_equality(bmap);
12528 if (div < 0 || k < 0)
12529 goto error;
12530 isl_int_set_si(bmap->div[div][0], 0);
12531 isl_seq_cpy(bmap->eq[k] + o_bmap,
12532 ma->p[i]->v->el + o_ma, 1 + n_param);
12533 o_bmap += 1 + n_param;
12534 o_ma += 1 + n_param;
12535 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12536 o_bmap += n_before;
12537 isl_seq_cpy(bmap->eq[k] + o_bmap,
12538 ma->p[i]->v->el + o_ma, n_in);
12539 o_bmap += n_in;
12540 o_ma += n_in;
12541 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12542 o_bmap += n_after;
12543 isl_seq_cpy(bmap->eq[k] + o_bmap,
12544 ma->p[i]->v->el + o_ma, n_div_ma);
12545 o_bmap += n_div_ma;
12546 o_ma += n_div_ma;
12547 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12548 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12549 total++;
12552 return bmap;
12553 error:
12554 isl_basic_map_free(bmap);
12555 return NULL;
12558 /* Replace the domain or range space (depending on "type) of "space" by "set".
12560 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12561 enum isl_dim_type type, __isl_take isl_space *set)
12563 if (type == isl_dim_in) {
12564 space = isl_space_range(space);
12565 space = isl_space_map_from_domain_and_range(set, space);
12566 } else {
12567 space = isl_space_domain(space);
12568 space = isl_space_map_from_domain_and_range(space, set);
12571 return space;
12574 /* Compute the preimage of the domain or range (depending on "type")
12575 * of "bmap" under the function represented by "ma".
12576 * In other words, plug in "ma" in the domain or range of "bmap".
12577 * The result is a basic map that lives in the same space as "bmap"
12578 * except that the domain or range has been replaced by
12579 * the domain space of "ma".
12581 * If bmap is represented by
12583 * A(p) + S u + B x + T v + C(divs) >= 0,
12585 * where u and x are input and output dimensions if type == isl_dim_out
12586 * while x and v are input and output dimensions if type == isl_dim_in,
12587 * and ma is represented by
12589 * x = D(p) + F(y) + G(divs')
12591 * then the result is
12593 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12595 * The divs in the input set are similarly adjusted.
12596 * In particular
12598 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12600 * becomes
12602 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12603 * B_i G(divs') + c_i(divs))/n_i)
12605 * If bmap is not a rational map and if F(y) involves any denominators
12607 * x_i = (f_i y + h_i)/m_i
12609 * then additional constraints are added to ensure that we only
12610 * map back integer points. That is we enforce
12612 * f_i y + h_i = m_i alpha_i
12614 * with alpha_i an additional existentially quantified variable.
12616 * We first copy over the divs from "ma".
12617 * Then we add the modified constraints and divs from "bmap".
12618 * Finally, we add the stride constraints, if needed.
12620 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12621 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12622 __isl_take isl_multi_aff *ma)
12624 int i, k;
12625 isl_space *space;
12626 isl_basic_map *res = NULL;
12627 int n_before, n_after, n_div_bmap, n_div_ma;
12628 isl_int f, c1, c2, g;
12629 int rational, strides;
12631 isl_int_init(f);
12632 isl_int_init(c1);
12633 isl_int_init(c2);
12634 isl_int_init(g);
12636 ma = isl_multi_aff_align_divs(ma);
12637 if (!bmap || !ma)
12638 goto error;
12639 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12640 goto error;
12642 if (type == isl_dim_in) {
12643 n_before = 0;
12644 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12645 } else {
12646 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12647 n_after = 0;
12649 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12650 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12652 space = isl_multi_aff_get_domain_space(ma);
12653 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12654 rational = isl_basic_map_is_rational(bmap);
12655 strides = rational ? 0 : multi_aff_strides(ma);
12656 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12657 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12658 if (rational)
12659 res = isl_basic_map_set_rational(res);
12661 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12662 if (isl_basic_map_alloc_div(res) < 0)
12663 goto error;
12665 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12666 goto error;
12668 for (i = 0; i < bmap->n_eq; ++i) {
12669 k = isl_basic_map_alloc_equality(res);
12670 if (k < 0)
12671 goto error;
12672 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12673 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12676 for (i = 0; i < bmap->n_ineq; ++i) {
12677 k = isl_basic_map_alloc_inequality(res);
12678 if (k < 0)
12679 goto error;
12680 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12681 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12684 for (i = 0; i < bmap->n_div; ++i) {
12685 if (isl_int_is_zero(bmap->div[i][0])) {
12686 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12687 continue;
12689 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12690 n_before, n_after, n_div_ma, n_div_bmap,
12691 f, c1, c2, g, 1);
12694 if (strides)
12695 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12697 isl_int_clear(f);
12698 isl_int_clear(c1);
12699 isl_int_clear(c2);
12700 isl_int_clear(g);
12701 isl_basic_map_free(bmap);
12702 isl_multi_aff_free(ma);
12703 res = isl_basic_set_simplify(res);
12704 return isl_basic_map_finalize(res);
12705 error:
12706 isl_int_clear(f);
12707 isl_int_clear(c1);
12708 isl_int_clear(c2);
12709 isl_int_clear(g);
12710 isl_basic_map_free(bmap);
12711 isl_multi_aff_free(ma);
12712 isl_basic_map_free(res);
12713 return NULL;
12716 /* Compute the preimage of "bset" under the function represented by "ma".
12717 * In other words, plug in "ma" in "bset". The result is a basic set
12718 * that lives in the domain space of "ma".
12720 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12721 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12723 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12726 /* Compute the preimage of the domain of "bmap" under the function
12727 * represented by "ma".
12728 * In other words, plug in "ma" in the domain of "bmap".
12729 * The result is a basic map that lives in the same space as "bmap"
12730 * except that the domain has been replaced by the domain space of "ma".
12732 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12733 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12735 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12738 /* Compute the preimage of the range of "bmap" under the function
12739 * represented by "ma".
12740 * In other words, plug in "ma" in the range of "bmap".
12741 * The result is a basic map that lives in the same space as "bmap"
12742 * except that the range has been replaced by the domain space of "ma".
12744 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12745 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12747 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12750 /* Check if the range of "ma" is compatible with the domain or range
12751 * (depending on "type") of "map".
12752 * Return -1 if anything is wrong.
12754 static int check_map_compatible_range_multi_aff(
12755 __isl_keep isl_map *map, enum isl_dim_type type,
12756 __isl_keep isl_multi_aff *ma)
12758 int m;
12759 isl_space *ma_space;
12761 ma_space = isl_multi_aff_get_space(ma);
12762 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12763 isl_space_free(ma_space);
12764 if (m >= 0 && !m)
12765 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12766 "spaces don't match", return -1);
12767 return m;
12770 /* Compute the preimage of the domain or range (depending on "type")
12771 * of "map" under the function represented by "ma".
12772 * In other words, plug in "ma" in the domain or range of "map".
12773 * The result is a map that lives in the same space as "map"
12774 * except that the domain or range has been replaced by
12775 * the domain space of "ma".
12777 * The parameters are assumed to have been aligned.
12779 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12780 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12782 int i;
12783 isl_space *space;
12785 map = isl_map_cow(map);
12786 ma = isl_multi_aff_align_divs(ma);
12787 if (!map || !ma)
12788 goto error;
12789 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12790 goto error;
12792 for (i = 0; i < map->n; ++i) {
12793 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12794 isl_multi_aff_copy(ma));
12795 if (!map->p[i])
12796 goto error;
12799 space = isl_multi_aff_get_domain_space(ma);
12800 space = isl_space_set(isl_map_get_space(map), type, space);
12802 isl_space_free(map->dim);
12803 map->dim = space;
12804 if (!map->dim)
12805 goto error;
12807 isl_multi_aff_free(ma);
12808 if (map->n > 1)
12809 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12810 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12811 return map;
12812 error:
12813 isl_multi_aff_free(ma);
12814 isl_map_free(map);
12815 return NULL;
12818 /* Compute the preimage of the domain or range (depending on "type")
12819 * of "map" under the function represented by "ma".
12820 * In other words, plug in "ma" in the domain or range of "map".
12821 * The result is a map that lives in the same space as "map"
12822 * except that the domain or range has been replaced by
12823 * the domain space of "ma".
12825 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12826 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12828 if (!map || !ma)
12829 goto error;
12831 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12832 return map_preimage_multi_aff(map, type, ma);
12834 if (!isl_space_has_named_params(map->dim) ||
12835 !isl_space_has_named_params(ma->space))
12836 isl_die(map->ctx, isl_error_invalid,
12837 "unaligned unnamed parameters", goto error);
12838 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12839 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12841 return map_preimage_multi_aff(map, type, ma);
12842 error:
12843 isl_multi_aff_free(ma);
12844 return isl_map_free(map);
12847 /* Compute the preimage of "set" under the function represented by "ma".
12848 * In other words, plug in "ma" in "set". The result is a set
12849 * that lives in the domain space of "ma".
12851 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12852 __isl_take isl_multi_aff *ma)
12854 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12857 /* Compute the preimage of the domain of "map" under the function
12858 * represented by "ma".
12859 * In other words, plug in "ma" in the domain of "map".
12860 * The result is a map that lives in the same space as "map"
12861 * except that the domain has been replaced by the domain space of "ma".
12863 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12864 __isl_take isl_multi_aff *ma)
12866 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12869 /* Compute the preimage of the range of "map" under the function
12870 * represented by "ma".
12871 * In other words, plug in "ma" in the range of "map".
12872 * The result is a map that lives in the same space as "map"
12873 * except that the range has been replaced by the domain space of "ma".
12875 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12876 __isl_take isl_multi_aff *ma)
12878 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12881 /* Compute the preimage of "map" under the function represented by "pma".
12882 * In other words, plug in "pma" in the domain or range of "map".
12883 * The result is a map that lives in the same space as "map",
12884 * except that the space of type "type" has been replaced by
12885 * the domain space of "pma".
12887 * The parameters of "map" and "pma" are assumed to have been aligned.
12889 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12890 __isl_take isl_map *map, enum isl_dim_type type,
12891 __isl_take isl_pw_multi_aff *pma)
12893 int i;
12894 isl_map *res;
12896 if (!pma)
12897 goto error;
12899 if (pma->n == 0) {
12900 isl_pw_multi_aff_free(pma);
12901 res = isl_map_empty(isl_map_get_space(map));
12902 isl_map_free(map);
12903 return res;
12906 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12907 isl_multi_aff_copy(pma->p[0].maff));
12908 if (type == isl_dim_in)
12909 res = isl_map_intersect_domain(res,
12910 isl_map_copy(pma->p[0].set));
12911 else
12912 res = isl_map_intersect_range(res,
12913 isl_map_copy(pma->p[0].set));
12915 for (i = 1; i < pma->n; ++i) {
12916 isl_map *res_i;
12918 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12919 isl_multi_aff_copy(pma->p[i].maff));
12920 if (type == isl_dim_in)
12921 res_i = isl_map_intersect_domain(res_i,
12922 isl_map_copy(pma->p[i].set));
12923 else
12924 res_i = isl_map_intersect_range(res_i,
12925 isl_map_copy(pma->p[i].set));
12926 res = isl_map_union(res, res_i);
12929 isl_pw_multi_aff_free(pma);
12930 isl_map_free(map);
12931 return res;
12932 error:
12933 isl_pw_multi_aff_free(pma);
12934 isl_map_free(map);
12935 return NULL;
12938 /* Compute the preimage of "map" under the function represented by "pma".
12939 * In other words, plug in "pma" in the domain or range of "map".
12940 * The result is a map that lives in the same space as "map",
12941 * except that the space of type "type" has been replaced by
12942 * the domain space of "pma".
12944 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12945 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12947 if (!map || !pma)
12948 goto error;
12950 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12951 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12953 if (!isl_space_has_named_params(map->dim) ||
12954 !isl_space_has_named_params(pma->dim))
12955 isl_die(map->ctx, isl_error_invalid,
12956 "unaligned unnamed parameters", goto error);
12957 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12958 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12960 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12961 error:
12962 isl_pw_multi_aff_free(pma);
12963 return isl_map_free(map);
12966 /* Compute the preimage of "set" under the function represented by "pma".
12967 * In other words, plug in "pma" in "set". The result is a set
12968 * that lives in the domain space of "pma".
12970 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12971 __isl_take isl_pw_multi_aff *pma)
12973 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12976 /* Compute the preimage of the domain of "map" under the function
12977 * represented by "pma".
12978 * In other words, plug in "pma" in the domain of "map".
12979 * The result is a map that lives in the same space as "map",
12980 * except that domain space has been replaced by the domain space of "pma".
12982 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12983 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12985 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12988 /* Compute the preimage of the range of "map" under the function
12989 * represented by "pma".
12990 * In other words, plug in "pma" in the range of "map".
12991 * The result is a map that lives in the same space as "map",
12992 * except that range space has been replaced by the domain space of "pma".
12994 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12995 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12997 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
13000 /* Compute the preimage of "map" under the function represented by "mpa".
13001 * In other words, plug in "mpa" in the domain or range of "map".
13002 * The result is a map that lives in the same space as "map",
13003 * except that the space of type "type" has been replaced by
13004 * the domain space of "mpa".
13006 * If the map does not involve any constraints that refer to the
13007 * dimensions of the substituted space, then the only possible
13008 * effect of "mpa" on the map is to map the space to a different space.
13009 * We create a separate isl_multi_aff to effectuate this change
13010 * in order to avoid spurious splitting of the map along the pieces
13011 * of "mpa".
13013 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
13014 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
13016 int n;
13017 isl_pw_multi_aff *pma;
13019 if (!map || !mpa)
13020 goto error;
13022 n = isl_map_dim(map, type);
13023 if (!isl_map_involves_dims(map, type, 0, n)) {
13024 isl_space *space;
13025 isl_multi_aff *ma;
13027 space = isl_multi_pw_aff_get_space(mpa);
13028 isl_multi_pw_aff_free(mpa);
13029 ma = isl_multi_aff_zero(space);
13030 return isl_map_preimage_multi_aff(map, type, ma);
13033 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13034 return isl_map_preimage_pw_multi_aff(map, type, pma);
13035 error:
13036 isl_map_free(map);
13037 isl_multi_pw_aff_free(mpa);
13038 return NULL;
13041 /* Compute the preimage of "map" under the function represented by "mpa".
13042 * In other words, plug in "mpa" in the domain "map".
13043 * The result is a map that lives in the same space as "map",
13044 * except that domain space has been replaced by the domain space of "mpa".
13046 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13047 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13049 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13052 /* Compute the preimage of "set" by the function represented by "mpa".
13053 * In other words, plug in "mpa" in "set".
13055 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13056 __isl_take isl_multi_pw_aff *mpa)
13058 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);