isl_scheduler.c: inter_coefficients: use isl_map_to_basic_set_try_get
[isl.git] / isl_map.c
blobb9b5eb5bfa98dd18cbf805736cbac778eedb1dc2
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
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
18 #include <string.h>
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
21 #include <isl_blk.h>
22 #include <isl/constraint.h>
23 #include "isl_space_private.h"
24 #include "isl_equalities.h"
25 #include <isl_lp_private.h>
26 #include <isl_seq.h>
27 #include <isl/set.h>
28 #include <isl/map.h>
29 #include <isl_reordering.h>
30 #include "isl_sample.h"
31 #include <isl_sort.h>
32 #include "isl_tab.h"
33 #include <isl/vec.h>
34 #include <isl_mat_private.h>
35 #include <isl_vec_private.h>
36 #include <isl_dim_map.h>
37 #include <isl_local_space_private.h>
38 #include <isl_aff_private.h>
39 #include <isl_options_private.h>
40 #include <isl_morph.h>
41 #include <isl_val_private.h>
42 #include <isl/deprecated/map_int.h>
43 #include <isl/deprecated/set_int.h>
45 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
47 switch (type) {
48 case isl_dim_param: return dim->nparam;
49 case isl_dim_in: return dim->n_in;
50 case isl_dim_out: return dim->n_out;
51 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
52 default: return 0;
56 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
58 switch (type) {
59 case isl_dim_param: return 1;
60 case isl_dim_in: return 1 + dim->nparam;
61 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
62 default: return 0;
66 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
67 enum isl_dim_type type)
69 if (!bmap)
70 return 0;
71 switch (type) {
72 case isl_dim_cst: return 1;
73 case isl_dim_param:
74 case isl_dim_in:
75 case isl_dim_out: return isl_space_dim(bmap->dim, type);
76 case isl_dim_div: return bmap->n_div;
77 case isl_dim_all: return isl_basic_map_total_dim(bmap);
78 default: return 0;
82 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
84 return map ? n(map->dim, type) : 0;
87 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
89 return set ? n(set->dim, type) : 0;
92 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
93 enum isl_dim_type type)
95 isl_space *dim = bmap->dim;
96 switch (type) {
97 case isl_dim_cst: return 0;
98 case isl_dim_param: return 1;
99 case isl_dim_in: return 1 + dim->nparam;
100 case isl_dim_out: return 1 + dim->nparam + dim->n_in;
101 case isl_dim_div: return 1 + dim->nparam + dim->n_in + dim->n_out;
102 default: return 0;
106 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
107 enum isl_dim_type type)
109 return isl_basic_map_offset(bset, type);
112 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
114 return pos(map->dim, type);
117 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
118 enum isl_dim_type type)
120 return isl_basic_map_dim(bset, type);
123 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
125 return isl_basic_set_dim(bset, isl_dim_set);
128 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
130 return isl_basic_set_dim(bset, isl_dim_param);
133 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
135 if (!bset)
136 return 0;
137 return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
140 unsigned isl_set_n_dim(__isl_keep isl_set *set)
142 return isl_set_dim(set, isl_dim_set);
145 unsigned isl_set_n_param(__isl_keep isl_set *set)
147 return isl_set_dim(set, isl_dim_param);
150 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
152 return bmap ? bmap->dim->n_in : 0;
155 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
157 return bmap ? bmap->dim->n_out : 0;
160 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
162 return bmap ? bmap->dim->nparam : 0;
165 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
167 return bmap ? bmap->n_div : 0;
170 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
172 return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
175 unsigned isl_map_n_in(const struct isl_map *map)
177 return map ? map->dim->n_in : 0;
180 unsigned isl_map_n_out(const struct isl_map *map)
182 return map ? map->dim->n_out : 0;
185 unsigned isl_map_n_param(const struct isl_map *map)
187 return map ? map->dim->nparam : 0;
190 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
192 int m;
193 if (!map || !set)
194 return -1;
195 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
196 if (m < 0 || !m)
197 return m;
198 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
199 set->dim, isl_dim_set);
202 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
203 struct isl_basic_set *bset)
205 int m;
206 if (!bmap || !bset)
207 return -1;
208 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
209 if (m < 0 || !m)
210 return m;
211 return isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
212 bset->dim, isl_dim_set);
215 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
217 int m;
218 if (!map || !set)
219 return -1;
220 m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
221 if (m < 0 || !m)
222 return m;
223 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
224 set->dim, isl_dim_set);
227 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
228 struct isl_basic_set *bset)
230 int m;
231 if (!bmap || !bset)
232 return -1;
233 m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
234 if (m < 0 || !m)
235 return m;
236 return isl_space_tuple_is_equal(bmap->dim, isl_dim_out,
237 bset->dim, isl_dim_set);
240 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
242 return bmap ? bmap->ctx : NULL;
245 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
247 return bset ? bset->ctx : NULL;
250 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
252 return map ? map->ctx : NULL;
255 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
257 return set ? set->ctx : NULL;
260 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
262 if (!bmap)
263 return NULL;
264 return isl_space_copy(bmap->dim);
267 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
269 if (!bset)
270 return NULL;
271 return isl_space_copy(bset->dim);
274 /* Extract the divs in "bmap" as a matrix.
276 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
278 int i;
279 isl_ctx *ctx;
280 isl_mat *div;
281 unsigned total;
282 unsigned cols;
284 if (!bmap)
285 return NULL;
287 ctx = isl_basic_map_get_ctx(bmap);
288 total = isl_space_dim(bmap->dim, isl_dim_all);
289 cols = 1 + 1 + total + bmap->n_div;
290 div = isl_mat_alloc(ctx, bmap->n_div, cols);
291 if (!div)
292 return NULL;
294 for (i = 0; i < bmap->n_div; ++i)
295 isl_seq_cpy(div->row[i], bmap->div[i], cols);
297 return div;
300 /* Extract the divs in "bset" as a matrix.
302 __isl_give isl_mat *isl_basic_set_get_divs(__isl_keep isl_basic_set *bset)
304 return isl_basic_map_get_divs(bset);
307 __isl_give isl_local_space *isl_basic_map_get_local_space(
308 __isl_keep isl_basic_map *bmap)
310 isl_mat *div;
312 if (!bmap)
313 return NULL;
315 div = isl_basic_map_get_divs(bmap);
316 return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
319 __isl_give isl_local_space *isl_basic_set_get_local_space(
320 __isl_keep isl_basic_set *bset)
322 return isl_basic_map_get_local_space(bset);
325 /* For each known div d = floor(f/m), add the constraints
327 * f - m d >= 0
328 * -(f-(n-1)) + m d >= 0
330 * Do not finalize the result.
332 static __isl_give isl_basic_map *add_known_div_constraints(
333 __isl_take isl_basic_map *bmap)
335 int i;
336 unsigned n_div;
338 if (!bmap)
339 return NULL;
340 n_div = isl_basic_map_dim(bmap, isl_dim_div);
341 if (n_div == 0)
342 return bmap;
343 bmap = isl_basic_map_cow(bmap);
344 bmap = isl_basic_map_extend_constraints(bmap, 0, 2 * n_div);
345 if (!bmap)
346 return NULL;
347 for (i = 0; i < n_div; ++i) {
348 if (isl_int_is_zero(bmap->div[i][0]))
349 continue;
350 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
351 return isl_basic_map_free(bmap);
354 return bmap;
357 __isl_give isl_basic_map *isl_basic_map_from_local_space(
358 __isl_take isl_local_space *ls)
360 int i;
361 int n_div;
362 isl_basic_map *bmap;
364 if (!ls)
365 return NULL;
367 n_div = isl_local_space_dim(ls, isl_dim_div);
368 bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
369 n_div, 0, 2 * n_div);
371 for (i = 0; i < n_div; ++i)
372 if (isl_basic_map_alloc_div(bmap) < 0)
373 goto error;
375 for (i = 0; i < n_div; ++i)
376 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
377 bmap = add_known_div_constraints(bmap);
379 isl_local_space_free(ls);
380 return bmap;
381 error:
382 isl_local_space_free(ls);
383 isl_basic_map_free(bmap);
384 return NULL;
387 __isl_give isl_basic_set *isl_basic_set_from_local_space(
388 __isl_take isl_local_space *ls)
390 return isl_basic_map_from_local_space(ls);
393 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
395 if (!map)
396 return NULL;
397 return isl_space_copy(map->dim);
400 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
402 if (!set)
403 return NULL;
404 return isl_space_copy(set->dim);
407 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
408 __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
410 bmap = isl_basic_map_cow(bmap);
411 if (!bmap)
412 return NULL;
413 bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
414 if (!bmap->dim)
415 goto error;
416 bmap = isl_basic_map_finalize(bmap);
417 return bmap;
418 error:
419 isl_basic_map_free(bmap);
420 return NULL;
423 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
424 __isl_take isl_basic_set *bset, const char *s)
426 return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
429 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
430 enum isl_dim_type type)
432 return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
435 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
436 enum isl_dim_type type, const char *s)
438 int i;
440 map = isl_map_cow(map);
441 if (!map)
442 return NULL;
444 map->dim = isl_space_set_tuple_name(map->dim, type, s);
445 if (!map->dim)
446 goto error;
448 for (i = 0; i < map->n; ++i) {
449 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
450 if (!map->p[i])
451 goto error;
454 return map;
455 error:
456 isl_map_free(map);
457 return NULL;
460 /* Replace the identifier of the tuple of type "type" by "id".
462 __isl_give isl_basic_map *isl_basic_map_set_tuple_id(
463 __isl_take isl_basic_map *bmap,
464 enum isl_dim_type type, __isl_take isl_id *id)
466 bmap = isl_basic_map_cow(bmap);
467 if (!bmap)
468 goto error;
469 bmap->dim = isl_space_set_tuple_id(bmap->dim, type, id);
470 if (!bmap->dim)
471 return isl_basic_map_free(bmap);
472 bmap = isl_basic_map_finalize(bmap);
473 return bmap;
474 error:
475 isl_id_free(id);
476 return NULL;
479 /* Replace the identifier of the tuple by "id".
481 __isl_give isl_basic_set *isl_basic_set_set_tuple_id(
482 __isl_take isl_basic_set *bset, __isl_take isl_id *id)
484 return isl_basic_map_set_tuple_id(bset, isl_dim_set, id);
487 /* Does the input or output tuple have a name?
489 isl_bool isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
491 return map ? isl_space_has_tuple_name(map->dim, type) : isl_bool_error;
494 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
495 enum isl_dim_type type)
497 return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
500 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
501 const char *s)
503 return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
506 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
507 enum isl_dim_type type, __isl_take isl_id *id)
509 map = isl_map_cow(map);
510 if (!map)
511 goto error;
513 map->dim = isl_space_set_tuple_id(map->dim, type, id);
515 return isl_map_reset_space(map, isl_space_copy(map->dim));
516 error:
517 isl_id_free(id);
518 return NULL;
521 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
522 __isl_take isl_id *id)
524 return isl_map_set_tuple_id(set, isl_dim_set, id);
527 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
528 enum isl_dim_type type)
530 map = isl_map_cow(map);
531 if (!map)
532 return NULL;
534 map->dim = isl_space_reset_tuple_id(map->dim, type);
536 return isl_map_reset_space(map, isl_space_copy(map->dim));
539 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
541 return isl_map_reset_tuple_id(set, isl_dim_set);
544 isl_bool isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
546 return map ? isl_space_has_tuple_id(map->dim, type) : isl_bool_error;
549 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
550 enum isl_dim_type type)
552 return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
555 isl_bool isl_set_has_tuple_id(__isl_keep isl_set *set)
557 return isl_map_has_tuple_id(set, isl_dim_set);
560 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
562 return isl_map_get_tuple_id(set, isl_dim_set);
565 /* Does the set tuple have a name?
567 isl_bool isl_set_has_tuple_name(__isl_keep isl_set *set)
569 if (!set)
570 return isl_bool_error;
571 return isl_space_has_tuple_name(set->dim, isl_dim_set);
575 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
577 return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
580 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
582 return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
585 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
586 enum isl_dim_type type, unsigned pos)
588 return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
591 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
592 enum isl_dim_type type, unsigned pos)
594 return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
597 /* Does the given dimension have a name?
599 isl_bool isl_map_has_dim_name(__isl_keep isl_map *map,
600 enum isl_dim_type type, unsigned pos)
602 if (!map)
603 return isl_bool_error;
604 return isl_space_has_dim_name(map->dim, type, pos);
607 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
608 enum isl_dim_type type, unsigned pos)
610 return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
613 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
614 enum isl_dim_type type, unsigned pos)
616 return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
619 /* Does the given dimension have a name?
621 isl_bool isl_set_has_dim_name(__isl_keep isl_set *set,
622 enum isl_dim_type type, unsigned pos)
624 if (!set)
625 return isl_bool_error;
626 return isl_space_has_dim_name(set->dim, type, pos);
629 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
630 __isl_take isl_basic_map *bmap,
631 enum isl_dim_type type, unsigned pos, const char *s)
633 bmap = isl_basic_map_cow(bmap);
634 if (!bmap)
635 return NULL;
636 bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
637 if (!bmap->dim)
638 goto error;
639 return isl_basic_map_finalize(bmap);
640 error:
641 isl_basic_map_free(bmap);
642 return NULL;
645 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
646 enum isl_dim_type type, unsigned pos, const char *s)
648 int i;
650 map = isl_map_cow(map);
651 if (!map)
652 return NULL;
654 map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
655 if (!map->dim)
656 goto error;
658 for (i = 0; i < map->n; ++i) {
659 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
660 if (!map->p[i])
661 goto error;
664 return map;
665 error:
666 isl_map_free(map);
667 return NULL;
670 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
671 __isl_take isl_basic_set *bset,
672 enum isl_dim_type type, unsigned pos, const char *s)
674 return (isl_basic_set *)isl_basic_map_set_dim_name(
675 (isl_basic_map *)bset, type, pos, s);
678 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
679 enum isl_dim_type type, unsigned pos, const char *s)
681 return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
684 isl_bool isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
685 enum isl_dim_type type, unsigned pos)
687 if (!bmap)
688 return isl_bool_error;
689 return isl_space_has_dim_id(bmap->dim, type, pos);
692 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
693 enum isl_dim_type type, unsigned pos)
695 return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
698 isl_bool isl_map_has_dim_id(__isl_keep isl_map *map,
699 enum isl_dim_type type, unsigned pos)
701 return map ? isl_space_has_dim_id(map->dim, type, pos) : isl_bool_error;
704 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
705 enum isl_dim_type type, unsigned pos)
707 return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
710 isl_bool isl_set_has_dim_id(__isl_keep isl_set *set,
711 enum isl_dim_type type, unsigned pos)
713 return isl_map_has_dim_id(set, type, pos);
716 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
717 enum isl_dim_type type, unsigned pos)
719 return isl_map_get_dim_id(set, type, pos);
722 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
723 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
725 map = isl_map_cow(map);
726 if (!map)
727 goto error;
729 map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
731 return isl_map_reset_space(map, isl_space_copy(map->dim));
732 error:
733 isl_id_free(id);
734 return NULL;
737 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
738 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
740 return isl_map_set_dim_id(set, type, pos, id);
743 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
744 __isl_keep isl_id *id)
746 if (!map)
747 return -1;
748 return isl_space_find_dim_by_id(map->dim, type, id);
751 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
752 __isl_keep isl_id *id)
754 return isl_map_find_dim_by_id(set, type, id);
757 /* Return the position of the dimension of the given type and name
758 * in "bmap".
759 * Return -1 if no such dimension can be found.
761 int isl_basic_map_find_dim_by_name(__isl_keep isl_basic_map *bmap,
762 enum isl_dim_type type, const char *name)
764 if (!bmap)
765 return -1;
766 return isl_space_find_dim_by_name(bmap->dim, type, name);
769 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
770 const char *name)
772 if (!map)
773 return -1;
774 return isl_space_find_dim_by_name(map->dim, type, name);
777 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
778 const char *name)
780 return isl_map_find_dim_by_name(set, type, name);
783 /* Reset the user pointer on all identifiers of parameters and tuples
784 * of the space of "map".
786 __isl_give isl_map *isl_map_reset_user(__isl_take isl_map *map)
788 isl_space *space;
790 space = isl_map_get_space(map);
791 space = isl_space_reset_user(space);
792 map = isl_map_reset_space(map, space);
794 return map;
797 /* Reset the user pointer on all identifiers of parameters and tuples
798 * of the space of "set".
800 __isl_give isl_set *isl_set_reset_user(__isl_take isl_set *set)
802 return isl_map_reset_user(set);
805 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
807 if (!bmap)
808 return -1;
809 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
812 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
814 return isl_basic_map_is_rational(bset);
817 /* Does "bmap" contain any rational points?
819 * If "bmap" has an equality for each dimension, equating the dimension
820 * to an integer constant, then it has no rational points, even if it
821 * is marked as rational.
823 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
825 int has_rational = 1;
826 unsigned total;
828 if (!bmap)
829 return -1;
830 if (isl_basic_map_plain_is_empty(bmap))
831 return 0;
832 if (!isl_basic_map_is_rational(bmap))
833 return 0;
834 bmap = isl_basic_map_copy(bmap);
835 bmap = isl_basic_map_implicit_equalities(bmap);
836 if (!bmap)
837 return -1;
838 total = isl_basic_map_total_dim(bmap);
839 if (bmap->n_eq == total) {
840 int i, j;
841 for (i = 0; i < bmap->n_eq; ++i) {
842 j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
843 if (j < 0)
844 break;
845 if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
846 !isl_int_is_negone(bmap->eq[i][1 + j]))
847 break;
848 j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
849 total - j - 1);
850 if (j >= 0)
851 break;
853 if (i == bmap->n_eq)
854 has_rational = 0;
856 isl_basic_map_free(bmap);
858 return has_rational;
861 /* Does "map" contain any rational points?
863 int isl_map_has_rational(__isl_keep isl_map *map)
865 int i;
866 int has_rational;
868 if (!map)
869 return -1;
870 for (i = 0; i < map->n; ++i) {
871 has_rational = isl_basic_map_has_rational(map->p[i]);
872 if (has_rational < 0)
873 return -1;
874 if (has_rational)
875 return 1;
877 return 0;
880 /* Does "set" contain any rational points?
882 int isl_set_has_rational(__isl_keep isl_set *set)
884 return isl_map_has_rational(set);
887 /* Is this basic set a parameter domain?
889 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
891 if (!bset)
892 return -1;
893 return isl_space_is_params(bset->dim);
896 /* Is this set a parameter domain?
898 isl_bool isl_set_is_params(__isl_keep isl_set *set)
900 if (!set)
901 return isl_bool_error;
902 return isl_space_is_params(set->dim);
905 /* Is this map actually a parameter domain?
906 * Users should never call this function. Outside of isl,
907 * a map can never be a parameter domain.
909 int isl_map_is_params(__isl_keep isl_map *map)
911 if (!map)
912 return -1;
913 return isl_space_is_params(map->dim);
916 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
917 struct isl_basic_map *bmap, unsigned extra,
918 unsigned n_eq, unsigned n_ineq)
920 int i;
921 size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
923 bmap->ctx = ctx;
924 isl_ctx_ref(ctx);
926 bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
927 if (isl_blk_is_error(bmap->block))
928 goto error;
930 bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
931 if ((n_ineq + n_eq) && !bmap->ineq)
932 goto error;
934 if (extra == 0) {
935 bmap->block2 = isl_blk_empty();
936 bmap->div = NULL;
937 } else {
938 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
939 if (isl_blk_is_error(bmap->block2))
940 goto error;
942 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
943 if (!bmap->div)
944 goto error;
947 for (i = 0; i < n_ineq + n_eq; ++i)
948 bmap->ineq[i] = bmap->block.data + i * row_size;
950 for (i = 0; i < extra; ++i)
951 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
953 bmap->ref = 1;
954 bmap->flags = 0;
955 bmap->c_size = n_eq + n_ineq;
956 bmap->eq = bmap->ineq + n_ineq;
957 bmap->extra = extra;
958 bmap->n_eq = 0;
959 bmap->n_ineq = 0;
960 bmap->n_div = 0;
961 bmap->sample = NULL;
963 return bmap;
964 error:
965 isl_basic_map_free(bmap);
966 return NULL;
969 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
970 unsigned nparam, unsigned dim, unsigned extra,
971 unsigned n_eq, unsigned n_ineq)
973 struct isl_basic_map *bmap;
974 isl_space *space;
976 space = isl_space_set_alloc(ctx, nparam, dim);
977 if (!space)
978 return NULL;
980 bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
981 return (struct isl_basic_set *)bmap;
984 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
985 unsigned extra, unsigned n_eq, unsigned n_ineq)
987 struct isl_basic_map *bmap;
988 if (!dim)
989 return NULL;
990 isl_assert(dim->ctx, dim->n_in == 0, goto error);
991 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
992 return (struct isl_basic_set *)bmap;
993 error:
994 isl_space_free(dim);
995 return NULL;
998 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
999 unsigned extra, unsigned n_eq, unsigned n_ineq)
1001 struct isl_basic_map *bmap;
1003 if (!dim)
1004 return NULL;
1005 bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
1006 if (!bmap)
1007 goto error;
1008 bmap->dim = dim;
1010 return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
1011 error:
1012 isl_space_free(dim);
1013 return NULL;
1016 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
1017 unsigned nparam, unsigned in, unsigned out, unsigned extra,
1018 unsigned n_eq, unsigned n_ineq)
1020 struct isl_basic_map *bmap;
1021 isl_space *dim;
1023 dim = isl_space_alloc(ctx, nparam, in, out);
1024 if (!dim)
1025 return NULL;
1027 bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1028 return bmap;
1031 static void dup_constraints(
1032 struct isl_basic_map *dst, struct isl_basic_map *src)
1034 int i;
1035 unsigned total = isl_basic_map_total_dim(src);
1037 for (i = 0; i < src->n_eq; ++i) {
1038 int j = isl_basic_map_alloc_equality(dst);
1039 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
1042 for (i = 0; i < src->n_ineq; ++i) {
1043 int j = isl_basic_map_alloc_inequality(dst);
1044 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
1047 for (i = 0; i < src->n_div; ++i) {
1048 int j = isl_basic_map_alloc_div(dst);
1049 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
1051 ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
1054 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
1056 struct isl_basic_map *dup;
1058 if (!bmap)
1059 return NULL;
1060 dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
1061 bmap->n_div, bmap->n_eq, bmap->n_ineq);
1062 if (!dup)
1063 return NULL;
1064 dup_constraints(dup, bmap);
1065 dup->flags = bmap->flags;
1066 dup->sample = isl_vec_copy(bmap->sample);
1067 return dup;
1070 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
1072 struct isl_basic_map *dup;
1074 dup = isl_basic_map_dup((struct isl_basic_map *)bset);
1075 return (struct isl_basic_set *)dup;
1078 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
1080 if (!bset)
1081 return NULL;
1083 if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
1084 bset->ref++;
1085 return bset;
1087 return isl_basic_set_dup(bset);
1090 struct isl_set *isl_set_copy(struct isl_set *set)
1092 if (!set)
1093 return NULL;
1095 set->ref++;
1096 return set;
1099 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
1101 if (!bmap)
1102 return NULL;
1104 if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
1105 bmap->ref++;
1106 return bmap;
1108 bmap = isl_basic_map_dup(bmap);
1109 if (bmap)
1110 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
1111 return bmap;
1114 struct isl_map *isl_map_copy(struct isl_map *map)
1116 if (!map)
1117 return NULL;
1119 map->ref++;
1120 return map;
1123 __isl_null isl_basic_map *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1125 if (!bmap)
1126 return NULL;
1128 if (--bmap->ref > 0)
1129 return NULL;
1131 isl_ctx_deref(bmap->ctx);
1132 free(bmap->div);
1133 isl_blk_free(bmap->ctx, bmap->block2);
1134 free(bmap->ineq);
1135 isl_blk_free(bmap->ctx, bmap->block);
1136 isl_vec_free(bmap->sample);
1137 isl_space_free(bmap->dim);
1138 free(bmap);
1140 return NULL;
1143 __isl_null isl_basic_set *isl_basic_set_free(__isl_take isl_basic_set *bset)
1145 return isl_basic_map_free((struct isl_basic_map *)bset);
1148 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1150 return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1153 __isl_give isl_map *isl_map_align_params_map_map_and(
1154 __isl_take isl_map *map1, __isl_take isl_map *map2,
1155 __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1156 __isl_take isl_map *map2))
1158 if (!map1 || !map2)
1159 goto error;
1160 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1161 return fn(map1, map2);
1162 if (!isl_space_has_named_params(map1->dim) ||
1163 !isl_space_has_named_params(map2->dim))
1164 isl_die(map1->ctx, isl_error_invalid,
1165 "unaligned unnamed parameters", goto error);
1166 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1167 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1168 return fn(map1, map2);
1169 error:
1170 isl_map_free(map1);
1171 isl_map_free(map2);
1172 return NULL;
1175 isl_bool isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1176 __isl_keep isl_map *map2,
1177 isl_bool (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1179 isl_bool r;
1181 if (!map1 || !map2)
1182 return isl_bool_error;
1183 if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1184 return fn(map1, map2);
1185 if (!isl_space_has_named_params(map1->dim) ||
1186 !isl_space_has_named_params(map2->dim))
1187 isl_die(map1->ctx, isl_error_invalid,
1188 "unaligned unnamed parameters", return isl_bool_error);
1189 map1 = isl_map_copy(map1);
1190 map2 = isl_map_copy(map2);
1191 map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1192 map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1193 r = fn(map1, map2);
1194 isl_map_free(map1);
1195 isl_map_free(map2);
1196 return r;
1199 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1201 struct isl_ctx *ctx;
1202 if (!bmap)
1203 return -1;
1204 ctx = bmap->ctx;
1205 isl_assert(ctx, room_for_con(bmap, 1), return -1);
1206 isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1207 return -1);
1208 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1209 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1210 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1211 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1212 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1213 if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1214 isl_int *t;
1215 int j = isl_basic_map_alloc_inequality(bmap);
1216 if (j < 0)
1217 return -1;
1218 t = bmap->ineq[j];
1219 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1220 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1221 bmap->eq[-1] = t;
1222 bmap->n_eq++;
1223 bmap->n_ineq--;
1224 bmap->eq--;
1225 return 0;
1227 isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1228 bmap->extra - bmap->n_div);
1229 return bmap->n_eq++;
1232 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1234 return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1237 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1239 if (!bmap)
1240 return -1;
1241 isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1242 bmap->n_eq -= n;
1243 return 0;
1246 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1248 return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1251 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1253 isl_int *t;
1254 if (!bmap)
1255 return -1;
1256 isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1258 if (pos != bmap->n_eq - 1) {
1259 t = bmap->eq[pos];
1260 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1261 bmap->eq[bmap->n_eq - 1] = t;
1263 bmap->n_eq--;
1264 return 0;
1267 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1269 return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1272 /* Turn inequality "pos" of "bmap" into an equality.
1274 * In particular, we move the inequality in front of the equalities
1275 * and move the last inequality in the position of the moved inequality.
1276 * Note that isl_tab_make_equalities_explicit depends on this particular
1277 * change in the ordering of the constraints.
1279 void isl_basic_map_inequality_to_equality(
1280 struct isl_basic_map *bmap, unsigned pos)
1282 isl_int *t;
1284 t = bmap->ineq[pos];
1285 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1286 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1287 bmap->eq[-1] = t;
1288 bmap->n_eq++;
1289 bmap->n_ineq--;
1290 bmap->eq--;
1291 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1292 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1293 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1294 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1297 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1299 return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1302 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1304 struct isl_ctx *ctx;
1305 if (!bmap)
1306 return -1;
1307 ctx = bmap->ctx;
1308 isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1309 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1310 ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1311 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1312 ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1313 isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1314 1 + isl_basic_map_total_dim(bmap),
1315 bmap->extra - bmap->n_div);
1316 return bmap->n_ineq++;
1319 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1321 return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1324 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1326 if (!bmap)
1327 return -1;
1328 isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1329 bmap->n_ineq -= n;
1330 return 0;
1333 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1335 return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1338 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1340 isl_int *t;
1341 if (!bmap)
1342 return -1;
1343 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1345 if (pos != bmap->n_ineq - 1) {
1346 t = bmap->ineq[pos];
1347 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1348 bmap->ineq[bmap->n_ineq - 1] = t;
1349 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1351 bmap->n_ineq--;
1352 return 0;
1355 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1357 return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1360 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1361 isl_int *eq)
1363 int k;
1365 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1366 if (!bmap)
1367 return NULL;
1368 k = isl_basic_map_alloc_equality(bmap);
1369 if (k < 0)
1370 goto error;
1371 isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1372 return bmap;
1373 error:
1374 isl_basic_map_free(bmap);
1375 return NULL;
1378 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1379 isl_int *eq)
1381 return (isl_basic_set *)
1382 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1385 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1386 isl_int *ineq)
1388 int k;
1390 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1391 if (!bmap)
1392 return NULL;
1393 k = isl_basic_map_alloc_inequality(bmap);
1394 if (k < 0)
1395 goto error;
1396 isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1397 return bmap;
1398 error:
1399 isl_basic_map_free(bmap);
1400 return NULL;
1403 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1404 isl_int *ineq)
1406 return (isl_basic_set *)
1407 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1410 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1412 if (!bmap)
1413 return -1;
1414 isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1415 isl_seq_clr(bmap->div[bmap->n_div] +
1416 1 + 1 + isl_basic_map_total_dim(bmap),
1417 bmap->extra - bmap->n_div);
1418 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1419 return bmap->n_div++;
1422 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1424 return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1427 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1429 if (!bmap)
1430 return -1;
1431 isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1432 bmap->n_div -= n;
1433 return 0;
1436 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1438 return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1441 /* Copy constraint from src to dst, putting the vars of src at offset
1442 * dim_off in dst and the divs of src at offset div_off in dst.
1443 * If both sets are actually map, then dim_off applies to the input
1444 * variables.
1446 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1447 struct isl_basic_map *src_map, isl_int *src,
1448 unsigned in_off, unsigned out_off, unsigned div_off)
1450 unsigned src_nparam = isl_basic_map_n_param(src_map);
1451 unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1452 unsigned src_in = isl_basic_map_n_in(src_map);
1453 unsigned dst_in = isl_basic_map_n_in(dst_map);
1454 unsigned src_out = isl_basic_map_n_out(src_map);
1455 unsigned dst_out = isl_basic_map_n_out(dst_map);
1456 isl_int_set(dst[0], src[0]);
1457 isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1458 if (dst_nparam > src_nparam)
1459 isl_seq_clr(dst+1+src_nparam,
1460 dst_nparam - src_nparam);
1461 isl_seq_clr(dst+1+dst_nparam, in_off);
1462 isl_seq_cpy(dst+1+dst_nparam+in_off,
1463 src+1+src_nparam,
1464 isl_min(dst_in-in_off, src_in));
1465 if (dst_in-in_off > src_in)
1466 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1467 dst_in - in_off - src_in);
1468 isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1469 isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1470 src+1+src_nparam+src_in,
1471 isl_min(dst_out-out_off, src_out));
1472 if (dst_out-out_off > src_out)
1473 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1474 dst_out - out_off - src_out);
1475 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1476 isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1477 src+1+src_nparam+src_in+src_out,
1478 isl_min(dst_map->extra-div_off, src_map->n_div));
1479 if (dst_map->n_div-div_off > src_map->n_div)
1480 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1481 div_off+src_map->n_div,
1482 dst_map->n_div - div_off - src_map->n_div);
1485 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1486 struct isl_basic_map *src_map, isl_int *src,
1487 unsigned in_off, unsigned out_off, unsigned div_off)
1489 isl_int_set(dst[0], src[0]);
1490 copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1493 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1494 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1496 int i;
1497 unsigned div_off;
1499 if (!bmap1 || !bmap2)
1500 goto error;
1502 div_off = bmap1->n_div;
1504 for (i = 0; i < bmap2->n_eq; ++i) {
1505 int i1 = isl_basic_map_alloc_equality(bmap1);
1506 if (i1 < 0)
1507 goto error;
1508 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1509 i_pos, o_pos, div_off);
1512 for (i = 0; i < bmap2->n_ineq; ++i) {
1513 int i1 = isl_basic_map_alloc_inequality(bmap1);
1514 if (i1 < 0)
1515 goto error;
1516 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1517 i_pos, o_pos, div_off);
1520 for (i = 0; i < bmap2->n_div; ++i) {
1521 int i1 = isl_basic_map_alloc_div(bmap1);
1522 if (i1 < 0)
1523 goto error;
1524 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1525 i_pos, o_pos, div_off);
1528 isl_basic_map_free(bmap2);
1530 return bmap1;
1532 error:
1533 isl_basic_map_free(bmap1);
1534 isl_basic_map_free(bmap2);
1535 return NULL;
1538 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1539 struct isl_basic_set *bset2, unsigned pos)
1541 return (struct isl_basic_set *)
1542 add_constraints((struct isl_basic_map *)bset1,
1543 (struct isl_basic_map *)bset2, 0, pos);
1546 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1547 __isl_take isl_space *dim, unsigned extra,
1548 unsigned n_eq, unsigned n_ineq)
1550 struct isl_basic_map *ext;
1551 unsigned flags;
1552 int dims_ok;
1554 if (!dim)
1555 goto error;
1557 if (!base)
1558 goto error;
1560 dims_ok = isl_space_is_equal(base->dim, dim) &&
1561 base->extra >= base->n_div + extra;
1563 if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1564 room_for_ineq(base, n_ineq)) {
1565 isl_space_free(dim);
1566 return base;
1569 isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1570 isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1571 isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1572 extra += base->extra;
1573 n_eq += base->n_eq;
1574 n_ineq += base->n_ineq;
1576 ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1577 dim = NULL;
1578 if (!ext)
1579 goto error;
1581 if (dims_ok)
1582 ext->sample = isl_vec_copy(base->sample);
1583 flags = base->flags;
1584 ext = add_constraints(ext, base, 0, 0);
1585 if (ext) {
1586 ext->flags = flags;
1587 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1590 return ext;
1592 error:
1593 isl_space_free(dim);
1594 isl_basic_map_free(base);
1595 return NULL;
1598 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1599 __isl_take isl_space *dim, unsigned extra,
1600 unsigned n_eq, unsigned n_ineq)
1602 return (struct isl_basic_set *)
1603 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1604 extra, n_eq, n_ineq);
1607 struct isl_basic_map *isl_basic_map_extend_constraints(
1608 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1610 if (!base)
1611 return NULL;
1612 return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1613 0, n_eq, n_ineq);
1616 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1617 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1618 unsigned n_eq, unsigned n_ineq)
1620 struct isl_basic_map *bmap;
1621 isl_space *dim;
1623 if (!base)
1624 return NULL;
1625 dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1626 if (!dim)
1627 goto error;
1629 bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1630 return bmap;
1631 error:
1632 isl_basic_map_free(base);
1633 return NULL;
1636 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1637 unsigned nparam, unsigned dim, unsigned extra,
1638 unsigned n_eq, unsigned n_ineq)
1640 return (struct isl_basic_set *)
1641 isl_basic_map_extend((struct isl_basic_map *)base,
1642 nparam, 0, dim, extra, n_eq, n_ineq);
1645 struct isl_basic_set *isl_basic_set_extend_constraints(
1646 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1648 return (struct isl_basic_set *)
1649 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1650 n_eq, n_ineq);
1653 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1655 return (struct isl_basic_set *)
1656 isl_basic_map_cow((struct isl_basic_map *)bset);
1659 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1661 if (!bmap)
1662 return NULL;
1664 if (bmap->ref > 1) {
1665 bmap->ref--;
1666 bmap = isl_basic_map_dup(bmap);
1668 if (bmap) {
1669 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1670 ISL_F_CLR(bmap, ISL_BASIC_MAP_REDUCED_COEFFICIENTS);
1672 return bmap;
1675 struct isl_set *isl_set_cow(struct isl_set *set)
1677 if (!set)
1678 return NULL;
1680 if (set->ref == 1)
1681 return set;
1682 set->ref--;
1683 return isl_set_dup(set);
1686 struct isl_map *isl_map_cow(struct isl_map *map)
1688 if (!map)
1689 return NULL;
1691 if (map->ref == 1)
1692 return map;
1693 map->ref--;
1694 return isl_map_dup(map);
1697 static void swap_vars(struct isl_blk blk, isl_int *a,
1698 unsigned a_len, unsigned b_len)
1700 isl_seq_cpy(blk.data, a+a_len, b_len);
1701 isl_seq_cpy(blk.data+b_len, a, a_len);
1702 isl_seq_cpy(a, blk.data, b_len+a_len);
1705 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1706 __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1708 int i;
1709 struct isl_blk blk;
1711 if (!bmap)
1712 goto error;
1714 isl_assert(bmap->ctx,
1715 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1717 if (n1 == 0 || n2 == 0)
1718 return bmap;
1720 bmap = isl_basic_map_cow(bmap);
1721 if (!bmap)
1722 return NULL;
1724 blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1725 if (isl_blk_is_error(blk))
1726 goto error;
1728 for (i = 0; i < bmap->n_eq; ++i)
1729 swap_vars(blk,
1730 bmap->eq[i] + pos, n1, n2);
1732 for (i = 0; i < bmap->n_ineq; ++i)
1733 swap_vars(blk,
1734 bmap->ineq[i] + pos, n1, n2);
1736 for (i = 0; i < bmap->n_div; ++i)
1737 swap_vars(blk,
1738 bmap->div[i]+1 + pos, n1, n2);
1740 isl_blk_free(bmap->ctx, blk);
1742 ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1743 bmap = isl_basic_map_gauss(bmap, NULL);
1744 return isl_basic_map_finalize(bmap);
1745 error:
1746 isl_basic_map_free(bmap);
1747 return NULL;
1750 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1752 int i = 0;
1753 unsigned total;
1754 if (!bmap)
1755 goto error;
1756 total = isl_basic_map_total_dim(bmap);
1757 isl_basic_map_free_div(bmap, bmap->n_div);
1758 isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1759 if (bmap->n_eq > 0)
1760 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1761 else {
1762 i = isl_basic_map_alloc_equality(bmap);
1763 if (i < 0)
1764 goto error;
1766 isl_int_set_si(bmap->eq[i][0], 1);
1767 isl_seq_clr(bmap->eq[i]+1, total);
1768 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1769 isl_vec_free(bmap->sample);
1770 bmap->sample = NULL;
1771 return isl_basic_map_finalize(bmap);
1772 error:
1773 isl_basic_map_free(bmap);
1774 return NULL;
1777 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1779 return (struct isl_basic_set *)
1780 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1783 /* Swap divs "a" and "b" in "bmap" (without modifying any of the constraints
1784 * of "bmap").
1786 static void swap_div(__isl_keep isl_basic_map *bmap, int a, int b)
1788 isl_int *t = bmap->div[a];
1789 bmap->div[a] = bmap->div[b];
1790 bmap->div[b] = t;
1793 /* Swap divs "a" and "b" in "bmap" and adjust the constraints and
1794 * div definitions accordingly.
1796 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1798 int i;
1799 unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1801 swap_div(bmap, a, b);
1803 for (i = 0; i < bmap->n_eq; ++i)
1804 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1806 for (i = 0; i < bmap->n_ineq; ++i)
1807 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1809 for (i = 0; i < bmap->n_div; ++i)
1810 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1811 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1814 /* Eliminate the specified n dimensions starting at first from the
1815 * constraints, without removing the dimensions from the space.
1816 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1818 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1819 enum isl_dim_type type, unsigned first, unsigned n)
1821 int i;
1823 if (!map)
1824 return NULL;
1825 if (n == 0)
1826 return map;
1828 if (first + n > isl_map_dim(map, type) || first + n < first)
1829 isl_die(map->ctx, isl_error_invalid,
1830 "index out of bounds", goto error);
1832 map = isl_map_cow(map);
1833 if (!map)
1834 return NULL;
1836 for (i = 0; i < map->n; ++i) {
1837 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1838 if (!map->p[i])
1839 goto error;
1841 return map;
1842 error:
1843 isl_map_free(map);
1844 return NULL;
1847 /* Eliminate the specified n dimensions starting at first from the
1848 * constraints, without removing the dimensions from the space.
1849 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1851 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1852 enum isl_dim_type type, unsigned first, unsigned n)
1854 return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1857 /* Eliminate the specified n dimensions starting at first from the
1858 * constraints, without removing the dimensions from the space.
1859 * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1861 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1862 unsigned first, unsigned n)
1864 return isl_set_eliminate(set, isl_dim_set, first, n);
1867 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1868 __isl_take isl_basic_map *bmap)
1870 if (!bmap)
1871 return NULL;
1872 bmap = isl_basic_map_eliminate_vars(bmap,
1873 isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1874 if (!bmap)
1875 return NULL;
1876 bmap->n_div = 0;
1877 return isl_basic_map_finalize(bmap);
1880 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1881 __isl_take isl_basic_set *bset)
1883 return (struct isl_basic_set *)isl_basic_map_remove_divs(
1884 (struct isl_basic_map *)bset);
1887 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1889 int i;
1891 if (!map)
1892 return NULL;
1893 if (map->n == 0)
1894 return map;
1896 map = isl_map_cow(map);
1897 if (!map)
1898 return NULL;
1900 for (i = 0; i < map->n; ++i) {
1901 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1902 if (!map->p[i])
1903 goto error;
1905 return map;
1906 error:
1907 isl_map_free(map);
1908 return NULL;
1911 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1913 return isl_map_remove_divs(set);
1916 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1917 enum isl_dim_type type, unsigned first, unsigned n)
1919 if (!bmap)
1920 return NULL;
1921 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1922 goto error);
1923 if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1924 return bmap;
1925 bmap = isl_basic_map_eliminate_vars(bmap,
1926 isl_basic_map_offset(bmap, type) - 1 + first, n);
1927 if (!bmap)
1928 return bmap;
1929 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1930 return bmap;
1931 bmap = isl_basic_map_drop(bmap, type, first, n);
1932 return bmap;
1933 error:
1934 isl_basic_map_free(bmap);
1935 return NULL;
1938 /* Return true if the definition of the given div (recursively) involves
1939 * any of the given variables.
1941 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1942 unsigned first, unsigned n)
1944 int i;
1945 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1947 if (isl_int_is_zero(bmap->div[div][0]))
1948 return 0;
1949 if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1950 return 1;
1952 for (i = bmap->n_div - 1; i >= 0; --i) {
1953 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1954 continue;
1955 if (div_involves_vars(bmap, i, first, n))
1956 return 1;
1959 return 0;
1962 /* Try and add a lower and/or upper bound on "div" to "bmap"
1963 * based on inequality "i".
1964 * "total" is the total number of variables (excluding the divs).
1965 * "v" is a temporary object that can be used during the calculations.
1966 * If "lb" is set, then a lower bound should be constructed.
1967 * If "ub" is set, then an upper bound should be constructed.
1969 * The calling function has already checked that the inequality does not
1970 * reference "div", but we still need to check that the inequality is
1971 * of the right form. We'll consider the case where we want to construct
1972 * a lower bound. The construction of upper bounds is similar.
1974 * Let "div" be of the form
1976 * q = floor((a + f(x))/d)
1978 * We essentially check if constraint "i" is of the form
1980 * b + f(x) >= 0
1982 * so that we can use it to derive a lower bound on "div".
1983 * However, we allow a slightly more general form
1985 * b + g(x) >= 0
1987 * with the condition that the coefficients of g(x) - f(x) are all
1988 * divisible by d.
1989 * Rewriting this constraint as
1991 * 0 >= -b - g(x)
1993 * adding a + f(x) to both sides and dividing by d, we obtain
1995 * (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1997 * Taking the floor on both sides, we obtain
1999 * q >= floor((a-b)/d) + (f(x)-g(x))/d
2001 * or
2003 * (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
2005 * In the case of an upper bound, we construct the constraint
2007 * (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
2010 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
2011 __isl_take isl_basic_map *bmap, int div, int i,
2012 unsigned total, isl_int v, int lb, int ub)
2014 int j;
2016 for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
2017 if (lb) {
2018 isl_int_sub(v, bmap->ineq[i][1 + j],
2019 bmap->div[div][1 + 1 + j]);
2020 lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
2022 if (ub) {
2023 isl_int_add(v, bmap->ineq[i][1 + j],
2024 bmap->div[div][1 + 1 + j]);
2025 ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
2028 if (!lb && !ub)
2029 return bmap;
2031 bmap = isl_basic_map_cow(bmap);
2032 bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
2033 if (lb) {
2034 int k = isl_basic_map_alloc_inequality(bmap);
2035 if (k < 0)
2036 goto error;
2037 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2038 isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
2039 bmap->div[div][1 + j]);
2040 isl_int_cdiv_q(bmap->ineq[k][j],
2041 bmap->ineq[k][j], bmap->div[div][0]);
2043 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
2045 if (ub) {
2046 int k = isl_basic_map_alloc_inequality(bmap);
2047 if (k < 0)
2048 goto error;
2049 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
2050 isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
2051 bmap->div[div][1 + j]);
2052 isl_int_fdiv_q(bmap->ineq[k][j],
2053 bmap->ineq[k][j], bmap->div[div][0]);
2055 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
2058 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2059 return bmap;
2060 error:
2061 isl_basic_map_free(bmap);
2062 return NULL;
2065 /* This function is called right before "div" is eliminated from "bmap"
2066 * using Fourier-Motzkin.
2067 * Look through the constraints of "bmap" for constraints on the argument
2068 * of the integer division and use them to construct constraints on the
2069 * integer division itself. These constraints can then be combined
2070 * during the Fourier-Motzkin elimination.
2071 * Note that it is only useful to introduce lower bounds on "div"
2072 * if "bmap" already contains upper bounds on "div" as the newly
2073 * introduce lower bounds can then be combined with the pre-existing
2074 * upper bounds. Similarly for upper bounds.
2075 * We therefore first check if "bmap" contains any lower and/or upper bounds
2076 * on "div".
2078 * It is interesting to note that the introduction of these constraints
2079 * can indeed lead to more accurate results, even when compared to
2080 * deriving constraints on the argument of "div" from constraints on "div".
2081 * Consider, for example, the set
2083 * { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
2085 * The second constraint can be rewritten as
2087 * 2 * [(-i-2j+3)/4] + k >= 0
2089 * from which we can derive
2091 * -i - 2j + 3 >= -2k
2093 * or
2095 * i + 2j <= 3 + 2k
2097 * Combined with the first constraint, we obtain
2099 * -3 <= 3 + 2k or k >= -3
2101 * If, on the other hand we derive a constraint on [(i+2j)/4] from
2102 * the first constraint, we obtain
2104 * [(i + 2j)/4] >= [-3/4] = -1
2106 * Combining this constraint with the second constraint, we obtain
2108 * k >= -2
2110 static __isl_give isl_basic_map *insert_bounds_on_div(
2111 __isl_take isl_basic_map *bmap, int div)
2113 int i;
2114 int check_lb, check_ub;
2115 isl_int v;
2116 unsigned total;
2118 if (!bmap)
2119 return NULL;
2121 if (isl_int_is_zero(bmap->div[div][0]))
2122 return bmap;
2124 total = isl_space_dim(bmap->dim, isl_dim_all);
2126 check_lb = 0;
2127 check_ub = 0;
2128 for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2129 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2130 if (s > 0)
2131 check_ub = 1;
2132 if (s < 0)
2133 check_lb = 1;
2136 if (!check_lb && !check_ub)
2137 return bmap;
2139 isl_int_init(v);
2141 for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2142 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2143 continue;
2145 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2146 check_lb, check_ub);
2149 isl_int_clear(v);
2151 return bmap;
2154 /* Remove all divs (recursively) involving any of the given dimensions
2155 * in their definitions.
2157 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2158 __isl_take isl_basic_map *bmap,
2159 enum isl_dim_type type, unsigned first, unsigned n)
2161 int i;
2163 if (!bmap)
2164 return NULL;
2165 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2166 goto error);
2167 first += isl_basic_map_offset(bmap, type);
2169 for (i = bmap->n_div - 1; i >= 0; --i) {
2170 if (!div_involves_vars(bmap, i, first, n))
2171 continue;
2172 bmap = insert_bounds_on_div(bmap, i);
2173 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2174 if (!bmap)
2175 return NULL;
2176 i = bmap->n_div;
2179 return bmap;
2180 error:
2181 isl_basic_map_free(bmap);
2182 return NULL;
2185 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2186 __isl_take isl_basic_set *bset,
2187 enum isl_dim_type type, unsigned first, unsigned n)
2189 return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2192 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2193 enum isl_dim_type type, unsigned first, unsigned n)
2195 int i;
2197 if (!map)
2198 return NULL;
2199 if (map->n == 0)
2200 return map;
2202 map = isl_map_cow(map);
2203 if (!map)
2204 return NULL;
2206 for (i = 0; i < map->n; ++i) {
2207 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2208 type, first, n);
2209 if (!map->p[i])
2210 goto error;
2212 return map;
2213 error:
2214 isl_map_free(map);
2215 return NULL;
2218 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2219 enum isl_dim_type type, unsigned first, unsigned n)
2221 return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2222 type, first, n);
2225 /* Does the desciption of "bmap" depend on the specified dimensions?
2226 * We also check whether the dimensions appear in any of the div definitions.
2227 * In principle there is no need for this check. If the dimensions appear
2228 * in a div definition, they also appear in the defining constraints of that
2229 * div.
2231 isl_bool isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2232 enum isl_dim_type type, unsigned first, unsigned n)
2234 int i;
2236 if (!bmap)
2237 return isl_bool_error;
2239 if (first + n > isl_basic_map_dim(bmap, type))
2240 isl_die(bmap->ctx, isl_error_invalid,
2241 "index out of bounds", return isl_bool_error);
2243 first += isl_basic_map_offset(bmap, type);
2244 for (i = 0; i < bmap->n_eq; ++i)
2245 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2246 return isl_bool_true;
2247 for (i = 0; i < bmap->n_ineq; ++i)
2248 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2249 return isl_bool_true;
2250 for (i = 0; i < bmap->n_div; ++i) {
2251 if (isl_int_is_zero(bmap->div[i][0]))
2252 continue;
2253 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2254 return isl_bool_true;
2257 return isl_bool_false;
2260 isl_bool isl_map_involves_dims(__isl_keep isl_map *map,
2261 enum isl_dim_type type, unsigned first, unsigned n)
2263 int i;
2265 if (!map)
2266 return isl_bool_error;
2268 if (first + n > isl_map_dim(map, type))
2269 isl_die(map->ctx, isl_error_invalid,
2270 "index out of bounds", return isl_bool_error);
2272 for (i = 0; i < map->n; ++i) {
2273 isl_bool involves = isl_basic_map_involves_dims(map->p[i],
2274 type, first, n);
2275 if (involves < 0 || involves)
2276 return involves;
2279 return isl_bool_false;
2282 isl_bool isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2283 enum isl_dim_type type, unsigned first, unsigned n)
2285 return isl_basic_map_involves_dims(bset, type, first, n);
2288 isl_bool isl_set_involves_dims(__isl_keep isl_set *set,
2289 enum isl_dim_type type, unsigned first, unsigned n)
2291 return isl_map_involves_dims(set, type, first, n);
2294 /* Return true if the definition of the given div is unknown or depends
2295 * on unknown divs.
2297 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2299 int i;
2300 unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2302 if (isl_int_is_zero(bmap->div[div][0]))
2303 return 1;
2305 for (i = bmap->n_div - 1; i >= 0; --i) {
2306 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2307 continue;
2308 if (div_is_unknown(bmap, i))
2309 return 1;
2312 return 0;
2315 /* Remove all divs that are unknown or defined in terms of unknown divs.
2317 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2318 __isl_take isl_basic_map *bmap)
2320 int i;
2322 if (!bmap)
2323 return NULL;
2325 for (i = bmap->n_div - 1; i >= 0; --i) {
2326 if (!div_is_unknown(bmap, i))
2327 continue;
2328 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2329 if (!bmap)
2330 return NULL;
2331 i = bmap->n_div;
2334 return bmap;
2337 /* Remove all divs that are unknown or defined in terms of unknown divs.
2339 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2340 __isl_take isl_basic_set *bset)
2342 return isl_basic_map_remove_unknown_divs(bset);
2345 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2347 int i;
2349 if (!map)
2350 return NULL;
2351 if (map->n == 0)
2352 return map;
2354 map = isl_map_cow(map);
2355 if (!map)
2356 return NULL;
2358 for (i = 0; i < map->n; ++i) {
2359 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2360 if (!map->p[i])
2361 goto error;
2363 return map;
2364 error:
2365 isl_map_free(map);
2366 return NULL;
2369 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2371 return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2374 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2375 __isl_take isl_basic_set *bset,
2376 enum isl_dim_type type, unsigned first, unsigned n)
2378 return (isl_basic_set *)
2379 isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2382 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2383 enum isl_dim_type type, unsigned first, unsigned n)
2385 int i;
2387 if (n == 0)
2388 return map;
2390 map = isl_map_cow(map);
2391 if (!map)
2392 return NULL;
2393 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2395 for (i = 0; i < map->n; ++i) {
2396 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2397 isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2398 if (!map->p[i])
2399 goto error;
2401 map = isl_map_drop(map, type, first, n);
2402 return map;
2403 error:
2404 isl_map_free(map);
2405 return NULL;
2408 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2409 enum isl_dim_type type, unsigned first, unsigned n)
2411 return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2414 /* Project out n inputs starting at first using Fourier-Motzkin */
2415 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2416 unsigned first, unsigned n)
2418 return isl_map_remove_dims(map, isl_dim_in, first, n);
2421 static void dump_term(struct isl_basic_map *bmap,
2422 isl_int c, int pos, FILE *out)
2424 const char *name;
2425 unsigned in = isl_basic_map_n_in(bmap);
2426 unsigned dim = in + isl_basic_map_n_out(bmap);
2427 unsigned nparam = isl_basic_map_n_param(bmap);
2428 if (!pos)
2429 isl_int_print(out, c, 0);
2430 else {
2431 if (!isl_int_is_one(c))
2432 isl_int_print(out, c, 0);
2433 if (pos < 1 + nparam) {
2434 name = isl_space_get_dim_name(bmap->dim,
2435 isl_dim_param, pos - 1);
2436 if (name)
2437 fprintf(out, "%s", name);
2438 else
2439 fprintf(out, "p%d", pos - 1);
2440 } else if (pos < 1 + nparam + in)
2441 fprintf(out, "i%d", pos - 1 - nparam);
2442 else if (pos < 1 + nparam + dim)
2443 fprintf(out, "o%d", pos - 1 - nparam - in);
2444 else
2445 fprintf(out, "e%d", pos - 1 - nparam - dim);
2449 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2450 int sign, FILE *out)
2452 int i;
2453 int first;
2454 unsigned len = 1 + isl_basic_map_total_dim(bmap);
2455 isl_int v;
2457 isl_int_init(v);
2458 for (i = 0, first = 1; i < len; ++i) {
2459 if (isl_int_sgn(c[i]) * sign <= 0)
2460 continue;
2461 if (!first)
2462 fprintf(out, " + ");
2463 first = 0;
2464 isl_int_abs(v, c[i]);
2465 dump_term(bmap, v, i, out);
2467 isl_int_clear(v);
2468 if (first)
2469 fprintf(out, "0");
2472 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2473 const char *op, FILE *out, int indent)
2475 int i;
2477 fprintf(out, "%*s", indent, "");
2479 dump_constraint_sign(bmap, c, 1, out);
2480 fprintf(out, " %s ", op);
2481 dump_constraint_sign(bmap, c, -1, out);
2483 fprintf(out, "\n");
2485 for (i = bmap->n_div; i < bmap->extra; ++i) {
2486 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2487 continue;
2488 fprintf(out, "%*s", indent, "");
2489 fprintf(out, "ERROR: unused div coefficient not zero\n");
2490 abort();
2494 static void dump_constraints(struct isl_basic_map *bmap,
2495 isl_int **c, unsigned n,
2496 const char *op, FILE *out, int indent)
2498 int i;
2500 for (i = 0; i < n; ++i)
2501 dump_constraint(bmap, c[i], op, out, indent);
2504 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2506 int j;
2507 int first = 1;
2508 unsigned total = isl_basic_map_total_dim(bmap);
2510 for (j = 0; j < 1 + total; ++j) {
2511 if (isl_int_is_zero(exp[j]))
2512 continue;
2513 if (!first && isl_int_is_pos(exp[j]))
2514 fprintf(out, "+");
2515 dump_term(bmap, exp[j], j, out);
2516 first = 0;
2520 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2522 int i;
2524 dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2525 dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2527 for (i = 0; i < bmap->n_div; ++i) {
2528 fprintf(out, "%*s", indent, "");
2529 fprintf(out, "e%d = [(", i);
2530 dump_affine(bmap, bmap->div[i]+1, out);
2531 fprintf(out, ")/");
2532 isl_int_print(out, bmap->div[i][0], 0);
2533 fprintf(out, "]\n");
2537 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2538 FILE *out, int indent)
2540 if (!bset) {
2541 fprintf(out, "null basic set\n");
2542 return;
2545 fprintf(out, "%*s", indent, "");
2546 fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2547 bset->ref, bset->dim->nparam, bset->dim->n_out,
2548 bset->extra, bset->flags);
2549 dump((struct isl_basic_map *)bset, out, indent);
2552 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2553 FILE *out, int indent)
2555 if (!bmap) {
2556 fprintf(out, "null basic map\n");
2557 return;
2560 fprintf(out, "%*s", indent, "");
2561 fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2562 "flags: %x, n_name: %d\n",
2563 bmap->ref,
2564 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2565 bmap->extra, bmap->flags, bmap->dim->n_id);
2566 dump(bmap, out, indent);
2569 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2571 unsigned total;
2572 if (!bmap)
2573 return -1;
2574 total = isl_basic_map_total_dim(bmap);
2575 isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2576 isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2577 isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2578 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2579 return 0;
2582 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2583 unsigned flags)
2585 struct isl_set *set;
2587 if (!dim)
2588 return NULL;
2589 isl_assert(dim->ctx, dim->n_in == 0, goto error);
2590 isl_assert(dim->ctx, n >= 0, goto error);
2591 set = isl_alloc(dim->ctx, struct isl_set,
2592 sizeof(struct isl_set) +
2593 (n - 1) * sizeof(struct isl_basic_set *));
2594 if (!set)
2595 goto error;
2597 set->ctx = dim->ctx;
2598 isl_ctx_ref(set->ctx);
2599 set->ref = 1;
2600 set->size = n;
2601 set->n = 0;
2602 set->dim = dim;
2603 set->flags = flags;
2604 return set;
2605 error:
2606 isl_space_free(dim);
2607 return NULL;
2610 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2611 unsigned nparam, unsigned dim, int n, unsigned flags)
2613 struct isl_set *set;
2614 isl_space *dims;
2616 dims = isl_space_alloc(ctx, nparam, 0, dim);
2617 if (!dims)
2618 return NULL;
2620 set = isl_set_alloc_space(dims, n, flags);
2621 return set;
2624 /* Make sure "map" has room for at least "n" more basic maps.
2626 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2628 int i;
2629 struct isl_map *grown = NULL;
2631 if (!map)
2632 return NULL;
2633 isl_assert(map->ctx, n >= 0, goto error);
2634 if (map->n + n <= map->size)
2635 return map;
2636 grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2637 if (!grown)
2638 goto error;
2639 for (i = 0; i < map->n; ++i) {
2640 grown->p[i] = isl_basic_map_copy(map->p[i]);
2641 if (!grown->p[i])
2642 goto error;
2643 grown->n++;
2645 isl_map_free(map);
2646 return grown;
2647 error:
2648 isl_map_free(grown);
2649 isl_map_free(map);
2650 return NULL;
2653 /* Make sure "set" has room for at least "n" more basic sets.
2655 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2657 return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2660 struct isl_set *isl_set_dup(struct isl_set *set)
2662 int i;
2663 struct isl_set *dup;
2665 if (!set)
2666 return NULL;
2668 dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2669 if (!dup)
2670 return NULL;
2671 for (i = 0; i < set->n; ++i)
2672 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2673 return dup;
2676 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2678 return isl_map_from_basic_map(bset);
2681 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2683 struct isl_map *map;
2685 if (!bmap)
2686 return NULL;
2688 map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2689 return isl_map_add_basic_map(map, bmap);
2692 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2693 __isl_take isl_basic_set *bset)
2695 return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2696 (struct isl_basic_map *)bset);
2699 __isl_null isl_set *isl_set_free(__isl_take isl_set *set)
2701 int i;
2703 if (!set)
2704 return NULL;
2706 if (--set->ref > 0)
2707 return NULL;
2709 isl_ctx_deref(set->ctx);
2710 for (i = 0; i < set->n; ++i)
2711 isl_basic_set_free(set->p[i]);
2712 isl_space_free(set->dim);
2713 free(set);
2715 return NULL;
2718 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2720 int i;
2722 if (!set) {
2723 fprintf(out, "null set\n");
2724 return;
2727 fprintf(out, "%*s", indent, "");
2728 fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2729 set->ref, set->n, set->dim->nparam, set->dim->n_out,
2730 set->flags);
2731 for (i = 0; i < set->n; ++i) {
2732 fprintf(out, "%*s", indent, "");
2733 fprintf(out, "basic set %d:\n", i);
2734 isl_basic_set_print_internal(set->p[i], out, indent+4);
2738 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2740 int i;
2742 if (!map) {
2743 fprintf(out, "null map\n");
2744 return;
2747 fprintf(out, "%*s", indent, "");
2748 fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2749 "flags: %x, n_name: %d\n",
2750 map->ref, map->n, map->dim->nparam, map->dim->n_in,
2751 map->dim->n_out, map->flags, map->dim->n_id);
2752 for (i = 0; i < map->n; ++i) {
2753 fprintf(out, "%*s", indent, "");
2754 fprintf(out, "basic map %d:\n", i);
2755 isl_basic_map_print_internal(map->p[i], out, indent+4);
2759 struct isl_basic_map *isl_basic_map_intersect_domain(
2760 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2762 struct isl_basic_map *bmap_domain;
2764 if (!bmap || !bset)
2765 goto error;
2767 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2768 bset->dim, isl_dim_param), goto error);
2770 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2771 isl_assert(bset->ctx,
2772 isl_basic_map_compatible_domain(bmap, bset), goto error);
2774 bmap = isl_basic_map_cow(bmap);
2775 if (!bmap)
2776 goto error;
2777 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2778 bset->n_div, bset->n_eq, bset->n_ineq);
2779 bmap_domain = isl_basic_map_from_domain(bset);
2780 bmap = add_constraints(bmap, bmap_domain, 0, 0);
2782 bmap = isl_basic_map_simplify(bmap);
2783 return isl_basic_map_finalize(bmap);
2784 error:
2785 isl_basic_map_free(bmap);
2786 isl_basic_set_free(bset);
2787 return NULL;
2790 struct isl_basic_map *isl_basic_map_intersect_range(
2791 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2793 struct isl_basic_map *bmap_range;
2795 if (!bmap || !bset)
2796 goto error;
2798 isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2799 bset->dim, isl_dim_param), goto error);
2801 if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2802 isl_assert(bset->ctx,
2803 isl_basic_map_compatible_range(bmap, bset), goto error);
2805 if (isl_basic_set_plain_is_universe(bset)) {
2806 isl_basic_set_free(bset);
2807 return bmap;
2810 bmap = isl_basic_map_cow(bmap);
2811 if (!bmap)
2812 goto error;
2813 bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2814 bset->n_div, bset->n_eq, bset->n_ineq);
2815 bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2816 bmap = add_constraints(bmap, bmap_range, 0, 0);
2818 bmap = isl_basic_map_simplify(bmap);
2819 return isl_basic_map_finalize(bmap);
2820 error:
2821 isl_basic_map_free(bmap);
2822 isl_basic_set_free(bset);
2823 return NULL;
2826 isl_bool isl_basic_map_contains(__isl_keep isl_basic_map *bmap,
2827 __isl_keep isl_vec *vec)
2829 int i;
2830 unsigned total;
2831 isl_int s;
2833 if (!bmap || !vec)
2834 return isl_bool_error;
2836 total = 1 + isl_basic_map_total_dim(bmap);
2837 if (total != vec->size)
2838 return isl_bool_error;
2840 isl_int_init(s);
2842 for (i = 0; i < bmap->n_eq; ++i) {
2843 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2844 if (!isl_int_is_zero(s)) {
2845 isl_int_clear(s);
2846 return isl_bool_false;
2850 for (i = 0; i < bmap->n_ineq; ++i) {
2851 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2852 if (isl_int_is_neg(s)) {
2853 isl_int_clear(s);
2854 return isl_bool_false;
2858 isl_int_clear(s);
2860 return isl_bool_true;
2863 isl_bool isl_basic_set_contains(__isl_keep isl_basic_set *bset,
2864 __isl_keep isl_vec *vec)
2866 return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2869 struct isl_basic_map *isl_basic_map_intersect(
2870 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2872 struct isl_vec *sample = NULL;
2874 if (!bmap1 || !bmap2)
2875 goto error;
2877 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2878 bmap2->dim, isl_dim_param), goto error);
2879 if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2880 isl_space_dim(bmap1->dim, isl_dim_param) &&
2881 isl_space_dim(bmap2->dim, isl_dim_all) !=
2882 isl_space_dim(bmap2->dim, isl_dim_param))
2883 return isl_basic_map_intersect(bmap2, bmap1);
2885 if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2886 isl_space_dim(bmap2->dim, isl_dim_param))
2887 isl_assert(bmap1->ctx,
2888 isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2890 if (isl_basic_map_plain_is_empty(bmap1)) {
2891 isl_basic_map_free(bmap2);
2892 return bmap1;
2894 if (isl_basic_map_plain_is_empty(bmap2)) {
2895 isl_basic_map_free(bmap1);
2896 return bmap2;
2899 if (bmap1->sample &&
2900 isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2901 isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2902 sample = isl_vec_copy(bmap1->sample);
2903 else if (bmap2->sample &&
2904 isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2905 isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2906 sample = isl_vec_copy(bmap2->sample);
2908 bmap1 = isl_basic_map_cow(bmap1);
2909 if (!bmap1)
2910 goto error;
2911 bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2912 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2913 bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2915 if (!bmap1)
2916 isl_vec_free(sample);
2917 else if (sample) {
2918 isl_vec_free(bmap1->sample);
2919 bmap1->sample = sample;
2922 bmap1 = isl_basic_map_simplify(bmap1);
2923 return isl_basic_map_finalize(bmap1);
2924 error:
2925 if (sample)
2926 isl_vec_free(sample);
2927 isl_basic_map_free(bmap1);
2928 isl_basic_map_free(bmap2);
2929 return NULL;
2932 struct isl_basic_set *isl_basic_set_intersect(
2933 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2935 return (struct isl_basic_set *)
2936 isl_basic_map_intersect(
2937 (struct isl_basic_map *)bset1,
2938 (struct isl_basic_map *)bset2);
2941 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2942 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2944 return isl_basic_set_intersect(bset1, bset2);
2947 /* Special case of isl_map_intersect, where both map1 and map2
2948 * are convex, without any divs and such that either map1 or map2
2949 * contains a single constraint. This constraint is then simply
2950 * added to the other map.
2952 static __isl_give isl_map *map_intersect_add_constraint(
2953 __isl_take isl_map *map1, __isl_take isl_map *map2)
2955 isl_assert(map1->ctx, map1->n == 1, goto error);
2956 isl_assert(map2->ctx, map1->n == 1, goto error);
2957 isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2958 isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2960 if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2961 return isl_map_intersect(map2, map1);
2963 isl_assert(map2->ctx,
2964 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2966 map1 = isl_map_cow(map1);
2967 if (!map1)
2968 goto error;
2969 if (isl_map_plain_is_empty(map1)) {
2970 isl_map_free(map2);
2971 return map1;
2973 map1->p[0] = isl_basic_map_cow(map1->p[0]);
2974 if (map2->p[0]->n_eq == 1)
2975 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2976 else
2977 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2978 map2->p[0]->ineq[0]);
2980 map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2981 map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2982 if (!map1->p[0])
2983 goto error;
2985 if (isl_basic_map_plain_is_empty(map1->p[0])) {
2986 isl_basic_map_free(map1->p[0]);
2987 map1->n = 0;
2990 isl_map_free(map2);
2992 return map1;
2993 error:
2994 isl_map_free(map1);
2995 isl_map_free(map2);
2996 return NULL;
2999 /* map2 may be either a parameter domain or a map living in the same
3000 * space as map1.
3002 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
3003 __isl_take isl_map *map2)
3005 unsigned flags = 0;
3006 isl_map *result;
3007 int i, j;
3009 if (!map1 || !map2)
3010 goto error;
3012 if ((isl_map_plain_is_empty(map1) ||
3013 isl_map_plain_is_universe(map2)) &&
3014 isl_space_is_equal(map1->dim, map2->dim)) {
3015 isl_map_free(map2);
3016 return map1;
3018 if ((isl_map_plain_is_empty(map2) ||
3019 isl_map_plain_is_universe(map1)) &&
3020 isl_space_is_equal(map1->dim, map2->dim)) {
3021 isl_map_free(map1);
3022 return map2;
3025 if (map1->n == 1 && map2->n == 1 &&
3026 map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
3027 isl_space_is_equal(map1->dim, map2->dim) &&
3028 (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
3029 map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
3030 return map_intersect_add_constraint(map1, map2);
3032 if (isl_space_dim(map2->dim, isl_dim_all) !=
3033 isl_space_dim(map2->dim, isl_dim_param))
3034 isl_assert(map1->ctx,
3035 isl_space_is_equal(map1->dim, map2->dim), goto error);
3037 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3038 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3039 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3041 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3042 map1->n * map2->n, flags);
3043 if (!result)
3044 goto error;
3045 for (i = 0; i < map1->n; ++i)
3046 for (j = 0; j < map2->n; ++j) {
3047 struct isl_basic_map *part;
3048 part = isl_basic_map_intersect(
3049 isl_basic_map_copy(map1->p[i]),
3050 isl_basic_map_copy(map2->p[j]));
3051 if (isl_basic_map_is_empty(part) < 0)
3052 part = isl_basic_map_free(part);
3053 result = isl_map_add_basic_map(result, part);
3054 if (!result)
3055 goto error;
3057 isl_map_free(map1);
3058 isl_map_free(map2);
3059 return result;
3060 error:
3061 isl_map_free(map1);
3062 isl_map_free(map2);
3063 return NULL;
3066 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
3067 __isl_take isl_map *map2)
3069 if (!map1 || !map2)
3070 goto error;
3071 if (!isl_space_is_equal(map1->dim, map2->dim))
3072 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
3073 "spaces don't match", goto error);
3074 return map_intersect_internal(map1, map2);
3075 error:
3076 isl_map_free(map1);
3077 isl_map_free(map2);
3078 return NULL;
3081 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
3082 __isl_take isl_map *map2)
3084 return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
3087 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
3089 return (struct isl_set *)
3090 isl_map_intersect((struct isl_map *)set1,
3091 (struct isl_map *)set2);
3094 /* map_intersect_internal accepts intersections
3095 * with parameter domains, so we can just call that function.
3097 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
3098 __isl_take isl_set *params)
3100 return map_intersect_internal(map, params);
3103 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
3104 __isl_take isl_map *map2)
3106 return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
3109 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
3110 __isl_take isl_set *params)
3112 return isl_map_intersect_params(set, params);
3115 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3117 isl_space *space;
3118 unsigned pos, n1, n2;
3120 if (!bmap)
3121 return NULL;
3122 bmap = isl_basic_map_cow(bmap);
3123 if (!bmap)
3124 return NULL;
3125 space = isl_space_reverse(isl_space_copy(bmap->dim));
3126 pos = isl_basic_map_offset(bmap, isl_dim_in);
3127 n1 = isl_basic_map_dim(bmap, isl_dim_in);
3128 n2 = isl_basic_map_dim(bmap, isl_dim_out);
3129 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
3130 return isl_basic_map_reset_space(bmap, space);
3133 static __isl_give isl_basic_map *basic_map_space_reset(
3134 __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3136 isl_space *space;
3138 if (!bmap)
3139 return NULL;
3140 if (!isl_space_is_named_or_nested(bmap->dim, type))
3141 return bmap;
3143 space = isl_basic_map_get_space(bmap);
3144 space = isl_space_reset(space, type);
3145 bmap = isl_basic_map_reset_space(bmap, space);
3146 return bmap;
3149 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3150 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3151 unsigned pos, unsigned n)
3153 isl_space *res_dim;
3154 struct isl_basic_map *res;
3155 struct isl_dim_map *dim_map;
3156 unsigned total, off;
3157 enum isl_dim_type t;
3159 if (n == 0)
3160 return basic_map_space_reset(bmap, type);
3162 if (!bmap)
3163 return NULL;
3165 res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3167 total = isl_basic_map_total_dim(bmap) + n;
3168 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3169 off = 0;
3170 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3171 if (t != type) {
3172 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3173 } else {
3174 unsigned size = isl_basic_map_dim(bmap, t);
3175 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3176 0, pos, off);
3177 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3178 pos, size - pos, off + pos + n);
3180 off += isl_space_dim(res_dim, t);
3182 isl_dim_map_div(dim_map, bmap, off);
3184 res = isl_basic_map_alloc_space(res_dim,
3185 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3186 if (isl_basic_map_is_rational(bmap))
3187 res = isl_basic_map_set_rational(res);
3188 if (isl_basic_map_plain_is_empty(bmap)) {
3189 isl_basic_map_free(bmap);
3190 free(dim_map);
3191 return isl_basic_map_set_to_empty(res);
3193 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3194 return isl_basic_map_finalize(res);
3197 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3198 __isl_take isl_basic_set *bset,
3199 enum isl_dim_type type, unsigned pos, unsigned n)
3201 return isl_basic_map_insert_dims(bset, type, pos, n);
3204 __isl_give isl_basic_map *isl_basic_map_add_dims(__isl_take isl_basic_map *bmap,
3205 enum isl_dim_type type, unsigned n)
3207 if (!bmap)
3208 return NULL;
3209 return isl_basic_map_insert_dims(bmap, type,
3210 isl_basic_map_dim(bmap, type), n);
3213 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3214 enum isl_dim_type type, unsigned n)
3216 if (!bset)
3217 return NULL;
3218 isl_assert(bset->ctx, type != isl_dim_in, goto error);
3219 return isl_basic_map_add_dims(bset, type, n);
3220 error:
3221 isl_basic_set_free(bset);
3222 return NULL;
3225 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3226 enum isl_dim_type type)
3228 isl_space *space;
3230 if (!map || !isl_space_is_named_or_nested(map->dim, type))
3231 return map;
3233 space = isl_map_get_space(map);
3234 space = isl_space_reset(space, type);
3235 map = isl_map_reset_space(map, space);
3236 return map;
3239 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3240 enum isl_dim_type type, unsigned pos, unsigned n)
3242 int i;
3244 if (n == 0)
3245 return map_space_reset(map, type);
3247 map = isl_map_cow(map);
3248 if (!map)
3249 return NULL;
3251 map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3252 if (!map->dim)
3253 goto error;
3255 for (i = 0; i < map->n; ++i) {
3256 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3257 if (!map->p[i])
3258 goto error;
3261 return map;
3262 error:
3263 isl_map_free(map);
3264 return NULL;
3267 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3268 enum isl_dim_type type, unsigned pos, unsigned n)
3270 return isl_map_insert_dims(set, type, pos, n);
3273 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3274 enum isl_dim_type type, unsigned n)
3276 if (!map)
3277 return NULL;
3278 return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3281 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3282 enum isl_dim_type type, unsigned n)
3284 if (!set)
3285 return NULL;
3286 isl_assert(set->ctx, type != isl_dim_in, goto error);
3287 return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3288 error:
3289 isl_set_free(set);
3290 return NULL;
3293 __isl_give isl_basic_map *isl_basic_map_move_dims(
3294 __isl_take isl_basic_map *bmap,
3295 enum isl_dim_type dst_type, unsigned dst_pos,
3296 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3298 struct isl_dim_map *dim_map;
3299 struct isl_basic_map *res;
3300 enum isl_dim_type t;
3301 unsigned total, off;
3303 if (!bmap)
3304 return NULL;
3305 if (n == 0)
3306 return bmap;
3308 isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3309 goto error);
3311 if (dst_type == src_type && dst_pos == src_pos)
3312 return bmap;
3314 isl_assert(bmap->ctx, dst_type != src_type, goto error);
3316 if (pos(bmap->dim, dst_type) + dst_pos ==
3317 pos(bmap->dim, src_type) + src_pos +
3318 ((src_type < dst_type) ? n : 0)) {
3319 bmap = isl_basic_map_cow(bmap);
3320 if (!bmap)
3321 return NULL;
3323 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3324 src_type, src_pos, n);
3325 if (!bmap->dim)
3326 goto error;
3328 bmap = isl_basic_map_finalize(bmap);
3330 return bmap;
3333 total = isl_basic_map_total_dim(bmap);
3334 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3336 off = 0;
3337 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3338 unsigned size = isl_space_dim(bmap->dim, t);
3339 if (t == dst_type) {
3340 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3341 0, dst_pos, off);
3342 off += dst_pos;
3343 isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3344 src_pos, n, off);
3345 off += n;
3346 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3347 dst_pos, size - dst_pos, off);
3348 off += size - dst_pos;
3349 } else if (t == src_type) {
3350 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3351 0, src_pos, off);
3352 off += src_pos;
3353 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3354 src_pos + n, size - src_pos - n, off);
3355 off += size - src_pos - n;
3356 } else {
3357 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3358 off += size;
3361 isl_dim_map_div(dim_map, bmap, off);
3363 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3364 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3365 bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3366 if (!bmap)
3367 goto error;
3369 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3370 src_type, src_pos, n);
3371 if (!bmap->dim)
3372 goto error;
3374 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3375 bmap = isl_basic_map_gauss(bmap, NULL);
3376 bmap = isl_basic_map_finalize(bmap);
3378 return bmap;
3379 error:
3380 isl_basic_map_free(bmap);
3381 return NULL;
3384 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3385 enum isl_dim_type dst_type, unsigned dst_pos,
3386 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3388 return (isl_basic_set *)isl_basic_map_move_dims(
3389 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3392 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3393 enum isl_dim_type dst_type, unsigned dst_pos,
3394 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3396 if (!set)
3397 return NULL;
3398 isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3399 return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3400 src_type, src_pos, n);
3401 error:
3402 isl_set_free(set);
3403 return NULL;
3406 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3407 enum isl_dim_type dst_type, unsigned dst_pos,
3408 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3410 int i;
3412 if (!map)
3413 return NULL;
3414 if (n == 0)
3415 return map;
3417 isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3418 goto error);
3420 if (dst_type == src_type && dst_pos == src_pos)
3421 return map;
3423 isl_assert(map->ctx, dst_type != src_type, goto error);
3425 map = isl_map_cow(map);
3426 if (!map)
3427 return NULL;
3429 map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3430 if (!map->dim)
3431 goto error;
3433 for (i = 0; i < map->n; ++i) {
3434 map->p[i] = isl_basic_map_move_dims(map->p[i],
3435 dst_type, dst_pos,
3436 src_type, src_pos, n);
3437 if (!map->p[i])
3438 goto error;
3441 return map;
3442 error:
3443 isl_map_free(map);
3444 return NULL;
3447 /* Move the specified dimensions to the last columns right before
3448 * the divs. Don't change the dimension specification of bmap.
3449 * That's the responsibility of the caller.
3451 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3452 enum isl_dim_type type, unsigned first, unsigned n)
3454 struct isl_dim_map *dim_map;
3455 struct isl_basic_map *res;
3456 enum isl_dim_type t;
3457 unsigned total, off;
3459 if (!bmap)
3460 return NULL;
3461 if (pos(bmap->dim, type) + first + n ==
3462 1 + isl_space_dim(bmap->dim, isl_dim_all))
3463 return bmap;
3465 total = isl_basic_map_total_dim(bmap);
3466 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3468 off = 0;
3469 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3470 unsigned size = isl_space_dim(bmap->dim, t);
3471 if (t == type) {
3472 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3473 0, first, off);
3474 off += first;
3475 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3476 first, n, total - bmap->n_div - n);
3477 isl_dim_map_dim_range(dim_map, bmap->dim, t,
3478 first + n, size - (first + n), off);
3479 off += size - (first + n);
3480 } else {
3481 isl_dim_map_dim(dim_map, bmap->dim, t, off);
3482 off += size;
3485 isl_dim_map_div(dim_map, bmap, off + n);
3487 res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3488 bmap->n_div, bmap->n_eq, bmap->n_ineq);
3489 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3490 return res;
3493 /* Insert "n" rows in the divs of "bmap".
3495 * The number of columns is not changed, which means that the last
3496 * dimensions of "bmap" are being reintepreted as the new divs.
3497 * The space of "bmap" is not adjusted, however, which means
3498 * that "bmap" is left in an inconsistent state. Removing "n" dimensions
3499 * from the space of "bmap" is the responsibility of the caller.
3501 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3502 int n)
3504 int i;
3505 size_t row_size;
3506 isl_int **new_div;
3507 isl_int *old;
3509 bmap = isl_basic_map_cow(bmap);
3510 if (!bmap)
3511 return NULL;
3513 row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3514 old = bmap->block2.data;
3515 bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3516 (bmap->extra + n) * (1 + row_size));
3517 if (!bmap->block2.data)
3518 return isl_basic_map_free(bmap);
3519 new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3520 if (!new_div)
3521 return isl_basic_map_free(bmap);
3522 for (i = 0; i < n; ++i) {
3523 new_div[i] = bmap->block2.data +
3524 (bmap->extra + i) * (1 + row_size);
3525 isl_seq_clr(new_div[i], 1 + row_size);
3527 for (i = 0; i < bmap->extra; ++i)
3528 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3529 free(bmap->div);
3530 bmap->div = new_div;
3531 bmap->n_div += n;
3532 bmap->extra += n;
3534 return bmap;
3537 /* Drop constraints from "bmap" that only involve the variables
3538 * of "type" in the range [first, first + n] that are not related
3539 * to any of the variables outside that interval.
3540 * These constraints cannot influence the values for the variables
3541 * outside the interval, except in case they cause "bmap" to be empty.
3542 * Only drop the constraints if "bmap" is known to be non-empty.
3544 static __isl_give isl_basic_map *drop_irrelevant_constraints(
3545 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3546 unsigned first, unsigned n)
3548 int i;
3549 int *groups;
3550 unsigned dim, n_div;
3551 isl_bool non_empty;
3553 non_empty = isl_basic_map_plain_is_non_empty(bmap);
3554 if (non_empty < 0)
3555 return isl_basic_map_free(bmap);
3556 if (!non_empty)
3557 return bmap;
3559 dim = isl_basic_map_dim(bmap, isl_dim_all);
3560 n_div = isl_basic_map_dim(bmap, isl_dim_div);
3561 groups = isl_calloc_array(isl_basic_map_get_ctx(bmap), int, dim);
3562 if (!groups)
3563 return isl_basic_map_free(bmap);
3564 first += isl_basic_map_offset(bmap, type) - 1;
3565 for (i = 0; i < first; ++i)
3566 groups[i] = -1;
3567 for (i = first + n; i < dim - n_div; ++i)
3568 groups[i] = -1;
3570 bmap = isl_basic_map_drop_unrelated_constraints(bmap, groups);
3572 return bmap;
3575 /* Turn the n dimensions of type type, starting at first
3576 * into existentially quantified variables.
3578 * If a subset of the projected out variables are unrelated
3579 * to any of the variables that remain, then the constraints
3580 * involving this subset are simply dropped first.
3582 __isl_give isl_basic_map *isl_basic_map_project_out(
3583 __isl_take isl_basic_map *bmap,
3584 enum isl_dim_type type, unsigned first, unsigned n)
3586 if (n == 0)
3587 return basic_map_space_reset(bmap, type);
3588 if (type == isl_dim_div)
3589 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
3590 "cannot project out existentially quantified variables",
3591 return isl_basic_map_free(bmap));
3593 bmap = drop_irrelevant_constraints(bmap, type, first, n);
3594 if (!bmap)
3595 return NULL;
3597 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3598 return isl_basic_map_remove_dims(bmap, type, first, n);
3600 isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3601 goto error);
3603 bmap = move_last(bmap, type, first, n);
3604 bmap = isl_basic_map_cow(bmap);
3605 bmap = insert_div_rows(bmap, n);
3606 if (!bmap)
3607 return NULL;
3609 bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3610 if (!bmap->dim)
3611 goto error;
3612 bmap = isl_basic_map_simplify(bmap);
3613 bmap = isl_basic_map_drop_redundant_divs(bmap);
3614 return isl_basic_map_finalize(bmap);
3615 error:
3616 isl_basic_map_free(bmap);
3617 return NULL;
3620 /* Turn the n dimensions of type type, starting at first
3621 * into existentially quantified variables.
3623 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3624 enum isl_dim_type type, unsigned first, unsigned n)
3626 return (isl_basic_set *)isl_basic_map_project_out(
3627 (isl_basic_map *)bset, type, first, n);
3630 /* Turn the n dimensions of type type, starting at first
3631 * into existentially quantified variables.
3633 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3634 enum isl_dim_type type, unsigned first, unsigned n)
3636 int i;
3638 if (!map)
3639 return NULL;
3641 if (n == 0)
3642 return map_space_reset(map, type);
3644 isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3646 map = isl_map_cow(map);
3647 if (!map)
3648 return NULL;
3650 map->dim = isl_space_drop_dims(map->dim, type, first, n);
3651 if (!map->dim)
3652 goto error;
3654 for (i = 0; i < map->n; ++i) {
3655 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3656 if (!map->p[i])
3657 goto error;
3660 return map;
3661 error:
3662 isl_map_free(map);
3663 return NULL;
3666 /* Turn the n dimensions of type type, starting at first
3667 * into existentially quantified variables.
3669 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3670 enum isl_dim_type type, unsigned first, unsigned n)
3672 return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3675 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3677 int i, j;
3679 for (i = 0; i < n; ++i) {
3680 j = isl_basic_map_alloc_div(bmap);
3681 if (j < 0)
3682 goto error;
3683 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3685 return bmap;
3686 error:
3687 isl_basic_map_free(bmap);
3688 return NULL;
3691 struct isl_basic_map *isl_basic_map_apply_range(
3692 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3694 isl_space *dim_result = NULL;
3695 struct isl_basic_map *bmap;
3696 unsigned n_in, n_out, n, nparam, total, pos;
3697 struct isl_dim_map *dim_map1, *dim_map2;
3699 if (!bmap1 || !bmap2)
3700 goto error;
3701 if (!isl_space_match(bmap1->dim, isl_dim_param,
3702 bmap2->dim, isl_dim_param))
3703 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3704 "parameters don't match", goto error);
3705 if (!isl_space_tuple_is_equal(bmap1->dim, isl_dim_out,
3706 bmap2->dim, isl_dim_in))
3707 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
3708 "spaces don't match", goto error);
3710 dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3711 isl_space_copy(bmap2->dim));
3713 n_in = isl_basic_map_n_in(bmap1);
3714 n_out = isl_basic_map_n_out(bmap2);
3715 n = isl_basic_map_n_out(bmap1);
3716 nparam = isl_basic_map_n_param(bmap1);
3718 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3719 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3720 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3721 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3722 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3723 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3724 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3725 isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3726 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3727 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3728 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3730 bmap = isl_basic_map_alloc_space(dim_result,
3731 bmap1->n_div + bmap2->n_div + n,
3732 bmap1->n_eq + bmap2->n_eq,
3733 bmap1->n_ineq + bmap2->n_ineq);
3734 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3735 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3736 bmap = add_divs(bmap, n);
3737 bmap = isl_basic_map_simplify(bmap);
3738 bmap = isl_basic_map_drop_redundant_divs(bmap);
3739 return isl_basic_map_finalize(bmap);
3740 error:
3741 isl_basic_map_free(bmap1);
3742 isl_basic_map_free(bmap2);
3743 return NULL;
3746 struct isl_basic_set *isl_basic_set_apply(
3747 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3749 if (!bset || !bmap)
3750 goto error;
3752 isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3753 goto error);
3755 return (struct isl_basic_set *)
3756 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3757 error:
3758 isl_basic_set_free(bset);
3759 isl_basic_map_free(bmap);
3760 return NULL;
3763 struct isl_basic_map *isl_basic_map_apply_domain(
3764 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3766 if (!bmap1 || !bmap2)
3767 goto error;
3769 isl_assert(bmap1->ctx,
3770 isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3771 isl_assert(bmap1->ctx,
3772 isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3773 goto error);
3775 bmap1 = isl_basic_map_reverse(bmap1);
3776 bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3777 return isl_basic_map_reverse(bmap1);
3778 error:
3779 isl_basic_map_free(bmap1);
3780 isl_basic_map_free(bmap2);
3781 return NULL;
3784 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3785 * A \cap B -> f(A) + f(B)
3787 struct isl_basic_map *isl_basic_map_sum(
3788 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3790 unsigned n_in, n_out, nparam, total, pos;
3791 struct isl_basic_map *bmap = NULL;
3792 struct isl_dim_map *dim_map1, *dim_map2;
3793 int i;
3795 if (!bmap1 || !bmap2)
3796 goto error;
3798 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3799 goto error);
3801 nparam = isl_basic_map_n_param(bmap1);
3802 n_in = isl_basic_map_n_in(bmap1);
3803 n_out = isl_basic_map_n_out(bmap1);
3805 total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3806 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3807 dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3808 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3809 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3810 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3811 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3812 isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3813 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3814 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3815 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3817 bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3818 bmap1->n_div + bmap2->n_div + 2 * n_out,
3819 bmap1->n_eq + bmap2->n_eq + n_out,
3820 bmap1->n_ineq + bmap2->n_ineq);
3821 for (i = 0; i < n_out; ++i) {
3822 int j = isl_basic_map_alloc_equality(bmap);
3823 if (j < 0)
3824 goto error;
3825 isl_seq_clr(bmap->eq[j], 1+total);
3826 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3827 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3828 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3830 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3831 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3832 bmap = add_divs(bmap, 2 * n_out);
3834 bmap = isl_basic_map_simplify(bmap);
3835 return isl_basic_map_finalize(bmap);
3836 error:
3837 isl_basic_map_free(bmap);
3838 isl_basic_map_free(bmap1);
3839 isl_basic_map_free(bmap2);
3840 return NULL;
3843 /* Given two maps A -> f(A) and B -> g(B), construct a map
3844 * A \cap B -> f(A) + f(B)
3846 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3848 struct isl_map *result;
3849 int i, j;
3851 if (!map1 || !map2)
3852 goto error;
3854 isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3856 result = isl_map_alloc_space(isl_space_copy(map1->dim),
3857 map1->n * map2->n, 0);
3858 if (!result)
3859 goto error;
3860 for (i = 0; i < map1->n; ++i)
3861 for (j = 0; j < map2->n; ++j) {
3862 struct isl_basic_map *part;
3863 part = isl_basic_map_sum(
3864 isl_basic_map_copy(map1->p[i]),
3865 isl_basic_map_copy(map2->p[j]));
3866 if (isl_basic_map_is_empty(part))
3867 isl_basic_map_free(part);
3868 else
3869 result = isl_map_add_basic_map(result, part);
3870 if (!result)
3871 goto error;
3873 isl_map_free(map1);
3874 isl_map_free(map2);
3875 return result;
3876 error:
3877 isl_map_free(map1);
3878 isl_map_free(map2);
3879 return NULL;
3882 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3883 __isl_take isl_set *set2)
3885 return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3888 /* Given a basic map A -> f(A), construct A -> -f(A).
3890 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3892 int i, j;
3893 unsigned off, n;
3895 bmap = isl_basic_map_cow(bmap);
3896 if (!bmap)
3897 return NULL;
3899 n = isl_basic_map_dim(bmap, isl_dim_out);
3900 off = isl_basic_map_offset(bmap, isl_dim_out);
3901 for (i = 0; i < bmap->n_eq; ++i)
3902 for (j = 0; j < n; ++j)
3903 isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3904 for (i = 0; i < bmap->n_ineq; ++i)
3905 for (j = 0; j < n; ++j)
3906 isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3907 for (i = 0; i < bmap->n_div; ++i)
3908 for (j = 0; j < n; ++j)
3909 isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3910 bmap = isl_basic_map_gauss(bmap, NULL);
3911 return isl_basic_map_finalize(bmap);
3914 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3916 return isl_basic_map_neg(bset);
3919 /* Given a map A -> f(A), construct A -> -f(A).
3921 struct isl_map *isl_map_neg(struct isl_map *map)
3923 int i;
3925 map = isl_map_cow(map);
3926 if (!map)
3927 return NULL;
3929 for (i = 0; i < map->n; ++i) {
3930 map->p[i] = isl_basic_map_neg(map->p[i]);
3931 if (!map->p[i])
3932 goto error;
3935 return map;
3936 error:
3937 isl_map_free(map);
3938 return NULL;
3941 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3943 return (isl_set *)isl_map_neg((isl_map *)set);
3946 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3947 * A -> floor(f(A)/d).
3949 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3950 isl_int d)
3952 unsigned n_in, n_out, nparam, total, pos;
3953 struct isl_basic_map *result = NULL;
3954 struct isl_dim_map *dim_map;
3955 int i;
3957 if (!bmap)
3958 return NULL;
3960 nparam = isl_basic_map_n_param(bmap);
3961 n_in = isl_basic_map_n_in(bmap);
3962 n_out = isl_basic_map_n_out(bmap);
3964 total = nparam + n_in + n_out + bmap->n_div + n_out;
3965 dim_map = isl_dim_map_alloc(bmap->ctx, total);
3966 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3967 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3968 isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3969 isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3971 result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3972 bmap->n_div + n_out,
3973 bmap->n_eq, bmap->n_ineq + 2 * n_out);
3974 result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3975 result = add_divs(result, n_out);
3976 for (i = 0; i < n_out; ++i) {
3977 int j;
3978 j = isl_basic_map_alloc_inequality(result);
3979 if (j < 0)
3980 goto error;
3981 isl_seq_clr(result->ineq[j], 1+total);
3982 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3983 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3984 j = isl_basic_map_alloc_inequality(result);
3985 if (j < 0)
3986 goto error;
3987 isl_seq_clr(result->ineq[j], 1+total);
3988 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3989 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3990 isl_int_sub_ui(result->ineq[j][0], d, 1);
3993 result = isl_basic_map_simplify(result);
3994 return isl_basic_map_finalize(result);
3995 error:
3996 isl_basic_map_free(result);
3997 return NULL;
4000 /* Given a map A -> f(A) and an integer d, construct a map
4001 * A -> floor(f(A)/d).
4003 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
4005 int i;
4007 map = isl_map_cow(map);
4008 if (!map)
4009 return NULL;
4011 ISL_F_CLR(map, ISL_MAP_DISJOINT);
4012 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4013 for (i = 0; i < map->n; ++i) {
4014 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
4015 if (!map->p[i])
4016 goto error;
4019 return map;
4020 error:
4021 isl_map_free(map);
4022 return NULL;
4025 /* Given a map A -> f(A) and an integer d, construct a map
4026 * A -> floor(f(A)/d).
4028 __isl_give isl_map *isl_map_floordiv_val(__isl_take isl_map *map,
4029 __isl_take isl_val *d)
4031 if (!map || !d)
4032 goto error;
4033 if (!isl_val_is_int(d))
4034 isl_die(isl_val_get_ctx(d), isl_error_invalid,
4035 "expecting integer denominator", goto error);
4036 map = isl_map_floordiv(map, d->n);
4037 isl_val_free(d);
4038 return map;
4039 error:
4040 isl_map_free(map);
4041 isl_val_free(d);
4042 return NULL;
4045 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
4047 int i;
4048 unsigned nparam;
4049 unsigned n_in;
4051 i = isl_basic_map_alloc_equality(bmap);
4052 if (i < 0)
4053 goto error;
4054 nparam = isl_basic_map_n_param(bmap);
4055 n_in = isl_basic_map_n_in(bmap);
4056 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
4057 isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
4058 isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
4059 return isl_basic_map_finalize(bmap);
4060 error:
4061 isl_basic_map_free(bmap);
4062 return NULL;
4065 /* Add a constraints to "bmap" expressing i_pos < o_pos
4067 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
4069 int i;
4070 unsigned nparam;
4071 unsigned n_in;
4073 i = isl_basic_map_alloc_inequality(bmap);
4074 if (i < 0)
4075 goto error;
4076 nparam = isl_basic_map_n_param(bmap);
4077 n_in = isl_basic_map_n_in(bmap);
4078 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4079 isl_int_set_si(bmap->ineq[i][0], -1);
4080 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4081 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4082 return isl_basic_map_finalize(bmap);
4083 error:
4084 isl_basic_map_free(bmap);
4085 return NULL;
4088 /* Add a constraint to "bmap" expressing i_pos <= o_pos
4090 static __isl_give isl_basic_map *var_less_or_equal(
4091 __isl_take isl_basic_map *bmap, unsigned pos)
4093 int i;
4094 unsigned nparam;
4095 unsigned n_in;
4097 i = isl_basic_map_alloc_inequality(bmap);
4098 if (i < 0)
4099 goto error;
4100 nparam = isl_basic_map_n_param(bmap);
4101 n_in = isl_basic_map_n_in(bmap);
4102 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4103 isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
4104 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
4105 return isl_basic_map_finalize(bmap);
4106 error:
4107 isl_basic_map_free(bmap);
4108 return NULL;
4111 /* Add a constraints to "bmap" expressing i_pos > o_pos
4113 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
4115 int i;
4116 unsigned nparam;
4117 unsigned n_in;
4119 i = isl_basic_map_alloc_inequality(bmap);
4120 if (i < 0)
4121 goto error;
4122 nparam = isl_basic_map_n_param(bmap);
4123 n_in = isl_basic_map_n_in(bmap);
4124 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4125 isl_int_set_si(bmap->ineq[i][0], -1);
4126 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4127 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4128 return isl_basic_map_finalize(bmap);
4129 error:
4130 isl_basic_map_free(bmap);
4131 return NULL;
4134 /* Add a constraint to "bmap" expressing i_pos >= o_pos
4136 static __isl_give isl_basic_map *var_more_or_equal(
4137 __isl_take isl_basic_map *bmap, unsigned pos)
4139 int i;
4140 unsigned nparam;
4141 unsigned n_in;
4143 i = isl_basic_map_alloc_inequality(bmap);
4144 if (i < 0)
4145 goto error;
4146 nparam = isl_basic_map_n_param(bmap);
4147 n_in = isl_basic_map_n_in(bmap);
4148 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
4149 isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
4150 isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
4151 return isl_basic_map_finalize(bmap);
4152 error:
4153 isl_basic_map_free(bmap);
4154 return NULL;
4157 __isl_give isl_basic_map *isl_basic_map_equal(
4158 __isl_take isl_space *dim, unsigned n_equal)
4160 int i;
4161 struct isl_basic_map *bmap;
4162 bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
4163 if (!bmap)
4164 return NULL;
4165 for (i = 0; i < n_equal && bmap; ++i)
4166 bmap = var_equal(bmap, i);
4167 return isl_basic_map_finalize(bmap);
4170 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
4172 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
4173 unsigned pos)
4175 int i;
4176 struct isl_basic_map *bmap;
4177 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4178 if (!bmap)
4179 return NULL;
4180 for (i = 0; i < pos && bmap; ++i)
4181 bmap = var_equal(bmap, i);
4182 if (bmap)
4183 bmap = var_less(bmap, pos);
4184 return isl_basic_map_finalize(bmap);
4187 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
4189 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4190 __isl_take isl_space *dim, unsigned pos)
4192 int i;
4193 isl_basic_map *bmap;
4195 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4196 for (i = 0; i < pos; ++i)
4197 bmap = var_equal(bmap, i);
4198 bmap = var_less_or_equal(bmap, pos);
4199 return isl_basic_map_finalize(bmap);
4202 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4204 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4205 unsigned pos)
4207 int i;
4208 struct isl_basic_map *bmap;
4209 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4210 if (!bmap)
4211 return NULL;
4212 for (i = 0; i < pos && bmap; ++i)
4213 bmap = var_equal(bmap, i);
4214 if (bmap)
4215 bmap = var_more(bmap, pos);
4216 return isl_basic_map_finalize(bmap);
4219 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4221 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4222 __isl_take isl_space *dim, unsigned pos)
4224 int i;
4225 isl_basic_map *bmap;
4227 bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4228 for (i = 0; i < pos; ++i)
4229 bmap = var_equal(bmap, i);
4230 bmap = var_more_or_equal(bmap, pos);
4231 return isl_basic_map_finalize(bmap);
4234 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4235 unsigned n, int equal)
4237 struct isl_map *map;
4238 int i;
4240 if (n == 0 && equal)
4241 return isl_map_universe(dims);
4243 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4245 for (i = 0; i + 1 < n; ++i)
4246 map = isl_map_add_basic_map(map,
4247 isl_basic_map_less_at(isl_space_copy(dims), i));
4248 if (n > 0) {
4249 if (equal)
4250 map = isl_map_add_basic_map(map,
4251 isl_basic_map_less_or_equal_at(dims, n - 1));
4252 else
4253 map = isl_map_add_basic_map(map,
4254 isl_basic_map_less_at(dims, n - 1));
4255 } else
4256 isl_space_free(dims);
4258 return map;
4261 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4263 if (!dims)
4264 return NULL;
4265 return map_lex_lte_first(dims, dims->n_out, equal);
4268 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4270 return map_lex_lte_first(dim, n, 0);
4273 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4275 return map_lex_lte_first(dim, n, 1);
4278 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4280 return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4283 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4285 return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4288 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4289 unsigned n, int equal)
4291 struct isl_map *map;
4292 int i;
4294 if (n == 0 && equal)
4295 return isl_map_universe(dims);
4297 map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4299 for (i = 0; i + 1 < n; ++i)
4300 map = isl_map_add_basic_map(map,
4301 isl_basic_map_more_at(isl_space_copy(dims), i));
4302 if (n > 0) {
4303 if (equal)
4304 map = isl_map_add_basic_map(map,
4305 isl_basic_map_more_or_equal_at(dims, n - 1));
4306 else
4307 map = isl_map_add_basic_map(map,
4308 isl_basic_map_more_at(dims, n - 1));
4309 } else
4310 isl_space_free(dims);
4312 return map;
4315 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4317 if (!dims)
4318 return NULL;
4319 return map_lex_gte_first(dims, dims->n_out, equal);
4322 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4324 return map_lex_gte_first(dim, n, 0);
4327 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4329 return map_lex_gte_first(dim, n, 1);
4332 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4334 return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4337 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4339 return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4342 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4343 __isl_take isl_set *set2)
4345 isl_map *map;
4346 map = isl_map_lex_le(isl_set_get_space(set1));
4347 map = isl_map_intersect_domain(map, set1);
4348 map = isl_map_intersect_range(map, set2);
4349 return map;
4352 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4353 __isl_take isl_set *set2)
4355 isl_map *map;
4356 map = isl_map_lex_lt(isl_set_get_space(set1));
4357 map = isl_map_intersect_domain(map, set1);
4358 map = isl_map_intersect_range(map, set2);
4359 return map;
4362 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4363 __isl_take isl_set *set2)
4365 isl_map *map;
4366 map = isl_map_lex_ge(isl_set_get_space(set1));
4367 map = isl_map_intersect_domain(map, set1);
4368 map = isl_map_intersect_range(map, set2);
4369 return map;
4372 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4373 __isl_take isl_set *set2)
4375 isl_map *map;
4376 map = isl_map_lex_gt(isl_set_get_space(set1));
4377 map = isl_map_intersect_domain(map, set1);
4378 map = isl_map_intersect_range(map, set2);
4379 return map;
4382 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4383 __isl_take isl_map *map2)
4385 isl_map *map;
4386 map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4387 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4388 map = isl_map_apply_range(map, isl_map_reverse(map2));
4389 return map;
4392 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4393 __isl_take isl_map *map2)
4395 isl_map *map;
4396 map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4397 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4398 map = isl_map_apply_range(map, isl_map_reverse(map2));
4399 return map;
4402 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4403 __isl_take isl_map *map2)
4405 isl_map *map;
4406 map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4407 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4408 map = isl_map_apply_range(map, isl_map_reverse(map2));
4409 return map;
4412 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4413 __isl_take isl_map *map2)
4415 isl_map *map;
4416 map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4417 map = isl_map_apply_domain(map, isl_map_reverse(map1));
4418 map = isl_map_apply_range(map, isl_map_reverse(map2));
4419 return map;
4422 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4423 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4425 struct isl_basic_map *bmap;
4427 bset = isl_basic_set_cow(bset);
4428 if (!bset || !dim)
4429 goto error;
4431 isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4432 isl_space_free(bset->dim);
4433 bmap = (struct isl_basic_map *) bset;
4434 bmap->dim = dim;
4435 return isl_basic_map_finalize(bmap);
4436 error:
4437 isl_basic_set_free(bset);
4438 isl_space_free(dim);
4439 return NULL;
4442 /* For a div d = floor(f/m), add the constraint
4444 * f - m d >= 0
4446 static int add_upper_div_constraint(__isl_keep isl_basic_map *bmap,
4447 unsigned pos, isl_int *div)
4449 int i;
4450 unsigned total = isl_basic_map_total_dim(bmap);
4452 i = isl_basic_map_alloc_inequality(bmap);
4453 if (i < 0)
4454 return -1;
4455 isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4456 isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4458 return 0;
4461 /* For a div d = floor(f/m), add the constraint
4463 * -(f-(n-1)) + m d >= 0
4465 static int add_lower_div_constraint(__isl_keep isl_basic_map *bmap,
4466 unsigned pos, isl_int *div)
4468 int i;
4469 unsigned total = isl_basic_map_total_dim(bmap);
4471 i = isl_basic_map_alloc_inequality(bmap);
4472 if (i < 0)
4473 return -1;
4474 isl_seq_neg(bmap->ineq[i], div + 1, 1 + total);
4475 isl_int_set(bmap->ineq[i][1 + pos], div[0]);
4476 isl_int_add(bmap->ineq[i][0], bmap->ineq[i][0], bmap->ineq[i][1 + pos]);
4477 isl_int_sub_ui(bmap->ineq[i][0], bmap->ineq[i][0], 1);
4479 return 0;
4482 /* For a div d = floor(f/m), add the constraints
4484 * f - m d >= 0
4485 * -(f-(n-1)) + m d >= 0
4487 * Note that the second constraint is the negation of
4489 * f - m d >= n
4491 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4492 unsigned pos, isl_int *div)
4494 if (add_upper_div_constraint(bmap, pos, div) < 0)
4495 return -1;
4496 if (add_lower_div_constraint(bmap, pos, div) < 0)
4497 return -1;
4498 return 0;
4501 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4502 unsigned pos, isl_int *div)
4504 return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4505 pos, div);
4508 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4510 unsigned total = isl_basic_map_total_dim(bmap);
4511 unsigned div_pos = total - bmap->n_div + div;
4513 return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4514 bmap->div[div]);
4517 /* For each known div d = floor(f/m), add the constraints
4519 * f - m d >= 0
4520 * -(f-(n-1)) + m d >= 0
4522 * Remove duplicate constraints in case of some these div constraints
4523 * already appear in "bmap".
4525 __isl_give isl_basic_map *isl_basic_map_add_known_div_constraints(
4526 __isl_take isl_basic_map *bmap)
4528 unsigned n_div;
4530 if (!bmap)
4531 return NULL;
4532 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4533 if (n_div == 0)
4534 return bmap;
4536 bmap = add_known_div_constraints(bmap);
4537 bmap = isl_basic_map_remove_duplicate_constraints(bmap, NULL, 0);
4538 bmap = isl_basic_map_finalize(bmap);
4539 return bmap;
4542 /* Add the div constraint of sign "sign" for div "div" of "bmap".
4544 * In particular, if this div is of the form d = floor(f/m),
4545 * then add the constraint
4547 * f - m d >= 0
4549 * if sign < 0 or the constraint
4551 * -(f-(n-1)) + m d >= 0
4553 * if sign > 0.
4555 int isl_basic_map_add_div_constraint(__isl_keep isl_basic_map *bmap,
4556 unsigned div, int sign)
4558 unsigned total;
4559 unsigned div_pos;
4561 if (!bmap)
4562 return -1;
4564 total = isl_basic_map_total_dim(bmap);
4565 div_pos = total - bmap->n_div + div;
4567 if (sign < 0)
4568 return add_upper_div_constraint(bmap, div_pos, bmap->div[div]);
4569 else
4570 return add_lower_div_constraint(bmap, div_pos, bmap->div[div]);
4573 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4575 return isl_basic_map_add_div_constraints(bset, div);
4578 struct isl_basic_set *isl_basic_map_underlying_set(
4579 struct isl_basic_map *bmap)
4581 if (!bmap)
4582 goto error;
4583 if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4584 bmap->n_div == 0 &&
4585 !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4586 !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4587 return (struct isl_basic_set *)bmap;
4588 bmap = isl_basic_map_cow(bmap);
4589 if (!bmap)
4590 goto error;
4591 bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4592 if (!bmap->dim)
4593 goto error;
4594 bmap->extra -= bmap->n_div;
4595 bmap->n_div = 0;
4596 bmap = isl_basic_map_finalize(bmap);
4597 return (struct isl_basic_set *)bmap;
4598 error:
4599 isl_basic_map_free(bmap);
4600 return NULL;
4603 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4604 __isl_take isl_basic_set *bset)
4606 return isl_basic_map_underlying_set((isl_basic_map *)bset);
4609 /* Replace each element in "list" by the result of applying
4610 * isl_basic_map_underlying_set to the element.
4612 __isl_give isl_basic_set_list *isl_basic_map_list_underlying_set(
4613 __isl_take isl_basic_map_list *list)
4615 int i, n;
4617 if (!list)
4618 return NULL;
4620 n = isl_basic_map_list_n_basic_map(list);
4621 for (i = 0; i < n; ++i) {
4622 isl_basic_map *bmap;
4623 isl_basic_set *bset;
4625 bmap = isl_basic_map_list_get_basic_map(list, i);
4626 bset = isl_basic_set_underlying_set(bmap);
4627 list = isl_basic_set_list_set_basic_set(list, i, bset);
4630 return list;
4633 struct isl_basic_map *isl_basic_map_overlying_set(
4634 struct isl_basic_set *bset, struct isl_basic_map *like)
4636 struct isl_basic_map *bmap;
4637 struct isl_ctx *ctx;
4638 unsigned total;
4639 int i;
4641 if (!bset || !like)
4642 goto error;
4643 ctx = bset->ctx;
4644 isl_assert(ctx, bset->n_div == 0, goto error);
4645 isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4646 isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4647 goto error);
4648 if (like->n_div == 0) {
4649 isl_space *space = isl_basic_map_get_space(like);
4650 isl_basic_map_free(like);
4651 return isl_basic_map_reset_space(bset, space);
4653 bset = isl_basic_set_cow(bset);
4654 if (!bset)
4655 goto error;
4656 total = bset->dim->n_out + bset->extra;
4657 bmap = (struct isl_basic_map *)bset;
4658 isl_space_free(bmap->dim);
4659 bmap->dim = isl_space_copy(like->dim);
4660 if (!bmap->dim)
4661 goto error;
4662 bmap->n_div = like->n_div;
4663 bmap->extra += like->n_div;
4664 if (bmap->extra) {
4665 unsigned ltotal;
4666 isl_int **div;
4667 ltotal = total - bmap->extra + like->extra;
4668 if (ltotal > total)
4669 ltotal = total;
4670 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4671 bmap->extra * (1 + 1 + total));
4672 if (isl_blk_is_error(bmap->block2))
4673 goto error;
4674 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4675 if (!div)
4676 goto error;
4677 bmap->div = div;
4678 for (i = 0; i < bmap->extra; ++i)
4679 bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4680 for (i = 0; i < like->n_div; ++i) {
4681 isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4682 isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4684 bmap = isl_basic_map_add_known_div_constraints(bmap);
4686 isl_basic_map_free(like);
4687 bmap = isl_basic_map_simplify(bmap);
4688 bmap = isl_basic_map_finalize(bmap);
4689 return bmap;
4690 error:
4691 isl_basic_map_free(like);
4692 isl_basic_set_free(bset);
4693 return NULL;
4696 struct isl_basic_set *isl_basic_set_from_underlying_set(
4697 struct isl_basic_set *bset, struct isl_basic_set *like)
4699 return (struct isl_basic_set *)
4700 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4703 struct isl_set *isl_set_from_underlying_set(
4704 struct isl_set *set, struct isl_basic_set *like)
4706 int i;
4708 if (!set || !like)
4709 goto error;
4710 isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4711 goto error);
4712 if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4713 isl_basic_set_free(like);
4714 return set;
4716 set = isl_set_cow(set);
4717 if (!set)
4718 goto error;
4719 for (i = 0; i < set->n; ++i) {
4720 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4721 isl_basic_set_copy(like));
4722 if (!set->p[i])
4723 goto error;
4725 isl_space_free(set->dim);
4726 set->dim = isl_space_copy(like->dim);
4727 if (!set->dim)
4728 goto error;
4729 isl_basic_set_free(like);
4730 return set;
4731 error:
4732 isl_basic_set_free(like);
4733 isl_set_free(set);
4734 return NULL;
4737 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4739 int i;
4741 map = isl_map_cow(map);
4742 if (!map)
4743 return NULL;
4744 map->dim = isl_space_cow(map->dim);
4745 if (!map->dim)
4746 goto error;
4748 for (i = 1; i < map->n; ++i)
4749 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4750 goto error);
4751 for (i = 0; i < map->n; ++i) {
4752 map->p[i] = (struct isl_basic_map *)
4753 isl_basic_map_underlying_set(map->p[i]);
4754 if (!map->p[i])
4755 goto error;
4757 if (map->n == 0)
4758 map->dim = isl_space_underlying(map->dim, 0);
4759 else {
4760 isl_space_free(map->dim);
4761 map->dim = isl_space_copy(map->p[0]->dim);
4763 if (!map->dim)
4764 goto error;
4765 return (struct isl_set *)map;
4766 error:
4767 isl_map_free(map);
4768 return NULL;
4771 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4773 return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4776 /* Replace the space of "bmap" by "space".
4778 * If the space of "bmap" is identical to "space" (including the identifiers
4779 * of the input and output dimensions), then simply return the original input.
4781 __isl_give isl_basic_map *isl_basic_map_reset_space(
4782 __isl_take isl_basic_map *bmap, __isl_take isl_space *space)
4784 isl_bool equal;
4786 if (!bmap)
4787 goto error;
4788 equal = isl_space_is_equal(bmap->dim, space);
4789 if (equal >= 0 && equal)
4790 equal = isl_space_match(bmap->dim, isl_dim_in,
4791 space, isl_dim_in);
4792 if (equal >= 0 && equal)
4793 equal = isl_space_match(bmap->dim, isl_dim_out,
4794 space, isl_dim_out);
4795 if (equal < 0)
4796 goto error;
4797 if (equal) {
4798 isl_space_free(space);
4799 return bmap;
4801 bmap = isl_basic_map_cow(bmap);
4802 if (!bmap || !space)
4803 goto error;
4805 isl_space_free(bmap->dim);
4806 bmap->dim = space;
4808 bmap = isl_basic_map_finalize(bmap);
4810 return bmap;
4811 error:
4812 isl_basic_map_free(bmap);
4813 isl_space_free(space);
4814 return NULL;
4817 __isl_give isl_basic_set *isl_basic_set_reset_space(
4818 __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4820 return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4821 dim);
4824 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4825 __isl_take isl_space *dim)
4827 int i;
4829 map = isl_map_cow(map);
4830 if (!map || !dim)
4831 goto error;
4833 for (i = 0; i < map->n; ++i) {
4834 map->p[i] = isl_basic_map_reset_space(map->p[i],
4835 isl_space_copy(dim));
4836 if (!map->p[i])
4837 goto error;
4839 isl_space_free(map->dim);
4840 map->dim = dim;
4842 return map;
4843 error:
4844 isl_map_free(map);
4845 isl_space_free(dim);
4846 return NULL;
4849 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4850 __isl_take isl_space *dim)
4852 return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4855 /* Compute the parameter domain of the given basic set.
4857 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4859 isl_space *space;
4860 unsigned n;
4862 if (isl_basic_set_is_params(bset))
4863 return bset;
4865 n = isl_basic_set_dim(bset, isl_dim_set);
4866 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4867 space = isl_basic_set_get_space(bset);
4868 space = isl_space_params(space);
4869 bset = isl_basic_set_reset_space(bset, space);
4870 return bset;
4873 /* Construct a zero-dimensional basic set with the given parameter domain.
4875 __isl_give isl_basic_set *isl_basic_set_from_params(
4876 __isl_take isl_basic_set *bset)
4878 isl_space *space;
4879 space = isl_basic_set_get_space(bset);
4880 space = isl_space_set_from_params(space);
4881 bset = isl_basic_set_reset_space(bset, space);
4882 return bset;
4885 /* Compute the parameter domain of the given set.
4887 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4889 isl_space *space;
4890 unsigned n;
4892 if (isl_set_is_params(set))
4893 return set;
4895 n = isl_set_dim(set, isl_dim_set);
4896 set = isl_set_project_out(set, isl_dim_set, 0, n);
4897 space = isl_set_get_space(set);
4898 space = isl_space_params(space);
4899 set = isl_set_reset_space(set, space);
4900 return set;
4903 /* Construct a zero-dimensional set with the given parameter domain.
4905 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4907 isl_space *space;
4908 space = isl_set_get_space(set);
4909 space = isl_space_set_from_params(space);
4910 set = isl_set_reset_space(set, space);
4911 return set;
4914 /* Compute the parameter domain of the given map.
4916 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4918 isl_space *space;
4919 unsigned n;
4921 n = isl_map_dim(map, isl_dim_in);
4922 map = isl_map_project_out(map, isl_dim_in, 0, n);
4923 n = isl_map_dim(map, isl_dim_out);
4924 map = isl_map_project_out(map, isl_dim_out, 0, n);
4925 space = isl_map_get_space(map);
4926 space = isl_space_params(space);
4927 map = isl_map_reset_space(map, space);
4928 return map;
4931 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4933 isl_space *space;
4934 unsigned n_out;
4936 if (!bmap)
4937 return NULL;
4938 space = isl_space_domain(isl_basic_map_get_space(bmap));
4940 n_out = isl_basic_map_n_out(bmap);
4941 bmap = isl_basic_map_project_out(bmap, isl_dim_out, 0, n_out);
4943 return isl_basic_map_reset_space(bmap, space);
4946 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4948 if (!bmap)
4949 return -1;
4950 return isl_space_may_be_set(bmap->dim);
4953 /* Is this basic map actually a set?
4954 * Users should never call this function. Outside of isl,
4955 * the type should indicate whether something is a set or a map.
4957 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4959 if (!bmap)
4960 return -1;
4961 return isl_space_is_set(bmap->dim);
4964 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4966 if (!bmap)
4967 return NULL;
4968 if (isl_basic_map_is_set(bmap))
4969 return bmap;
4970 return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4973 __isl_give isl_basic_map *isl_basic_map_domain_map(
4974 __isl_take isl_basic_map *bmap)
4976 int i, k;
4977 isl_space *dim;
4978 isl_basic_map *domain;
4979 int nparam, n_in, n_out;
4980 unsigned total;
4982 nparam = isl_basic_map_dim(bmap, isl_dim_param);
4983 n_in = isl_basic_map_dim(bmap, isl_dim_in);
4984 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4986 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4987 domain = isl_basic_map_universe(dim);
4989 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4990 bmap = isl_basic_map_apply_range(bmap, domain);
4991 bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4993 total = isl_basic_map_total_dim(bmap);
4995 for (i = 0; i < n_in; ++i) {
4996 k = isl_basic_map_alloc_equality(bmap);
4997 if (k < 0)
4998 goto error;
4999 isl_seq_clr(bmap->eq[k], 1 + total);
5000 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
5001 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5004 bmap = isl_basic_map_gauss(bmap, NULL);
5005 return isl_basic_map_finalize(bmap);
5006 error:
5007 isl_basic_map_free(bmap);
5008 return NULL;
5011 __isl_give isl_basic_map *isl_basic_map_range_map(
5012 __isl_take isl_basic_map *bmap)
5014 int i, k;
5015 isl_space *dim;
5016 isl_basic_map *range;
5017 int nparam, n_in, n_out;
5018 unsigned total;
5020 nparam = isl_basic_map_dim(bmap, isl_dim_param);
5021 n_in = isl_basic_map_dim(bmap, isl_dim_in);
5022 n_out = isl_basic_map_dim(bmap, isl_dim_out);
5024 dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
5025 range = isl_basic_map_universe(dim);
5027 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5028 bmap = isl_basic_map_apply_range(bmap, range);
5029 bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
5031 total = isl_basic_map_total_dim(bmap);
5033 for (i = 0; i < n_out; ++i) {
5034 k = isl_basic_map_alloc_equality(bmap);
5035 if (k < 0)
5036 goto error;
5037 isl_seq_clr(bmap->eq[k], 1 + total);
5038 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
5039 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
5042 bmap = isl_basic_map_gauss(bmap, NULL);
5043 return isl_basic_map_finalize(bmap);
5044 error:
5045 isl_basic_map_free(bmap);
5046 return NULL;
5049 int isl_map_may_be_set(__isl_keep isl_map *map)
5051 if (!map)
5052 return -1;
5053 return isl_space_may_be_set(map->dim);
5056 /* Is this map actually a set?
5057 * Users should never call this function. Outside of isl,
5058 * the type should indicate whether something is a set or a map.
5060 int isl_map_is_set(__isl_keep isl_map *map)
5062 if (!map)
5063 return -1;
5064 return isl_space_is_set(map->dim);
5067 struct isl_set *isl_map_range(struct isl_map *map)
5069 int i;
5070 struct isl_set *set;
5072 if (!map)
5073 goto error;
5074 if (isl_map_is_set(map))
5075 return (isl_set *)map;
5077 map = isl_map_cow(map);
5078 if (!map)
5079 goto error;
5081 set = (struct isl_set *) map;
5082 set->dim = isl_space_range(set->dim);
5083 if (!set->dim)
5084 goto error;
5085 for (i = 0; i < map->n; ++i) {
5086 set->p[i] = isl_basic_map_range(map->p[i]);
5087 if (!set->p[i])
5088 goto error;
5090 ISL_F_CLR(set, ISL_MAP_DISJOINT);
5091 ISL_F_CLR(set, ISL_SET_NORMALIZED);
5092 return set;
5093 error:
5094 isl_map_free(map);
5095 return NULL;
5098 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
5100 int i;
5102 map = isl_map_cow(map);
5103 if (!map)
5104 return NULL;
5106 map->dim = isl_space_domain_map(map->dim);
5107 if (!map->dim)
5108 goto error;
5109 for (i = 0; i < map->n; ++i) {
5110 map->p[i] = isl_basic_map_domain_map(map->p[i]);
5111 if (!map->p[i])
5112 goto error;
5114 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5115 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5116 return map;
5117 error:
5118 isl_map_free(map);
5119 return NULL;
5122 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
5124 int i;
5125 isl_space *range_dim;
5127 map = isl_map_cow(map);
5128 if (!map)
5129 return NULL;
5131 range_dim = isl_space_range(isl_map_get_space(map));
5132 range_dim = isl_space_from_range(range_dim);
5133 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
5134 map->dim = isl_space_join(map->dim, range_dim);
5135 if (!map->dim)
5136 goto error;
5137 for (i = 0; i < map->n; ++i) {
5138 map->p[i] = isl_basic_map_range_map(map->p[i]);
5139 if (!map->p[i])
5140 goto error;
5142 ISL_F_CLR(map, ISL_MAP_DISJOINT);
5143 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5144 return map;
5145 error:
5146 isl_map_free(map);
5147 return NULL;
5150 /* Given a wrapped map of the form A[B -> C],
5151 * return the map A[B -> C] -> B.
5153 __isl_give isl_map *isl_set_wrapped_domain_map(__isl_take isl_set *set)
5155 isl_id *id;
5156 isl_map *map;
5158 if (!set)
5159 return NULL;
5160 if (!isl_set_has_tuple_id(set))
5161 return isl_map_domain_map(isl_set_unwrap(set));
5163 id = isl_set_get_tuple_id(set);
5164 map = isl_map_domain_map(isl_set_unwrap(set));
5165 map = isl_map_set_tuple_id(map, isl_dim_in, id);
5167 return map;
5170 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
5171 __isl_take isl_space *dim)
5173 int i;
5174 struct isl_map *map = NULL;
5176 set = isl_set_cow(set);
5177 if (!set || !dim)
5178 goto error;
5179 isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
5180 map = (struct isl_map *)set;
5181 for (i = 0; i < set->n; ++i) {
5182 map->p[i] = isl_basic_map_from_basic_set(
5183 set->p[i], isl_space_copy(dim));
5184 if (!map->p[i])
5185 goto error;
5187 isl_space_free(map->dim);
5188 map->dim = dim;
5189 return map;
5190 error:
5191 isl_space_free(dim);
5192 isl_set_free(set);
5193 return NULL;
5196 __isl_give isl_basic_map *isl_basic_map_from_domain(
5197 __isl_take isl_basic_set *bset)
5199 return isl_basic_map_reverse(isl_basic_map_from_range(bset));
5202 __isl_give isl_basic_map *isl_basic_map_from_range(
5203 __isl_take isl_basic_set *bset)
5205 isl_space *space;
5206 space = isl_basic_set_get_space(bset);
5207 space = isl_space_from_range(space);
5208 bset = isl_basic_set_reset_space(bset, space);
5209 return (isl_basic_map *)bset;
5212 /* Create a relation with the given set as range.
5213 * The domain of the created relation is a zero-dimensional
5214 * flat anonymous space.
5216 __isl_give isl_map *isl_map_from_range(__isl_take isl_set *set)
5218 isl_space *space;
5219 space = isl_set_get_space(set);
5220 space = isl_space_from_range(space);
5221 set = isl_set_reset_space(set, space);
5222 return (struct isl_map *)set;
5225 /* Create a relation with the given set as domain.
5226 * The range of the created relation is a zero-dimensional
5227 * flat anonymous space.
5229 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
5231 return isl_map_reverse(isl_map_from_range(set));
5234 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
5235 __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
5237 return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
5240 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
5241 __isl_take isl_set *range)
5243 return isl_map_apply_range(isl_map_reverse(domain), range);
5246 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
5247 unsigned flags)
5249 struct isl_map *map;
5251 if (!dim)
5252 return NULL;
5253 if (n < 0)
5254 isl_die(dim->ctx, isl_error_internal,
5255 "negative number of basic maps", goto error);
5256 map = isl_alloc(dim->ctx, struct isl_map,
5257 sizeof(struct isl_map) +
5258 (n - 1) * sizeof(struct isl_basic_map *));
5259 if (!map)
5260 goto error;
5262 map->ctx = dim->ctx;
5263 isl_ctx_ref(map->ctx);
5264 map->ref = 1;
5265 map->size = n;
5266 map->n = 0;
5267 map->dim = dim;
5268 map->flags = flags;
5269 return map;
5270 error:
5271 isl_space_free(dim);
5272 return NULL;
5275 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
5276 unsigned nparam, unsigned in, unsigned out, int n,
5277 unsigned flags)
5279 struct isl_map *map;
5280 isl_space *dims;
5282 dims = isl_space_alloc(ctx, nparam, in, out);
5283 if (!dims)
5284 return NULL;
5286 map = isl_map_alloc_space(dims, n, flags);
5287 return map;
5290 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5292 struct isl_basic_map *bmap;
5293 bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5294 bmap = isl_basic_map_set_to_empty(bmap);
5295 return bmap;
5298 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5300 struct isl_basic_set *bset;
5301 bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5302 bset = isl_basic_set_set_to_empty(bset);
5303 return bset;
5306 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5308 struct isl_basic_map *bmap;
5309 bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5310 bmap = isl_basic_map_finalize(bmap);
5311 return bmap;
5314 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5316 struct isl_basic_set *bset;
5317 bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5318 bset = isl_basic_set_finalize(bset);
5319 return bset;
5322 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5324 int i;
5325 unsigned total = isl_space_dim(dim, isl_dim_all);
5326 isl_basic_map *bmap;
5328 bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5329 for (i = 0; i < total; ++i) {
5330 int k = isl_basic_map_alloc_inequality(bmap);
5331 if (k < 0)
5332 goto error;
5333 isl_seq_clr(bmap->ineq[k], 1 + total);
5334 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5336 return bmap;
5337 error:
5338 isl_basic_map_free(bmap);
5339 return NULL;
5342 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5344 return isl_basic_map_nat_universe(dim);
5347 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5349 return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5352 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5354 return isl_map_nat_universe(dim);
5357 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5359 return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5362 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5364 return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5367 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5369 struct isl_map *map;
5370 if (!dim)
5371 return NULL;
5372 map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5373 map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5374 return map;
5377 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5379 struct isl_set *set;
5380 if (!dim)
5381 return NULL;
5382 set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5383 set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5384 return set;
5387 struct isl_map *isl_map_dup(struct isl_map *map)
5389 int i;
5390 struct isl_map *dup;
5392 if (!map)
5393 return NULL;
5394 dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5395 for (i = 0; i < map->n; ++i)
5396 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5397 return dup;
5400 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5401 __isl_take isl_basic_map *bmap)
5403 if (!bmap || !map)
5404 goto error;
5405 if (isl_basic_map_plain_is_empty(bmap)) {
5406 isl_basic_map_free(bmap);
5407 return map;
5409 isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5410 isl_assert(map->ctx, map->n < map->size, goto error);
5411 map->p[map->n] = bmap;
5412 map->n++;
5413 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5414 return map;
5415 error:
5416 if (map)
5417 isl_map_free(map);
5418 if (bmap)
5419 isl_basic_map_free(bmap);
5420 return NULL;
5423 __isl_null isl_map *isl_map_free(__isl_take isl_map *map)
5425 int i;
5427 if (!map)
5428 return NULL;
5430 if (--map->ref > 0)
5431 return NULL;
5433 isl_ctx_deref(map->ctx);
5434 for (i = 0; i < map->n; ++i)
5435 isl_basic_map_free(map->p[i]);
5436 isl_space_free(map->dim);
5437 free(map);
5439 return NULL;
5442 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5443 struct isl_basic_map *bmap, unsigned pos, int value)
5445 int j;
5447 bmap = isl_basic_map_cow(bmap);
5448 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5449 j = isl_basic_map_alloc_equality(bmap);
5450 if (j < 0)
5451 goto error;
5452 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5453 isl_int_set_si(bmap->eq[j][pos], -1);
5454 isl_int_set_si(bmap->eq[j][0], value);
5455 bmap = isl_basic_map_simplify(bmap);
5456 return isl_basic_map_finalize(bmap);
5457 error:
5458 isl_basic_map_free(bmap);
5459 return NULL;
5462 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5463 __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5465 int j;
5467 bmap = isl_basic_map_cow(bmap);
5468 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5469 j = isl_basic_map_alloc_equality(bmap);
5470 if (j < 0)
5471 goto error;
5472 isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5473 isl_int_set_si(bmap->eq[j][pos], -1);
5474 isl_int_set(bmap->eq[j][0], value);
5475 bmap = isl_basic_map_simplify(bmap);
5476 return isl_basic_map_finalize(bmap);
5477 error:
5478 isl_basic_map_free(bmap);
5479 return NULL;
5482 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5483 enum isl_dim_type type, unsigned pos, int value)
5485 if (!bmap)
5486 return NULL;
5487 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5488 return isl_basic_map_fix_pos_si(bmap,
5489 isl_basic_map_offset(bmap, type) + pos, value);
5490 error:
5491 isl_basic_map_free(bmap);
5492 return NULL;
5495 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5496 enum isl_dim_type type, unsigned pos, isl_int value)
5498 if (!bmap)
5499 return NULL;
5500 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5501 return isl_basic_map_fix_pos(bmap,
5502 isl_basic_map_offset(bmap, type) + pos, value);
5503 error:
5504 isl_basic_map_free(bmap);
5505 return NULL;
5508 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5509 * to be equal to "v".
5511 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5512 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5514 if (!bmap || !v)
5515 goto error;
5516 if (!isl_val_is_int(v))
5517 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5518 "expecting integer value", goto error);
5519 if (pos >= isl_basic_map_dim(bmap, type))
5520 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5521 "index out of bounds", goto error);
5522 pos += isl_basic_map_offset(bmap, type);
5523 bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5524 isl_val_free(v);
5525 return bmap;
5526 error:
5527 isl_basic_map_free(bmap);
5528 isl_val_free(v);
5529 return NULL;
5532 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5533 * to be equal to "v".
5535 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5536 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5538 return isl_basic_map_fix_val(bset, type, pos, v);
5541 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5542 enum isl_dim_type type, unsigned pos, int value)
5544 return (struct isl_basic_set *)
5545 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5546 type, pos, value);
5549 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5550 enum isl_dim_type type, unsigned pos, isl_int value)
5552 return (struct isl_basic_set *)
5553 isl_basic_map_fix((struct isl_basic_map *)bset,
5554 type, pos, value);
5557 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5558 unsigned input, int value)
5560 return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5563 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5564 unsigned dim, int value)
5566 return (struct isl_basic_set *)
5567 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5568 isl_dim_set, dim, value);
5571 static int remove_if_empty(__isl_keep isl_map *map, int i)
5573 int empty = isl_basic_map_plain_is_empty(map->p[i]);
5575 if (empty < 0)
5576 return -1;
5577 if (!empty)
5578 return 0;
5580 isl_basic_map_free(map->p[i]);
5581 if (i != map->n - 1) {
5582 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5583 map->p[i] = map->p[map->n - 1];
5585 map->n--;
5587 return 0;
5590 /* Perform "fn" on each basic map of "map", where we may not be holding
5591 * the only reference to "map".
5592 * In particular, "fn" should be a semantics preserving operation
5593 * that we want to apply to all copies of "map". We therefore need
5594 * to be careful not to modify "map" in a way that breaks "map"
5595 * in case anything goes wrong.
5597 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5598 __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5600 struct isl_basic_map *bmap;
5601 int i;
5603 if (!map)
5604 return NULL;
5606 for (i = map->n - 1; i >= 0; --i) {
5607 bmap = isl_basic_map_copy(map->p[i]);
5608 bmap = fn(bmap);
5609 if (!bmap)
5610 goto error;
5611 isl_basic_map_free(map->p[i]);
5612 map->p[i] = bmap;
5613 if (remove_if_empty(map, i) < 0)
5614 goto error;
5617 return map;
5618 error:
5619 isl_map_free(map);
5620 return NULL;
5623 struct isl_map *isl_map_fix_si(struct isl_map *map,
5624 enum isl_dim_type type, unsigned pos, int value)
5626 int i;
5628 map = isl_map_cow(map);
5629 if (!map)
5630 return NULL;
5632 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5633 for (i = map->n - 1; i >= 0; --i) {
5634 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5635 if (remove_if_empty(map, i) < 0)
5636 goto error;
5638 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5639 return map;
5640 error:
5641 isl_map_free(map);
5642 return NULL;
5645 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5646 enum isl_dim_type type, unsigned pos, int value)
5648 return (struct isl_set *)
5649 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5652 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5653 enum isl_dim_type type, unsigned pos, isl_int value)
5655 int i;
5657 map = isl_map_cow(map);
5658 if (!map)
5659 return NULL;
5661 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5662 for (i = 0; i < map->n; ++i) {
5663 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5664 if (!map->p[i])
5665 goto error;
5667 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5668 return map;
5669 error:
5670 isl_map_free(map);
5671 return NULL;
5674 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5675 enum isl_dim_type type, unsigned pos, isl_int value)
5677 return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5680 /* Fix the value of the variable at position "pos" of type "type" of "map"
5681 * to be equal to "v".
5683 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5684 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5686 int i;
5688 map = isl_map_cow(map);
5689 if (!map || !v)
5690 goto error;
5692 if (!isl_val_is_int(v))
5693 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5694 "expecting integer value", goto error);
5695 if (pos >= isl_map_dim(map, type))
5696 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5697 "index out of bounds", goto error);
5698 for (i = map->n - 1; i >= 0; --i) {
5699 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5700 isl_val_copy(v));
5701 if (remove_if_empty(map, i) < 0)
5702 goto error;
5704 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5705 isl_val_free(v);
5706 return map;
5707 error:
5708 isl_map_free(map);
5709 isl_val_free(v);
5710 return NULL;
5713 /* Fix the value of the variable at position "pos" of type "type" of "set"
5714 * to be equal to "v".
5716 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5717 enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5719 return isl_map_fix_val(set, type, pos, v);
5722 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5723 unsigned input, int value)
5725 return isl_map_fix_si(map, isl_dim_in, input, value);
5728 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5730 return (struct isl_set *)
5731 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5734 static __isl_give isl_basic_map *basic_map_bound_si(
5735 __isl_take isl_basic_map *bmap,
5736 enum isl_dim_type type, unsigned pos, int value, int upper)
5738 int j;
5740 if (!bmap)
5741 return NULL;
5742 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5743 pos += isl_basic_map_offset(bmap, type);
5744 bmap = isl_basic_map_cow(bmap);
5745 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5746 j = isl_basic_map_alloc_inequality(bmap);
5747 if (j < 0)
5748 goto error;
5749 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5750 if (upper) {
5751 isl_int_set_si(bmap->ineq[j][pos], -1);
5752 isl_int_set_si(bmap->ineq[j][0], value);
5753 } else {
5754 isl_int_set_si(bmap->ineq[j][pos], 1);
5755 isl_int_set_si(bmap->ineq[j][0], -value);
5757 bmap = isl_basic_map_simplify(bmap);
5758 return isl_basic_map_finalize(bmap);
5759 error:
5760 isl_basic_map_free(bmap);
5761 return NULL;
5764 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5765 __isl_take isl_basic_map *bmap,
5766 enum isl_dim_type type, unsigned pos, int value)
5768 return basic_map_bound_si(bmap, type, pos, value, 0);
5771 /* Constrain the values of the given dimension to be no greater than "value".
5773 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5774 __isl_take isl_basic_map *bmap,
5775 enum isl_dim_type type, unsigned pos, int value)
5777 return basic_map_bound_si(bmap, type, pos, value, 1);
5780 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5781 unsigned dim, isl_int value)
5783 int j;
5785 bset = isl_basic_set_cow(bset);
5786 bset = isl_basic_set_extend_constraints(bset, 0, 1);
5787 j = isl_basic_set_alloc_inequality(bset);
5788 if (j < 0)
5789 goto error;
5790 isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5791 isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5792 isl_int_neg(bset->ineq[j][0], value);
5793 bset = isl_basic_set_simplify(bset);
5794 return isl_basic_set_finalize(bset);
5795 error:
5796 isl_basic_set_free(bset);
5797 return NULL;
5800 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5801 enum isl_dim_type type, unsigned pos, int value, int upper)
5803 int i;
5805 map = isl_map_cow(map);
5806 if (!map)
5807 return NULL;
5809 isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5810 for (i = 0; i < map->n; ++i) {
5811 map->p[i] = basic_map_bound_si(map->p[i],
5812 type, pos, value, upper);
5813 if (!map->p[i])
5814 goto error;
5816 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5817 return map;
5818 error:
5819 isl_map_free(map);
5820 return NULL;
5823 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5824 enum isl_dim_type type, unsigned pos, int value)
5826 return map_bound_si(map, type, pos, value, 0);
5829 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5830 enum isl_dim_type type, unsigned pos, int value)
5832 return map_bound_si(map, type, pos, value, 1);
5835 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5836 enum isl_dim_type type, unsigned pos, int value)
5838 return (struct isl_set *)
5839 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5842 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5843 enum isl_dim_type type, unsigned pos, int value)
5845 return isl_map_upper_bound_si(set, type, pos, value);
5848 /* Bound the given variable of "bmap" from below (or above is "upper"
5849 * is set) to "value".
5851 static __isl_give isl_basic_map *basic_map_bound(
5852 __isl_take isl_basic_map *bmap,
5853 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5855 int j;
5857 if (!bmap)
5858 return NULL;
5859 if (pos >= isl_basic_map_dim(bmap, type))
5860 isl_die(bmap->ctx, isl_error_invalid,
5861 "index out of bounds", goto error);
5862 pos += isl_basic_map_offset(bmap, type);
5863 bmap = isl_basic_map_cow(bmap);
5864 bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5865 j = isl_basic_map_alloc_inequality(bmap);
5866 if (j < 0)
5867 goto error;
5868 isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5869 if (upper) {
5870 isl_int_set_si(bmap->ineq[j][pos], -1);
5871 isl_int_set(bmap->ineq[j][0], value);
5872 } else {
5873 isl_int_set_si(bmap->ineq[j][pos], 1);
5874 isl_int_neg(bmap->ineq[j][0], value);
5876 bmap = isl_basic_map_simplify(bmap);
5877 return isl_basic_map_finalize(bmap);
5878 error:
5879 isl_basic_map_free(bmap);
5880 return NULL;
5883 /* Bound the given variable of "map" from below (or above is "upper"
5884 * is set) to "value".
5886 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5887 enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5889 int i;
5891 map = isl_map_cow(map);
5892 if (!map)
5893 return NULL;
5895 if (pos >= isl_map_dim(map, type))
5896 isl_die(map->ctx, isl_error_invalid,
5897 "index out of bounds", goto error);
5898 for (i = map->n - 1; i >= 0; --i) {
5899 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5900 if (remove_if_empty(map, i) < 0)
5901 goto error;
5903 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5904 return map;
5905 error:
5906 isl_map_free(map);
5907 return NULL;
5910 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5911 enum isl_dim_type type, unsigned pos, isl_int value)
5913 return map_bound(map, type, pos, value, 0);
5916 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5917 enum isl_dim_type type, unsigned pos, isl_int value)
5919 return map_bound(map, type, pos, value, 1);
5922 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5923 enum isl_dim_type type, unsigned pos, isl_int value)
5925 return isl_map_lower_bound(set, type, pos, value);
5928 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5929 enum isl_dim_type type, unsigned pos, isl_int value)
5931 return isl_map_upper_bound(set, type, pos, value);
5934 /* Force the values of the variable at position "pos" of type "type" of "set"
5935 * to be no smaller than "value".
5937 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5938 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5940 if (!value)
5941 goto error;
5942 if (!isl_val_is_int(value))
5943 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5944 "expecting integer value", goto error);
5945 set = isl_set_lower_bound(set, type, pos, value->n);
5946 isl_val_free(value);
5947 return set;
5948 error:
5949 isl_val_free(value);
5950 isl_set_free(set);
5951 return NULL;
5954 /* Force the values of the variable at position "pos" of type "type" of "set"
5955 * to be no greater than "value".
5957 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5958 enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5960 if (!value)
5961 goto error;
5962 if (!isl_val_is_int(value))
5963 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5964 "expecting integer value", goto error);
5965 set = isl_set_upper_bound(set, type, pos, value->n);
5966 isl_val_free(value);
5967 return set;
5968 error:
5969 isl_val_free(value);
5970 isl_set_free(set);
5971 return NULL;
5974 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5975 isl_int value)
5977 int i;
5979 set = isl_set_cow(set);
5980 if (!set)
5981 return NULL;
5983 isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5984 for (i = 0; i < set->n; ++i) {
5985 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5986 if (!set->p[i])
5987 goto error;
5989 return set;
5990 error:
5991 isl_set_free(set);
5992 return NULL;
5995 struct isl_map *isl_map_reverse(struct isl_map *map)
5997 int i;
5999 map = isl_map_cow(map);
6000 if (!map)
6001 return NULL;
6003 map->dim = isl_space_reverse(map->dim);
6004 if (!map->dim)
6005 goto error;
6006 for (i = 0; i < map->n; ++i) {
6007 map->p[i] = isl_basic_map_reverse(map->p[i]);
6008 if (!map->p[i])
6009 goto error;
6011 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6012 return map;
6013 error:
6014 isl_map_free(map);
6015 return NULL;
6018 static struct isl_map *isl_basic_map_partial_lexopt(
6019 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6020 struct isl_set **empty, int max)
6022 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
6025 struct isl_map *isl_basic_map_partial_lexmax(
6026 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6027 struct isl_set **empty)
6029 return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
6032 struct isl_map *isl_basic_map_partial_lexmin(
6033 struct isl_basic_map *bmap, struct isl_basic_set *dom,
6034 struct isl_set **empty)
6036 return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
6039 struct isl_set *isl_basic_set_partial_lexmin(
6040 struct isl_basic_set *bset, struct isl_basic_set *dom,
6041 struct isl_set **empty)
6043 return (struct isl_set *)
6044 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
6045 dom, empty);
6048 struct isl_set *isl_basic_set_partial_lexmax(
6049 struct isl_basic_set *bset, struct isl_basic_set *dom,
6050 struct isl_set **empty)
6052 return (struct isl_set *)
6053 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
6054 dom, empty);
6057 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
6058 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6059 __isl_give isl_set **empty)
6061 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
6064 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
6065 __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
6066 __isl_give isl_set **empty)
6068 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
6071 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
6072 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6073 __isl_give isl_set **empty)
6075 return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
6078 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
6079 __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
6080 __isl_give isl_set **empty)
6082 return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
6085 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
6086 __isl_take isl_basic_map *bmap, int max)
6088 isl_basic_set *dom = NULL;
6089 isl_space *dom_space;
6091 if (!bmap)
6092 goto error;
6093 dom_space = isl_space_domain(isl_space_copy(bmap->dim));
6094 dom = isl_basic_set_universe(dom_space);
6095 return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
6096 error:
6097 isl_basic_map_free(bmap);
6098 return NULL;
6101 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
6102 __isl_take isl_basic_map *bmap)
6104 return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
6107 #undef TYPE
6108 #define TYPE isl_pw_multi_aff
6109 #undef SUFFIX
6110 #define SUFFIX _pw_multi_aff
6111 #undef EMPTY
6112 #define EMPTY isl_pw_multi_aff_empty
6113 #undef ADD
6114 #define ADD isl_pw_multi_aff_union_add
6115 #include "isl_map_lexopt_templ.c"
6117 /* Given a map "map", compute the lexicographically minimal
6118 * (or maximal) image element for each domain element in dom,
6119 * in the form of an isl_pw_multi_aff.
6120 * Set *empty to those elements in dom that do not have an image element.
6122 * We first compute the lexicographically minimal or maximal element
6123 * in the first basic map. This results in a partial solution "res"
6124 * and a subset "todo" of dom that still need to be handled.
6125 * We then consider each of the remaining maps in "map" and successively
6126 * update both "res" and "todo".
6128 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
6129 __isl_take isl_map *map, __isl_take isl_set *dom,
6130 __isl_give isl_set **empty, int max)
6132 int i;
6133 isl_pw_multi_aff *res;
6134 isl_set *todo;
6136 if (!map || !dom)
6137 goto error;
6139 if (isl_map_plain_is_empty(map)) {
6140 if (empty)
6141 *empty = dom;
6142 else
6143 isl_set_free(dom);
6144 return isl_pw_multi_aff_from_map(map);
6147 res = basic_map_partial_lexopt_pw_multi_aff(
6148 isl_basic_map_copy(map->p[0]),
6149 isl_set_copy(dom), &todo, max);
6151 for (i = 1; i < map->n; ++i) {
6152 isl_pw_multi_aff *res_i;
6153 isl_set *todo_i;
6155 res_i = basic_map_partial_lexopt_pw_multi_aff(
6156 isl_basic_map_copy(map->p[i]),
6157 isl_set_copy(dom), &todo_i, max);
6159 if (max)
6160 res = isl_pw_multi_aff_union_lexmax(res, res_i);
6161 else
6162 res = isl_pw_multi_aff_union_lexmin(res, res_i);
6164 todo = isl_set_intersect(todo, todo_i);
6167 isl_set_free(dom);
6168 isl_map_free(map);
6170 if (empty)
6171 *empty = todo;
6172 else
6173 isl_set_free(todo);
6175 return res;
6176 error:
6177 if (empty)
6178 *empty = NULL;
6179 isl_set_free(dom);
6180 isl_map_free(map);
6181 return NULL;
6184 #undef TYPE
6185 #define TYPE isl_map
6186 #undef SUFFIX
6187 #define SUFFIX
6188 #undef EMPTY
6189 #define EMPTY isl_map_empty
6190 #undef ADD
6191 #define ADD isl_map_union_disjoint
6192 #include "isl_map_lexopt_templ.c"
6194 /* Given a map "map", compute the lexicographically minimal
6195 * (or maximal) image element for each domain element in dom.
6196 * Set *empty to those elements in dom that do not have an image element.
6198 * We first compute the lexicographically minimal or maximal element
6199 * in the first basic map. This results in a partial solution "res"
6200 * and a subset "todo" of dom that still need to be handled.
6201 * We then consider each of the remaining maps in "map" and successively
6202 * update both "res" and "todo".
6204 * Let res^k and todo^k be the results after k steps and let i = k + 1.
6205 * Assume we are computing the lexicographical maximum.
6206 * We first compute the lexicographically maximal element in basic map i.
6207 * This results in a partial solution res_i and a subset todo_i.
6208 * Then we combine these results with those obtain for the first k basic maps
6209 * to obtain a result that is valid for the first k+1 basic maps.
6210 * In particular, the set where there is no solution is the set where
6211 * there is no solution for the first k basic maps and also no solution
6212 * for the ith basic map, i.e.,
6214 * todo^i = todo^k * todo_i
6216 * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6217 * solutions, arbitrarily breaking ties in favor of res^k.
6218 * That is, when res^k(a) >= res_i(a), we pick res^k and
6219 * when res^k(a) < res_i(a), we pick res_i. (Here, ">=" and "<" denote
6220 * the lexicographic order.)
6221 * In practice, we compute
6223 * res^k * (res_i . "<=")
6225 * and
6227 * res_i * (res^k . "<")
6229 * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6230 * where only one of res^k and res_i provides a solution and we simply pick
6231 * that one, i.e.,
6233 * res^k * todo_i
6234 * and
6235 * res_i * todo^k
6237 * Note that we only compute these intersections when dom(res^k) intersects
6238 * dom(res_i). Otherwise, the only effect of these intersections is to
6239 * potentially break up res^k and res_i into smaller pieces.
6240 * We want to avoid such splintering as much as possible.
6241 * In fact, an earlier implementation of this function would look for
6242 * better results in the domain of res^k and for extra results in todo^k,
6243 * but this would always result in a splintering according to todo^k,
6244 * even when the domain of basic map i is disjoint from the domains of
6245 * the previous basic maps.
6247 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6248 __isl_take isl_map *map, __isl_take isl_set *dom,
6249 __isl_give isl_set **empty, int max)
6251 int i;
6252 struct isl_map *res;
6253 struct isl_set *todo;
6255 if (!map || !dom)
6256 goto error;
6258 if (isl_map_plain_is_empty(map)) {
6259 if (empty)
6260 *empty = dom;
6261 else
6262 isl_set_free(dom);
6263 return map;
6266 res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6267 isl_set_copy(dom), &todo, max);
6269 for (i = 1; i < map->n; ++i) {
6270 isl_map *lt, *le;
6271 isl_map *res_i;
6272 isl_set *todo_i;
6273 isl_space *dim = isl_space_range(isl_map_get_space(res));
6275 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6276 isl_set_copy(dom), &todo_i, max);
6278 if (max) {
6279 lt = isl_map_lex_lt(isl_space_copy(dim));
6280 le = isl_map_lex_le(dim);
6281 } else {
6282 lt = isl_map_lex_gt(isl_space_copy(dim));
6283 le = isl_map_lex_ge(dim);
6285 lt = isl_map_apply_range(isl_map_copy(res), lt);
6286 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6287 le = isl_map_apply_range(isl_map_copy(res_i), le);
6288 le = isl_map_intersect(le, isl_map_copy(res));
6290 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6291 res = isl_map_intersect_domain(res,
6292 isl_set_copy(todo_i));
6293 res_i = isl_map_intersect_domain(res_i,
6294 isl_set_copy(todo));
6297 res = isl_map_union_disjoint(res, res_i);
6298 res = isl_map_union_disjoint(res, lt);
6299 res = isl_map_union_disjoint(res, le);
6301 todo = isl_set_intersect(todo, todo_i);
6304 isl_set_free(dom);
6305 isl_map_free(map);
6307 if (empty)
6308 *empty = todo;
6309 else
6310 isl_set_free(todo);
6312 return res;
6313 error:
6314 if (empty)
6315 *empty = NULL;
6316 isl_set_free(dom);
6317 isl_map_free(map);
6318 return NULL;
6321 __isl_give isl_map *isl_map_partial_lexmax(
6322 __isl_take isl_map *map, __isl_take isl_set *dom,
6323 __isl_give isl_set **empty)
6325 return isl_map_partial_lexopt(map, dom, empty, 1);
6328 __isl_give isl_map *isl_map_partial_lexmin(
6329 __isl_take isl_map *map, __isl_take isl_set *dom,
6330 __isl_give isl_set **empty)
6332 return isl_map_partial_lexopt(map, dom, empty, 0);
6335 __isl_give isl_set *isl_set_partial_lexmin(
6336 __isl_take isl_set *set, __isl_take isl_set *dom,
6337 __isl_give isl_set **empty)
6339 return (struct isl_set *)
6340 isl_map_partial_lexmin((struct isl_map *)set,
6341 dom, empty);
6344 __isl_give isl_set *isl_set_partial_lexmax(
6345 __isl_take isl_set *set, __isl_take isl_set *dom,
6346 __isl_give isl_set **empty)
6348 return (struct isl_set *)
6349 isl_map_partial_lexmax((struct isl_map *)set,
6350 dom, empty);
6353 /* Compute the lexicographic minimum (or maximum if "max" is set)
6354 * of "bmap" over its domain.
6356 * Since we are not interested in the part of the domain space where
6357 * there is no solution, we initialize the domain to those constraints
6358 * of "bmap" that only involve the parameters and the input dimensions.
6359 * This relieves the parametric programming engine from detecting those
6360 * inequalities and transferring them to the context. More importantly,
6361 * it ensures that those inequalities are transferred first and not
6362 * intermixed with inequalities that actually split the domain.
6364 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6366 int n_div;
6367 int n_out;
6368 isl_basic_map *copy;
6369 isl_basic_set *dom;
6371 n_div = isl_basic_map_dim(bmap, isl_dim_div);
6372 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6373 copy = isl_basic_map_copy(bmap);
6374 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6375 isl_dim_div, 0, n_div);
6376 copy = isl_basic_map_drop_constraints_involving_dims(copy,
6377 isl_dim_out, 0, n_out);
6378 dom = isl_basic_map_domain(copy);
6379 return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6382 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6384 return isl_basic_map_lexopt(bmap, 0);
6387 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6389 return isl_basic_map_lexopt(bmap, 1);
6392 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6394 return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6397 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6399 return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6402 /* Extract the first and only affine expression from list
6403 * and then add it to *pwaff with the given dom.
6404 * This domain is known to be disjoint from other domains
6405 * because of the way isl_basic_map_foreach_lexmax works.
6407 static int update_dim_opt(__isl_take isl_basic_set *dom,
6408 __isl_take isl_aff_list *list, void *user)
6410 isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6411 isl_aff *aff;
6412 isl_pw_aff **pwaff = user;
6413 isl_pw_aff *pwaff_i;
6415 if (!list)
6416 goto error;
6417 if (isl_aff_list_n_aff(list) != 1)
6418 isl_die(ctx, isl_error_internal,
6419 "expecting single element list", goto error);
6421 aff = isl_aff_list_get_aff(list, 0);
6422 pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6424 *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6426 isl_aff_list_free(list);
6428 return 0;
6429 error:
6430 isl_basic_set_free(dom);
6431 isl_aff_list_free(list);
6432 return -1;
6435 /* Given a basic map with one output dimension, compute the minimum or
6436 * maximum of that dimension as an isl_pw_aff.
6438 * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6439 * call update_dim_opt on each leaf of the result.
6441 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6442 int max)
6444 isl_space *dim = isl_basic_map_get_space(bmap);
6445 isl_pw_aff *pwaff;
6446 int r;
6448 dim = isl_space_from_domain(isl_space_domain(dim));
6449 dim = isl_space_add_dims(dim, isl_dim_out, 1);
6450 pwaff = isl_pw_aff_empty(dim);
6452 r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6453 if (r < 0)
6454 return isl_pw_aff_free(pwaff);
6456 return pwaff;
6459 /* Compute the minimum or maximum of the given output dimension
6460 * as a function of the parameters and the input dimensions,
6461 * but independently of the other output dimensions.
6463 * We first project out the other output dimension and then compute
6464 * the "lexicographic" maximum in each basic map, combining the results
6465 * using isl_pw_aff_union_max.
6467 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6468 int max)
6470 int i;
6471 isl_pw_aff *pwaff;
6472 unsigned n_out;
6474 n_out = isl_map_dim(map, isl_dim_out);
6475 map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6476 map = isl_map_project_out(map, isl_dim_out, 0, pos);
6477 if (!map)
6478 return NULL;
6480 if (map->n == 0) {
6481 isl_space *dim = isl_map_get_space(map);
6482 isl_map_free(map);
6483 return isl_pw_aff_empty(dim);
6486 pwaff = basic_map_dim_opt(map->p[0], max);
6487 for (i = 1; i < map->n; ++i) {
6488 isl_pw_aff *pwaff_i;
6490 pwaff_i = basic_map_dim_opt(map->p[i], max);
6491 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6494 isl_map_free(map);
6496 return pwaff;
6499 /* Compute the maximum of the given output dimension as a function of the
6500 * parameters and input dimensions, but independently of
6501 * the other output dimensions.
6503 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6505 return map_dim_opt(map, pos, 1);
6508 /* Compute the minimum or maximum of the given set dimension
6509 * as a function of the parameters,
6510 * but independently of the other set dimensions.
6512 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6513 int max)
6515 return map_dim_opt(set, pos, max);
6518 /* Compute the maximum of the given set dimension as a function of the
6519 * parameters, but independently of the other set dimensions.
6521 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6523 return set_dim_opt(set, pos, 1);
6526 /* Compute the minimum of the given set dimension as a function of the
6527 * parameters, but independently of the other set dimensions.
6529 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6531 return set_dim_opt(set, pos, 0);
6534 /* Apply a preimage specified by "mat" on the parameters of "bset".
6535 * bset is assumed to have only parameters and divs.
6537 static struct isl_basic_set *basic_set_parameter_preimage(
6538 struct isl_basic_set *bset, struct isl_mat *mat)
6540 unsigned nparam;
6542 if (!bset || !mat)
6543 goto error;
6545 bset->dim = isl_space_cow(bset->dim);
6546 if (!bset->dim)
6547 goto error;
6549 nparam = isl_basic_set_dim(bset, isl_dim_param);
6551 isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6553 bset->dim->nparam = 0;
6554 bset->dim->n_out = nparam;
6555 bset = isl_basic_set_preimage(bset, mat);
6556 if (bset) {
6557 bset->dim->nparam = bset->dim->n_out;
6558 bset->dim->n_out = 0;
6560 return bset;
6561 error:
6562 isl_mat_free(mat);
6563 isl_basic_set_free(bset);
6564 return NULL;
6567 /* Apply a preimage specified by "mat" on the parameters of "set".
6568 * set is assumed to have only parameters and divs.
6570 static __isl_give isl_set *set_parameter_preimage(__isl_take isl_set *set,
6571 __isl_take isl_mat *mat)
6573 isl_space *space;
6574 unsigned nparam;
6576 if (!set || !mat)
6577 goto error;
6579 nparam = isl_set_dim(set, isl_dim_param);
6581 if (mat->n_row != 1 + nparam)
6582 isl_die(isl_set_get_ctx(set), isl_error_internal,
6583 "unexpected number of rows", goto error);
6585 space = isl_set_get_space(set);
6586 space = isl_space_move_dims(space, isl_dim_set, 0,
6587 isl_dim_param, 0, nparam);
6588 set = isl_set_reset_space(set, space);
6589 set = isl_set_preimage(set, mat);
6590 nparam = isl_set_dim(set, isl_dim_out);
6591 space = isl_set_get_space(set);
6592 space = isl_space_move_dims(space, isl_dim_param, 0,
6593 isl_dim_out, 0, nparam);
6594 set = isl_set_reset_space(set, space);
6595 return set;
6596 error:
6597 isl_mat_free(mat);
6598 isl_set_free(set);
6599 return NULL;
6602 /* Intersect the basic set "bset" with the affine space specified by the
6603 * equalities in "eq".
6605 static struct isl_basic_set *basic_set_append_equalities(
6606 struct isl_basic_set *bset, struct isl_mat *eq)
6608 int i, k;
6609 unsigned len;
6611 if (!bset || !eq)
6612 goto error;
6614 bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6615 eq->n_row, 0);
6616 if (!bset)
6617 goto error;
6619 len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6620 for (i = 0; i < eq->n_row; ++i) {
6621 k = isl_basic_set_alloc_equality(bset);
6622 if (k < 0)
6623 goto error;
6624 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6625 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6627 isl_mat_free(eq);
6629 bset = isl_basic_set_gauss(bset, NULL);
6630 bset = isl_basic_set_finalize(bset);
6632 return bset;
6633 error:
6634 isl_mat_free(eq);
6635 isl_basic_set_free(bset);
6636 return NULL;
6639 /* Intersect the set "set" with the affine space specified by the
6640 * equalities in "eq".
6642 static struct isl_set *set_append_equalities(struct isl_set *set,
6643 struct isl_mat *eq)
6645 int i;
6647 if (!set || !eq)
6648 goto error;
6650 for (i = 0; i < set->n; ++i) {
6651 set->p[i] = basic_set_append_equalities(set->p[i],
6652 isl_mat_copy(eq));
6653 if (!set->p[i])
6654 goto error;
6656 isl_mat_free(eq);
6657 return set;
6658 error:
6659 isl_mat_free(eq);
6660 isl_set_free(set);
6661 return NULL;
6664 /* Given a basic set "bset" that only involves parameters and existentially
6665 * quantified variables, return the index of the first equality
6666 * that only involves parameters. If there is no such equality then
6667 * return bset->n_eq.
6669 * This function assumes that isl_basic_set_gauss has been called on "bset".
6671 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6673 int i, j;
6674 unsigned nparam, n_div;
6676 if (!bset)
6677 return -1;
6679 nparam = isl_basic_set_dim(bset, isl_dim_param);
6680 n_div = isl_basic_set_dim(bset, isl_dim_div);
6682 for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6683 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6684 ++i;
6687 return i;
6690 /* Compute an explicit representation for the existentially quantified
6691 * variables in "bset" by computing the "minimal value" of the set
6692 * variables. Since there are no set variables, the computation of
6693 * the minimal value essentially computes an explicit representation
6694 * of the non-empty part(s) of "bset".
6696 * The input only involves parameters and existentially quantified variables.
6697 * All equalities among parameters have been removed.
6699 * Since the existentially quantified variables in the result are in general
6700 * going to be different from those in the input, we first replace
6701 * them by the minimal number of variables based on their equalities.
6702 * This should simplify the parametric integer programming.
6704 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6706 isl_morph *morph1, *morph2;
6707 isl_set *set;
6708 unsigned n;
6710 if (!bset)
6711 return NULL;
6712 if (bset->n_eq == 0)
6713 return isl_basic_set_lexmin(bset);
6715 morph1 = isl_basic_set_parameter_compression(bset);
6716 bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6717 bset = isl_basic_set_lift(bset);
6718 morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6719 bset = isl_morph_basic_set(morph2, bset);
6720 n = isl_basic_set_dim(bset, isl_dim_set);
6721 bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6723 set = isl_basic_set_lexmin(bset);
6725 set = isl_morph_set(isl_morph_inverse(morph1), set);
6727 return set;
6730 /* Project the given basic set onto its parameter domain, possibly introducing
6731 * new, explicit, existential variables in the constraints.
6732 * The input has parameters and (possibly implicit) existential variables.
6733 * The output has the same parameters, but only
6734 * explicit existentially quantified variables.
6736 * The actual projection is performed by pip, but pip doesn't seem
6737 * to like equalities very much, so we first remove the equalities
6738 * among the parameters by performing a variable compression on
6739 * the parameters. Afterward, an inverse transformation is performed
6740 * and the equalities among the parameters are inserted back in.
6742 * The variable compression on the parameters may uncover additional
6743 * equalities that were only implicit before. We therefore check
6744 * if there are any new parameter equalities in the result and
6745 * if so recurse. The removal of parameter equalities is required
6746 * for the parameter compression performed by base_compute_divs.
6748 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6750 int i;
6751 struct isl_mat *eq;
6752 struct isl_mat *T, *T2;
6753 struct isl_set *set;
6754 unsigned nparam;
6756 bset = isl_basic_set_cow(bset);
6757 if (!bset)
6758 return NULL;
6760 if (bset->n_eq == 0)
6761 return base_compute_divs(bset);
6763 bset = isl_basic_set_gauss(bset, NULL);
6764 if (!bset)
6765 return NULL;
6766 if (isl_basic_set_plain_is_empty(bset))
6767 return isl_set_from_basic_set(bset);
6769 i = first_parameter_equality(bset);
6770 if (i == bset->n_eq)
6771 return base_compute_divs(bset);
6773 nparam = isl_basic_set_dim(bset, isl_dim_param);
6774 eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6775 0, 1 + nparam);
6776 eq = isl_mat_cow(eq);
6777 T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6778 if (T && T->n_col == 0) {
6779 isl_mat_free(T);
6780 isl_mat_free(T2);
6781 isl_mat_free(eq);
6782 bset = isl_basic_set_set_to_empty(bset);
6783 return isl_set_from_basic_set(bset);
6785 bset = basic_set_parameter_preimage(bset, T);
6787 i = first_parameter_equality(bset);
6788 if (!bset)
6789 set = NULL;
6790 else if (i == bset->n_eq)
6791 set = base_compute_divs(bset);
6792 else
6793 set = parameter_compute_divs(bset);
6794 set = set_parameter_preimage(set, T2);
6795 set = set_append_equalities(set, eq);
6796 return set;
6799 /* Insert the divs from "ls" before those of "bmap".
6801 * The number of columns is not changed, which means that the last
6802 * dimensions of "bmap" are being reintepreted as the divs from "ls".
6803 * The caller is responsible for removing the same number of dimensions
6804 * from the space of "bmap".
6806 static __isl_give isl_basic_map *insert_divs_from_local_space(
6807 __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6809 int i;
6810 int n_div;
6811 int old_n_div;
6813 n_div = isl_local_space_dim(ls, isl_dim_div);
6814 if (n_div == 0)
6815 return bmap;
6817 old_n_div = bmap->n_div;
6818 bmap = insert_div_rows(bmap, n_div);
6819 if (!bmap)
6820 return NULL;
6822 for (i = 0; i < n_div; ++i) {
6823 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6824 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6827 return bmap;
6830 /* Replace the space of "bmap" by the space and divs of "ls".
6832 * If "ls" has any divs, then we simplify the result since we may
6833 * have discovered some additional equalities that could simplify
6834 * the div expressions.
6836 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6837 __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6839 int n_div;
6841 bmap = isl_basic_map_cow(bmap);
6842 if (!bmap || !ls)
6843 goto error;
6845 n_div = isl_local_space_dim(ls, isl_dim_div);
6846 bmap = insert_divs_from_local_space(bmap, ls);
6847 if (!bmap)
6848 goto error;
6850 isl_space_free(bmap->dim);
6851 bmap->dim = isl_local_space_get_space(ls);
6852 if (!bmap->dim)
6853 goto error;
6855 isl_local_space_free(ls);
6856 if (n_div > 0)
6857 bmap = isl_basic_map_simplify(bmap);
6858 bmap = isl_basic_map_finalize(bmap);
6859 return bmap;
6860 error:
6861 isl_basic_map_free(bmap);
6862 isl_local_space_free(ls);
6863 return NULL;
6866 /* Replace the space of "map" by the space and divs of "ls".
6868 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6869 __isl_take isl_local_space *ls)
6871 int i;
6873 map = isl_map_cow(map);
6874 if (!map || !ls)
6875 goto error;
6877 for (i = 0; i < map->n; ++i) {
6878 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6879 isl_local_space_copy(ls));
6880 if (!map->p[i])
6881 goto error;
6883 isl_space_free(map->dim);
6884 map->dim = isl_local_space_get_space(ls);
6885 if (!map->dim)
6886 goto error;
6888 isl_local_space_free(ls);
6889 return map;
6890 error:
6891 isl_local_space_free(ls);
6892 isl_map_free(map);
6893 return NULL;
6896 /* Compute an explicit representation for the existentially
6897 * quantified variables for which do not know any explicit representation yet.
6899 * We first sort the existentially quantified variables so that the
6900 * existentially quantified variables for which we already have an explicit
6901 * representation are placed before those for which we do not.
6902 * The input dimensions, the output dimensions and the existentially
6903 * quantified variables for which we already have an explicit
6904 * representation are then turned into parameters.
6905 * compute_divs returns a map with the same parameters and
6906 * no input or output dimensions and the dimension specification
6907 * is reset to that of the input, including the existentially quantified
6908 * variables for which we already had an explicit representation.
6910 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6912 struct isl_basic_set *bset;
6913 struct isl_set *set;
6914 struct isl_map *map;
6915 isl_space *dim;
6916 isl_local_space *ls;
6917 unsigned nparam;
6918 unsigned n_in;
6919 unsigned n_out;
6920 int n_known;
6921 int i;
6923 bmap = isl_basic_map_sort_divs(bmap);
6924 bmap = isl_basic_map_cow(bmap);
6925 if (!bmap)
6926 return NULL;
6928 n_known = isl_basic_map_first_unknown_div(bmap);
6929 if (n_known < 0)
6930 return isl_map_from_basic_map(isl_basic_map_free(bmap));
6932 nparam = isl_basic_map_dim(bmap, isl_dim_param);
6933 n_in = isl_basic_map_dim(bmap, isl_dim_in);
6934 n_out = isl_basic_map_dim(bmap, isl_dim_out);
6935 dim = isl_space_set_alloc(bmap->ctx,
6936 nparam + n_in + n_out + n_known, 0);
6937 if (!dim)
6938 goto error;
6940 ls = isl_basic_map_get_local_space(bmap);
6941 ls = isl_local_space_drop_dims(ls, isl_dim_div,
6942 n_known, bmap->n_div - n_known);
6943 if (n_known > 0) {
6944 for (i = n_known; i < bmap->n_div; ++i)
6945 swap_div(bmap, i - n_known, i);
6946 bmap->n_div -= n_known;
6947 bmap->extra -= n_known;
6949 bmap = isl_basic_map_reset_space(bmap, dim);
6950 bset = (struct isl_basic_set *)bmap;
6952 set = parameter_compute_divs(bset);
6953 map = (struct isl_map *)set;
6954 map = replace_space_by_local_space(map, ls);
6956 return map;
6957 error:
6958 isl_basic_map_free(bmap);
6959 return NULL;
6962 /* Remove the explicit representation of local variable "div",
6963 * if there is any.
6965 __isl_give isl_basic_map *isl_basic_map_mark_div_unknown(
6966 __isl_take isl_basic_map *bmap, int div)
6968 isl_bool known;
6970 known = isl_basic_map_div_is_known(bmap, div);
6971 if (known < 0)
6972 return isl_basic_map_free(bmap);
6973 if (!known)
6974 return bmap;
6976 bmap = isl_basic_map_cow(bmap);
6977 if (!bmap)
6978 return NULL;
6979 isl_int_set_si(bmap->div[div][0], 0);
6980 return bmap;
6983 /* Does local variable "div" of "bmap" have an explicit representation?
6985 isl_bool isl_basic_map_div_is_known(__isl_keep isl_basic_map *bmap, int div)
6987 if (!bmap)
6988 return isl_bool_error;
6989 if (div < 0 || div >= isl_basic_map_dim(bmap, isl_dim_div))
6990 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
6991 "position out of bounds", return isl_bool_error);
6992 return !isl_int_is_zero(bmap->div[div][0]);
6995 /* Return the position of the first local variable that does not
6996 * have an explicit representation.
6997 * Return the total number of local variables if they all have
6998 * an explicit representation.
6999 * Return -1 on error.
7001 int isl_basic_map_first_unknown_div(__isl_keep isl_basic_map *bmap)
7003 int i;
7005 if (!bmap)
7006 return -1;
7008 for (i = 0; i < bmap->n_div; ++i) {
7009 if (!isl_basic_map_div_is_known(bmap, i))
7010 return i;
7012 return bmap->n_div;
7015 /* Does "bmap" have an explicit representation for all local variables?
7017 isl_bool isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
7019 int first, n;
7021 n = isl_basic_map_dim(bmap, isl_dim_div);
7022 first = isl_basic_map_first_unknown_div(bmap);
7023 if (first < 0)
7024 return isl_bool_error;
7025 return first == n;
7028 /* Do all basic maps in "map" have an explicit representation
7029 * for all local variables?
7031 isl_bool isl_map_divs_known(__isl_keep isl_map *map)
7033 int i;
7035 if (!map)
7036 return isl_bool_error;
7038 for (i = 0; i < map->n; ++i) {
7039 int known = isl_basic_map_divs_known(map->p[i]);
7040 if (known <= 0)
7041 return known;
7044 return isl_bool_true;
7047 /* If bmap contains any unknown divs, then compute explicit
7048 * expressions for them. However, this computation may be
7049 * quite expensive, so first try to remove divs that aren't
7050 * strictly needed.
7052 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
7054 int known;
7055 struct isl_map *map;
7057 known = isl_basic_map_divs_known(bmap);
7058 if (known < 0)
7059 goto error;
7060 if (known)
7061 return isl_map_from_basic_map(bmap);
7063 bmap = isl_basic_map_drop_redundant_divs(bmap);
7065 known = isl_basic_map_divs_known(bmap);
7066 if (known < 0)
7067 goto error;
7068 if (known)
7069 return isl_map_from_basic_map(bmap);
7071 map = compute_divs(bmap);
7072 return map;
7073 error:
7074 isl_basic_map_free(bmap);
7075 return NULL;
7078 struct isl_map *isl_map_compute_divs(struct isl_map *map)
7080 int i;
7081 int known;
7082 struct isl_map *res;
7084 if (!map)
7085 return NULL;
7086 if (map->n == 0)
7087 return map;
7089 known = isl_map_divs_known(map);
7090 if (known < 0) {
7091 isl_map_free(map);
7092 return NULL;
7094 if (known)
7095 return map;
7097 res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
7098 for (i = 1 ; i < map->n; ++i) {
7099 struct isl_map *r2;
7100 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
7101 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
7102 res = isl_map_union_disjoint(res, r2);
7103 else
7104 res = isl_map_union(res, r2);
7106 isl_map_free(map);
7108 return res;
7111 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
7113 return (struct isl_set *)
7114 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
7117 struct isl_set *isl_set_compute_divs(struct isl_set *set)
7119 return (struct isl_set *)
7120 isl_map_compute_divs((struct isl_map *)set);
7123 struct isl_set *isl_map_domain(struct isl_map *map)
7125 int i;
7126 struct isl_set *set;
7128 if (!map)
7129 goto error;
7131 map = isl_map_cow(map);
7132 if (!map)
7133 return NULL;
7135 set = (struct isl_set *)map;
7136 set->dim = isl_space_domain(set->dim);
7137 if (!set->dim)
7138 goto error;
7139 for (i = 0; i < map->n; ++i) {
7140 set->p[i] = isl_basic_map_domain(map->p[i]);
7141 if (!set->p[i])
7142 goto error;
7144 ISL_F_CLR(set, ISL_MAP_DISJOINT);
7145 ISL_F_CLR(set, ISL_SET_NORMALIZED);
7146 return set;
7147 error:
7148 isl_map_free(map);
7149 return NULL;
7152 /* Return the union of "map1" and "map2", where we assume for now that
7153 * "map1" and "map2" are disjoint. Note that the basic maps inside
7154 * "map1" or "map2" may not be disjoint from each other.
7155 * Also note that this function is also called from isl_map_union,
7156 * which takes care of handling the situation where "map1" and "map2"
7157 * may not be disjoint.
7159 * If one of the inputs is empty, we can simply return the other input.
7160 * Similarly, if one of the inputs is universal, then it is equal to the union.
7162 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
7163 __isl_take isl_map *map2)
7165 int i;
7166 unsigned flags = 0;
7167 struct isl_map *map = NULL;
7168 int is_universe;
7170 if (!map1 || !map2)
7171 goto error;
7173 if (!isl_space_is_equal(map1->dim, map2->dim))
7174 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
7175 "spaces don't match", goto error);
7177 if (map1->n == 0) {
7178 isl_map_free(map1);
7179 return map2;
7181 if (map2->n == 0) {
7182 isl_map_free(map2);
7183 return map1;
7186 is_universe = isl_map_plain_is_universe(map1);
7187 if (is_universe < 0)
7188 goto error;
7189 if (is_universe) {
7190 isl_map_free(map2);
7191 return map1;
7194 is_universe = isl_map_plain_is_universe(map2);
7195 if (is_universe < 0)
7196 goto error;
7197 if (is_universe) {
7198 isl_map_free(map1);
7199 return map2;
7202 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7203 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7204 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7206 map = isl_map_alloc_space(isl_space_copy(map1->dim),
7207 map1->n + map2->n, flags);
7208 if (!map)
7209 goto error;
7210 for (i = 0; i < map1->n; ++i) {
7211 map = isl_map_add_basic_map(map,
7212 isl_basic_map_copy(map1->p[i]));
7213 if (!map)
7214 goto error;
7216 for (i = 0; i < map2->n; ++i) {
7217 map = isl_map_add_basic_map(map,
7218 isl_basic_map_copy(map2->p[i]));
7219 if (!map)
7220 goto error;
7222 isl_map_free(map1);
7223 isl_map_free(map2);
7224 return map;
7225 error:
7226 isl_map_free(map);
7227 isl_map_free(map1);
7228 isl_map_free(map2);
7229 return NULL;
7232 /* Return the union of "map1" and "map2", where "map1" and "map2" are
7233 * guaranteed to be disjoint by the caller.
7235 * Note that this functions is called from within isl_map_make_disjoint,
7236 * so we have to be careful not to touch the constraints of the inputs
7237 * in any way.
7239 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7240 __isl_take isl_map *map2)
7242 return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7245 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7246 * not be disjoint. The parameters are assumed to have been aligned.
7248 * We currently simply call map_union_disjoint, the internal operation
7249 * of which does not really depend on the inputs being disjoint.
7250 * If the result contains more than one basic map, then we clear
7251 * the disjoint flag since the result may contain basic maps from
7252 * both inputs and these are not guaranteed to be disjoint.
7254 * As a special case, if "map1" and "map2" are obviously equal,
7255 * then we simply return "map1".
7257 static __isl_give isl_map *map_union_aligned(__isl_take isl_map *map1,
7258 __isl_take isl_map *map2)
7260 int equal;
7262 if (!map1 || !map2)
7263 goto error;
7265 equal = isl_map_plain_is_equal(map1, map2);
7266 if (equal < 0)
7267 goto error;
7268 if (equal) {
7269 isl_map_free(map2);
7270 return map1;
7273 map1 = map_union_disjoint(map1, map2);
7274 if (!map1)
7275 return NULL;
7276 if (map1->n > 1)
7277 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7278 return map1;
7279 error:
7280 isl_map_free(map1);
7281 isl_map_free(map2);
7282 return NULL;
7285 /* Return the union of "map1" and "map2", where "map1" and "map2" may
7286 * not be disjoint.
7288 __isl_give isl_map *isl_map_union(__isl_take isl_map *map1,
7289 __isl_take isl_map *map2)
7291 return isl_map_align_params_map_map_and(map1, map2, &map_union_aligned);
7294 struct isl_set *isl_set_union_disjoint(
7295 struct isl_set *set1, struct isl_set *set2)
7297 return (struct isl_set *)
7298 isl_map_union_disjoint(
7299 (struct isl_map *)set1, (struct isl_map *)set2);
7302 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7304 return (struct isl_set *)
7305 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7308 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7309 * the results.
7311 * "map" and "set" are assumed to be compatible and non-NULL.
7313 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7314 __isl_take isl_set *set,
7315 __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7316 __isl_take isl_basic_set *bset))
7318 unsigned flags = 0;
7319 struct isl_map *result;
7320 int i, j;
7322 if (isl_set_plain_is_universe(set)) {
7323 isl_set_free(set);
7324 return map;
7327 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7328 ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7329 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7331 result = isl_map_alloc_space(isl_space_copy(map->dim),
7332 map->n * set->n, flags);
7333 for (i = 0; result && i < map->n; ++i)
7334 for (j = 0; j < set->n; ++j) {
7335 result = isl_map_add_basic_map(result,
7336 fn(isl_basic_map_copy(map->p[i]),
7337 isl_basic_set_copy(set->p[j])));
7338 if (!result)
7339 break;
7342 isl_map_free(map);
7343 isl_set_free(set);
7344 return result;
7347 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7348 __isl_take isl_set *set)
7350 if (!map || !set)
7351 goto error;
7353 if (!isl_map_compatible_range(map, set))
7354 isl_die(set->ctx, isl_error_invalid,
7355 "incompatible spaces", goto error);
7357 return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7358 error:
7359 isl_map_free(map);
7360 isl_set_free(set);
7361 return NULL;
7364 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7365 __isl_take isl_set *set)
7367 return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7370 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
7371 __isl_take isl_set *set)
7373 if (!map || !set)
7374 goto error;
7376 if (!isl_map_compatible_domain(map, set))
7377 isl_die(set->ctx, isl_error_invalid,
7378 "incompatible spaces", goto error);
7380 return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
7381 error:
7382 isl_map_free(map);
7383 isl_set_free(set);
7384 return NULL;
7387 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
7388 __isl_take isl_set *set)
7390 return isl_map_align_params_map_map_and(map, set,
7391 &map_intersect_domain);
7394 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7395 __isl_take isl_map *map2)
7397 if (!map1 || !map2)
7398 goto error;
7399 map1 = isl_map_reverse(map1);
7400 map1 = isl_map_apply_range(map1, map2);
7401 return isl_map_reverse(map1);
7402 error:
7403 isl_map_free(map1);
7404 isl_map_free(map2);
7405 return NULL;
7408 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7409 __isl_take isl_map *map2)
7411 return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7414 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7415 __isl_take isl_map *map2)
7417 isl_space *dim_result;
7418 struct isl_map *result;
7419 int i, j;
7421 if (!map1 || !map2)
7422 goto error;
7424 dim_result = isl_space_join(isl_space_copy(map1->dim),
7425 isl_space_copy(map2->dim));
7427 result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7428 if (!result)
7429 goto error;
7430 for (i = 0; i < map1->n; ++i)
7431 for (j = 0; j < map2->n; ++j) {
7432 result = isl_map_add_basic_map(result,
7433 isl_basic_map_apply_range(
7434 isl_basic_map_copy(map1->p[i]),
7435 isl_basic_map_copy(map2->p[j])));
7436 if (!result)
7437 goto error;
7439 isl_map_free(map1);
7440 isl_map_free(map2);
7441 if (result && result->n <= 1)
7442 ISL_F_SET(result, ISL_MAP_DISJOINT);
7443 return result;
7444 error:
7445 isl_map_free(map1);
7446 isl_map_free(map2);
7447 return NULL;
7450 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7451 __isl_take isl_map *map2)
7453 return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7457 * returns range - domain
7459 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7461 isl_space *target_space;
7462 struct isl_basic_set *bset;
7463 unsigned dim;
7464 unsigned nparam;
7465 int i;
7467 if (!bmap)
7468 goto error;
7469 isl_assert(bmap->ctx, isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7470 bmap->dim, isl_dim_out),
7471 goto error);
7472 target_space = isl_space_domain(isl_basic_map_get_space(bmap));
7473 dim = isl_basic_map_n_in(bmap);
7474 nparam = isl_basic_map_n_param(bmap);
7475 bmap = isl_basic_map_from_range(isl_basic_map_wrap(bmap));
7476 bmap = isl_basic_map_add_dims(bmap, isl_dim_in, dim);
7477 bmap = isl_basic_map_extend_constraints(bmap, dim, 0);
7478 for (i = 0; i < dim; ++i) {
7479 int j = isl_basic_map_alloc_equality(bmap);
7480 if (j < 0) {
7481 bmap = isl_basic_map_free(bmap);
7482 break;
7484 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7485 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7486 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], 1);
7487 isl_int_set_si(bmap->eq[j][1+nparam+2*dim+i], -1);
7489 bset = isl_basic_map_domain(bmap);
7490 bset = isl_basic_set_reset_space(bset, target_space);
7491 return bset;
7492 error:
7493 isl_basic_map_free(bmap);
7494 return NULL;
7498 * returns range - domain
7500 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7502 int i;
7503 isl_space *dim;
7504 struct isl_set *result;
7506 if (!map)
7507 return NULL;
7509 isl_assert(map->ctx, isl_space_tuple_is_equal(map->dim, isl_dim_in,
7510 map->dim, isl_dim_out),
7511 goto error);
7512 dim = isl_map_get_space(map);
7513 dim = isl_space_domain(dim);
7514 result = isl_set_alloc_space(dim, map->n, 0);
7515 if (!result)
7516 goto error;
7517 for (i = 0; i < map->n; ++i)
7518 result = isl_set_add_basic_set(result,
7519 isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7520 isl_map_free(map);
7521 return result;
7522 error:
7523 isl_map_free(map);
7524 return NULL;
7528 * returns [domain -> range] -> range - domain
7530 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7531 __isl_take isl_basic_map *bmap)
7533 int i, k;
7534 isl_space *dim;
7535 isl_basic_map *domain;
7536 int nparam, n;
7537 unsigned total;
7539 if (!isl_space_tuple_is_equal(bmap->dim, isl_dim_in,
7540 bmap->dim, isl_dim_out))
7541 isl_die(bmap->ctx, isl_error_invalid,
7542 "domain and range don't match", goto error);
7544 nparam = isl_basic_map_dim(bmap, isl_dim_param);
7545 n = isl_basic_map_dim(bmap, isl_dim_in);
7547 dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7548 domain = isl_basic_map_universe(dim);
7550 bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7551 bmap = isl_basic_map_apply_range(bmap, domain);
7552 bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7554 total = isl_basic_map_total_dim(bmap);
7556 for (i = 0; i < n; ++i) {
7557 k = isl_basic_map_alloc_equality(bmap);
7558 if (k < 0)
7559 goto error;
7560 isl_seq_clr(bmap->eq[k], 1 + total);
7561 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7562 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7563 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7566 bmap = isl_basic_map_gauss(bmap, NULL);
7567 return isl_basic_map_finalize(bmap);
7568 error:
7569 isl_basic_map_free(bmap);
7570 return NULL;
7574 * returns [domain -> range] -> range - domain
7576 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7578 int i;
7579 isl_space *domain_dim;
7581 if (!map)
7582 return NULL;
7584 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
7585 map->dim, isl_dim_out))
7586 isl_die(map->ctx, isl_error_invalid,
7587 "domain and range don't match", goto error);
7589 map = isl_map_cow(map);
7590 if (!map)
7591 return NULL;
7593 domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7594 map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7595 map->dim = isl_space_join(map->dim, domain_dim);
7596 if (!map->dim)
7597 goto error;
7598 for (i = 0; i < map->n; ++i) {
7599 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7600 if (!map->p[i])
7601 goto error;
7603 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7604 return map;
7605 error:
7606 isl_map_free(map);
7607 return NULL;
7610 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7612 struct isl_basic_map *bmap;
7613 unsigned nparam;
7614 unsigned dim;
7615 int i;
7617 if (!dims)
7618 return NULL;
7620 nparam = dims->nparam;
7621 dim = dims->n_out;
7622 bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7623 if (!bmap)
7624 goto error;
7626 for (i = 0; i < dim; ++i) {
7627 int j = isl_basic_map_alloc_equality(bmap);
7628 if (j < 0)
7629 goto error;
7630 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7631 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7632 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7634 return isl_basic_map_finalize(bmap);
7635 error:
7636 isl_basic_map_free(bmap);
7637 return NULL;
7640 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7642 if (!dim)
7643 return NULL;
7644 if (dim->n_in != dim->n_out)
7645 isl_die(dim->ctx, isl_error_invalid,
7646 "number of input and output dimensions needs to be "
7647 "the same", goto error);
7648 return basic_map_identity(dim);
7649 error:
7650 isl_space_free(dim);
7651 return NULL;
7654 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7656 return isl_map_from_basic_map(isl_basic_map_identity(dim));
7659 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7661 isl_space *dim = isl_set_get_space(set);
7662 isl_map *id;
7663 id = isl_map_identity(isl_space_map_from_set(dim));
7664 return isl_map_intersect_range(id, set);
7667 /* Construct a basic set with all set dimensions having only non-negative
7668 * values.
7670 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7671 __isl_take isl_space *space)
7673 int i;
7674 unsigned nparam;
7675 unsigned dim;
7676 struct isl_basic_set *bset;
7678 if (!space)
7679 return NULL;
7680 nparam = space->nparam;
7681 dim = space->n_out;
7682 bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7683 if (!bset)
7684 return NULL;
7685 for (i = 0; i < dim; ++i) {
7686 int k = isl_basic_set_alloc_inequality(bset);
7687 if (k < 0)
7688 goto error;
7689 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7690 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7692 return bset;
7693 error:
7694 isl_basic_set_free(bset);
7695 return NULL;
7698 /* Construct the half-space x_pos >= 0.
7700 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7701 int pos)
7703 int k;
7704 isl_basic_set *nonneg;
7706 nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7707 k = isl_basic_set_alloc_inequality(nonneg);
7708 if (k < 0)
7709 goto error;
7710 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7711 isl_int_set_si(nonneg->ineq[k][pos], 1);
7713 return isl_basic_set_finalize(nonneg);
7714 error:
7715 isl_basic_set_free(nonneg);
7716 return NULL;
7719 /* Construct the half-space x_pos <= -1.
7721 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7723 int k;
7724 isl_basic_set *neg;
7726 neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7727 k = isl_basic_set_alloc_inequality(neg);
7728 if (k < 0)
7729 goto error;
7730 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7731 isl_int_set_si(neg->ineq[k][0], -1);
7732 isl_int_set_si(neg->ineq[k][pos], -1);
7734 return isl_basic_set_finalize(neg);
7735 error:
7736 isl_basic_set_free(neg);
7737 return NULL;
7740 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7741 enum isl_dim_type type, unsigned first, unsigned n)
7743 int i;
7744 unsigned offset;
7745 isl_basic_set *nonneg;
7746 isl_basic_set *neg;
7748 if (!set)
7749 return NULL;
7750 if (n == 0)
7751 return set;
7753 isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7755 offset = pos(set->dim, type);
7756 for (i = 0; i < n; ++i) {
7757 nonneg = nonneg_halfspace(isl_set_get_space(set),
7758 offset + first + i);
7759 neg = neg_halfspace(isl_set_get_space(set), offset + first + i);
7761 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7764 return set;
7765 error:
7766 isl_set_free(set);
7767 return NULL;
7770 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7771 int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7772 void *user)
7774 isl_set *half;
7776 if (!set)
7777 return -1;
7778 if (isl_set_plain_is_empty(set)) {
7779 isl_set_free(set);
7780 return 0;
7782 if (first == len)
7783 return fn(set, signs, user);
7785 signs[first] = 1;
7786 half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7787 1 + first));
7788 half = isl_set_intersect(half, isl_set_copy(set));
7789 if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7790 goto error;
7792 signs[first] = -1;
7793 half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7794 1 + first));
7795 half = isl_set_intersect(half, set);
7796 return foreach_orthant(half, signs, first + 1, len, fn, user);
7797 error:
7798 isl_set_free(set);
7799 return -1;
7802 /* Call "fn" on the intersections of "set" with each of the orthants
7803 * (except for obviously empty intersections). The orthant is identified
7804 * by the signs array, with each entry having value 1 or -1 according
7805 * to the sign of the corresponding variable.
7807 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7808 int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7809 void *user)
7811 unsigned nparam;
7812 unsigned nvar;
7813 int *signs;
7814 int r;
7816 if (!set)
7817 return -1;
7818 if (isl_set_plain_is_empty(set))
7819 return 0;
7821 nparam = isl_set_dim(set, isl_dim_param);
7822 nvar = isl_set_dim(set, isl_dim_set);
7824 signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7826 r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7827 fn, user);
7829 free(signs);
7831 return r;
7834 isl_bool isl_set_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7836 return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7839 isl_bool isl_basic_map_is_subset(__isl_keep isl_basic_map *bmap1,
7840 __isl_keep isl_basic_map *bmap2)
7842 int is_subset;
7843 struct isl_map *map1;
7844 struct isl_map *map2;
7846 if (!bmap1 || !bmap2)
7847 return isl_bool_error;
7849 map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7850 map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7852 is_subset = isl_map_is_subset(map1, map2);
7854 isl_map_free(map1);
7855 isl_map_free(map2);
7857 return is_subset;
7860 isl_bool isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7861 __isl_keep isl_basic_set *bset2)
7863 return isl_basic_map_is_subset(bset1, bset2);
7866 isl_bool isl_basic_map_is_equal(__isl_keep isl_basic_map *bmap1,
7867 __isl_keep isl_basic_map *bmap2)
7869 isl_bool is_subset;
7871 if (!bmap1 || !bmap2)
7872 return isl_bool_error;
7873 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7874 if (is_subset != isl_bool_true)
7875 return is_subset;
7876 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7877 return is_subset;
7880 isl_bool isl_basic_set_is_equal(__isl_keep isl_basic_set *bset1,
7881 __isl_keep isl_basic_set *bset2)
7883 return isl_basic_map_is_equal(
7884 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7887 isl_bool isl_map_is_empty(__isl_keep isl_map *map)
7889 int i;
7890 int is_empty;
7892 if (!map)
7893 return isl_bool_error;
7894 for (i = 0; i < map->n; ++i) {
7895 is_empty = isl_basic_map_is_empty(map->p[i]);
7896 if (is_empty < 0)
7897 return isl_bool_error;
7898 if (!is_empty)
7899 return isl_bool_false;
7901 return isl_bool_true;
7904 isl_bool isl_map_plain_is_empty(__isl_keep isl_map *map)
7906 return map ? map->n == 0 : isl_bool_error;
7909 isl_bool isl_set_plain_is_empty(__isl_keep isl_set *set)
7911 return set ? set->n == 0 : isl_bool_error;
7914 isl_bool isl_set_is_empty(__isl_keep isl_set *set)
7916 return isl_map_is_empty((struct isl_map *)set);
7919 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7921 if (!map1 || !map2)
7922 return -1;
7924 return isl_space_is_equal(map1->dim, map2->dim);
7927 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7929 if (!set1 || !set2)
7930 return -1;
7932 return isl_space_is_equal(set1->dim, set2->dim);
7935 static isl_bool map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7937 isl_bool is_subset;
7939 if (!map1 || !map2)
7940 return isl_bool_error;
7941 is_subset = isl_map_is_subset(map1, map2);
7942 if (is_subset != isl_bool_true)
7943 return is_subset;
7944 is_subset = isl_map_is_subset(map2, map1);
7945 return is_subset;
7948 isl_bool isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7950 return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7953 isl_bool isl_basic_map_is_strict_subset(
7954 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7956 isl_bool is_subset;
7958 if (!bmap1 || !bmap2)
7959 return isl_bool_error;
7960 is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7961 if (is_subset != isl_bool_true)
7962 return is_subset;
7963 is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7964 if (is_subset == isl_bool_error)
7965 return is_subset;
7966 return !is_subset;
7969 isl_bool isl_map_is_strict_subset(__isl_keep isl_map *map1,
7970 __isl_keep isl_map *map2)
7972 isl_bool is_subset;
7974 if (!map1 || !map2)
7975 return isl_bool_error;
7976 is_subset = isl_map_is_subset(map1, map2);
7977 if (is_subset != isl_bool_true)
7978 return is_subset;
7979 is_subset = isl_map_is_subset(map2, map1);
7980 if (is_subset == isl_bool_error)
7981 return is_subset;
7982 return !is_subset;
7985 isl_bool isl_set_is_strict_subset(__isl_keep isl_set *set1,
7986 __isl_keep isl_set *set2)
7988 return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7991 /* Is "bmap" obviously equal to the universe with the same space?
7993 * That is, does it not have any constraints?
7995 isl_bool isl_basic_map_plain_is_universe(__isl_keep isl_basic_map *bmap)
7997 if (!bmap)
7998 return isl_bool_error;
7999 return bmap->n_eq == 0 && bmap->n_ineq == 0;
8002 /* Is "bset" obviously equal to the universe with the same space?
8004 isl_bool isl_basic_set_plain_is_universe(__isl_keep isl_basic_set *bset)
8006 return isl_basic_map_plain_is_universe(bset);
8009 /* If "c" does not involve any existentially quantified variables,
8010 * then set *univ to false and abort
8012 static isl_stat involves_divs(__isl_take isl_constraint *c, void *user)
8014 isl_bool *univ = user;
8015 unsigned n;
8017 n = isl_constraint_dim(c, isl_dim_div);
8018 *univ = isl_constraint_involves_dims(c, isl_dim_div, 0, n);
8019 isl_constraint_free(c);
8020 if (*univ < 0 || !*univ)
8021 return isl_stat_error;
8022 return isl_stat_ok;
8025 /* Is "bmap" equal to the universe with the same space?
8027 * First check if it is obviously equal to the universe.
8028 * If not and if there are any constraints not involving
8029 * existentially quantified variables, then it is certainly
8030 * not equal to the universe.
8031 * Otherwise, check if the universe is a subset of "bmap".
8033 isl_bool isl_basic_map_is_universe(__isl_keep isl_basic_map *bmap)
8035 isl_bool univ;
8036 isl_basic_map *test;
8038 univ = isl_basic_map_plain_is_universe(bmap);
8039 if (univ < 0 || univ)
8040 return univ;
8041 if (isl_basic_map_dim(bmap, isl_dim_div) == 0)
8042 return isl_bool_false;
8043 univ = isl_bool_true;
8044 if (isl_basic_map_foreach_constraint(bmap, &involves_divs, &univ) < 0 &&
8045 univ)
8046 return isl_bool_error;
8047 if (univ < 0 || !univ)
8048 return univ;
8049 test = isl_basic_map_universe(isl_basic_map_get_space(bmap));
8050 univ = isl_basic_map_is_subset(test, bmap);
8051 isl_basic_map_free(test);
8052 return univ;
8055 /* Is "bset" equal to the universe with the same space?
8057 isl_bool isl_basic_set_is_universe(__isl_keep isl_basic_set *bset)
8059 return isl_basic_map_is_universe(bset);
8062 isl_bool isl_map_plain_is_universe(__isl_keep isl_map *map)
8064 int i;
8066 if (!map)
8067 return isl_bool_error;
8069 for (i = 0; i < map->n; ++i) {
8070 isl_bool r = isl_basic_map_plain_is_universe(map->p[i]);
8071 if (r < 0 || r)
8072 return r;
8075 return isl_bool_false;
8078 isl_bool isl_set_plain_is_universe(__isl_keep isl_set *set)
8080 return isl_map_plain_is_universe((isl_map *) set);
8083 isl_bool isl_basic_map_is_empty(__isl_keep isl_basic_map *bmap)
8085 struct isl_basic_set *bset = NULL;
8086 struct isl_vec *sample = NULL;
8087 isl_bool empty, non_empty;
8089 if (!bmap)
8090 return isl_bool_error;
8092 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
8093 return isl_bool_true;
8095 if (isl_basic_map_plain_is_universe(bmap))
8096 return isl_bool_false;
8098 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
8099 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
8100 copy = isl_basic_map_remove_redundancies(copy);
8101 empty = isl_basic_map_plain_is_empty(copy);
8102 isl_basic_map_free(copy);
8103 return empty;
8106 non_empty = isl_basic_map_plain_is_non_empty(bmap);
8107 if (non_empty < 0)
8108 return isl_bool_error;
8109 if (non_empty)
8110 return isl_bool_false;
8111 isl_vec_free(bmap->sample);
8112 bmap->sample = NULL;
8113 bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
8114 if (!bset)
8115 return isl_bool_error;
8116 sample = isl_basic_set_sample_vec(bset);
8117 if (!sample)
8118 return isl_bool_error;
8119 empty = sample->size == 0;
8120 isl_vec_free(bmap->sample);
8121 bmap->sample = sample;
8122 if (empty)
8123 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
8125 return empty;
8128 isl_bool isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
8130 if (!bmap)
8131 return isl_bool_error;
8132 return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
8135 isl_bool isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
8137 if (!bset)
8138 return isl_bool_error;
8139 return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
8142 /* Is "bmap" known to be non-empty?
8144 * That is, is the cached sample still valid?
8146 isl_bool isl_basic_map_plain_is_non_empty(__isl_keep isl_basic_map *bmap)
8148 unsigned total;
8150 if (!bmap)
8151 return isl_bool_error;
8152 if (!bmap->sample)
8153 return isl_bool_false;
8154 total = 1 + isl_basic_map_total_dim(bmap);
8155 if (bmap->sample->size != total)
8156 return isl_bool_false;
8157 return isl_basic_map_contains(bmap, bmap->sample);
8160 isl_bool isl_basic_set_is_empty(__isl_keep isl_basic_set *bset)
8162 return isl_basic_map_is_empty((struct isl_basic_map *)bset);
8165 struct isl_map *isl_basic_map_union(
8166 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8168 struct isl_map *map;
8169 if (!bmap1 || !bmap2)
8170 goto error;
8172 isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
8174 map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
8175 if (!map)
8176 goto error;
8177 map = isl_map_add_basic_map(map, bmap1);
8178 map = isl_map_add_basic_map(map, bmap2);
8179 return map;
8180 error:
8181 isl_basic_map_free(bmap1);
8182 isl_basic_map_free(bmap2);
8183 return NULL;
8186 struct isl_set *isl_basic_set_union(
8187 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
8189 return (struct isl_set *)isl_basic_map_union(
8190 (struct isl_basic_map *)bset1,
8191 (struct isl_basic_map *)bset2);
8194 /* Order divs such that any div only depends on previous divs */
8195 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
8197 int i;
8198 unsigned off;
8200 if (!bmap)
8201 return NULL;
8203 off = isl_space_dim(bmap->dim, isl_dim_all);
8205 for (i = 0; i < bmap->n_div; ++i) {
8206 int pos;
8207 if (isl_int_is_zero(bmap->div[i][0]))
8208 continue;
8209 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
8210 bmap->n_div-i);
8211 if (pos == -1)
8212 continue;
8213 if (pos == 0)
8214 isl_die(isl_basic_map_get_ctx(bmap), isl_error_internal,
8215 "integer division depends on itself",
8216 return isl_basic_map_free(bmap));
8217 isl_basic_map_swap_div(bmap, i, i + pos);
8218 --i;
8220 return bmap;
8223 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
8225 return (struct isl_basic_set *)
8226 isl_basic_map_order_divs((struct isl_basic_map *)bset);
8229 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
8231 int i;
8233 if (!map)
8234 return 0;
8236 for (i = 0; i < map->n; ++i) {
8237 map->p[i] = isl_basic_map_order_divs(map->p[i]);
8238 if (!map->p[i])
8239 goto error;
8242 return map;
8243 error:
8244 isl_map_free(map);
8245 return NULL;
8248 /* Apply the expansion computed by isl_merge_divs.
8249 * The expansion itself is given by "exp" while the resulting
8250 * list of divs is given by "div".
8252 * Move the integer divisions of "bset" into the right position
8253 * according to "exp" and then introduce the additional integer
8254 * divisions, adding div constraints.
8255 * The moving should be done first to avoid moving coefficients
8256 * in the definitions of the extra integer divisions.
8258 __isl_give isl_basic_set *isl_basic_set_expand_divs(
8259 __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
8261 int i, j;
8262 int n_div;
8264 bset = isl_basic_set_cow(bset);
8265 if (!bset || !div)
8266 goto error;
8268 if (div->n_row < bset->n_div)
8269 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
8270 "not an expansion", goto error);
8272 n_div = bset->n_div;
8273 bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
8274 div->n_row - n_div, 0,
8275 2 * (div->n_row - n_div));
8277 for (i = n_div; i < div->n_row; ++i)
8278 if (isl_basic_set_alloc_div(bset) < 0)
8279 goto error;
8281 for (j = n_div - 1; j >= 0; --j) {
8282 if (exp[j] == j)
8283 break;
8284 isl_basic_map_swap_div(bset, j, exp[j]);
8286 j = 0;
8287 for (i = 0; i < div->n_row; ++i) {
8288 if (j < n_div && exp[j] == i) {
8289 j++;
8290 } else {
8291 isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
8292 if (isl_basic_map_add_div_constraints(bset, i) < 0)
8293 goto error;
8297 isl_mat_free(div);
8298 return bset;
8299 error:
8300 isl_basic_set_free(bset);
8301 isl_mat_free(div);
8302 return NULL;
8305 /* Look for a div in dst that corresponds to the div "div" in src.
8306 * The divs before "div" in src and dst are assumed to be the same.
8308 * Returns -1 if no corresponding div was found and the position
8309 * of the corresponding div in dst otherwise.
8311 static int find_div(struct isl_basic_map *dst,
8312 struct isl_basic_map *src, unsigned div)
8314 int i;
8316 unsigned total = isl_space_dim(src->dim, isl_dim_all);
8318 isl_assert(dst->ctx, div <= dst->n_div, return -1);
8319 for (i = div; i < dst->n_div; ++i)
8320 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8321 isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8322 dst->n_div - div) == -1)
8323 return i;
8324 return -1;
8327 /* Align the divs of "dst" to those of "src", adding divs from "src"
8328 * if needed. That is, make sure that the first src->n_div divs
8329 * of the result are equal to those of src.
8331 * The result is not finalized as by design it will have redundant
8332 * divs if any divs from "src" were copied.
8334 __isl_give isl_basic_map *isl_basic_map_align_divs(
8335 __isl_take isl_basic_map *dst, __isl_keep isl_basic_map *src)
8337 int i;
8338 int known, extended;
8339 unsigned total;
8341 if (!dst || !src)
8342 return isl_basic_map_free(dst);
8344 if (src->n_div == 0)
8345 return dst;
8347 known = isl_basic_map_divs_known(src);
8348 if (known < 0)
8349 return isl_basic_map_free(dst);
8350 if (!known)
8351 isl_die(isl_basic_map_get_ctx(src), isl_error_invalid,
8352 "some src divs are unknown",
8353 return isl_basic_map_free(dst));
8355 src = isl_basic_map_order_divs(src);
8357 extended = 0;
8358 total = isl_space_dim(src->dim, isl_dim_all);
8359 for (i = 0; i < src->n_div; ++i) {
8360 int j = find_div(dst, src, i);
8361 if (j < 0) {
8362 if (!extended) {
8363 int extra = src->n_div - i;
8364 dst = isl_basic_map_cow(dst);
8365 if (!dst)
8366 return NULL;
8367 dst = isl_basic_map_extend_space(dst,
8368 isl_space_copy(dst->dim),
8369 extra, 0, 2 * extra);
8370 extended = 1;
8372 j = isl_basic_map_alloc_div(dst);
8373 if (j < 0)
8374 return isl_basic_map_free(dst);
8375 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8376 isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8377 if (isl_basic_map_add_div_constraints(dst, j) < 0)
8378 return isl_basic_map_free(dst);
8380 if (j != i)
8381 isl_basic_map_swap_div(dst, i, j);
8383 return dst;
8386 struct isl_basic_set *isl_basic_set_align_divs(
8387 struct isl_basic_set *dst, struct isl_basic_set *src)
8389 return (struct isl_basic_set *)isl_basic_map_align_divs(
8390 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8393 struct isl_map *isl_map_align_divs(struct isl_map *map)
8395 int i;
8397 if (!map)
8398 return NULL;
8399 if (map->n == 0)
8400 return map;
8401 map = isl_map_compute_divs(map);
8402 map = isl_map_cow(map);
8403 if (!map)
8404 return NULL;
8406 for (i = 1; i < map->n; ++i)
8407 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8408 for (i = 1; i < map->n; ++i) {
8409 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8410 if (!map->p[i])
8411 return isl_map_free(map);
8414 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8415 return map;
8418 struct isl_set *isl_set_align_divs(struct isl_set *set)
8420 return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8423 /* Align the divs of the basic maps in "map" to those
8424 * of the basic maps in "list", as well as to the other basic maps in "map".
8425 * The elements in "list" are assumed to have known divs.
8427 __isl_give isl_map *isl_map_align_divs_to_basic_map_list(
8428 __isl_take isl_map *map, __isl_keep isl_basic_map_list *list)
8430 int i, n;
8432 map = isl_map_compute_divs(map);
8433 map = isl_map_cow(map);
8434 if (!map || !list)
8435 return isl_map_free(map);
8436 if (map->n == 0)
8437 return map;
8439 n = isl_basic_map_list_n_basic_map(list);
8440 for (i = 0; i < n; ++i) {
8441 isl_basic_map *bmap;
8443 bmap = isl_basic_map_list_get_basic_map(list, i);
8444 map->p[0] = isl_basic_map_align_divs(map->p[0], bmap);
8445 isl_basic_map_free(bmap);
8447 if (!map->p[0])
8448 return isl_map_free(map);
8450 return isl_map_align_divs(map);
8453 /* Align the divs of each element of "list" to those of "bmap".
8454 * Both "bmap" and the elements of "list" are assumed to have known divs.
8456 __isl_give isl_basic_map_list *isl_basic_map_list_align_divs_to_basic_map(
8457 __isl_take isl_basic_map_list *list, __isl_keep isl_basic_map *bmap)
8459 int i, n;
8461 if (!list || !bmap)
8462 return isl_basic_map_list_free(list);
8464 n = isl_basic_map_list_n_basic_map(list);
8465 for (i = 0; i < n; ++i) {
8466 isl_basic_map *bmap_i;
8468 bmap_i = isl_basic_map_list_get_basic_map(list, i);
8469 bmap_i = isl_basic_map_align_divs(bmap_i, bmap);
8470 list = isl_basic_map_list_set_basic_map(list, i, bmap_i);
8473 return list;
8476 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8477 __isl_take isl_map *map)
8479 if (!set || !map)
8480 goto error;
8481 isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8482 map = isl_map_intersect_domain(map, set);
8483 set = isl_map_range(map);
8484 return set;
8485 error:
8486 isl_set_free(set);
8487 isl_map_free(map);
8488 return NULL;
8491 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8492 __isl_take isl_map *map)
8494 return isl_map_align_params_map_map_and(set, map, &set_apply);
8497 /* There is no need to cow as removing empty parts doesn't change
8498 * the meaning of the set.
8500 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8502 int i;
8504 if (!map)
8505 return NULL;
8507 for (i = map->n - 1; i >= 0; --i)
8508 remove_if_empty(map, i);
8510 return map;
8513 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8515 return (struct isl_set *)
8516 isl_map_remove_empty_parts((struct isl_map *)set);
8519 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8521 struct isl_basic_map *bmap;
8522 if (!map || map->n == 0)
8523 return NULL;
8524 bmap = map->p[map->n-1];
8525 isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8526 return isl_basic_map_copy(bmap);
8529 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8531 return (struct isl_basic_set *)
8532 isl_map_copy_basic_map((struct isl_map *)set);
8535 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8536 __isl_keep isl_basic_map *bmap)
8538 int i;
8540 if (!map || !bmap)
8541 goto error;
8542 for (i = map->n-1; i >= 0; --i) {
8543 if (map->p[i] != bmap)
8544 continue;
8545 map = isl_map_cow(map);
8546 if (!map)
8547 goto error;
8548 isl_basic_map_free(map->p[i]);
8549 if (i != map->n-1) {
8550 ISL_F_CLR(map, ISL_SET_NORMALIZED);
8551 map->p[i] = map->p[map->n-1];
8553 map->n--;
8554 return map;
8556 return map;
8557 error:
8558 isl_map_free(map);
8559 return NULL;
8562 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8563 struct isl_basic_set *bset)
8565 return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8566 (struct isl_basic_map *)bset);
8569 /* Given two basic sets bset1 and bset2, compute the maximal difference
8570 * between the values of dimension pos in bset1 and those in bset2
8571 * for any common value of the parameters and dimensions preceding pos.
8573 static enum isl_lp_result basic_set_maximal_difference_at(
8574 __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8575 int pos, isl_int *opt)
8577 isl_space *dims;
8578 struct isl_basic_map *bmap1 = NULL;
8579 struct isl_basic_map *bmap2 = NULL;
8580 struct isl_ctx *ctx;
8581 struct isl_vec *obj;
8582 unsigned total;
8583 unsigned nparam;
8584 unsigned dim1, dim2;
8585 enum isl_lp_result res;
8587 if (!bset1 || !bset2)
8588 return isl_lp_error;
8590 nparam = isl_basic_set_n_param(bset1);
8591 dim1 = isl_basic_set_n_dim(bset1);
8592 dim2 = isl_basic_set_n_dim(bset2);
8593 dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8594 bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8595 dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8596 bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8597 if (!bmap1 || !bmap2)
8598 goto error;
8599 bmap1 = isl_basic_map_cow(bmap1);
8600 bmap1 = isl_basic_map_extend(bmap1, nparam,
8601 pos, (dim1 - pos) + (dim2 - pos),
8602 bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8603 bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8604 if (!bmap1)
8605 goto error2;
8606 total = isl_basic_map_total_dim(bmap1);
8607 ctx = bmap1->ctx;
8608 obj = isl_vec_alloc(ctx, 1 + total);
8609 if (!obj)
8610 goto error2;
8611 isl_seq_clr(obj->block.data, 1 + total);
8612 isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8613 isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8614 res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8615 opt, NULL, NULL);
8616 isl_basic_map_free(bmap1);
8617 isl_vec_free(obj);
8618 return res;
8619 error:
8620 isl_basic_map_free(bmap2);
8621 error2:
8622 isl_basic_map_free(bmap1);
8623 return isl_lp_error;
8626 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8627 * for any common value of the parameters and dimensions preceding pos
8628 * in both basic sets, the values of dimension pos in bset1 are
8629 * smaller or larger than those in bset2.
8631 * Returns
8632 * 1 if bset1 follows bset2
8633 * -1 if bset1 precedes bset2
8634 * 0 if bset1 and bset2 are incomparable
8635 * -2 if some error occurred.
8637 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8638 struct isl_basic_set *bset2, int pos)
8640 isl_int opt;
8641 enum isl_lp_result res;
8642 int cmp;
8644 isl_int_init(opt);
8646 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8648 if (res == isl_lp_empty)
8649 cmp = 0;
8650 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8651 res == isl_lp_unbounded)
8652 cmp = 1;
8653 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8654 cmp = -1;
8655 else
8656 cmp = -2;
8658 isl_int_clear(opt);
8659 return cmp;
8662 /* Given two basic sets bset1 and bset2, check whether
8663 * for any common value of the parameters and dimensions preceding pos
8664 * there is a value of dimension pos in bset1 that is larger
8665 * than a value of the same dimension in bset2.
8667 * Return
8668 * 1 if there exists such a pair
8669 * 0 if there is no such pair, but there is a pair of equal values
8670 * -1 otherwise
8671 * -2 if some error occurred.
8673 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8674 __isl_keep isl_basic_set *bset2, int pos)
8676 isl_int opt;
8677 enum isl_lp_result res;
8678 int cmp;
8680 isl_int_init(opt);
8682 res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8684 if (res == isl_lp_empty)
8685 cmp = -1;
8686 else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8687 res == isl_lp_unbounded)
8688 cmp = 1;
8689 else if (res == isl_lp_ok && isl_int_is_neg(opt))
8690 cmp = -1;
8691 else if (res == isl_lp_ok)
8692 cmp = 0;
8693 else
8694 cmp = -2;
8696 isl_int_clear(opt);
8697 return cmp;
8700 /* Given two sets set1 and set2, check whether
8701 * for any common value of the parameters and dimensions preceding pos
8702 * there is a value of dimension pos in set1 that is larger
8703 * than a value of the same dimension in set2.
8705 * Return
8706 * 1 if there exists such a pair
8707 * 0 if there is no such pair, but there is a pair of equal values
8708 * -1 otherwise
8709 * -2 if some error occurred.
8711 int isl_set_follows_at(__isl_keep isl_set *set1,
8712 __isl_keep isl_set *set2, int pos)
8714 int i, j;
8715 int follows = -1;
8717 if (!set1 || !set2)
8718 return -2;
8720 for (i = 0; i < set1->n; ++i)
8721 for (j = 0; j < set2->n; ++j) {
8722 int f;
8723 f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8724 if (f == 1 || f == -2)
8725 return f;
8726 if (f > follows)
8727 follows = f;
8730 return follows;
8733 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8734 unsigned pos, isl_int *val)
8736 int i;
8737 int d;
8738 unsigned total;
8740 if (!bmap)
8741 return -1;
8742 total = isl_basic_map_total_dim(bmap);
8743 for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8744 for (; d+1 > pos; --d)
8745 if (!isl_int_is_zero(bmap->eq[i][1+d]))
8746 break;
8747 if (d != pos)
8748 continue;
8749 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8750 return 0;
8751 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8752 return 0;
8753 if (!isl_int_is_one(bmap->eq[i][1+d]))
8754 return 0;
8755 if (val)
8756 isl_int_neg(*val, bmap->eq[i][0]);
8757 return 1;
8759 return 0;
8762 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8763 unsigned pos, isl_int *val)
8765 int i;
8766 isl_int v;
8767 isl_int tmp;
8768 int fixed;
8770 if (!map)
8771 return -1;
8772 if (map->n == 0)
8773 return 0;
8774 if (map->n == 1)
8775 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val);
8776 isl_int_init(v);
8777 isl_int_init(tmp);
8778 fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v);
8779 for (i = 1; fixed == 1 && i < map->n; ++i) {
8780 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp);
8781 if (fixed == 1 && isl_int_ne(tmp, v))
8782 fixed = 0;
8784 if (val)
8785 isl_int_set(*val, v);
8786 isl_int_clear(tmp);
8787 isl_int_clear(v);
8788 return fixed;
8791 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8792 unsigned pos, isl_int *val)
8794 return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8795 pos, val);
8798 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8799 isl_int *val)
8801 return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8804 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8805 enum isl_dim_type type, unsigned pos, isl_int *val)
8807 if (pos >= isl_basic_map_dim(bmap, type))
8808 return -1;
8809 return isl_basic_map_plain_has_fixed_var(bmap,
8810 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8813 /* If "bmap" obviously lies on a hyperplane where the given dimension
8814 * has a fixed value, then return that value.
8815 * Otherwise return NaN.
8817 __isl_give isl_val *isl_basic_map_plain_get_val_if_fixed(
8818 __isl_keep isl_basic_map *bmap,
8819 enum isl_dim_type type, unsigned pos)
8821 isl_ctx *ctx;
8822 isl_val *v;
8823 int fixed;
8825 if (!bmap)
8826 return NULL;
8827 ctx = isl_basic_map_get_ctx(bmap);
8828 v = isl_val_alloc(ctx);
8829 if (!v)
8830 return NULL;
8831 fixed = isl_basic_map_plain_is_fixed(bmap, type, pos, &v->n);
8832 if (fixed < 0)
8833 return isl_val_free(v);
8834 if (fixed) {
8835 isl_int_set_si(v->d, 1);
8836 return v;
8838 isl_val_free(v);
8839 return isl_val_nan(ctx);
8842 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8843 enum isl_dim_type type, unsigned pos, isl_int *val)
8845 if (pos >= isl_map_dim(map, type))
8846 return -1;
8847 return isl_map_plain_has_fixed_var(map,
8848 map_offset(map, type) - 1 + pos, val);
8851 /* If "map" obviously lies on a hyperplane where the given dimension
8852 * has a fixed value, then return that value.
8853 * Otherwise return NaN.
8855 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8856 enum isl_dim_type type, unsigned pos)
8858 isl_ctx *ctx;
8859 isl_val *v;
8860 int fixed;
8862 if (!map)
8863 return NULL;
8864 ctx = isl_map_get_ctx(map);
8865 v = isl_val_alloc(ctx);
8866 if (!v)
8867 return NULL;
8868 fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8869 if (fixed < 0)
8870 return isl_val_free(v);
8871 if (fixed) {
8872 isl_int_set_si(v->d, 1);
8873 return v;
8875 isl_val_free(v);
8876 return isl_val_nan(ctx);
8879 /* If "set" obviously lies on a hyperplane where the given dimension
8880 * has a fixed value, then return that value.
8881 * Otherwise return NaN.
8883 __isl_give isl_val *isl_set_plain_get_val_if_fixed(__isl_keep isl_set *set,
8884 enum isl_dim_type type, unsigned pos)
8886 return isl_map_plain_get_val_if_fixed(set, type, pos);
8889 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8890 enum isl_dim_type type, unsigned pos, isl_int *val)
8892 return isl_map_plain_is_fixed(set, type, pos, val);
8895 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8896 * then return this fixed value in *val.
8898 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8899 unsigned dim, isl_int *val)
8901 return isl_basic_set_plain_has_fixed_var(bset,
8902 isl_basic_set_n_param(bset) + dim, val);
8905 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8906 * then return this fixed value in *val.
8908 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8909 unsigned dim, isl_int *val)
8911 return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8914 /* Check if input variable in has fixed value and if so and if val is not NULL,
8915 * then return this fixed value in *val.
8917 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8918 unsigned in, isl_int *val)
8920 return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8923 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8924 * and if val is not NULL, then return this lower bound in *val.
8926 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8927 __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8929 int i, i_eq = -1, i_ineq = -1;
8930 isl_int *c;
8931 unsigned total;
8932 unsigned nparam;
8934 if (!bset)
8935 return -1;
8936 total = isl_basic_set_total_dim(bset);
8937 nparam = isl_basic_set_n_param(bset);
8938 for (i = 0; i < bset->n_eq; ++i) {
8939 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8940 continue;
8941 if (i_eq != -1)
8942 return 0;
8943 i_eq = i;
8945 for (i = 0; i < bset->n_ineq; ++i) {
8946 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8947 continue;
8948 if (i_eq != -1 || i_ineq != -1)
8949 return 0;
8950 i_ineq = i;
8952 if (i_eq == -1 && i_ineq == -1)
8953 return 0;
8954 c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8955 /* The coefficient should always be one due to normalization. */
8956 if (!isl_int_is_one(c[1+nparam+dim]))
8957 return 0;
8958 if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8959 return 0;
8960 if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8961 total - nparam - dim - 1) != -1)
8962 return 0;
8963 if (val)
8964 isl_int_neg(*val, c[0]);
8965 return 1;
8968 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8969 unsigned dim, isl_int *val)
8971 int i;
8972 isl_int v;
8973 isl_int tmp;
8974 int fixed;
8976 if (!set)
8977 return -1;
8978 if (set->n == 0)
8979 return 0;
8980 if (set->n == 1)
8981 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8982 dim, val);
8983 isl_int_init(v);
8984 isl_int_init(tmp);
8985 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8986 dim, &v);
8987 for (i = 1; fixed == 1 && i < set->n; ++i) {
8988 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8989 dim, &tmp);
8990 if (fixed == 1 && isl_int_ne(tmp, v))
8991 fixed = 0;
8993 if (val)
8994 isl_int_set(*val, v);
8995 isl_int_clear(tmp);
8996 isl_int_clear(v);
8997 return fixed;
9000 /* Return -1 if the constraint "c1" should be sorted before "c2"
9001 * and 1 if it should be sorted after "c2".
9002 * Return 0 if the two constraints are the same (up to the constant term).
9004 * In particular, if a constraint involves later variables than another
9005 * then it is sorted after this other constraint.
9006 * uset_gist depends on constraints without existentially quantified
9007 * variables sorting first.
9009 * For constraints that have the same latest variable, those
9010 * with the same coefficient for this latest variable (first in absolute value
9011 * and then in actual value) are grouped together.
9012 * This is useful for detecting pairs of constraints that can
9013 * be chained in their printed representation.
9015 * Finally, within a group, constraints are sorted according to
9016 * their coefficients (excluding the constant term).
9018 static int sort_constraint_cmp(const void *p1, const void *p2, void *arg)
9020 isl_int **c1 = (isl_int **) p1;
9021 isl_int **c2 = (isl_int **) p2;
9022 int l1, l2;
9023 unsigned size = *(unsigned *) arg;
9024 int cmp;
9026 l1 = isl_seq_last_non_zero(*c1 + 1, size);
9027 l2 = isl_seq_last_non_zero(*c2 + 1, size);
9029 if (l1 != l2)
9030 return l1 - l2;
9032 cmp = isl_int_abs_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9033 if (cmp != 0)
9034 return cmp;
9035 cmp = isl_int_cmp((*c1)[1 + l1], (*c2)[1 + l1]);
9036 if (cmp != 0)
9037 return -cmp;
9039 return isl_seq_cmp(*c1 + 1, *c2 + 1, size);
9042 /* Return -1 if the constraint "c1" of "bmap" is sorted before "c2"
9043 * by isl_basic_map_sort_constraints, 1 if it is sorted after "c2"
9044 * and 0 if the two constraints are the same (up to the constant term).
9046 int isl_basic_map_constraint_cmp(__isl_keep isl_basic_map *bmap,
9047 isl_int *c1, isl_int *c2)
9049 unsigned total;
9051 if (!bmap)
9052 return -2;
9053 total = isl_basic_map_total_dim(bmap);
9054 return sort_constraint_cmp(&c1, &c2, &total);
9057 __isl_give isl_basic_map *isl_basic_map_sort_constraints(
9058 __isl_take isl_basic_map *bmap)
9060 unsigned total;
9062 if (!bmap)
9063 return NULL;
9064 if (bmap->n_ineq == 0)
9065 return bmap;
9066 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9067 return bmap;
9068 total = isl_basic_map_total_dim(bmap);
9069 if (isl_sort(bmap->ineq, bmap->n_ineq, sizeof(isl_int *),
9070 &sort_constraint_cmp, &total) < 0)
9071 return isl_basic_map_free(bmap);
9072 return bmap;
9075 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
9076 __isl_take isl_basic_set *bset)
9078 return (struct isl_basic_set *)isl_basic_map_sort_constraints(
9079 (struct isl_basic_map *)bset);
9082 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
9084 if (!bmap)
9085 return NULL;
9086 if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
9087 return bmap;
9088 bmap = isl_basic_map_remove_redundancies(bmap);
9089 bmap = isl_basic_map_sort_constraints(bmap);
9090 if (bmap)
9091 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
9092 return bmap;
9095 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
9097 return (struct isl_basic_set *)isl_basic_map_normalize(
9098 (struct isl_basic_map *)bset);
9101 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
9102 const __isl_keep isl_basic_map *bmap2)
9104 int i, cmp;
9105 unsigned total;
9107 if (!bmap1 || !bmap2)
9108 return -1;
9110 if (bmap1 == bmap2)
9111 return 0;
9112 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
9113 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
9114 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
9115 if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
9116 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
9117 if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
9118 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9119 if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
9120 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
9121 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
9122 ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9123 return 0;
9124 if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
9125 return 1;
9126 if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
9127 return -1;
9128 if (bmap1->n_eq != bmap2->n_eq)
9129 return bmap1->n_eq - bmap2->n_eq;
9130 if (bmap1->n_ineq != bmap2->n_ineq)
9131 return bmap1->n_ineq - bmap2->n_ineq;
9132 if (bmap1->n_div != bmap2->n_div)
9133 return bmap1->n_div - bmap2->n_div;
9134 total = isl_basic_map_total_dim(bmap1);
9135 for (i = 0; i < bmap1->n_eq; ++i) {
9136 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
9137 if (cmp)
9138 return cmp;
9140 for (i = 0; i < bmap1->n_ineq; ++i) {
9141 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
9142 if (cmp)
9143 return cmp;
9145 for (i = 0; i < bmap1->n_div; ++i) {
9146 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
9147 if (cmp)
9148 return cmp;
9150 return 0;
9153 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
9154 const __isl_keep isl_basic_set *bset2)
9156 return isl_basic_map_plain_cmp(bset1, bset2);
9159 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
9161 int i, cmp;
9163 if (set1 == set2)
9164 return 0;
9165 if (set1->n != set2->n)
9166 return set1->n - set2->n;
9168 for (i = 0; i < set1->n; ++i) {
9169 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
9170 if (cmp)
9171 return cmp;
9174 return 0;
9177 isl_bool isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
9178 __isl_keep isl_basic_map *bmap2)
9180 if (!bmap1 || !bmap2)
9181 return isl_bool_error;
9182 return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
9185 isl_bool isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
9186 __isl_keep isl_basic_set *bset2)
9188 return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
9189 (isl_basic_map *)bset2);
9192 static int qsort_bmap_cmp(const void *p1, const void *p2)
9194 const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
9195 const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
9197 return isl_basic_map_plain_cmp(bmap1, bmap2);
9200 /* Sort the basic maps of "map" and remove duplicate basic maps.
9202 * While removing basic maps, we make sure that the basic maps remain
9203 * sorted because isl_map_normalize expects the basic maps of the result
9204 * to be sorted.
9206 static __isl_give isl_map *sort_and_remove_duplicates(__isl_take isl_map *map)
9208 int i, j;
9210 map = isl_map_remove_empty_parts(map);
9211 if (!map)
9212 return NULL;
9213 qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
9214 for (i = map->n - 1; i >= 1; --i) {
9215 if (!isl_basic_map_plain_is_equal(map->p[i - 1], map->p[i]))
9216 continue;
9217 isl_basic_map_free(map->p[i-1]);
9218 for (j = i; j < map->n; ++j)
9219 map->p[j - 1] = map->p[j];
9220 map->n--;
9223 return map;
9226 /* Remove obvious duplicates among the basic maps of "map".
9228 * Unlike isl_map_normalize, this function does not remove redundant
9229 * constraints and only removes duplicates that have exactly the same
9230 * constraints in the input. It does sort the constraints and
9231 * the basic maps to ease the detection of duplicates.
9233 * If "map" has already been normalized or if the basic maps are
9234 * disjoint, then there can be no duplicates.
9236 __isl_give isl_map *isl_map_remove_obvious_duplicates(__isl_take isl_map *map)
9238 int i;
9239 isl_basic_map *bmap;
9241 if (!map)
9242 return NULL;
9243 if (map->n <= 1)
9244 return map;
9245 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED | ISL_MAP_DISJOINT))
9246 return map;
9247 for (i = 0; i < map->n; ++i) {
9248 bmap = isl_basic_map_copy(map->p[i]);
9249 bmap = isl_basic_map_sort_constraints(bmap);
9250 if (!bmap)
9251 return isl_map_free(map);
9252 isl_basic_map_free(map->p[i]);
9253 map->p[i] = bmap;
9256 map = sort_and_remove_duplicates(map);
9257 return map;
9260 /* We normalize in place, but if anything goes wrong we need
9261 * to return NULL, so we need to make sure we don't change the
9262 * meaning of any possible other copies of map.
9264 __isl_give isl_map *isl_map_normalize(__isl_take isl_map *map)
9266 int i;
9267 struct isl_basic_map *bmap;
9269 if (!map)
9270 return NULL;
9271 if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
9272 return map;
9273 for (i = 0; i < map->n; ++i) {
9274 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
9275 if (!bmap)
9276 goto error;
9277 isl_basic_map_free(map->p[i]);
9278 map->p[i] = bmap;
9281 map = sort_and_remove_duplicates(map);
9282 if (map)
9283 ISL_F_SET(map, ISL_MAP_NORMALIZED);
9284 return map;
9285 error:
9286 isl_map_free(map);
9287 return NULL;
9290 struct isl_set *isl_set_normalize(struct isl_set *set)
9292 return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
9295 isl_bool isl_map_plain_is_equal(__isl_keep isl_map *map1,
9296 __isl_keep isl_map *map2)
9298 int i;
9299 isl_bool equal;
9301 if (!map1 || !map2)
9302 return isl_bool_error;
9304 if (map1 == map2)
9305 return isl_bool_true;
9306 if (!isl_space_is_equal(map1->dim, map2->dim))
9307 return isl_bool_false;
9309 map1 = isl_map_copy(map1);
9310 map2 = isl_map_copy(map2);
9311 map1 = isl_map_normalize(map1);
9312 map2 = isl_map_normalize(map2);
9313 if (!map1 || !map2)
9314 goto error;
9315 equal = map1->n == map2->n;
9316 for (i = 0; equal && i < map1->n; ++i) {
9317 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
9318 if (equal < 0)
9319 goto error;
9321 isl_map_free(map1);
9322 isl_map_free(map2);
9323 return equal;
9324 error:
9325 isl_map_free(map1);
9326 isl_map_free(map2);
9327 return isl_bool_error;
9330 isl_bool isl_set_plain_is_equal(__isl_keep isl_set *set1,
9331 __isl_keep isl_set *set2)
9333 return isl_map_plain_is_equal((struct isl_map *)set1,
9334 (struct isl_map *)set2);
9337 /* Return an interval that ranges from min to max (inclusive)
9339 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
9340 isl_int min, isl_int max)
9342 int k;
9343 struct isl_basic_set *bset = NULL;
9345 bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
9346 if (!bset)
9347 goto error;
9349 k = isl_basic_set_alloc_inequality(bset);
9350 if (k < 0)
9351 goto error;
9352 isl_int_set_si(bset->ineq[k][1], 1);
9353 isl_int_neg(bset->ineq[k][0], min);
9355 k = isl_basic_set_alloc_inequality(bset);
9356 if (k < 0)
9357 goto error;
9358 isl_int_set_si(bset->ineq[k][1], -1);
9359 isl_int_set(bset->ineq[k][0], max);
9361 return bset;
9362 error:
9363 isl_basic_set_free(bset);
9364 return NULL;
9367 /* Return the basic maps in "map" as a list.
9369 __isl_give isl_basic_map_list *isl_map_get_basic_map_list(
9370 __isl_keep isl_map *map)
9372 int i;
9373 isl_ctx *ctx;
9374 isl_basic_map_list *list;
9376 if (!map)
9377 return NULL;
9378 ctx = isl_map_get_ctx(map);
9379 list = isl_basic_map_list_alloc(ctx, map->n);
9381 for (i = 0; i < map->n; ++i) {
9382 isl_basic_map *bmap;
9384 bmap = isl_basic_map_copy(map->p[i]);
9385 list = isl_basic_map_list_add(list, bmap);
9388 return list;
9391 /* Return the intersection of the elements in the non-empty list "list".
9392 * All elements are assumed to live in the same space.
9394 __isl_give isl_basic_map *isl_basic_map_list_intersect(
9395 __isl_take isl_basic_map_list *list)
9397 int i, n;
9398 isl_basic_map *bmap;
9400 if (!list)
9401 return NULL;
9402 n = isl_basic_map_list_n_basic_map(list);
9403 if (n < 1)
9404 isl_die(isl_basic_map_list_get_ctx(list), isl_error_invalid,
9405 "expecting non-empty list", goto error);
9407 bmap = isl_basic_map_list_get_basic_map(list, 0);
9408 for (i = 1; i < n; ++i) {
9409 isl_basic_map *bmap_i;
9411 bmap_i = isl_basic_map_list_get_basic_map(list, i);
9412 bmap = isl_basic_map_intersect(bmap, bmap_i);
9415 isl_basic_map_list_free(list);
9416 return bmap;
9417 error:
9418 isl_basic_map_list_free(list);
9419 return NULL;
9422 /* Return the intersection of the elements in the non-empty list "list".
9423 * All elements are assumed to live in the same space.
9425 __isl_give isl_basic_set *isl_basic_set_list_intersect(
9426 __isl_take isl_basic_set_list *list)
9428 return isl_basic_map_list_intersect(list);
9431 /* Return the union of the elements in the non-empty list "list".
9432 * All elements are assumed to live in the same space.
9434 __isl_give isl_set *isl_set_list_union(__isl_take isl_set_list *list)
9436 int i, n;
9437 isl_set *set;
9439 if (!list)
9440 return NULL;
9441 n = isl_set_list_n_set(list);
9442 if (n < 1)
9443 isl_die(isl_set_list_get_ctx(list), isl_error_invalid,
9444 "expecting non-empty list", goto error);
9446 set = isl_set_list_get_set(list, 0);
9447 for (i = 1; i < n; ++i) {
9448 isl_set *set_i;
9450 set_i = isl_set_list_get_set(list, i);
9451 set = isl_set_union(set, set_i);
9454 isl_set_list_free(list);
9455 return set;
9456 error:
9457 isl_set_list_free(list);
9458 return NULL;
9461 /* Return the Cartesian product of the basic sets in list (in the given order).
9463 __isl_give isl_basic_set *isl_basic_set_list_product(
9464 __isl_take struct isl_basic_set_list *list)
9466 int i;
9467 unsigned dim;
9468 unsigned nparam;
9469 unsigned extra;
9470 unsigned n_eq;
9471 unsigned n_ineq;
9472 struct isl_basic_set *product = NULL;
9474 if (!list)
9475 goto error;
9476 isl_assert(list->ctx, list->n > 0, goto error);
9477 isl_assert(list->ctx, list->p[0], goto error);
9478 nparam = isl_basic_set_n_param(list->p[0]);
9479 dim = isl_basic_set_n_dim(list->p[0]);
9480 extra = list->p[0]->n_div;
9481 n_eq = list->p[0]->n_eq;
9482 n_ineq = list->p[0]->n_ineq;
9483 for (i = 1; i < list->n; ++i) {
9484 isl_assert(list->ctx, list->p[i], goto error);
9485 isl_assert(list->ctx,
9486 nparam == isl_basic_set_n_param(list->p[i]), goto error);
9487 dim += isl_basic_set_n_dim(list->p[i]);
9488 extra += list->p[i]->n_div;
9489 n_eq += list->p[i]->n_eq;
9490 n_ineq += list->p[i]->n_ineq;
9492 product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
9493 n_eq, n_ineq);
9494 if (!product)
9495 goto error;
9496 dim = 0;
9497 for (i = 0; i < list->n; ++i) {
9498 isl_basic_set_add_constraints(product,
9499 isl_basic_set_copy(list->p[i]), dim);
9500 dim += isl_basic_set_n_dim(list->p[i]);
9502 isl_basic_set_list_free(list);
9503 return product;
9504 error:
9505 isl_basic_set_free(product);
9506 isl_basic_set_list_free(list);
9507 return NULL;
9510 struct isl_basic_map *isl_basic_map_product(
9511 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
9513 isl_space *dim_result = NULL;
9514 struct isl_basic_map *bmap;
9515 unsigned in1, in2, out1, out2, nparam, total, pos;
9516 struct isl_dim_map *dim_map1, *dim_map2;
9518 if (!bmap1 || !bmap2)
9519 goto error;
9521 isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
9522 bmap2->dim, isl_dim_param), goto error);
9523 dim_result = isl_space_product(isl_space_copy(bmap1->dim),
9524 isl_space_copy(bmap2->dim));
9526 in1 = isl_basic_map_n_in(bmap1);
9527 in2 = isl_basic_map_n_in(bmap2);
9528 out1 = isl_basic_map_n_out(bmap1);
9529 out2 = isl_basic_map_n_out(bmap2);
9530 nparam = isl_basic_map_n_param(bmap1);
9532 total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
9533 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9534 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9535 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9536 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9537 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9538 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9539 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9540 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9541 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9542 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9544 bmap = isl_basic_map_alloc_space(dim_result,
9545 bmap1->n_div + bmap2->n_div,
9546 bmap1->n_eq + bmap2->n_eq,
9547 bmap1->n_ineq + bmap2->n_ineq);
9548 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9549 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9550 bmap = isl_basic_map_simplify(bmap);
9551 return isl_basic_map_finalize(bmap);
9552 error:
9553 isl_basic_map_free(bmap1);
9554 isl_basic_map_free(bmap2);
9555 return NULL;
9558 __isl_give isl_basic_map *isl_basic_map_flat_product(
9559 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9561 isl_basic_map *prod;
9563 prod = isl_basic_map_product(bmap1, bmap2);
9564 prod = isl_basic_map_flatten(prod);
9565 return prod;
9568 __isl_give isl_basic_set *isl_basic_set_flat_product(
9569 __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9571 return isl_basic_map_flat_range_product(bset1, bset2);
9574 __isl_give isl_basic_map *isl_basic_map_domain_product(
9575 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9577 isl_space *space_result = NULL;
9578 isl_basic_map *bmap;
9579 unsigned in1, in2, out, nparam, total, pos;
9580 struct isl_dim_map *dim_map1, *dim_map2;
9582 if (!bmap1 || !bmap2)
9583 goto error;
9585 space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9586 isl_space_copy(bmap2->dim));
9588 in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9589 in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9590 out = isl_basic_map_dim(bmap1, isl_dim_out);
9591 nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9593 total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9594 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9595 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9596 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9597 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9598 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9599 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9600 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9601 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9602 isl_dim_map_div(dim_map1, bmap1, pos += out);
9603 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9605 bmap = isl_basic_map_alloc_space(space_result,
9606 bmap1->n_div + bmap2->n_div,
9607 bmap1->n_eq + bmap2->n_eq,
9608 bmap1->n_ineq + bmap2->n_ineq);
9609 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9610 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9611 bmap = isl_basic_map_simplify(bmap);
9612 return isl_basic_map_finalize(bmap);
9613 error:
9614 isl_basic_map_free(bmap1);
9615 isl_basic_map_free(bmap2);
9616 return NULL;
9619 __isl_give isl_basic_map *isl_basic_map_range_product(
9620 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9622 isl_space *dim_result = NULL;
9623 isl_basic_map *bmap;
9624 unsigned in, out1, out2, nparam, total, pos;
9625 struct isl_dim_map *dim_map1, *dim_map2;
9627 if (!bmap1 || !bmap2)
9628 goto error;
9630 if (!isl_space_match(bmap1->dim, isl_dim_param,
9631 bmap2->dim, isl_dim_param))
9632 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9633 "parameters don't match", goto error);
9635 dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9636 isl_space_copy(bmap2->dim));
9638 in = isl_basic_map_dim(bmap1, isl_dim_in);
9639 out1 = isl_basic_map_n_out(bmap1);
9640 out2 = isl_basic_map_n_out(bmap2);
9641 nparam = isl_basic_map_n_param(bmap1);
9643 total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9644 dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9645 dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9646 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9647 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9648 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9649 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9650 isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9651 isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9652 isl_dim_map_div(dim_map1, bmap1, pos += out2);
9653 isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9655 bmap = isl_basic_map_alloc_space(dim_result,
9656 bmap1->n_div + bmap2->n_div,
9657 bmap1->n_eq + bmap2->n_eq,
9658 bmap1->n_ineq + bmap2->n_ineq);
9659 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9660 bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9661 bmap = isl_basic_map_simplify(bmap);
9662 return isl_basic_map_finalize(bmap);
9663 error:
9664 isl_basic_map_free(bmap1);
9665 isl_basic_map_free(bmap2);
9666 return NULL;
9669 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9670 __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9672 isl_basic_map *prod;
9674 prod = isl_basic_map_range_product(bmap1, bmap2);
9675 prod = isl_basic_map_flatten_range(prod);
9676 return prod;
9679 /* Apply "basic_map_product" to each pair of basic maps in "map1" and "map2"
9680 * and collect the results.
9681 * The result live in the space obtained by calling "space_product"
9682 * on the spaces of "map1" and "map2".
9683 * If "remove_duplicates" is set then the result may contain duplicates
9684 * (even if the inputs do not) and so we try and remove the obvious
9685 * duplicates.
9687 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9688 __isl_take isl_map *map2,
9689 __isl_give isl_space *(*space_product)(__isl_take isl_space *left,
9690 __isl_take isl_space *right),
9691 __isl_give isl_basic_map *(*basic_map_product)(
9692 __isl_take isl_basic_map *left,
9693 __isl_take isl_basic_map *right),
9694 int remove_duplicates)
9696 unsigned flags = 0;
9697 struct isl_map *result;
9698 int i, j;
9700 if (!map1 || !map2)
9701 goto error;
9703 isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9704 map2->dim, isl_dim_param), goto error);
9706 if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9707 ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9708 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9710 result = isl_map_alloc_space(space_product(isl_space_copy(map1->dim),
9711 isl_space_copy(map2->dim)),
9712 map1->n * map2->n, flags);
9713 if (!result)
9714 goto error;
9715 for (i = 0; i < map1->n; ++i)
9716 for (j = 0; j < map2->n; ++j) {
9717 struct isl_basic_map *part;
9718 part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9719 isl_basic_map_copy(map2->p[j]));
9720 if (isl_basic_map_is_empty(part))
9721 isl_basic_map_free(part);
9722 else
9723 result = isl_map_add_basic_map(result, part);
9724 if (!result)
9725 goto error;
9727 if (remove_duplicates)
9728 result = isl_map_remove_obvious_duplicates(result);
9729 isl_map_free(map1);
9730 isl_map_free(map2);
9731 return result;
9732 error:
9733 isl_map_free(map1);
9734 isl_map_free(map2);
9735 return NULL;
9738 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9740 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9741 __isl_take isl_map *map2)
9743 return map_product(map1, map2, &isl_space_product,
9744 &isl_basic_map_product, 0);
9747 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9748 __isl_take isl_map *map2)
9750 return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9753 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9755 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9756 __isl_take isl_map *map2)
9758 isl_map *prod;
9760 prod = isl_map_product(map1, map2);
9761 prod = isl_map_flatten(prod);
9762 return prod;
9765 /* Given two set A and B, construct its Cartesian product A x B.
9767 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9769 return isl_map_range_product(set1, set2);
9772 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9773 __isl_take isl_set *set2)
9775 return isl_map_flat_range_product(set1, set2);
9778 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9780 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9781 __isl_take isl_map *map2)
9783 return map_product(map1, map2, &isl_space_domain_product,
9784 &isl_basic_map_domain_product, 1);
9787 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9789 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9790 __isl_take isl_map *map2)
9792 return map_product(map1, map2, &isl_space_range_product,
9793 &isl_basic_map_range_product, 1);
9796 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9797 __isl_take isl_map *map2)
9799 return isl_map_align_params_map_map_and(map1, map2,
9800 &map_domain_product_aligned);
9803 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9804 __isl_take isl_map *map2)
9806 return isl_map_align_params_map_map_and(map1, map2,
9807 &map_range_product_aligned);
9810 /* Given a map of the form [A -> B] -> [C -> D], return the map A -> C.
9812 __isl_give isl_map *isl_map_factor_domain(__isl_take isl_map *map)
9814 isl_space *space;
9815 int total1, keep1, total2, keep2;
9817 if (!map)
9818 return NULL;
9819 if (!isl_space_domain_is_wrapping(map->dim) ||
9820 !isl_space_range_is_wrapping(map->dim))
9821 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9822 "not a product", return isl_map_free(map));
9824 space = isl_map_get_space(map);
9825 total1 = isl_space_dim(space, isl_dim_in);
9826 total2 = isl_space_dim(space, isl_dim_out);
9827 space = isl_space_factor_domain(space);
9828 keep1 = isl_space_dim(space, isl_dim_in);
9829 keep2 = isl_space_dim(space, isl_dim_out);
9830 map = isl_map_project_out(map, isl_dim_in, keep1, total1 - keep1);
9831 map = isl_map_project_out(map, isl_dim_out, keep2, total2 - keep2);
9832 map = isl_map_reset_space(map, space);
9834 return map;
9837 /* Given a map of the form [A -> B] -> [C -> D], return the map B -> D.
9839 __isl_give isl_map *isl_map_factor_range(__isl_take isl_map *map)
9841 isl_space *space;
9842 int total1, keep1, total2, keep2;
9844 if (!map)
9845 return NULL;
9846 if (!isl_space_domain_is_wrapping(map->dim) ||
9847 !isl_space_range_is_wrapping(map->dim))
9848 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9849 "not a product", return isl_map_free(map));
9851 space = isl_map_get_space(map);
9852 total1 = isl_space_dim(space, isl_dim_in);
9853 total2 = isl_space_dim(space, isl_dim_out);
9854 space = isl_space_factor_range(space);
9855 keep1 = isl_space_dim(space, isl_dim_in);
9856 keep2 = isl_space_dim(space, isl_dim_out);
9857 map = isl_map_project_out(map, isl_dim_in, 0, total1 - keep1);
9858 map = isl_map_project_out(map, isl_dim_out, 0, total2 - keep2);
9859 map = isl_map_reset_space(map, space);
9861 return map;
9864 /* Given a map of the form [A -> B] -> C, return the map A -> C.
9866 __isl_give isl_map *isl_map_domain_factor_domain(__isl_take isl_map *map)
9868 isl_space *space;
9869 int total, keep;
9871 if (!map)
9872 return NULL;
9873 if (!isl_space_domain_is_wrapping(map->dim))
9874 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9875 "domain is not a product", return isl_map_free(map));
9877 space = isl_map_get_space(map);
9878 total = isl_space_dim(space, isl_dim_in);
9879 space = isl_space_domain_factor_domain(space);
9880 keep = isl_space_dim(space, isl_dim_in);
9881 map = isl_map_project_out(map, isl_dim_in, keep, total - keep);
9882 map = isl_map_reset_space(map, space);
9884 return map;
9887 /* Given a map of the form [A -> B] -> C, return the map B -> C.
9889 __isl_give isl_map *isl_map_domain_factor_range(__isl_take isl_map *map)
9891 isl_space *space;
9892 int total, keep;
9894 if (!map)
9895 return NULL;
9896 if (!isl_space_domain_is_wrapping(map->dim))
9897 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9898 "domain is not a product", return isl_map_free(map));
9900 space = isl_map_get_space(map);
9901 total = isl_space_dim(space, isl_dim_in);
9902 space = isl_space_domain_factor_range(space);
9903 keep = isl_space_dim(space, isl_dim_in);
9904 map = isl_map_project_out(map, isl_dim_in, 0, total - keep);
9905 map = isl_map_reset_space(map, space);
9907 return map;
9910 /* Given a map A -> [B -> C], extract the map A -> B.
9912 __isl_give isl_map *isl_map_range_factor_domain(__isl_take isl_map *map)
9914 isl_space *space;
9915 int total, keep;
9917 if (!map)
9918 return NULL;
9919 if (!isl_space_range_is_wrapping(map->dim))
9920 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9921 "range is not a product", return isl_map_free(map));
9923 space = isl_map_get_space(map);
9924 total = isl_space_dim(space, isl_dim_out);
9925 space = isl_space_range_factor_domain(space);
9926 keep = isl_space_dim(space, isl_dim_out);
9927 map = isl_map_project_out(map, isl_dim_out, keep, total - keep);
9928 map = isl_map_reset_space(map, space);
9930 return map;
9933 /* Given a map A -> [B -> C], extract the map A -> C.
9935 __isl_give isl_map *isl_map_range_factor_range(__isl_take isl_map *map)
9937 isl_space *space;
9938 int total, keep;
9940 if (!map)
9941 return NULL;
9942 if (!isl_space_range_is_wrapping(map->dim))
9943 isl_die(isl_map_get_ctx(map), isl_error_invalid,
9944 "range is not a product", return isl_map_free(map));
9946 space = isl_map_get_space(map);
9947 total = isl_space_dim(space, isl_dim_out);
9948 space = isl_space_range_factor_range(space);
9949 keep = isl_space_dim(space, isl_dim_out);
9950 map = isl_map_project_out(map, isl_dim_out, 0, total - keep);
9951 map = isl_map_reset_space(map, space);
9953 return map;
9956 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9958 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9959 __isl_take isl_map *map2)
9961 isl_map *prod;
9963 prod = isl_map_domain_product(map1, map2);
9964 prod = isl_map_flatten_domain(prod);
9965 return prod;
9968 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9970 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9971 __isl_take isl_map *map2)
9973 isl_map *prod;
9975 prod = isl_map_range_product(map1, map2);
9976 prod = isl_map_flatten_range(prod);
9977 return prod;
9980 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9982 int i;
9983 uint32_t hash = isl_hash_init();
9984 unsigned total;
9986 if (!bmap)
9987 return 0;
9988 bmap = isl_basic_map_copy(bmap);
9989 bmap = isl_basic_map_normalize(bmap);
9990 if (!bmap)
9991 return 0;
9992 total = isl_basic_map_total_dim(bmap);
9993 isl_hash_byte(hash, bmap->n_eq & 0xFF);
9994 for (i = 0; i < bmap->n_eq; ++i) {
9995 uint32_t c_hash;
9996 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9997 isl_hash_hash(hash, c_hash);
9999 isl_hash_byte(hash, bmap->n_ineq & 0xFF);
10000 for (i = 0; i < bmap->n_ineq; ++i) {
10001 uint32_t c_hash;
10002 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
10003 isl_hash_hash(hash, c_hash);
10005 isl_hash_byte(hash, bmap->n_div & 0xFF);
10006 for (i = 0; i < bmap->n_div; ++i) {
10007 uint32_t c_hash;
10008 if (isl_int_is_zero(bmap->div[i][0]))
10009 continue;
10010 isl_hash_byte(hash, i & 0xFF);
10011 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
10012 isl_hash_hash(hash, c_hash);
10014 isl_basic_map_free(bmap);
10015 return hash;
10018 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
10020 return isl_basic_map_get_hash((isl_basic_map *)bset);
10023 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
10025 int i;
10026 uint32_t hash;
10028 if (!map)
10029 return 0;
10030 map = isl_map_copy(map);
10031 map = isl_map_normalize(map);
10032 if (!map)
10033 return 0;
10035 hash = isl_hash_init();
10036 for (i = 0; i < map->n; ++i) {
10037 uint32_t bmap_hash;
10038 bmap_hash = isl_basic_map_get_hash(map->p[i]);
10039 isl_hash_hash(hash, bmap_hash);
10042 isl_map_free(map);
10044 return hash;
10047 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
10049 return isl_map_get_hash((isl_map *)set);
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.
10056 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
10058 int i;
10059 unsigned nparam;
10061 if (!bset)
10062 return -1;
10063 nparam = isl_basic_set_n_param(bset);
10064 for (i = 0; i < bset->n_eq; ++i)
10065 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
10066 return 1;
10067 return 0;
10070 /* Check if the value for dimension dim is completely determined
10071 * by the values of the other parameters and variables.
10072 * That is, check if dimension dim is involved in an equality
10073 * for each of the subsets.
10075 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
10077 int i;
10079 if (!set)
10080 return -1;
10081 for (i = 0; i < set->n; ++i) {
10082 int unique;
10083 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
10084 if (unique != 1)
10085 return unique;
10087 return 1;
10090 /* Return the number of basic maps in the (current) representation of "map".
10092 int isl_map_n_basic_map(__isl_keep isl_map *map)
10094 return map ? map->n : 0;
10097 int isl_set_n_basic_set(__isl_keep isl_set *set)
10099 return set ? set->n : 0;
10102 isl_stat isl_map_foreach_basic_map(__isl_keep isl_map *map,
10103 isl_stat (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
10105 int i;
10107 if (!map)
10108 return isl_stat_error;
10110 for (i = 0; i < map->n; ++i)
10111 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
10112 return isl_stat_error;
10114 return isl_stat_ok;
10117 isl_stat isl_set_foreach_basic_set(__isl_keep isl_set *set,
10118 isl_stat (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
10120 int i;
10122 if (!set)
10123 return isl_stat_error;
10125 for (i = 0; i < set->n; ++i)
10126 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
10127 return isl_stat_error;
10129 return isl_stat_ok;
10132 /* Return a list of basic sets, the union of which is equal to "set".
10134 __isl_give isl_basic_set_list *isl_set_get_basic_set_list(
10135 __isl_keep isl_set *set)
10137 int i;
10138 isl_basic_set_list *list;
10140 if (!set)
10141 return NULL;
10143 list = isl_basic_set_list_alloc(isl_set_get_ctx(set), set->n);
10144 for (i = 0; i < set->n; ++i) {
10145 isl_basic_set *bset;
10147 bset = isl_basic_set_copy(set->p[i]);
10148 list = isl_basic_set_list_add(list, bset);
10151 return list;
10154 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
10156 isl_space *dim;
10158 if (!bset)
10159 return NULL;
10161 bset = isl_basic_set_cow(bset);
10162 if (!bset)
10163 return NULL;
10165 dim = isl_basic_set_get_space(bset);
10166 dim = isl_space_lift(dim, bset->n_div);
10167 if (!dim)
10168 goto error;
10169 isl_space_free(bset->dim);
10170 bset->dim = dim;
10171 bset->extra -= bset->n_div;
10172 bset->n_div = 0;
10174 bset = isl_basic_set_finalize(bset);
10176 return bset;
10177 error:
10178 isl_basic_set_free(bset);
10179 return NULL;
10182 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
10184 int i;
10185 isl_space *dim;
10186 unsigned n_div;
10188 set = isl_set_align_divs(set);
10190 if (!set)
10191 return NULL;
10193 set = isl_set_cow(set);
10194 if (!set)
10195 return NULL;
10197 n_div = set->p[0]->n_div;
10198 dim = isl_set_get_space(set);
10199 dim = isl_space_lift(dim, n_div);
10200 if (!dim)
10201 goto error;
10202 isl_space_free(set->dim);
10203 set->dim = dim;
10205 for (i = 0; i < set->n; ++i) {
10206 set->p[i] = isl_basic_set_lift(set->p[i]);
10207 if (!set->p[i])
10208 goto error;
10211 return set;
10212 error:
10213 isl_set_free(set);
10214 return NULL;
10217 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
10219 isl_space *dim;
10220 struct isl_basic_map *bmap;
10221 unsigned n_set;
10222 unsigned n_div;
10223 unsigned n_param;
10224 unsigned total;
10225 int i, k, l;
10227 set = isl_set_align_divs(set);
10229 if (!set)
10230 return NULL;
10232 dim = isl_set_get_space(set);
10233 if (set->n == 0 || set->p[0]->n_div == 0) {
10234 isl_set_free(set);
10235 return isl_map_identity(isl_space_map_from_set(dim));
10238 n_div = set->p[0]->n_div;
10239 dim = isl_space_map_from_set(dim);
10240 n_param = isl_space_dim(dim, isl_dim_param);
10241 n_set = isl_space_dim(dim, isl_dim_in);
10242 dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
10243 bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
10244 for (i = 0; i < n_set; ++i)
10245 bmap = var_equal(bmap, i);
10247 total = n_param + n_set + n_set + n_div;
10248 for (i = 0; i < n_div; ++i) {
10249 k = isl_basic_map_alloc_inequality(bmap);
10250 if (k < 0)
10251 goto error;
10252 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
10253 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
10254 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
10255 set->p[0]->div[i]+1+1+n_param, n_set + n_div);
10256 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
10257 set->p[0]->div[i][0]);
10259 l = isl_basic_map_alloc_inequality(bmap);
10260 if (l < 0)
10261 goto error;
10262 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
10263 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
10264 set->p[0]->div[i][0]);
10265 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
10268 isl_set_free(set);
10269 bmap = isl_basic_map_simplify(bmap);
10270 bmap = isl_basic_map_finalize(bmap);
10271 return isl_map_from_basic_map(bmap);
10272 error:
10273 isl_set_free(set);
10274 isl_basic_map_free(bmap);
10275 return NULL;
10278 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
10280 unsigned dim;
10281 int size = 0;
10283 if (!bset)
10284 return -1;
10286 dim = isl_basic_set_total_dim(bset);
10287 size += bset->n_eq * (1 + dim);
10288 size += bset->n_ineq * (1 + dim);
10289 size += bset->n_div * (2 + dim);
10291 return size;
10294 int isl_set_size(__isl_keep isl_set *set)
10296 int i;
10297 int size = 0;
10299 if (!set)
10300 return -1;
10302 for (i = 0; i < set->n; ++i)
10303 size += isl_basic_set_size(set->p[i]);
10305 return size;
10308 /* Check if there is any lower bound (if lower == 0) and/or upper
10309 * bound (if upper == 0) on the specified dim.
10311 static isl_bool basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10312 enum isl_dim_type type, unsigned pos, int lower, int upper)
10314 int i;
10316 if (!bmap)
10317 return isl_bool_error;
10319 isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type),
10320 return isl_bool_error);
10322 pos += isl_basic_map_offset(bmap, type);
10324 for (i = 0; i < bmap->n_div; ++i) {
10325 if (isl_int_is_zero(bmap->div[i][0]))
10326 continue;
10327 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
10328 return isl_bool_true;
10331 for (i = 0; i < bmap->n_eq; ++i)
10332 if (!isl_int_is_zero(bmap->eq[i][pos]))
10333 return isl_bool_true;
10335 for (i = 0; i < bmap->n_ineq; ++i) {
10336 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
10337 if (sgn > 0)
10338 lower = 1;
10339 if (sgn < 0)
10340 upper = 1;
10343 return lower && upper;
10346 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
10347 enum isl_dim_type type, unsigned pos)
10349 return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
10352 isl_bool isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
10353 enum isl_dim_type type, unsigned pos)
10355 return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
10358 isl_bool isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
10359 enum isl_dim_type type, unsigned pos)
10361 return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
10364 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
10365 enum isl_dim_type type, unsigned pos)
10367 int i;
10369 if (!map)
10370 return -1;
10372 for (i = 0; i < map->n; ++i) {
10373 int bounded;
10374 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
10375 if (bounded < 0 || !bounded)
10376 return bounded;
10379 return 1;
10382 /* Return 1 if the specified dim is involved in both an upper bound
10383 * and a lower bound.
10385 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
10386 enum isl_dim_type type, unsigned pos)
10388 return isl_map_dim_is_bounded((isl_map *)set, type, pos);
10391 /* Does "map" have a bound (according to "fn") for any of its basic maps?
10393 static isl_bool has_any_bound(__isl_keep isl_map *map,
10394 enum isl_dim_type type, unsigned pos,
10395 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10396 enum isl_dim_type type, unsigned pos))
10398 int i;
10400 if (!map)
10401 return isl_bool_error;
10403 for (i = 0; i < map->n; ++i) {
10404 isl_bool bounded;
10405 bounded = fn(map->p[i], type, pos);
10406 if (bounded < 0 || bounded)
10407 return bounded;
10410 return isl_bool_false;
10413 /* Return 1 if the specified dim is involved in any lower bound.
10415 isl_bool isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
10416 enum isl_dim_type type, unsigned pos)
10418 return has_any_bound(set, type, pos,
10419 &isl_basic_map_dim_has_lower_bound);
10422 /* Return 1 if the specified dim is involved in any upper bound.
10424 isl_bool isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
10425 enum isl_dim_type type, unsigned pos)
10427 return has_any_bound(set, type, pos,
10428 &isl_basic_map_dim_has_upper_bound);
10431 /* Does "map" have a bound (according to "fn") for all of its basic maps?
10433 static isl_bool has_bound(__isl_keep isl_map *map,
10434 enum isl_dim_type type, unsigned pos,
10435 isl_bool (*fn)(__isl_keep isl_basic_map *bmap,
10436 enum isl_dim_type type, unsigned pos))
10438 int i;
10440 if (!map)
10441 return isl_bool_error;
10443 for (i = 0; i < map->n; ++i) {
10444 isl_bool bounded;
10445 bounded = fn(map->p[i], type, pos);
10446 if (bounded < 0 || !bounded)
10447 return bounded;
10450 return isl_bool_true;
10453 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
10455 isl_bool isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
10456 enum isl_dim_type type, unsigned pos)
10458 return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
10461 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
10463 isl_bool isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
10464 enum isl_dim_type type, unsigned pos)
10466 return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
10469 /* For each of the "n" variables starting at "first", determine
10470 * the sign of the variable and put the results in the first "n"
10471 * elements of the array "signs".
10472 * Sign
10473 * 1 means that the variable is non-negative
10474 * -1 means that the variable is non-positive
10475 * 0 means the variable attains both positive and negative values.
10477 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
10478 unsigned first, unsigned n, int *signs)
10480 isl_vec *bound = NULL;
10481 struct isl_tab *tab = NULL;
10482 struct isl_tab_undo *snap;
10483 int i;
10485 if (!bset || !signs)
10486 return -1;
10488 bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
10489 tab = isl_tab_from_basic_set(bset, 0);
10490 if (!bound || !tab)
10491 goto error;
10493 isl_seq_clr(bound->el, bound->size);
10494 isl_int_set_si(bound->el[0], -1);
10496 snap = isl_tab_snap(tab);
10497 for (i = 0; i < n; ++i) {
10498 int empty;
10500 isl_int_set_si(bound->el[1 + first + i], -1);
10501 if (isl_tab_add_ineq(tab, bound->el) < 0)
10502 goto error;
10503 empty = tab->empty;
10504 isl_int_set_si(bound->el[1 + first + i], 0);
10505 if (isl_tab_rollback(tab, snap) < 0)
10506 goto error;
10508 if (empty) {
10509 signs[i] = 1;
10510 continue;
10513 isl_int_set_si(bound->el[1 + first + i], 1);
10514 if (isl_tab_add_ineq(tab, bound->el) < 0)
10515 goto error;
10516 empty = tab->empty;
10517 isl_int_set_si(bound->el[1 + first + i], 0);
10518 if (isl_tab_rollback(tab, snap) < 0)
10519 goto error;
10521 signs[i] = empty ? -1 : 0;
10524 isl_tab_free(tab);
10525 isl_vec_free(bound);
10526 return 0;
10527 error:
10528 isl_tab_free(tab);
10529 isl_vec_free(bound);
10530 return -1;
10533 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
10534 enum isl_dim_type type, unsigned first, unsigned n, int *signs)
10536 if (!bset || !signs)
10537 return -1;
10538 isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
10539 return -1);
10541 first += pos(bset->dim, type) - 1;
10542 return isl_basic_set_vars_get_sign(bset, first, n, signs);
10545 /* Is it possible for the integer division "div" to depend (possibly
10546 * indirectly) on any output dimensions?
10548 * If the div is undefined, then we conservatively assume that it
10549 * may depend on them.
10550 * Otherwise, we check if it actually depends on them or on any integer
10551 * divisions that may depend on them.
10553 static int div_may_involve_output(__isl_keep isl_basic_map *bmap, int div)
10555 int i;
10556 unsigned n_out, o_out;
10557 unsigned n_div, o_div;
10559 if (isl_int_is_zero(bmap->div[div][0]))
10560 return 1;
10562 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10563 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10565 if (isl_seq_first_non_zero(bmap->div[div] + 1 + o_out, n_out) != -1)
10566 return 1;
10568 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10569 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10571 for (i = 0; i < n_div; ++i) {
10572 if (isl_int_is_zero(bmap->div[div][1 + o_div + i]))
10573 continue;
10574 if (div_may_involve_output(bmap, i))
10575 return 1;
10578 return 0;
10581 /* Return the first integer division of "bmap" in the range
10582 * [first, first + n[ that may depend on any output dimensions and
10583 * that has a non-zero coefficient in "c" (where the first coefficient
10584 * in "c" corresponds to integer division "first").
10586 static int first_div_may_involve_output(__isl_keep isl_basic_map *bmap,
10587 isl_int *c, int first, int n)
10589 int k;
10591 if (!bmap)
10592 return -1;
10594 for (k = first; k < first + n; ++k) {
10595 if (isl_int_is_zero(c[k]))
10596 continue;
10597 if (div_may_involve_output(bmap, k))
10598 return k;
10601 return first + n;
10604 /* Look for a pair of inequality constraints in "bmap" of the form
10606 * -l + i >= 0 or i >= l
10607 * and
10608 * n + l - i >= 0 or i <= l + n
10610 * with n < "m" and i the output dimension at position "pos".
10611 * (Note that n >= 0 as otherwise the two constraints would conflict.)
10612 * Furthermore, "l" is only allowed to involve parameters, input dimensions
10613 * and earlier output dimensions, as well as integer divisions that do
10614 * not involve any of the output dimensions.
10616 * Return the index of the first inequality constraint or bmap->n_ineq
10617 * if no such pair can be found.
10619 static int find_modulo_constraint_pair(__isl_keep isl_basic_map *bmap,
10620 int pos, isl_int m)
10622 int i, j;
10623 isl_ctx *ctx;
10624 unsigned total;
10625 unsigned n_div, o_div;
10626 unsigned n_out, o_out;
10627 int less;
10629 if (!bmap)
10630 return -1;
10632 ctx = isl_basic_map_get_ctx(bmap);
10633 total = isl_basic_map_total_dim(bmap);
10634 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10635 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10636 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10637 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10638 for (i = 0; i < bmap->n_ineq; ++i) {
10639 if (!isl_int_abs_eq(bmap->ineq[i][o_out + pos], ctx->one))
10640 continue;
10641 if (isl_seq_first_non_zero(bmap->ineq[i] + o_out + pos + 1,
10642 n_out - (pos + 1)) != -1)
10643 continue;
10644 if (first_div_may_involve_output(bmap, bmap->ineq[i] + o_div,
10645 0, n_div) < n_div)
10646 continue;
10647 for (j = i + 1; j < bmap->n_ineq; ++j) {
10648 if (!isl_int_abs_eq(bmap->ineq[j][o_out + pos],
10649 ctx->one))
10650 continue;
10651 if (!isl_seq_is_neg(bmap->ineq[i] + 1,
10652 bmap->ineq[j] + 1, total))
10653 continue;
10654 break;
10656 if (j >= bmap->n_ineq)
10657 continue;
10658 isl_int_add(bmap->ineq[i][0],
10659 bmap->ineq[i][0], bmap->ineq[j][0]);
10660 less = isl_int_abs_lt(bmap->ineq[i][0], m);
10661 isl_int_sub(bmap->ineq[i][0],
10662 bmap->ineq[i][0], bmap->ineq[j][0]);
10663 if (!less)
10664 continue;
10665 if (isl_int_is_one(bmap->ineq[i][o_out + pos]))
10666 return i;
10667 else
10668 return j;
10671 return bmap->n_ineq;
10674 /* Return the index of the equality of "bmap" that defines
10675 * the output dimension "pos" in terms of earlier dimensions.
10676 * The equality may also involve integer divisions, as long
10677 * as those integer divisions are defined in terms of
10678 * parameters or input dimensions.
10679 * In this case, *div is set to the number of integer divisions and
10680 * *ineq is set to the number of inequality constraints (provided
10681 * div and ineq are not NULL).
10683 * The equality may also involve a single integer division involving
10684 * the output dimensions (typically only output dimension "pos") as
10685 * long as the coefficient of output dimension "pos" is 1 or -1 and
10686 * there is a pair of constraints i >= l and i <= l + n, with i referring
10687 * to output dimension "pos", l an expression involving only earlier
10688 * dimensions and n smaller than the coefficient of the integer division
10689 * in the equality. In this case, the output dimension can be defined
10690 * in terms of a modulo expression that does not involve the integer division.
10691 * *div is then set to this single integer division and
10692 * *ineq is set to the index of constraint i >= l.
10694 * Return bmap->n_eq if there is no such equality.
10695 * Return -1 on error.
10697 int isl_basic_map_output_defining_equality(__isl_keep isl_basic_map *bmap,
10698 int pos, int *div, int *ineq)
10700 int j, k, l;
10701 unsigned n_out, o_out;
10702 unsigned n_div, o_div;
10704 if (!bmap)
10705 return -1;
10707 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10708 o_out = isl_basic_map_offset(bmap, isl_dim_out);
10709 n_div = isl_basic_map_dim(bmap, isl_dim_div);
10710 o_div = isl_basic_map_offset(bmap, isl_dim_div);
10712 if (ineq)
10713 *ineq = bmap->n_ineq;
10714 if (div)
10715 *div = n_div;
10716 for (j = 0; j < bmap->n_eq; ++j) {
10717 if (isl_int_is_zero(bmap->eq[j][o_out + pos]))
10718 continue;
10719 if (isl_seq_first_non_zero(bmap->eq[j] + o_out + pos + 1,
10720 n_out - (pos + 1)) != -1)
10721 continue;
10722 k = first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10723 0, n_div);
10724 if (k >= n_div)
10725 return j;
10726 if (!isl_int_is_one(bmap->eq[j][o_out + pos]) &&
10727 !isl_int_is_negone(bmap->eq[j][o_out + pos]))
10728 continue;
10729 if (first_div_may_involve_output(bmap, bmap->eq[j] + o_div,
10730 k + 1, n_div - (k+1)) < n_div)
10731 continue;
10732 l = find_modulo_constraint_pair(bmap, pos,
10733 bmap->eq[j][o_div + k]);
10734 if (l < 0)
10735 return -1;
10736 if (l >= bmap->n_ineq)
10737 continue;
10738 if (div)
10739 *div = k;
10740 if (ineq)
10741 *ineq = l;
10742 return j;
10745 return bmap->n_eq;
10748 /* Check if the given basic map is obviously single-valued.
10749 * In particular, for each output dimension, check that there is
10750 * an equality that defines the output dimension in terms of
10751 * earlier dimensions.
10753 isl_bool isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
10755 int i;
10756 unsigned n_out;
10758 if (!bmap)
10759 return isl_bool_error;
10761 n_out = isl_basic_map_dim(bmap, isl_dim_out);
10763 for (i = 0; i < n_out; ++i) {
10764 int eq;
10766 eq = isl_basic_map_output_defining_equality(bmap, i,
10767 NULL, NULL);
10768 if (eq < 0)
10769 return isl_bool_error;
10770 if (eq >= bmap->n_eq)
10771 return isl_bool_false;
10774 return isl_bool_true;
10777 /* Check if the given basic map is single-valued.
10778 * We simply compute
10780 * M \circ M^-1
10782 * and check if the result is a subset of the identity mapping.
10784 isl_bool isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
10786 isl_space *space;
10787 isl_basic_map *test;
10788 isl_basic_map *id;
10789 isl_bool sv;
10791 sv = isl_basic_map_plain_is_single_valued(bmap);
10792 if (sv < 0 || sv)
10793 return sv;
10795 test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
10796 test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
10798 space = isl_basic_map_get_space(bmap);
10799 space = isl_space_map_from_set(isl_space_range(space));
10800 id = isl_basic_map_identity(space);
10802 sv = isl_basic_map_is_subset(test, id);
10804 isl_basic_map_free(test);
10805 isl_basic_map_free(id);
10807 return sv;
10810 /* Check if the given map is obviously single-valued.
10812 isl_bool isl_map_plain_is_single_valued(__isl_keep isl_map *map)
10814 if (!map)
10815 return isl_bool_error;
10816 if (map->n == 0)
10817 return isl_bool_true;
10818 if (map->n >= 2)
10819 return isl_bool_false;
10821 return isl_basic_map_plain_is_single_valued(map->p[0]);
10824 /* Check if the given map is single-valued.
10825 * We simply compute
10827 * M \circ M^-1
10829 * and check if the result is a subset of the identity mapping.
10831 isl_bool isl_map_is_single_valued(__isl_keep isl_map *map)
10833 isl_space *dim;
10834 isl_map *test;
10835 isl_map *id;
10836 isl_bool sv;
10838 sv = isl_map_plain_is_single_valued(map);
10839 if (sv < 0 || sv)
10840 return sv;
10842 test = isl_map_reverse(isl_map_copy(map));
10843 test = isl_map_apply_range(test, isl_map_copy(map));
10845 dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
10846 id = isl_map_identity(dim);
10848 sv = isl_map_is_subset(test, id);
10850 isl_map_free(test);
10851 isl_map_free(id);
10853 return sv;
10856 isl_bool isl_map_is_injective(__isl_keep isl_map *map)
10858 isl_bool in;
10860 map = isl_map_copy(map);
10861 map = isl_map_reverse(map);
10862 in = isl_map_is_single_valued(map);
10863 isl_map_free(map);
10865 return in;
10868 /* Check if the given map is obviously injective.
10870 isl_bool isl_map_plain_is_injective(__isl_keep isl_map *map)
10872 isl_bool in;
10874 map = isl_map_copy(map);
10875 map = isl_map_reverse(map);
10876 in = isl_map_plain_is_single_valued(map);
10877 isl_map_free(map);
10879 return in;
10882 isl_bool isl_map_is_bijective(__isl_keep isl_map *map)
10884 isl_bool sv;
10886 sv = isl_map_is_single_valued(map);
10887 if (sv < 0 || !sv)
10888 return sv;
10890 return isl_map_is_injective(map);
10893 isl_bool isl_set_is_singleton(__isl_keep isl_set *set)
10895 return isl_map_is_single_valued((isl_map *)set);
10898 int isl_map_is_translation(__isl_keep isl_map *map)
10900 int ok;
10901 isl_set *delta;
10903 delta = isl_map_deltas(isl_map_copy(map));
10904 ok = isl_set_is_singleton(delta);
10905 isl_set_free(delta);
10907 return ok;
10910 static int unique(isl_int *p, unsigned pos, unsigned len)
10912 if (isl_seq_first_non_zero(p, pos) != -1)
10913 return 0;
10914 if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
10915 return 0;
10916 return 1;
10919 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
10921 int i, j;
10922 unsigned nvar;
10923 unsigned ovar;
10925 if (!bset)
10926 return -1;
10928 if (isl_basic_set_dim(bset, isl_dim_div) != 0)
10929 return 0;
10931 nvar = isl_basic_set_dim(bset, isl_dim_set);
10932 ovar = isl_space_offset(bset->dim, isl_dim_set);
10933 for (j = 0; j < nvar; ++j) {
10934 int lower = 0, upper = 0;
10935 for (i = 0; i < bset->n_eq; ++i) {
10936 if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
10937 continue;
10938 if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
10939 return 0;
10940 break;
10942 if (i < bset->n_eq)
10943 continue;
10944 for (i = 0; i < bset->n_ineq; ++i) {
10945 if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10946 continue;
10947 if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10948 return 0;
10949 if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10950 lower = 1;
10951 else
10952 upper = 1;
10954 if (!lower || !upper)
10955 return 0;
10958 return 1;
10961 int isl_set_is_box(__isl_keep isl_set *set)
10963 if (!set)
10964 return -1;
10965 if (set->n != 1)
10966 return 0;
10968 return isl_basic_set_is_box(set->p[0]);
10971 isl_bool isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10973 if (!bset)
10974 return isl_bool_error;
10976 return isl_space_is_wrapping(bset->dim);
10979 isl_bool isl_set_is_wrapping(__isl_keep isl_set *set)
10981 if (!set)
10982 return isl_bool_error;
10984 return isl_space_is_wrapping(set->dim);
10987 /* Modify the space of "map" through a call to "change".
10988 * If "can_change" is set (not NULL), then first call it to check
10989 * if the modification is allowed, printing the error message "cannot_change"
10990 * if it is not.
10992 static __isl_give isl_map *isl_map_change_space(__isl_take isl_map *map,
10993 isl_bool (*can_change)(__isl_keep isl_map *map),
10994 const char *cannot_change,
10995 __isl_give isl_space *(*change)(__isl_take isl_space *space))
10997 isl_bool ok;
10998 isl_space *space;
11000 if (!map)
11001 return NULL;
11003 ok = can_change ? can_change(map) : isl_bool_true;
11004 if (ok < 0)
11005 return isl_map_free(map);
11006 if (!ok)
11007 isl_die(isl_map_get_ctx(map), isl_error_invalid, cannot_change,
11008 return isl_map_free(map));
11010 space = change(isl_map_get_space(map));
11011 map = isl_map_reset_space(map, space);
11013 return map;
11016 /* Is the domain of "map" a wrapped relation?
11018 isl_bool isl_map_domain_is_wrapping(__isl_keep isl_map *map)
11020 if (!map)
11021 return isl_bool_error;
11023 return isl_space_domain_is_wrapping(map->dim);
11026 /* Is the range of "map" a wrapped relation?
11028 isl_bool isl_map_range_is_wrapping(__isl_keep isl_map *map)
11030 if (!map)
11031 return isl_bool_error;
11033 return isl_space_range_is_wrapping(map->dim);
11036 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
11038 bmap = isl_basic_map_cow(bmap);
11039 if (!bmap)
11040 return NULL;
11042 bmap->dim = isl_space_wrap(bmap->dim);
11043 if (!bmap->dim)
11044 goto error;
11046 bmap = isl_basic_map_finalize(bmap);
11048 return (isl_basic_set *)bmap;
11049 error:
11050 isl_basic_map_free(bmap);
11051 return NULL;
11054 /* Given a map A -> B, return the set (A -> B).
11056 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
11058 return isl_map_change_space(map, NULL, NULL, &isl_space_wrap);
11061 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
11063 bset = isl_basic_set_cow(bset);
11064 if (!bset)
11065 return NULL;
11067 bset->dim = isl_space_unwrap(bset->dim);
11068 if (!bset->dim)
11069 goto error;
11071 bset = isl_basic_set_finalize(bset);
11073 return (isl_basic_map *)bset;
11074 error:
11075 isl_basic_set_free(bset);
11076 return NULL;
11079 /* Given a set (A -> B), return the map A -> B.
11080 * Error out if "set" is not of the form (A -> B).
11082 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
11084 return isl_map_change_space(set, &isl_set_is_wrapping,
11085 "not a wrapping set", &isl_space_unwrap);
11088 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
11089 enum isl_dim_type type)
11091 if (!bmap)
11092 return NULL;
11094 if (!isl_space_is_named_or_nested(bmap->dim, type))
11095 return bmap;
11097 bmap = isl_basic_map_cow(bmap);
11098 if (!bmap)
11099 return NULL;
11101 bmap->dim = isl_space_reset(bmap->dim, type);
11102 if (!bmap->dim)
11103 goto error;
11105 bmap = isl_basic_map_finalize(bmap);
11107 return bmap;
11108 error:
11109 isl_basic_map_free(bmap);
11110 return NULL;
11113 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
11114 enum isl_dim_type type)
11116 int i;
11118 if (!map)
11119 return NULL;
11121 if (!isl_space_is_named_or_nested(map->dim, type))
11122 return map;
11124 map = isl_map_cow(map);
11125 if (!map)
11126 return NULL;
11128 for (i = 0; i < map->n; ++i) {
11129 map->p[i] = isl_basic_map_reset(map->p[i], type);
11130 if (!map->p[i])
11131 goto error;
11133 map->dim = isl_space_reset(map->dim, type);
11134 if (!map->dim)
11135 goto error;
11137 return map;
11138 error:
11139 isl_map_free(map);
11140 return NULL;
11143 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
11145 if (!bmap)
11146 return NULL;
11148 if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
11149 return bmap;
11151 bmap = isl_basic_map_cow(bmap);
11152 if (!bmap)
11153 return NULL;
11155 bmap->dim = isl_space_flatten(bmap->dim);
11156 if (!bmap->dim)
11157 goto error;
11159 bmap = isl_basic_map_finalize(bmap);
11161 return bmap;
11162 error:
11163 isl_basic_map_free(bmap);
11164 return NULL;
11167 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
11169 return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
11172 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
11173 __isl_take isl_basic_map *bmap)
11175 if (!bmap)
11176 return NULL;
11178 if (!bmap->dim->nested[0])
11179 return bmap;
11181 bmap = isl_basic_map_cow(bmap);
11182 if (!bmap)
11183 return NULL;
11185 bmap->dim = isl_space_flatten_domain(bmap->dim);
11186 if (!bmap->dim)
11187 goto error;
11189 bmap = isl_basic_map_finalize(bmap);
11191 return bmap;
11192 error:
11193 isl_basic_map_free(bmap);
11194 return NULL;
11197 __isl_give isl_basic_map *isl_basic_map_flatten_range(
11198 __isl_take isl_basic_map *bmap)
11200 if (!bmap)
11201 return NULL;
11203 if (!bmap->dim->nested[1])
11204 return bmap;
11206 bmap = isl_basic_map_cow(bmap);
11207 if (!bmap)
11208 return NULL;
11210 bmap->dim = isl_space_flatten_range(bmap->dim);
11211 if (!bmap->dim)
11212 goto error;
11214 bmap = isl_basic_map_finalize(bmap);
11216 return bmap;
11217 error:
11218 isl_basic_map_free(bmap);
11219 return NULL;
11222 /* Remove any internal structure from the spaces of domain and range of "map".
11224 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
11226 if (!map)
11227 return NULL;
11229 if (!map->dim->nested[0] && !map->dim->nested[1])
11230 return map;
11232 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten);
11235 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
11237 return (isl_set *)isl_map_flatten((isl_map *)set);
11240 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
11242 isl_space *dim, *flat_dim;
11243 isl_map *map;
11245 dim = isl_set_get_space(set);
11246 flat_dim = isl_space_flatten(isl_space_copy(dim));
11247 map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
11248 map = isl_map_intersect_domain(map, set);
11250 return map;
11253 /* Remove any internal structure from the space of the domain of "map".
11255 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
11257 if (!map)
11258 return NULL;
11260 if (!map->dim->nested[0])
11261 return map;
11263 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_domain);
11266 /* Remove any internal structure from the space of the range of "map".
11268 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
11270 if (!map)
11271 return NULL;
11273 if (!map->dim->nested[1])
11274 return map;
11276 return isl_map_change_space(map, NULL, NULL, &isl_space_flatten_range);
11279 /* Reorder the dimensions of "bmap" according to the given dim_map
11280 * and set the dimension specification to "dim" and
11281 * perform Gaussian elimination on the result.
11283 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
11284 __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
11286 isl_basic_map *res;
11287 unsigned flags;
11289 bmap = isl_basic_map_cow(bmap);
11290 if (!bmap || !dim || !dim_map)
11291 goto error;
11293 flags = bmap->flags;
11294 ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
11295 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
11296 ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
11297 res = isl_basic_map_alloc_space(dim,
11298 bmap->n_div, bmap->n_eq, bmap->n_ineq);
11299 res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
11300 if (res)
11301 res->flags = flags;
11302 res = isl_basic_map_gauss(res, NULL);
11303 res = isl_basic_map_finalize(res);
11304 return res;
11305 error:
11306 free(dim_map);
11307 isl_basic_map_free(bmap);
11308 isl_space_free(dim);
11309 return NULL;
11312 /* Reorder the dimensions of "map" according to given reordering.
11314 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
11315 __isl_take isl_reordering *r)
11317 int i;
11318 struct isl_dim_map *dim_map;
11320 map = isl_map_cow(map);
11321 dim_map = isl_dim_map_from_reordering(r);
11322 if (!map || !r || !dim_map)
11323 goto error;
11325 for (i = 0; i < map->n; ++i) {
11326 struct isl_dim_map *dim_map_i;
11328 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
11330 map->p[i] = isl_basic_map_realign(map->p[i],
11331 isl_space_copy(r->dim), dim_map_i);
11333 if (!map->p[i])
11334 goto error;
11337 map = isl_map_reset_space(map, isl_space_copy(r->dim));
11339 isl_reordering_free(r);
11340 free(dim_map);
11341 return map;
11342 error:
11343 free(dim_map);
11344 isl_map_free(map);
11345 isl_reordering_free(r);
11346 return NULL;
11349 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
11350 __isl_take isl_reordering *r)
11352 return (isl_set *)isl_map_realign((isl_map *)set, r);
11355 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
11356 __isl_take isl_space *model)
11358 isl_ctx *ctx;
11360 if (!map || !model)
11361 goto error;
11363 ctx = isl_space_get_ctx(model);
11364 if (!isl_space_has_named_params(model))
11365 isl_die(ctx, isl_error_invalid,
11366 "model has unnamed parameters", goto error);
11367 if (!isl_space_has_named_params(map->dim))
11368 isl_die(ctx, isl_error_invalid,
11369 "relation has unnamed parameters", goto error);
11370 if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
11371 isl_reordering *exp;
11373 model = isl_space_drop_dims(model, isl_dim_in,
11374 0, isl_space_dim(model, isl_dim_in));
11375 model = isl_space_drop_dims(model, isl_dim_out,
11376 0, isl_space_dim(model, isl_dim_out));
11377 exp = isl_parameter_alignment_reordering(map->dim, model);
11378 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
11379 map = isl_map_realign(map, exp);
11382 isl_space_free(model);
11383 return map;
11384 error:
11385 isl_space_free(model);
11386 isl_map_free(map);
11387 return NULL;
11390 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
11391 __isl_take isl_space *model)
11393 return isl_map_align_params(set, model);
11396 /* Align the parameters of "bmap" to those of "model", introducing
11397 * additional parameters if needed.
11399 __isl_give isl_basic_map *isl_basic_map_align_params(
11400 __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
11402 isl_ctx *ctx;
11404 if (!bmap || !model)
11405 goto error;
11407 ctx = isl_space_get_ctx(model);
11408 if (!isl_space_has_named_params(model))
11409 isl_die(ctx, isl_error_invalid,
11410 "model has unnamed parameters", goto error);
11411 if (!isl_space_has_named_params(bmap->dim))
11412 isl_die(ctx, isl_error_invalid,
11413 "relation has unnamed parameters", goto error);
11414 if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
11415 isl_reordering *exp;
11416 struct isl_dim_map *dim_map;
11418 model = isl_space_drop_dims(model, isl_dim_in,
11419 0, isl_space_dim(model, isl_dim_in));
11420 model = isl_space_drop_dims(model, isl_dim_out,
11421 0, isl_space_dim(model, isl_dim_out));
11422 exp = isl_parameter_alignment_reordering(bmap->dim, model);
11423 exp = isl_reordering_extend_space(exp,
11424 isl_basic_map_get_space(bmap));
11425 dim_map = isl_dim_map_from_reordering(exp);
11426 bmap = isl_basic_map_realign(bmap,
11427 exp ? isl_space_copy(exp->dim) : NULL,
11428 isl_dim_map_extend(dim_map, bmap));
11429 isl_reordering_free(exp);
11430 free(dim_map);
11433 isl_space_free(model);
11434 return bmap;
11435 error:
11436 isl_space_free(model);
11437 isl_basic_map_free(bmap);
11438 return NULL;
11441 /* Align the parameters of "bset" to those of "model", introducing
11442 * additional parameters if needed.
11444 __isl_give isl_basic_set *isl_basic_set_align_params(
11445 __isl_take isl_basic_set *bset, __isl_take isl_space *model)
11447 return isl_basic_map_align_params(bset, model);
11450 __isl_give isl_mat *isl_basic_map_equalities_matrix(
11451 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11452 enum isl_dim_type c2, enum isl_dim_type c3,
11453 enum isl_dim_type c4, enum isl_dim_type c5)
11455 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11456 struct isl_mat *mat;
11457 int i, j, k;
11458 int pos;
11460 if (!bmap)
11461 return NULL;
11462 mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
11463 isl_basic_map_total_dim(bmap) + 1);
11464 if (!mat)
11465 return NULL;
11466 for (i = 0; i < bmap->n_eq; ++i)
11467 for (j = 0, pos = 0; j < 5; ++j) {
11468 int off = isl_basic_map_offset(bmap, c[j]);
11469 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11470 isl_int_set(mat->row[i][pos],
11471 bmap->eq[i][off + k]);
11472 ++pos;
11476 return mat;
11479 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
11480 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
11481 enum isl_dim_type c2, enum isl_dim_type c3,
11482 enum isl_dim_type c4, enum isl_dim_type c5)
11484 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11485 struct isl_mat *mat;
11486 int i, j, k;
11487 int pos;
11489 if (!bmap)
11490 return NULL;
11491 mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
11492 isl_basic_map_total_dim(bmap) + 1);
11493 if (!mat)
11494 return NULL;
11495 for (i = 0; i < bmap->n_ineq; ++i)
11496 for (j = 0, pos = 0; j < 5; ++j) {
11497 int off = isl_basic_map_offset(bmap, c[j]);
11498 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11499 isl_int_set(mat->row[i][pos],
11500 bmap->ineq[i][off + k]);
11501 ++pos;
11505 return mat;
11508 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
11509 __isl_take isl_space *dim,
11510 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11511 enum isl_dim_type c2, enum isl_dim_type c3,
11512 enum isl_dim_type c4, enum isl_dim_type c5)
11514 enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
11515 isl_basic_map *bmap;
11516 unsigned total;
11517 unsigned extra;
11518 int i, j, k, l;
11519 int pos;
11521 if (!dim || !eq || !ineq)
11522 goto error;
11524 if (eq->n_col != ineq->n_col)
11525 isl_die(dim->ctx, isl_error_invalid,
11526 "equalities and inequalities matrices should have "
11527 "same number of columns", goto error);
11529 total = 1 + isl_space_dim(dim, isl_dim_all);
11531 if (eq->n_col < total)
11532 isl_die(dim->ctx, isl_error_invalid,
11533 "number of columns too small", goto error);
11535 extra = eq->n_col - total;
11537 bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
11538 eq->n_row, ineq->n_row);
11539 if (!bmap)
11540 goto error;
11541 for (i = 0; i < extra; ++i) {
11542 k = isl_basic_map_alloc_div(bmap);
11543 if (k < 0)
11544 goto error;
11545 isl_int_set_si(bmap->div[k][0], 0);
11547 for (i = 0; i < eq->n_row; ++i) {
11548 l = isl_basic_map_alloc_equality(bmap);
11549 if (l < 0)
11550 goto error;
11551 for (j = 0, pos = 0; j < 5; ++j) {
11552 int off = isl_basic_map_offset(bmap, c[j]);
11553 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11554 isl_int_set(bmap->eq[l][off + k],
11555 eq->row[i][pos]);
11556 ++pos;
11560 for (i = 0; i < ineq->n_row; ++i) {
11561 l = isl_basic_map_alloc_inequality(bmap);
11562 if (l < 0)
11563 goto error;
11564 for (j = 0, pos = 0; j < 5; ++j) {
11565 int off = isl_basic_map_offset(bmap, c[j]);
11566 for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
11567 isl_int_set(bmap->ineq[l][off + k],
11568 ineq->row[i][pos]);
11569 ++pos;
11574 isl_space_free(dim);
11575 isl_mat_free(eq);
11576 isl_mat_free(ineq);
11578 bmap = isl_basic_map_simplify(bmap);
11579 return isl_basic_map_finalize(bmap);
11580 error:
11581 isl_space_free(dim);
11582 isl_mat_free(eq);
11583 isl_mat_free(ineq);
11584 return NULL;
11587 __isl_give isl_mat *isl_basic_set_equalities_matrix(
11588 __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
11589 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11591 return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
11592 c1, c2, c3, c4, isl_dim_in);
11595 __isl_give isl_mat *isl_basic_set_inequalities_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_inequalities_matrix((isl_basic_map *)bset,
11600 c1, c2, c3, c4, isl_dim_in);
11603 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
11604 __isl_take isl_space *dim,
11605 __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
11606 enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
11608 return (isl_basic_set*)
11609 isl_basic_map_from_constraint_matrices(dim, eq, ineq,
11610 c1, c2, c3, c4, isl_dim_in);
11613 isl_bool isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
11615 if (!bmap)
11616 return isl_bool_error;
11618 return isl_space_can_zip(bmap->dim);
11621 isl_bool isl_map_can_zip(__isl_keep isl_map *map)
11623 if (!map)
11624 return isl_bool_error;
11626 return isl_space_can_zip(map->dim);
11629 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
11630 * (A -> C) -> (B -> D).
11632 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
11634 unsigned pos;
11635 unsigned n1;
11636 unsigned n2;
11638 if (!bmap)
11639 return NULL;
11641 if (!isl_basic_map_can_zip(bmap))
11642 isl_die(bmap->ctx, isl_error_invalid,
11643 "basic map cannot be zipped", goto error);
11644 pos = isl_basic_map_offset(bmap, isl_dim_in) +
11645 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
11646 n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
11647 n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
11648 bmap = isl_basic_map_cow(bmap);
11649 bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
11650 if (!bmap)
11651 return NULL;
11652 bmap->dim = isl_space_zip(bmap->dim);
11653 if (!bmap->dim)
11654 goto error;
11655 bmap = isl_basic_map_mark_final(bmap);
11656 return bmap;
11657 error:
11658 isl_basic_map_free(bmap);
11659 return NULL;
11662 /* Given a map (A -> B) -> (C -> D), return the corresponding map
11663 * (A -> C) -> (B -> D).
11665 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
11667 int i;
11669 if (!map)
11670 return NULL;
11672 if (!isl_map_can_zip(map))
11673 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
11674 goto error);
11676 map = isl_map_cow(map);
11677 if (!map)
11678 return NULL;
11680 for (i = 0; i < map->n; ++i) {
11681 map->p[i] = isl_basic_map_zip(map->p[i]);
11682 if (!map->p[i])
11683 goto error;
11686 map->dim = isl_space_zip(map->dim);
11687 if (!map->dim)
11688 goto error;
11690 return map;
11691 error:
11692 isl_map_free(map);
11693 return NULL;
11696 /* Can we apply isl_basic_map_curry to "bmap"?
11697 * That is, does it have a nested relation in its domain?
11699 isl_bool isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
11701 if (!bmap)
11702 return isl_bool_error;
11704 return isl_space_can_curry(bmap->dim);
11707 /* Can we apply isl_map_curry to "map"?
11708 * That is, does it have a nested relation in its domain?
11710 isl_bool isl_map_can_curry(__isl_keep isl_map *map)
11712 if (!map)
11713 return isl_bool_error;
11715 return isl_space_can_curry(map->dim);
11718 /* Given a basic map (A -> B) -> C, return the corresponding basic map
11719 * A -> (B -> C).
11721 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
11724 if (!bmap)
11725 return NULL;
11727 if (!isl_basic_map_can_curry(bmap))
11728 isl_die(bmap->ctx, isl_error_invalid,
11729 "basic map cannot be curried", goto error);
11730 bmap = isl_basic_map_cow(bmap);
11731 if (!bmap)
11732 return NULL;
11733 bmap->dim = isl_space_curry(bmap->dim);
11734 if (!bmap->dim)
11735 goto error;
11736 bmap = isl_basic_map_mark_final(bmap);
11737 return bmap;
11738 error:
11739 isl_basic_map_free(bmap);
11740 return NULL;
11743 /* Given a map (A -> B) -> C, return the corresponding map
11744 * A -> (B -> C).
11746 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
11748 return isl_map_change_space(map, &isl_map_can_curry,
11749 "map cannot be curried", &isl_space_curry);
11752 /* Can isl_map_range_curry be applied to "map"?
11753 * That is, does it have a nested relation in its range,
11754 * the domain of which is itself a nested relation?
11756 isl_bool isl_map_can_range_curry(__isl_keep isl_map *map)
11758 if (!map)
11759 return isl_bool_error;
11761 return isl_space_can_range_curry(map->dim);
11764 /* Given a map A -> ((B -> C) -> D), return the corresponding map
11765 * A -> (B -> (C -> D)).
11767 __isl_give isl_map *isl_map_range_curry(__isl_take isl_map *map)
11769 return isl_map_change_space(map, &isl_map_can_range_curry,
11770 "map range cannot be curried",
11771 &isl_space_range_curry);
11774 /* Can we apply isl_basic_map_uncurry to "bmap"?
11775 * That is, does it have a nested relation in its domain?
11777 isl_bool isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
11779 if (!bmap)
11780 return isl_bool_error;
11782 return isl_space_can_uncurry(bmap->dim);
11785 /* Can we apply isl_map_uncurry to "map"?
11786 * That is, does it have a nested relation in its domain?
11788 isl_bool isl_map_can_uncurry(__isl_keep isl_map *map)
11790 if (!map)
11791 return isl_bool_error;
11793 return isl_space_can_uncurry(map->dim);
11796 /* Given a basic map A -> (B -> C), return the corresponding basic map
11797 * (A -> B) -> C.
11799 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
11802 if (!bmap)
11803 return NULL;
11805 if (!isl_basic_map_can_uncurry(bmap))
11806 isl_die(bmap->ctx, isl_error_invalid,
11807 "basic map cannot be uncurried",
11808 return isl_basic_map_free(bmap));
11809 bmap = isl_basic_map_cow(bmap);
11810 if (!bmap)
11811 return NULL;
11812 bmap->dim = isl_space_uncurry(bmap->dim);
11813 if (!bmap->dim)
11814 return isl_basic_map_free(bmap);
11815 bmap = isl_basic_map_mark_final(bmap);
11816 return bmap;
11819 /* Given a map A -> (B -> C), return the corresponding map
11820 * (A -> B) -> C.
11822 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
11824 return isl_map_change_space(map, &isl_map_can_uncurry,
11825 "map cannot be uncurried", &isl_space_uncurry);
11828 /* Construct a basic map mapping the domain of the affine expression
11829 * to a one-dimensional range prescribed by the affine expression.
11831 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
11833 int k;
11834 int pos;
11835 isl_local_space *ls;
11836 isl_basic_map *bmap;
11838 if (!aff)
11839 return NULL;
11841 ls = isl_aff_get_local_space(aff);
11842 bmap = isl_basic_map_from_local_space(ls);
11843 bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
11844 k = isl_basic_map_alloc_equality(bmap);
11845 if (k < 0)
11846 goto error;
11848 pos = isl_basic_map_offset(bmap, isl_dim_out);
11849 isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
11850 isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
11851 isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
11852 aff->v->size - (pos + 1));
11854 isl_aff_free(aff);
11855 bmap = isl_basic_map_finalize(bmap);
11856 return bmap;
11857 error:
11858 isl_aff_free(aff);
11859 isl_basic_map_free(bmap);
11860 return NULL;
11863 /* Construct a map mapping the domain of the affine expression
11864 * to a one-dimensional range prescribed by the affine expression.
11866 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
11868 isl_basic_map *bmap;
11870 bmap = isl_basic_map_from_aff(aff);
11871 return isl_map_from_basic_map(bmap);
11874 /* Construct a basic map mapping the domain the multi-affine expression
11875 * to its range, with each dimension in the range equated to the
11876 * corresponding affine expression.
11878 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
11879 __isl_take isl_multi_aff *maff)
11881 int i;
11882 isl_space *space;
11883 isl_basic_map *bmap;
11885 if (!maff)
11886 return NULL;
11888 if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11889 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11890 "invalid space", goto error);
11892 space = isl_space_domain(isl_multi_aff_get_space(maff));
11893 bmap = isl_basic_map_universe(isl_space_from_domain(space));
11895 for (i = 0; i < maff->n; ++i) {
11896 isl_aff *aff;
11897 isl_basic_map *bmap_i;
11899 aff = isl_aff_copy(maff->p[i]);
11900 bmap_i = isl_basic_map_from_aff(aff);
11902 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11905 bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11907 isl_multi_aff_free(maff);
11908 return bmap;
11909 error:
11910 isl_multi_aff_free(maff);
11911 return NULL;
11914 /* Construct a map mapping the domain the multi-affine expression
11915 * to its range, with each dimension in the range equated to the
11916 * corresponding affine expression.
11918 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11920 isl_basic_map *bmap;
11922 bmap = isl_basic_map_from_multi_aff(maff);
11923 return isl_map_from_basic_map(bmap);
11926 /* Construct a basic map mapping a domain in the given space to
11927 * to an n-dimensional range, with n the number of elements in the list,
11928 * where each coordinate in the range is prescribed by the
11929 * corresponding affine expression.
11930 * The domains of all affine expressions in the list are assumed to match
11931 * domain_dim.
11933 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11934 __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11936 int i;
11937 isl_space *dim;
11938 isl_basic_map *bmap;
11940 if (!list)
11941 return NULL;
11943 dim = isl_space_from_domain(domain_dim);
11944 bmap = isl_basic_map_universe(dim);
11946 for (i = 0; i < list->n; ++i) {
11947 isl_aff *aff;
11948 isl_basic_map *bmap_i;
11950 aff = isl_aff_copy(list->p[i]);
11951 bmap_i = isl_basic_map_from_aff(aff);
11953 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11956 isl_aff_list_free(list);
11957 return bmap;
11960 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11961 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11963 return isl_map_equate(set, type1, pos1, type2, pos2);
11966 /* Construct a basic map where the given dimensions are equal to each other.
11968 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11969 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11971 isl_basic_map *bmap = NULL;
11972 int i;
11974 if (!space)
11975 return NULL;
11977 if (pos1 >= isl_space_dim(space, type1))
11978 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11979 "index out of bounds", goto error);
11980 if (pos2 >= isl_space_dim(space, type2))
11981 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11982 "index out of bounds", goto error);
11984 if (type1 == type2 && pos1 == pos2)
11985 return isl_basic_map_universe(space);
11987 bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11988 i = isl_basic_map_alloc_equality(bmap);
11989 if (i < 0)
11990 goto error;
11991 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11992 pos1 += isl_basic_map_offset(bmap, type1);
11993 pos2 += isl_basic_map_offset(bmap, type2);
11994 isl_int_set_si(bmap->eq[i][pos1], -1);
11995 isl_int_set_si(bmap->eq[i][pos2], 1);
11996 bmap = isl_basic_map_finalize(bmap);
11997 isl_space_free(space);
11998 return bmap;
11999 error:
12000 isl_space_free(space);
12001 isl_basic_map_free(bmap);
12002 return NULL;
12005 /* Add a constraint imposing that the given two dimensions are equal.
12007 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
12008 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12010 isl_basic_map *eq;
12012 eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12014 bmap = isl_basic_map_intersect(bmap, eq);
12016 return bmap;
12019 /* Add a constraint imposing that the given two dimensions are equal.
12021 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
12022 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12024 isl_basic_map *bmap;
12026 bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
12028 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12030 return map;
12033 /* Add a constraint imposing that the given two dimensions have opposite values.
12035 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
12036 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12038 isl_basic_map *bmap = NULL;
12039 int i;
12041 if (!map)
12042 return NULL;
12044 if (pos1 >= isl_map_dim(map, type1))
12045 isl_die(map->ctx, isl_error_invalid,
12046 "index out of bounds", goto error);
12047 if (pos2 >= isl_map_dim(map, type2))
12048 isl_die(map->ctx, isl_error_invalid,
12049 "index out of bounds", goto error);
12051 bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
12052 i = isl_basic_map_alloc_equality(bmap);
12053 if (i < 0)
12054 goto error;
12055 isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
12056 pos1 += isl_basic_map_offset(bmap, type1);
12057 pos2 += isl_basic_map_offset(bmap, type2);
12058 isl_int_set_si(bmap->eq[i][pos1], 1);
12059 isl_int_set_si(bmap->eq[i][pos2], 1);
12060 bmap = isl_basic_map_finalize(bmap);
12062 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12064 return map;
12065 error:
12066 isl_basic_map_free(bmap);
12067 isl_map_free(map);
12068 return NULL;
12071 /* Construct a constraint imposing that the value of the first dimension is
12072 * greater than or equal to that of the second.
12074 static __isl_give isl_constraint *constraint_order_ge(
12075 __isl_take isl_space *space, enum isl_dim_type type1, int pos1,
12076 enum isl_dim_type type2, int pos2)
12078 isl_constraint *c;
12080 if (!space)
12081 return NULL;
12083 c = isl_constraint_alloc_inequality(isl_local_space_from_space(space));
12085 if (pos1 >= isl_constraint_dim(c, type1))
12086 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12087 "index out of bounds", return isl_constraint_free(c));
12088 if (pos2 >= isl_constraint_dim(c, type2))
12089 isl_die(isl_constraint_get_ctx(c), isl_error_invalid,
12090 "index out of bounds", return isl_constraint_free(c));
12092 if (type1 == type2 && pos1 == pos2)
12093 return c;
12095 c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
12096 c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
12098 return c;
12101 /* Add a constraint imposing that the value of the first dimension is
12102 * greater than or equal to that of the second.
12104 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
12105 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12107 isl_constraint *c;
12108 isl_space *space;
12110 if (type1 == type2 && pos1 == pos2)
12111 return bmap;
12112 space = isl_basic_map_get_space(bmap);
12113 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12114 bmap = isl_basic_map_add_constraint(bmap, c);
12116 return bmap;
12119 /* Add a constraint imposing that the value of the first dimension is
12120 * greater than or equal to that of the second.
12122 __isl_give isl_map *isl_map_order_ge(__isl_take isl_map *map,
12123 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12125 isl_constraint *c;
12126 isl_space *space;
12128 if (type1 == type2 && pos1 == pos2)
12129 return map;
12130 space = isl_map_get_space(map);
12131 c = constraint_order_ge(space, type1, pos1, type2, pos2);
12132 map = isl_map_add_constraint(map, c);
12134 return map;
12137 /* Add a constraint imposing that the value of the first dimension is
12138 * less than or equal to that of the second.
12140 __isl_give isl_map *isl_map_order_le(__isl_take isl_map *map,
12141 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12143 return isl_map_order_ge(map, type2, pos2, type1, pos1);
12146 /* Construct a basic map where the value of the first dimension is
12147 * greater than that of the second.
12149 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
12150 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12152 isl_basic_map *bmap = NULL;
12153 int i;
12155 if (!space)
12156 return NULL;
12158 if (pos1 >= isl_space_dim(space, type1))
12159 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12160 "index out of bounds", goto error);
12161 if (pos2 >= isl_space_dim(space, type2))
12162 isl_die(isl_space_get_ctx(space), isl_error_invalid,
12163 "index out of bounds", goto error);
12165 if (type1 == type2 && pos1 == pos2)
12166 return isl_basic_map_empty(space);
12168 bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
12169 i = isl_basic_map_alloc_inequality(bmap);
12170 if (i < 0)
12171 return isl_basic_map_free(bmap);
12172 isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
12173 pos1 += isl_basic_map_offset(bmap, type1);
12174 pos2 += isl_basic_map_offset(bmap, type2);
12175 isl_int_set_si(bmap->ineq[i][pos1], 1);
12176 isl_int_set_si(bmap->ineq[i][pos2], -1);
12177 isl_int_set_si(bmap->ineq[i][0], -1);
12178 bmap = isl_basic_map_finalize(bmap);
12180 return bmap;
12181 error:
12182 isl_space_free(space);
12183 isl_basic_map_free(bmap);
12184 return NULL;
12187 /* Add a constraint imposing that the value of the first dimension is
12188 * greater than that of the second.
12190 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
12191 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12193 isl_basic_map *gt;
12195 gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
12197 bmap = isl_basic_map_intersect(bmap, gt);
12199 return bmap;
12202 /* Add a constraint imposing that the value of the first dimension is
12203 * greater than that of the second.
12205 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
12206 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12208 isl_basic_map *bmap;
12210 bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
12212 map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
12214 return map;
12217 /* Add a constraint imposing that the value of the first dimension is
12218 * smaller than that of the second.
12220 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
12221 enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
12223 return isl_map_order_gt(map, type2, pos2, type1, pos1);
12226 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
12227 int pos)
12229 isl_aff *div;
12230 isl_local_space *ls;
12232 if (!bmap)
12233 return NULL;
12235 if (!isl_basic_map_divs_known(bmap))
12236 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12237 "some divs are unknown", return NULL);
12239 ls = isl_basic_map_get_local_space(bmap);
12240 div = isl_local_space_get_div(ls, pos);
12241 isl_local_space_free(ls);
12243 return div;
12246 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
12247 int pos)
12249 return isl_basic_map_get_div(bset, pos);
12252 /* Plug in "subs" for dimension "type", "pos" of "bset".
12254 * Let i be the dimension to replace and let "subs" be of the form
12256 * f/d
12258 * Any integer division with a non-zero coefficient for i,
12260 * floor((a i + g)/m)
12262 * is replaced by
12264 * floor((a f + d g)/(m d))
12266 * Constraints of the form
12268 * a i + g
12270 * are replaced by
12272 * a f + d g
12274 * We currently require that "subs" is an integral expression.
12275 * Handling rational expressions may require us to add stride constraints
12276 * as we do in isl_basic_set_preimage_multi_aff.
12278 __isl_give isl_basic_set *isl_basic_set_substitute(
12279 __isl_take isl_basic_set *bset,
12280 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12282 int i;
12283 isl_int v;
12284 isl_ctx *ctx;
12286 if (bset && isl_basic_set_plain_is_empty(bset))
12287 return bset;
12289 bset = isl_basic_set_cow(bset);
12290 if (!bset || !subs)
12291 goto error;
12293 ctx = isl_basic_set_get_ctx(bset);
12294 if (!isl_space_is_equal(bset->dim, subs->ls->dim))
12295 isl_die(ctx, isl_error_invalid,
12296 "spaces don't match", goto error);
12297 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
12298 isl_die(ctx, isl_error_unsupported,
12299 "cannot handle divs yet", goto error);
12300 if (!isl_int_is_one(subs->v->el[0]))
12301 isl_die(ctx, isl_error_invalid,
12302 "can only substitute integer expressions", goto error);
12304 pos += isl_basic_set_offset(bset, type);
12306 isl_int_init(v);
12308 for (i = 0; i < bset->n_eq; ++i) {
12309 if (isl_int_is_zero(bset->eq[i][pos]))
12310 continue;
12311 isl_int_set(v, bset->eq[i][pos]);
12312 isl_int_set_si(bset->eq[i][pos], 0);
12313 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
12314 v, subs->v->el + 1, subs->v->size - 1);
12317 for (i = 0; i < bset->n_ineq; ++i) {
12318 if (isl_int_is_zero(bset->ineq[i][pos]))
12319 continue;
12320 isl_int_set(v, bset->ineq[i][pos]);
12321 isl_int_set_si(bset->ineq[i][pos], 0);
12322 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
12323 v, subs->v->el + 1, subs->v->size - 1);
12326 for (i = 0; i < bset->n_div; ++i) {
12327 if (isl_int_is_zero(bset->div[i][1 + pos]))
12328 continue;
12329 isl_int_set(v, bset->div[i][1 + pos]);
12330 isl_int_set_si(bset->div[i][1 + pos], 0);
12331 isl_seq_combine(bset->div[i] + 1,
12332 subs->v->el[0], bset->div[i] + 1,
12333 v, subs->v->el + 1, subs->v->size - 1);
12334 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
12337 isl_int_clear(v);
12339 bset = isl_basic_set_simplify(bset);
12340 return isl_basic_set_finalize(bset);
12341 error:
12342 isl_basic_set_free(bset);
12343 return NULL;
12346 /* Plug in "subs" for dimension "type", "pos" of "set".
12348 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
12349 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
12351 int i;
12353 if (set && isl_set_plain_is_empty(set))
12354 return set;
12356 set = isl_set_cow(set);
12357 if (!set || !subs)
12358 goto error;
12360 for (i = set->n - 1; i >= 0; --i) {
12361 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
12362 if (remove_if_empty(set, i) < 0)
12363 goto error;
12366 return set;
12367 error:
12368 isl_set_free(set);
12369 return NULL;
12372 /* Check if the range of "ma" is compatible with the domain or range
12373 * (depending on "type") of "bmap".
12374 * Return -1 if anything is wrong.
12376 static int check_basic_map_compatible_range_multi_aff(
12377 __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
12378 __isl_keep isl_multi_aff *ma)
12380 int m;
12381 isl_space *ma_space;
12383 ma_space = isl_multi_aff_get_space(ma);
12385 m = isl_space_match(bmap->dim, isl_dim_param, ma_space, isl_dim_param);
12386 if (m < 0)
12387 goto error;
12388 if (!m)
12389 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12390 "parameters don't match", goto error);
12391 m = isl_space_tuple_is_equal(bmap->dim, type, ma_space, isl_dim_out);
12392 if (m < 0)
12393 goto error;
12394 if (!m)
12395 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
12396 "spaces don't match", goto error);
12398 isl_space_free(ma_space);
12399 return m;
12400 error:
12401 isl_space_free(ma_space);
12402 return -1;
12405 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
12406 * coefficients before the transformed range of dimensions,
12407 * the "n_after" coefficients after the transformed range of dimensions
12408 * and the coefficients of the other divs in "bmap".
12410 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
12411 __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
12413 int i;
12414 int n_param;
12415 int n_set;
12416 isl_local_space *ls;
12418 if (n_div == 0)
12419 return 0;
12421 ls = isl_aff_get_domain_local_space(ma->p[0]);
12422 if (!ls)
12423 return -1;
12425 n_param = isl_local_space_dim(ls, isl_dim_param);
12426 n_set = isl_local_space_dim(ls, isl_dim_set);
12427 for (i = 0; i < n_div; ++i) {
12428 int o_bmap = 0, o_ls = 0;
12430 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
12431 o_bmap += 1 + 1 + n_param;
12432 o_ls += 1 + 1 + n_param;
12433 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
12434 o_bmap += n_before;
12435 isl_seq_cpy(bmap->div[i] + o_bmap,
12436 ls->div->row[i] + o_ls, n_set);
12437 o_bmap += n_set;
12438 o_ls += n_set;
12439 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
12440 o_bmap += n_after;
12441 isl_seq_cpy(bmap->div[i] + o_bmap,
12442 ls->div->row[i] + o_ls, n_div);
12443 o_bmap += n_div;
12444 o_ls += n_div;
12445 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
12446 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
12447 goto error;
12450 isl_local_space_free(ls);
12451 return 0;
12452 error:
12453 isl_local_space_free(ls);
12454 return -1;
12457 /* How many stride constraints does "ma" enforce?
12458 * That is, how many of the affine expressions have a denominator
12459 * different from one?
12461 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
12463 int i;
12464 int strides = 0;
12466 for (i = 0; i < ma->n; ++i)
12467 if (!isl_int_is_one(ma->p[i]->v->el[0]))
12468 strides++;
12470 return strides;
12473 /* For each affine expression in ma of the form
12475 * x_i = (f_i y + h_i)/m_i
12477 * with m_i different from one, add a constraint to "bmap"
12478 * of the form
12480 * f_i y + h_i = m_i alpha_i
12482 * with alpha_i an additional existentially quantified variable.
12484 * The input variables of "ma" correspond to a subset of the variables
12485 * of "bmap". There are "n_before" variables in "bmap" before this
12486 * subset and "n_after" variables after this subset.
12487 * The integer divisions of the affine expressions in "ma" are assumed
12488 * to have been aligned. There are "n_div_ma" of them and
12489 * they appear first in "bmap", straight after the "n_after" variables.
12491 static __isl_give isl_basic_map *add_ma_strides(
12492 __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
12493 int n_before, int n_after, int n_div_ma)
12495 int i, k;
12496 int div;
12497 int total;
12498 int n_param;
12499 int n_in;
12501 total = isl_basic_map_total_dim(bmap);
12502 n_param = isl_multi_aff_dim(ma, isl_dim_param);
12503 n_in = isl_multi_aff_dim(ma, isl_dim_in);
12504 for (i = 0; i < ma->n; ++i) {
12505 int o_bmap = 0, o_ma = 1;
12507 if (isl_int_is_one(ma->p[i]->v->el[0]))
12508 continue;
12509 div = isl_basic_map_alloc_div(bmap);
12510 k = isl_basic_map_alloc_equality(bmap);
12511 if (div < 0 || k < 0)
12512 goto error;
12513 isl_int_set_si(bmap->div[div][0], 0);
12514 isl_seq_cpy(bmap->eq[k] + o_bmap,
12515 ma->p[i]->v->el + o_ma, 1 + n_param);
12516 o_bmap += 1 + n_param;
12517 o_ma += 1 + n_param;
12518 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
12519 o_bmap += n_before;
12520 isl_seq_cpy(bmap->eq[k] + o_bmap,
12521 ma->p[i]->v->el + o_ma, n_in);
12522 o_bmap += n_in;
12523 o_ma += n_in;
12524 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
12525 o_bmap += n_after;
12526 isl_seq_cpy(bmap->eq[k] + o_bmap,
12527 ma->p[i]->v->el + o_ma, n_div_ma);
12528 o_bmap += n_div_ma;
12529 o_ma += n_div_ma;
12530 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
12531 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
12532 total++;
12535 return bmap;
12536 error:
12537 isl_basic_map_free(bmap);
12538 return NULL;
12541 /* Replace the domain or range space (depending on "type) of "space" by "set".
12543 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
12544 enum isl_dim_type type, __isl_take isl_space *set)
12546 if (type == isl_dim_in) {
12547 space = isl_space_range(space);
12548 space = isl_space_map_from_domain_and_range(set, space);
12549 } else {
12550 space = isl_space_domain(space);
12551 space = isl_space_map_from_domain_and_range(space, set);
12554 return space;
12557 /* Compute the preimage of the domain or range (depending on "type")
12558 * of "bmap" under the function represented by "ma".
12559 * In other words, plug in "ma" in the domain or range of "bmap".
12560 * The result is a basic map that lives in the same space as "bmap"
12561 * except that the domain or range has been replaced by
12562 * the domain space of "ma".
12564 * If bmap is represented by
12566 * A(p) + S u + B x + T v + C(divs) >= 0,
12568 * where u and x are input and output dimensions if type == isl_dim_out
12569 * while x and v are input and output dimensions if type == isl_dim_in,
12570 * and ma is represented by
12572 * x = D(p) + F(y) + G(divs')
12574 * then the result is
12576 * A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
12578 * The divs in the input set are similarly adjusted.
12579 * In particular
12581 * floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
12583 * becomes
12585 * floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
12586 * B_i G(divs') + c_i(divs))/n_i)
12588 * If bmap is not a rational map and if F(y) involves any denominators
12590 * x_i = (f_i y + h_i)/m_i
12592 * then additional constraints are added to ensure that we only
12593 * map back integer points. That is we enforce
12595 * f_i y + h_i = m_i alpha_i
12597 * with alpha_i an additional existentially quantified variable.
12599 * We first copy over the divs from "ma".
12600 * Then we add the modified constraints and divs from "bmap".
12601 * Finally, we add the stride constraints, if needed.
12603 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
12604 __isl_take isl_basic_map *bmap, enum isl_dim_type type,
12605 __isl_take isl_multi_aff *ma)
12607 int i, k;
12608 isl_space *space;
12609 isl_basic_map *res = NULL;
12610 int n_before, n_after, n_div_bmap, n_div_ma;
12611 isl_int f, c1, c2, g;
12612 int rational, strides;
12614 isl_int_init(f);
12615 isl_int_init(c1);
12616 isl_int_init(c2);
12617 isl_int_init(g);
12619 ma = isl_multi_aff_align_divs(ma);
12620 if (!bmap || !ma)
12621 goto error;
12622 if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
12623 goto error;
12625 if (type == isl_dim_in) {
12626 n_before = 0;
12627 n_after = isl_basic_map_dim(bmap, isl_dim_out);
12628 } else {
12629 n_before = isl_basic_map_dim(bmap, isl_dim_in);
12630 n_after = 0;
12632 n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
12633 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
12635 space = isl_multi_aff_get_domain_space(ma);
12636 space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
12637 rational = isl_basic_map_is_rational(bmap);
12638 strides = rational ? 0 : multi_aff_strides(ma);
12639 res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
12640 bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
12641 if (rational)
12642 res = isl_basic_map_set_rational(res);
12644 for (i = 0; i < n_div_ma + n_div_bmap; ++i)
12645 if (isl_basic_map_alloc_div(res) < 0)
12646 goto error;
12648 if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
12649 goto error;
12651 for (i = 0; i < bmap->n_eq; ++i) {
12652 k = isl_basic_map_alloc_equality(res);
12653 if (k < 0)
12654 goto error;
12655 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
12656 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12659 for (i = 0; i < bmap->n_ineq; ++i) {
12660 k = isl_basic_map_alloc_inequality(res);
12661 if (k < 0)
12662 goto error;
12663 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
12664 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
12667 for (i = 0; i < bmap->n_div; ++i) {
12668 if (isl_int_is_zero(bmap->div[i][0])) {
12669 isl_int_set_si(res->div[n_div_ma + i][0], 0);
12670 continue;
12672 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
12673 n_before, n_after, n_div_ma, n_div_bmap,
12674 f, c1, c2, g, 1);
12677 if (strides)
12678 res = add_ma_strides(res, ma, n_before, n_after, n_div_ma);
12680 isl_int_clear(f);
12681 isl_int_clear(c1);
12682 isl_int_clear(c2);
12683 isl_int_clear(g);
12684 isl_basic_map_free(bmap);
12685 isl_multi_aff_free(ma);
12686 res = isl_basic_set_simplify(res);
12687 return isl_basic_map_finalize(res);
12688 error:
12689 isl_int_clear(f);
12690 isl_int_clear(c1);
12691 isl_int_clear(c2);
12692 isl_int_clear(g);
12693 isl_basic_map_free(bmap);
12694 isl_multi_aff_free(ma);
12695 isl_basic_map_free(res);
12696 return NULL;
12699 /* Compute the preimage of "bset" under the function represented by "ma".
12700 * In other words, plug in "ma" in "bset". The result is a basic set
12701 * that lives in the domain space of "ma".
12703 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
12704 __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
12706 return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
12709 /* Compute the preimage of the domain of "bmap" under the function
12710 * represented by "ma".
12711 * In other words, plug in "ma" in the domain of "bmap".
12712 * The result is a basic map that lives in the same space as "bmap"
12713 * except that the domain has been replaced by the domain space of "ma".
12715 __isl_give isl_basic_map *isl_basic_map_preimage_domain_multi_aff(
12716 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12718 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_in, ma);
12721 /* Compute the preimage of the range of "bmap" under the function
12722 * represented by "ma".
12723 * In other words, plug in "ma" in the range of "bmap".
12724 * The result is a basic map that lives in the same space as "bmap"
12725 * except that the range has been replaced by the domain space of "ma".
12727 __isl_give isl_basic_map *isl_basic_map_preimage_range_multi_aff(
12728 __isl_take isl_basic_map *bmap, __isl_take isl_multi_aff *ma)
12730 return isl_basic_map_preimage_multi_aff(bmap, isl_dim_out, ma);
12733 /* Check if the range of "ma" is compatible with the domain or range
12734 * (depending on "type") of "map".
12735 * Return -1 if anything is wrong.
12737 static int check_map_compatible_range_multi_aff(
12738 __isl_keep isl_map *map, enum isl_dim_type type,
12739 __isl_keep isl_multi_aff *ma)
12741 int m;
12742 isl_space *ma_space;
12744 ma_space = isl_multi_aff_get_space(ma);
12745 m = isl_space_tuple_is_equal(map->dim, type, ma_space, isl_dim_out);
12746 isl_space_free(ma_space);
12747 if (m >= 0 && !m)
12748 isl_die(isl_map_get_ctx(map), isl_error_invalid,
12749 "spaces don't match", return -1);
12750 return m;
12753 /* Compute the preimage of the domain or range (depending on "type")
12754 * of "map" under the function represented by "ma".
12755 * In other words, plug in "ma" in the domain or range of "map".
12756 * The result is a map that lives in the same space as "map"
12757 * except that the domain or range has been replaced by
12758 * the domain space of "ma".
12760 * The parameters are assumed to have been aligned.
12762 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
12763 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12765 int i;
12766 isl_space *space;
12768 map = isl_map_cow(map);
12769 ma = isl_multi_aff_align_divs(ma);
12770 if (!map || !ma)
12771 goto error;
12772 if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
12773 goto error;
12775 for (i = 0; i < map->n; ++i) {
12776 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
12777 isl_multi_aff_copy(ma));
12778 if (!map->p[i])
12779 goto error;
12782 space = isl_multi_aff_get_domain_space(ma);
12783 space = isl_space_set(isl_map_get_space(map), type, space);
12785 isl_space_free(map->dim);
12786 map->dim = space;
12787 if (!map->dim)
12788 goto error;
12790 isl_multi_aff_free(ma);
12791 if (map->n > 1)
12792 ISL_F_CLR(map, ISL_MAP_DISJOINT);
12793 ISL_F_CLR(map, ISL_SET_NORMALIZED);
12794 return map;
12795 error:
12796 isl_multi_aff_free(ma);
12797 isl_map_free(map);
12798 return NULL;
12801 /* Compute the preimage of the domain or range (depending on "type")
12802 * of "map" under the function represented by "ma".
12803 * In other words, plug in "ma" in the domain or range of "map".
12804 * The result is a map that lives in the same space as "map"
12805 * except that the domain or range has been replaced by
12806 * the domain space of "ma".
12808 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
12809 enum isl_dim_type type, __isl_take isl_multi_aff *ma)
12811 if (!map || !ma)
12812 goto error;
12814 if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
12815 return map_preimage_multi_aff(map, type, ma);
12817 if (!isl_space_has_named_params(map->dim) ||
12818 !isl_space_has_named_params(ma->space))
12819 isl_die(map->ctx, isl_error_invalid,
12820 "unaligned unnamed parameters", goto error);
12821 map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
12822 ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
12824 return map_preimage_multi_aff(map, type, ma);
12825 error:
12826 isl_multi_aff_free(ma);
12827 return isl_map_free(map);
12830 /* Compute the preimage of "set" under the function represented by "ma".
12831 * In other words, plug in "ma" in "set". The result is a set
12832 * that lives in the domain space of "ma".
12834 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
12835 __isl_take isl_multi_aff *ma)
12837 return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
12840 /* Compute the preimage of the domain of "map" under the function
12841 * represented by "ma".
12842 * In other words, plug in "ma" in the domain of "map".
12843 * The result is a map that lives in the same space as "map"
12844 * except that the domain has been replaced by the domain space of "ma".
12846 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
12847 __isl_take isl_multi_aff *ma)
12849 return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
12852 /* Compute the preimage of the range of "map" under the function
12853 * represented by "ma".
12854 * In other words, plug in "ma" in the range of "map".
12855 * The result is a map that lives in the same space as "map"
12856 * except that the range has been replaced by the domain space of "ma".
12858 __isl_give isl_map *isl_map_preimage_range_multi_aff(__isl_take isl_map *map,
12859 __isl_take isl_multi_aff *ma)
12861 return isl_map_preimage_multi_aff(map, isl_dim_out, ma);
12864 /* Compute the preimage of "map" under the function represented by "pma".
12865 * In other words, plug in "pma" in the domain or range of "map".
12866 * The result is a map that lives in the same space as "map",
12867 * except that the space of type "type" has been replaced by
12868 * the domain space of "pma".
12870 * The parameters of "map" and "pma" are assumed to have been aligned.
12872 static __isl_give isl_map *isl_map_preimage_pw_multi_aff_aligned(
12873 __isl_take isl_map *map, enum isl_dim_type type,
12874 __isl_take isl_pw_multi_aff *pma)
12876 int i;
12877 isl_map *res;
12879 if (!pma)
12880 goto error;
12882 if (pma->n == 0) {
12883 isl_pw_multi_aff_free(pma);
12884 res = isl_map_empty(isl_map_get_space(map));
12885 isl_map_free(map);
12886 return res;
12889 res = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12890 isl_multi_aff_copy(pma->p[0].maff));
12891 if (type == isl_dim_in)
12892 res = isl_map_intersect_domain(res,
12893 isl_map_copy(pma->p[0].set));
12894 else
12895 res = isl_map_intersect_range(res,
12896 isl_map_copy(pma->p[0].set));
12898 for (i = 1; i < pma->n; ++i) {
12899 isl_map *res_i;
12901 res_i = isl_map_preimage_multi_aff(isl_map_copy(map), type,
12902 isl_multi_aff_copy(pma->p[i].maff));
12903 if (type == isl_dim_in)
12904 res_i = isl_map_intersect_domain(res_i,
12905 isl_map_copy(pma->p[i].set));
12906 else
12907 res_i = isl_map_intersect_range(res_i,
12908 isl_map_copy(pma->p[i].set));
12909 res = isl_map_union(res, res_i);
12912 isl_pw_multi_aff_free(pma);
12913 isl_map_free(map);
12914 return res;
12915 error:
12916 isl_pw_multi_aff_free(pma);
12917 isl_map_free(map);
12918 return NULL;
12921 /* Compute the preimage of "map" under the function represented by "pma".
12922 * In other words, plug in "pma" in the domain or range of "map".
12923 * The result is a map that lives in the same space as "map",
12924 * except that the space of type "type" has been replaced by
12925 * the domain space of "pma".
12927 __isl_give isl_map *isl_map_preimage_pw_multi_aff(__isl_take isl_map *map,
12928 enum isl_dim_type type, __isl_take isl_pw_multi_aff *pma)
12930 if (!map || !pma)
12931 goto error;
12933 if (isl_space_match(map->dim, isl_dim_param, pma->dim, isl_dim_param))
12934 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12936 if (!isl_space_has_named_params(map->dim) ||
12937 !isl_space_has_named_params(pma->dim))
12938 isl_die(map->ctx, isl_error_invalid,
12939 "unaligned unnamed parameters", goto error);
12940 map = isl_map_align_params(map, isl_pw_multi_aff_get_space(pma));
12941 pma = isl_pw_multi_aff_align_params(pma, isl_map_get_space(map));
12943 return isl_map_preimage_pw_multi_aff_aligned(map, type, pma);
12944 error:
12945 isl_pw_multi_aff_free(pma);
12946 return isl_map_free(map);
12949 /* Compute the preimage of "set" under the function represented by "pma".
12950 * In other words, plug in "pma" in "set". The result is a set
12951 * that lives in the domain space of "pma".
12953 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
12954 __isl_take isl_pw_multi_aff *pma)
12956 return isl_map_preimage_pw_multi_aff(set, isl_dim_set, pma);
12959 /* Compute the preimage of the domain of "map" under the function
12960 * represented by "pma".
12961 * In other words, plug in "pma" in the domain of "map".
12962 * The result is a map that lives in the same space as "map",
12963 * except that domain space has been replaced by the domain space of "pma".
12965 __isl_give isl_map *isl_map_preimage_domain_pw_multi_aff(
12966 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12968 return isl_map_preimage_pw_multi_aff(map, isl_dim_in, pma);
12971 /* Compute the preimage of the range of "map" under the function
12972 * represented by "pma".
12973 * In other words, plug in "pma" in the range of "map".
12974 * The result is a map that lives in the same space as "map",
12975 * except that range space has been replaced by the domain space of "pma".
12977 __isl_give isl_map *isl_map_preimage_range_pw_multi_aff(
12978 __isl_take isl_map *map, __isl_take isl_pw_multi_aff *pma)
12980 return isl_map_preimage_pw_multi_aff(map, isl_dim_out, pma);
12983 /* Compute the preimage of "map" under the function represented by "mpa".
12984 * In other words, plug in "mpa" in the domain or range of "map".
12985 * The result is a map that lives in the same space as "map",
12986 * except that the space of type "type" has been replaced by
12987 * the domain space of "mpa".
12989 * If the map does not involve any constraints that refer to the
12990 * dimensions of the substituted space, then the only possible
12991 * effect of "mpa" on the map is to map the space to a different space.
12992 * We create a separate isl_multi_aff to effectuate this change
12993 * in order to avoid spurious splitting of the map along the pieces
12994 * of "mpa".
12996 __isl_give isl_map *isl_map_preimage_multi_pw_aff(__isl_take isl_map *map,
12997 enum isl_dim_type type, __isl_take isl_multi_pw_aff *mpa)
12999 int n;
13000 isl_pw_multi_aff *pma;
13002 if (!map || !mpa)
13003 goto error;
13005 n = isl_map_dim(map, type);
13006 if (!isl_map_involves_dims(map, type, 0, n)) {
13007 isl_space *space;
13008 isl_multi_aff *ma;
13010 space = isl_multi_pw_aff_get_space(mpa);
13011 isl_multi_pw_aff_free(mpa);
13012 ma = isl_multi_aff_zero(space);
13013 return isl_map_preimage_multi_aff(map, type, ma);
13016 pma = isl_pw_multi_aff_from_multi_pw_aff(mpa);
13017 return isl_map_preimage_pw_multi_aff(map, type, pma);
13018 error:
13019 isl_map_free(map);
13020 isl_multi_pw_aff_free(mpa);
13021 return NULL;
13024 /* Compute the preimage of "map" under the function represented by "mpa".
13025 * In other words, plug in "mpa" in the domain "map".
13026 * The result is a map that lives in the same space as "map",
13027 * except that domain space has been replaced by the domain space of "mpa".
13029 __isl_give isl_map *isl_map_preimage_domain_multi_pw_aff(
13030 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa)
13032 return isl_map_preimage_multi_pw_aff(map, isl_dim_in, mpa);
13035 /* Compute the preimage of "set" by the function represented by "mpa".
13036 * In other words, plug in "mpa" in "set".
13038 __isl_give isl_set *isl_set_preimage_multi_pw_aff(__isl_take isl_set *set,
13039 __isl_take isl_multi_pw_aff *mpa)
13041 return isl_map_preimage_multi_pw_aff(set, isl_dim_set, mpa);